-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
125 lines (86 loc) · 3.3 KB
/
index.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var subtitles1 = [];
var subtitles2 = [];
var id = null;
$(function () {
$(document).on('submit', '#form-url', function (e) {
//e.preventDefault();
var form = $(this);
var url = form.find('#url').val(); //https://www.youtube.com/watch?v=rIzHUfDB28o
id = url.replace('https://www.youtube.com/watch?v=', '').replace('https://youtu.be/','');
form.find('#id').val(id);
});
$(document).on('click', '#languages .btn', function () {
var btn = $(this);
getSubtitles(btn.data('url'));
});
id = getUrlParameter('id');
if (id) {
loadSubtitles(id);
}
})
function loadSubtitles(id) {
getList(id);
}
function getList(id) {
$.ajax({
type: "GET",
url: 'https://www.youtube.com/api/timedtext?v=' + id + '&type=list',
dataType: "xml",
}).done(function (xml) {
$('#languages').html('');
$(xml).find("track").each(function (ti, tv) {
var code = $(tv).attr('lang_code');
var name = $(tv).attr('name');
var btn = $('<a class="btn btn-default mb-2">' + $(tv).attr('lang_original') + '</a>')
.data('url', 'https://www.youtube.com/api/timedtext?lang=' + code + '&v=' + id + '&name=' + name + '&fmt=json3');
$('#languages').append(btn);
});
var first = $(xml).find("track")[0];
var code = $(first).attr('lang_code');
var name = $(first).attr('name');
//add translate language
var btn = $('<a class="btn btn-default mb-2">' + 'Русский' + '</a>')
.data('url', 'https://www.youtube.com/api/timedtext?lang=' + code + '&v=' + id + '&name=' + name + '&tlang=ru&fmt=json3');
$('#languages').append(btn);
});
//<transcript_list docid="12433531826008611786" capture-installed="true">
// <track id="1" name="" lang_code="en" lang_original="English" lang_translated="English" lang_default="true" />
// <track id="0" name="" lang_code="de" lang_original="Deutsch" lang_translated="German" />
//</transcript_list>
}
function getSubtitles(url) {
//https://www.youtube.com/api/timedtext?lang=de-DE&v=hxjnotQy4VQ&name=UT&fmt=json3
$.ajax({
type: "GET",
url: url,
dataType: "json",
}).done(function (data) {
//console.log(data);
subtitles1 = subtitles2;
subtitles2 = _.map(data.events, function (ev) { return _.map(ev.segs, function (s) { return s.utf8; }).join("<br/>"); });
//console.log(subtitles2);
renderSubtitles();
});
}
function renderSubtitles() {
var merged = _.zip(subtitles1, subtitles2);
//console.log(merged);
var result = $('#result');
result.html('');
_.each(merged, function (s) {
result.append('<li>' + s[0] + (s.length>1?'<ul><li>'+(s[1]||'-')+'</li></ul>':'') + '</li>');
});
}
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = window.location.search.substring(1),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
}
}
};