-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add youtube as a source * search endpoint ready for youtube * pagination * pagination works * pass in tokens as header * change maximum results per page to 50 * fix bugs regarding youtube API * update thumbnail resolving * fix bugs * add google client id and secret * typo * remove not used import * move oauth2 class instantiate inside of the API function * changelog entry * changelog entry
- Loading branch information
1 parent
527f1c3
commit 375c008
Showing
14 changed files
with
21,083 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
var google = require('@googleapis/youtube'); | ||
var OAuth2 = google.auth.OAuth2; | ||
|
||
async function youtubeAPI(tokens, resolveName, id, args) { | ||
var oauth2Client = new OAuth2( | ||
GOOGLE_CLIENT_ID, | ||
GOOGLE_CLIENT_SECRET | ||
); | ||
|
||
oauth2Client.setCredentials({ | ||
access_token: tokens.googleaccesstoken, | ||
refresh_token: tokens.googlerefreshtoken, | ||
}); | ||
|
||
var youtube = google.youtube({ | ||
version: 'v3', | ||
auth:oauth2Client | ||
}); | ||
|
||
try { | ||
const pages = args['pages'] - 1; | ||
delete args['pages']; // Clean up non-existent 'pages' argument | ||
|
||
let allItems = []; // Initialize an empty array to hold all concatenated items | ||
|
||
switch(resolveName) { | ||
case 'search': | ||
let data = await youtube.search.list(args); | ||
allItems = allItems.concat(data.data.items); // Concatenate initial items | ||
|
||
let currentPage = 0; | ||
let nextPageToken = data.data.nextPageToken; | ||
|
||
while (currentPage < pages && nextPageToken) { | ||
const newArgs = { ...args, pageToken: nextPageToken }; | ||
const newData = await youtube.search.list(newArgs); | ||
allItems = allItems.concat(newData.data.items); // Safely concatenate new items | ||
|
||
nextPageToken = newData.data.nextPageToken; // Update the nextPageToken | ||
currentPage++; | ||
} | ||
|
||
return allItems; | ||
|
||
case 'playlist': | ||
return (await youtube.playlists.list({ | ||
part: 'contentDetails,id,player,snippet,status', | ||
id: id | ||
})).data.items; | ||
|
||
case 'channel': | ||
return (await youtube.channels.list({ | ||
part: 'invideoPromotion,brandingSettings,contentDetails,contentOwnerDetails,id,snippet,statistics,status,topicDetails', | ||
id: id | ||
})).data.items; | ||
|
||
case 'video': | ||
return (await youtube.videos.list({ | ||
part: 'contentDetails,id,liveStreamingDetails,player,recordingDetails,snippet,statistics,status,topicDetails', | ||
id: id | ||
})).data.items; | ||
|
||
case 'videoCommentThread': | ||
return (await youtube.commentThreads.list({ | ||
part: 'id,snippet', | ||
videoId: id, | ||
maxResults: args['maxResults'], | ||
searchTerms: args['searchTerms'] | ||
})).data.items; | ||
|
||
case 'channelCommentThread': | ||
return (await youtube.commentThreads.list({ | ||
part: 'id,snippet', | ||
channelId: id, | ||
maxResults: args['maxResults'], | ||
searchTerms: args['searchTerms'] | ||
})).data.items; | ||
|
||
default: | ||
throw new Error(`Unsupported resolveName: ${resolveName}`); | ||
} | ||
} catch (err) { | ||
console.error("An error occurred in the YouTube API call:", err); | ||
throw err; | ||
} | ||
} | ||
|
||
module.exports = youtubeAPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.