-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
63 lines (53 loc) · 2.55 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
const { getAllComments, getPaginatedComments, getNextCommentsPage } = require('./controllers/commentsController');
const { getAllVideosByChannelId, getAllPlaylistByChannelId, getPaginatedVideosByChannelId, getNextVideosPage } = require('./controllers/videoController')
function youtubeClient(apiKey) {
this.apiKey = apiKey;
//Comments
this.videoId = "";
this.commentsPageSize = "";
this.nextCommentsPageToken = "";
//Videos
this.channelId = "";
this.videosPageSize = "";
this.nextVideosPageToken = "";
}
youtubeClient.prototype.getAllComments = (videoId) => getAllComments(this.apiKey, videoId);
youtubeClient.prototype.getPaginatedComments = async function (videoId, pageSize) {
if (typeof videoId === 'number') throw new TypeError('expected a videoId string parameter');
const commentsData = await getPaginatedComments(this.apiKey, videoId, pageSize);
this.videoId = videoId;
this.commentsPageSize = pageSize;
this.nextCommentsPageToken = commentsData.nextCommentsPageToken;
return commentsData.comments;
};
youtubeClient.prototype.getNextCommentsPage = async function (pageSize) {
if (!this.nextCommentsPageToken) return [];
const size = pageSize ?? this.commentsPageSize;
const commentsData = await getNextCommentsPage(this.apiKey, this.videoId, this.nextCommentsPageToken, size)
this.nextCommentsPageToken = commentsData.nextCommentsPageToken;
return commentsData.comments;
};
youtubeClient.prototype.getAllVideos = async function (channelId) {
return getAllVideosByChannelId(this.apiKey, channelId);
};
youtubeClient.prototype.getPlaylist = async function (channelId) {
return getAllPlaylistByChannelId(this.apiKey, channelId);
};
//Max 50
youtubeClient.prototype.getPaginatedChannelVideos = async function (channelId, pageSize = 50) {
if (typeof channelId === 'number') throw new TypeError('expected a channelId string parameter');
const videosData = await getPaginatedVideosByChannelId(this.apiKey, channelId, pageSize);
this.channelId = channelId;
this.videosPageSize = pageSize;
this.nextVideosPageToken = videosData.nextPageToken;
return videosData.allVideosId;
}
//Max 50
youtubeClient.prototype.getNextVideosPage = async function (pageSize) {
if (!this.nextVideosPageToken) return [];
const size = pageSize ?? this.videosPageSize;
const videosData = await getNextVideosPage(this.apiKey, this.videoId, this.nextCommentsPageToken, size)
this.nextVideosPageToken = videosData.nextPageToken;
return videosData.allVideosId;
};
module.exports = youtubeClient;