-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (31 loc) · 993 Bytes
/
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
var https = require('https');
var cheerio = require('cheerio');
var Promise = require('bluebird');
module.exports = function (query) {
return new Promise(function (resolve, reject) {
try {
https.get('https://www.youtube.com/results?search_query=' + query, function(res) {
var data = '';
var results = [];
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var $ = cheerio.load(data);
var videos = $('.yt-lockup-video');
for (i = 0; i < videos.length; i++) {
var uri = $(videos[i]).find('.yt-uix-sessionlink').attr('href');
if (!uri.startsWith('/watch')) { continue; }
results.push({
title: $(videos[i]).find('.yt-lockup-title a').text(),
uri: 'https://youtube.com' + uri
});
}
resolve(results);
});
});
} catch (err) {
reject(err);
}
});
};