-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml_extractor.js
45 lines (43 loc) · 1.72 KB
/
xml_extractor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function fetch_transcript(video_identifier) {
var xml = null
var languages = ['cn', 'zh', 'jp', 'ja', 'ko', 'kr', 'es', 'fr', 'de', 'nl', 'en']
for (var i = 0; i < languages.length; i++) {
var language = languages[i]
xml = xml_extractor_http_get(language, video_identifier)
if (xml !== null) {
break
}
}
var transcript = xml.getElementsByTagName('transcript')[0]
for (var i = 0; i < transcript.childNodes.length; i++) {
var childNode = transcript.childNodes[i]
var start_time = childNode.getAttribute('start')
var duration = childNode.getAttribute('dur')
var text = childNode.textContent
text = text.replace('' ', 'g ')
text = text.replace('' ', 'g ')
text = text.replace('' ', 'g ')
text = text.replace(''', '')
text = text.replace(''', '')
text = text.replace(''', '')
text = text.replace(''', '')
text = text.replace(''', '')
text = text.replace(''', '')
text = text.replace(';', '')
text = text.replace(';', '')
text = text.replace(';', '')
text = text.replace(';', '')
text = text.replace(';', '')
console.log(text)
var translated_text = translate(text, language, userLanguageInTwoCharacters())
play_line(start_time, duration, translated_text)
}
}
function xml_extractor_http_get(language, video_identifier) {
console.log('Trying language ' + language)
var url = "https://video.google.com/timedtext?lang=" + language + "&v=" + video_identifier
var request = new XMLHttpRequest();
request.open("GET", url, false);
request.send();
return request.responseXML;
}