forked from 1core2life/smart-mirror-A-team
-
Notifications
You must be signed in to change notification settings - Fork 0
/
music.js
executable file
·46 lines (35 loc) · 1.09 KB
/
music.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
exports.searchMusic = function(res,musicName) {
var request = require('request');
var Youtube = require('youtube-node');
var urlencode = require('urlencode');
var EncodedName = urlencode(musicName);
var youtube = new Youtube();
var tmp=musicName;
youtube.setKey('AIzaSyCNGcipNkncK2BlwCNXDg3_mS3ZON1qOOI');
request({
url: 'https://www.youtube.com/iframe_api',
method: 'GET'
}, function(err, response, body) {
var youtubeURL=new Array();
var youtubeTitle=new Array();
var keyword = tmp;
var limit = 1;
youtube.addParam('order', 'relevance');
youtube.addParam('type', 'video');
youtube.search(keyword, limit, function(err, result) {
if (err) { console.log(err); return; }
var items = result["items"];
for (var i in items) {
var it = items[i];
var title = it["snippet"]["title"];
var video_id = it["id"]["videoId"];
var url = "https://www.youtube.com/embed/" + video_id+"?enablejsapi=1";
youtubeURL.push(url);
youtubeTitle.push(title);
}
res.render('music', {
youtubeURL: youtubeURL
});
});
});
};