-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdata_loader.js
76 lines (62 loc) · 2.19 KB
/
data_loader.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { URLParser } from "./url_parser.js";
export class DataLoader {
constructor() {
this.urlParser = new URLParser();
}
async getDefaultCC() {
const languages = await this.getCaptionType();
for (const language of languages) {
if ((language.langcode).includes("en")) {
return (language);
}
}
return (languages[0]);
}
async getCaption(name, langCode) {
const videoCode = await this.urlParser.getVideoCode();
const url = `http://video.google.com/timedtext?name=${name}&lang=${langCode}&v=${videoCode}`;
const rawCaption = await this.fetchAPI(url);
return this.parseXML(rawCaption);
}
async getCaptionType() {
const videoCode = await this.urlParser.getVideoCode();
const url = `http://video.google.com/timedtext?type=list&v=${videoCode}`;
const response = await this.fetchAPI(url);
const parsedCaptionType = this.parseXML(response);
return this.findCCType(parsedCaptionType);
}
async fetchAPI(url) {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open('GET', url);
request.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(request.response);
} else {
reject({
status: this.status,
statusText: request.statusText
});
}
}
request.send();
})
}
parseXML(rawXML) {
var parser = new DOMParser();
return parser.parseFromString(rawXML, "text/xml");
}
findCCType(parsedType) {
var tupledTypes = [];
var objType = parsedType.getElementsByTagName("track");
for (var trackTag of objType) {
let trackName = trackTag.getAttribute("name");
let trackLang = trackTag.getAttribute("lang_code");
tupledTypes.push({
name: trackName,
langcode: trackLang,
});
}
return tupledTypes;
}
}