-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (77 loc) · 1.8 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const request=require('request');
class YouTubeDataAPI {
constructor(key=""){
this.baseURL="https://www.googleapis.com/youtube/v3/";
this.key=key;
this.apiOptions={
maxResults:50
};
}
getChannels(filter){
return this.request("search?part=snippet&type=channel", filter);
}
getChannelDetails(filter){
return new Promise((resolve, reject)=>{
this.request("channels?part=snippet,statistics", filter).then((response)=>{
resolve({
filter:filter,
result:response
});
}).catch((error)=>{
reject(error);
});
});
}
getChannelStatistics(id){
return new Promise((resolve, reject)=>{
this.request("channels?part=statistics", {
id:id
}).then((response)=>{
resolve({
id:id,
result:response
});
});
});
}
getVideos(filter){
return this.request("search?part=snippet&type=video", filter);
}
getVideosFromChannel(filter){
return this.request("search?part=snippet&channelId="+filter.channelID+"&order=date&type=video", {
maxResults:this.apiOptions.maxResults,
pageToken:typeof filter.pageToken==="string"?filter.pageToken:""
});
}
getVideoDetails(filter){
return this.request("videos?part=snippet,statistics&id="+filter.videoIDs+"&order=date&type=video", {
maxResults:this.apiOptions.maxResults
});
}
request(url, filter){
if (this.key.length>0){
if (typeof filter!=="undefined"){
for (let key in filter){
url+="&"+key+"="+filter[key];
}
}
return new Promise((resolve, reject)=>{
request(this.baseURL+url+"&key="+this.key, {
json:true
}, (error, res, response)=>{
if (error!==null){
reject(error);
}
if (typeof response.error!=="undefined"){
reject(response.error);
}
resolve(response);
});
});
}
}
setKey(key){
this.key=key;
}
}
module.exports=YouTubeDataAPI;