-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube_client.js
73 lines (55 loc) · 2.23 KB
/
youtube_client.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
/**
* Created by attila on 04.03.17.
*/
const baseUrl = "https://www.googleapis.com/youtube/v3/search";
const GET = "GET";
const apiKey="AIzaSyCNJjCCQVanKUek5GjNImfu-RIHDa6Eyr0";
const targetPrefix = "https://youtu.be/";
var request = require('request');
var querystring = require("querystring");
var debugMode = false;
var debug = function (msg) {
if(debugMode)
console.log(msg);
};
var sendRequest = function(path, method, callback) {
console.log("- [REST] Sending " + method + " request to " + baseUrl + path + "...");
if(method === GET) {
request(baseUrl + path, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("- [REST] Success sending " + method + " request to " + baseUrl + path);
callback(JSON.parse(body));
} else {
var theResponse = response ? response.statusCode : null;
console.log("- [REST] Error sending " + method + " request to " + baseUrl + path + " -> \nStatus: "+theResponse + ": \nError: " + error + "\nBody: " + body);
}
});
}
};
/**
* @param artist Artist of the track
* @param track The track's name
* @param deepCallback (optional) function to carry out with the result of this method after the method
* @return NULL. if you want to use the result, use it by providing a deepCallback
*/
var getVideoLink =function (artist, track, deepCallback) {
var name = artist + " " + track;
console.log("[YOUTUBE] Setting up video search for " + name);
var query = querystring.stringify({part:"snippet",type:"video", q: name, key:apiKey});
var leUrl = "?"+query;
var callback = function(chunk) {
var list = chunk.items;
if (list.length === 0 ){
console.log("No videos for name "+ name);
return;
}
var result = {artist:artist, name:track,url:targetPrefix+list[0].id.videoId, thumbnail: list[0].snippet.thumbnails.medium.url};
debug("[YOUTUBE] Found result " + result.artist +", " + result.name + ", " + result.url);
if(deepCallback)
deepCallback(result);
};
return sendRequest(leUrl, GET, callback);
};
module.exports = {
getVideoLink: getVideoLink
};