-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (33 loc) · 1.2 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
const https = require('https');
function decode(encodedString) {
const translate_re = /&(nbsp|amp|quot|lt|gt);/g;
const translate = {
'nbsp': ' ',
'amp' : '&',
'quot': '\"',
'lt' : '<',
'gt' : '>',
};
return encodedString
.replace(translate_re, (match, entity) => translate[entity])
.replace(/&#(\d+);/gi, (match, numStr) => String.fromCharCode(parseInt(numStr, 10)));
}
exports.getYoutubeTags = function (videoId) {
return new Promise((resolve, reject) => {
https.get('https://www.youtube.com/watch?v=' + videoId, resp => {
let data = '';
resp.on('data', (chunk) => data += chunk);
resp.on('end', () => {
const searchStart = 'keywords" content="';
const searchEnd = '"';
const index = data.indexOf(searchStart);
if (index < 0) reject('Invalid videoId')
const content = data.slice(index + searchStart.length);
const tags = decode(content.slice(0, content.indexOf(searchEnd))).split(',');
if (tags.length === 1 && tags[0] === '') reject('Cannot fetch tags for this video');
resolve(tags.map(Function.prototype.call, String.prototype.trim));
});
resp.on('error', reject);
});
})
}