-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (55 loc) · 1.48 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
var Promise = require('bluebird');
const fetch = require('node-fetch')
function extractConfigData(html) {
var index,
start = 'ytplayer.config = ',
//end = '</script>';
end = ';ytplayer.load';
index = html.indexOf(start);
if (index === -1) {
return '';
}
html = html.slice(index + start.length);
index = html.indexOf(end);
if (index === -1) {
return '';
}
return html.slice(0, index);
}
function fetchMetadata(id) {
var url = 'http://www.youtube.com/watch?v=' + id;
return new Promise(function (fulfill, reject) {
var url = 'http://www.youtube.com/watch?v=' + id;
return fetch(url)
.then(res => res.text())
.then(body => {
jsonStr = extractConfigData(body);
if( jsonStr == '') {
reject('page error');
}
jsonObj = JSON.parse(jsonStr)
jsonObj = JSON.parse( jsonObj['args']['player_response']);
//console.log( JSON.parse( jsonObj['args']['player_response']));
fulfill(jsonObj);
}).catch(function(error) {
reject("parsing error");
});
});
}
module.exports = {
getMetadata: function (videoID) {
return new Promise(function (fulfill, reject) {
if (!videoID) {
reject(new Error('Unable to get video id.'));
return;
}
fetchMetadata(videoID).then(
function (d) {
fulfill( d);
}
).catch(function(error) {
reject(error);
});
});
}
};