async function getSubscriptions(auth, pageToken = '', allSubscriptions = []) { // get data from the subscriptions const { data } = await service.subscriptions.list({ mine: true, auth, part: 'snippet', maxResults: 50, // max is 50, doesn't really matter as it's batched order: 'alphabetical', // use alphabetical to always have the same order pageToken }); // push all the current subscriptions to the array of all subscriptions allSubscriptions.push(...data.items); // if there is another page of subscriptions to be looked at - repeat again if (data.nextPageToken) { await getSubscriptions(auth, data.nextPageToken, allSubscriptions); } return allSubscriptions; }
async fetchVideo (id) { const res = await this.api.videos.list({ part: 'snippet,contentDetails', id: id }).catch((err) => { console.log('[error] Something bad happened.') console.log('[error-info]', err) throw {code: -1, msg: 'Something bad happened.'} }) if (res.data.items.length == 0) { throw {code: 2, msg: 'Song cannot be accessed.'} } var title = res.data.items[0].snippet.title var url = 'https://youtu.be/' + id var duration = res.data.items[0].contentDetails.duration var song_data = { id: id, title: title, url: url, duration: duration } return song_data }
async function getUploads(channel, auth) { const id = channel.snippet.resourceId.channelId; // get list of all channel uploads const channelInfo = await service.channels.list({ id, auth, part: 'contentDetails' }); // destructure the uploads playlist (all channel videos) const { contentDetails: { relatedPlaylists: { uploads } } } = channelInfo.data.items[0]; const { data: { items } } = await service.playlistItems.list({ playlistId: uploads, auth, part: 'snippet', maxResults: 50 // 50 is the max }); items.forEach(getNewVideos); }
youtube.videos.list({ part: 'liveStreamingDetails', id: targetVideos.map((targetVideo) => targetVideo.yt_video_key).join(','), hl: 'ja', fields: 'items(id,liveStreamingDetails)', maxResults: 50, }) .then((ytResult) => { // Sanity check for YouTube respons contents if (!ytResult.data || !ytResult.data.items) { return Promise.reject(new GenericError('Invalid YouTube response', ytResult)); } // Return video list only return ytResult.data.items; }) .catch((err) => { // Log error information log.error('videoStatusAPI() YouTube fetch error', { error: err.toString(), videoKeys: targetVideos.map((targetVideo) => targetVideo.yt_video_key), }); // Return so that loop will not continue, and this call has no items returned return null; })
batch.map((batchItems) => ( // Fetch data from YouTube youtube.channels.list({ part: 'snippet,contentDetails,statistics', id: batchItems, maxResults: 50, }) .then((ytResult) => { // Sanity check for YouTube respons contents if (!ytResult.data || !ytResult.data.items) { return Promise.reject(new GenericError('YouTube response error', ytResult)); } // Return the array of channel information within the response return ytResult.data.items; }) .catch((err) => { // Log error information, and the channels included in the API that failed log.error('channelInfo() YouTube fetch error', { error: err.toString(), batchItems, }); return []; }) ))
async fetchList (id) { const res = await this.api.playlistItems.list({ part: 'snippet,contentDetails', maxResults: 50, playlistId: id }).catch((err) => { console.log('[error] Something bad happened.') console.log('[error-info]', err) throw {code: -1, msg: 'Something bad happened.'} }) const items = res.data.items const songList = [] console.log('[info] List length: ' + items.length) for (const item of items) { var id = item.snippet.resourceId.videoId var song_data = await this.fetchVideo(id).catch((err) => { if(err.code < 0) throw err }) if(song_data) { songList.push(song_data) } } console.log('[info] Finish retrieving list.') return songList }
youtube.videos.list({ part: 'snippet,status,contentDetails,liveStreamingDetails', id: targetVideos.map((targetVideo) => targetVideo.yt_video_key), hl: 'ja', fields: 'items(id,snippet,contentDetails,status/embeddable,liveStreamingDetails)', maxResults: 50, // keep at 50, not VIDEOS_MAX_QUERY }) .then((ytResult) => { // Sanity check for YouTube respons contents if (!ytResult.data || !ytResult.data.items) { return Promise.reject(new GenericError('Invalid YouTube response', ytResult)); } // Return video list only return ytResult.data.items; }) .catch((err) => { // Log error information log.error('videoInfoAPI() YouTube fetch error', { error: err.toString(), videoKeys: targetVideos.map((targetVideo) => targetVideo.yt_video_key), }); // Return so that loop will not continue, and this call has no items returned return null; })
while (hasNextPage) { const ytData = await youtube.commentThreads.list(
async search (query, pageToken) { const res = await this.api.search.list({ q: query, maxResults: 25, type: 'video,playlist', part: 'snippet', pageToken: pageToken }).catch((err) => { console.log('[error] Something bad happened when search.') console.log('[error-info]', err) throw {code: -1, msg: 'Something bad happened.'} }) var items = res.data.items var nextPageToken = res.nextPageToken var prevPageToken = res.prevPageToken items = items.map(function (item) { return { id: item.id.videoId, title: item.snippet.title, url: 'https://youtu.be/' + item.id.videoId, thumbnail: item.snippet.thumbnails.default } }) return {items: items, nextPageToken: nextPageToken, prevPageToken: prevPageToken} }
const ytData = await youtube.playlistItems.list({ part: 'id,snippet', playlistId,