-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
39 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,51 @@ | ||
import { parse } from 'node-html-parser' | ||
|
||
async function fetchTranscript(videoId) { | ||
const PAGE = await fetch('https://www.youtube.com/watch?v=' + videoId) | ||
.then((res) => res.text()) | ||
.then((html) => parse(html)) | ||
const videoPageHtml = await fetchHtml( | ||
`https://www.youtube.com/watch?v=${videoId}` | ||
) | ||
const playerScript = findPlayerScript(videoPageHtml) | ||
const captionsUrl = extractCaptionsUrl(playerScript) | ||
const captionsXml = await fetchXml( | ||
captionsUrl.replace('lang=de-DE', 'lang=ko-KR') | ||
) | ||
const transcript = extractTranscriptFromXml(captionsXml) | ||
return transcript | ||
} | ||
|
||
async function fetchHtml(url) { | ||
const response = await fetch(url) | ||
const html = await response.text() | ||
return parse(html) | ||
} | ||
|
||
const scripts = PAGE.getElementsByTagName('script') | ||
const playerScript = scripts.find((script) => | ||
function findPlayerScript(html) { | ||
const scripts = html.getElementsByTagName('script') | ||
return scripts.find((script) => | ||
script.textContent.includes('var ytInitialPlayerResponse = {') | ||
) | ||
} | ||
|
||
function extractCaptionsUrl(playerScript) { | ||
const dataString = playerScript.textContent | ||
?.split('var ytInitialPlayerResponse = ')?.[1] | ||
?.slice(0, -1) | ||
const data = JSON.parse(dataString.trim()) | ||
const captionsUrl = | ||
data.captions.playerCaptionsTracklistRenderer.captionTracks[0].baseUrl | ||
const captionsDefault = captionsUrl.replace('lang=de-DE', 'lang=ko-KR') | ||
return data.captions.playerCaptionsTracklistRenderer.captionTracks[0].baseUrl | ||
} | ||
|
||
const resXML = await fetch(captionsDefault) | ||
.then((res) => res.text()) | ||
.then((xml) => parse(xml)) | ||
async function fetchXml(url) { | ||
const response = await fetch(url) | ||
const xml = await response.text() | ||
return parse(xml) | ||
} | ||
|
||
let transcript = '' | ||
const chunks = resXML.getElementsByTagName('text') | ||
for (const chunk of chunks) { | ||
transcript += chunk.textContent | ||
} | ||
return transcript | ||
function extractTranscriptFromXml(xml) { | ||
const chunks = xml.getElementsByTagName('text') | ||
return Array.from(chunks).reduce( | ||
(transcript, chunk) => transcript + chunk.textContent, | ||
'' | ||
) | ||
} | ||
|
||
export default fetchTranscript |