This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyt-dl.js
82 lines (57 loc) · 2.01 KB
/
yt-dl.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
//get queryStringMap, listOfQueryStringMaps functions from https://gist.github.com/jerolimov/3756821
var queryStringMap = function(data) {
var result = {};
data.split('&').forEach(function(entry) {
result[
decodeURIComponent(entry.substring(0, entry.indexOf('=')))] =
decodeURIComponent(entry.substring(entry.indexOf('=') + 1));
});
return result;
};
var listOfQueryStringMaps = function(data) {
var result = [];
data.split(',').forEach(function(entry) {
result.push(queryStringMap(entry));
});
return result;
};
var getVideoFormat = function(format) {
var videoFormats = {
22: { fileType: 'mp4', resolution: 'hd720' },
43: { fileType: 'webm', resolution: '360p' },
18: { fileType: 'mp4', resolution: '360p' },
36: { fileType: '3gp', resolution: '240p' },
17: { fileType: '3gp', resolution: '144p' }
};
if (videoFormats[format]) {
return videoFormats[format];
} else {
return {
resolution : "Unrecognized",
fileType : "Unrecognized"
};
}
}
var get_info_video = function() {
if (inputUrl.text == '')
return;
var video_id = inputUrl.text.substring(inputUrl.text.indexOf('v=')).split('&')['0'].replace('v=', '')
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.youtube.com/get_video_info?video_id=" + video_id, true);
xhr.send();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var ready_ = xhr.responseText;
print(ready_.indexOf('errorcode=2'))
if (ready_.indexOf('errorcode=2') > -1)
return;
var query_ = listOfQueryStringMaps(queryStringMap(ready_)['url_encoded_fmt_stream_map']);
var format;
query_.forEach(function(videoEntry) {
format = getVideoFormat(videoEntry.itag);
console.log(decodeURIComponent(JSON.stringify(videoEntry, null, '\t')));
listView.append({url: videoEntry.url, "type": format.resolution + ' - ' + format.fileType});
});
}
}
}