forked from macCesar/ti.youtube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathti.youtube.js
79 lines (64 loc) · 1.83 KB
/
ti.youtube.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
function getUrlByVideoId(videoID, callback, errorCallback) {
const xhr = Ti.Network.createHTTPClient({
onload: function () {
let player_response = qsToJson(this.responseText).player_response;
if (player_response.playabilityStatus.playableInEmbed) {
let urls = {};
let formats = player_response.streamingData.formats;
let hlsManifestUrl = player_response.streamingData.hlsManifestUrl;
if (hlsManifestUrl) {
urls.medium = urls.high = urls.best = hlsManifestUrl;
callback(urls);
} else if (formats) {
// formats[0] typically contains 360p video ( medium quality )
if (formats[0]) {
urls.medium = formats[0].url;
}
// formats[1] typically contains 720p video ( hd720 quality )
if (formats[1]) {
urls.high = formats[1].url;
}
urls.best = (urls.high) ? urls.high : urls.medium;
callback(urls);
} else {
if (errorCallback) {
errorCallback({error: "no_valid_urls"});
} else {
alert('There are no valid URLs to play the video!');
}
}
} else {
if (errorCallback) {
errorCallback({error: "video_not_allowed"});
} else {
alert('This video cannot be played natively!');
}
}
},
onerror: function (e) {
Ti.API.info('error, HTTP status = ' + this.status);
if (errorCallback) {
errorCallback({error: "http_error", result: e});
} else {
alert(e.error);
}
},
timeout: 5000
});
xhr.open("GET", `https://www.youtube.com/get_video_info?video_id=${videoID}`);
xhr.send();
}
module.exports.getUrlByVideoId = getUrlByVideoId;
function qsToJson(qs) {
let res = {};
let pars = qs.split('&');
let kv, k, v;
for (let i in pars) {
kv = pars[i].split('=');
k = kv[0];
v = kv[1];
res[k] = decodeURIComponent(v);
}
res.player_response = JSON.parse(res.player_response);
return res;
}