Skip to content

Commit

Permalink
Add youtube as a source (#19)
Browse files Browse the repository at this point in the history
* 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
longshuicy authored May 15, 2024
1 parent 527f1c3 commit 375c008
Show file tree
Hide file tree
Showing 14 changed files with 21,083 additions and 77 deletions.
88 changes: 88 additions & 0 deletions API/youtubeAPI.js
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;
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add YouTube as a data source [#18](https://github.com/ncsa/standalone-smm-smile-graphql/issues/18)
-
## [0.3.0] - 2024-01-23
### Added
- Github action for auto generating docker container [#4](https://github.com/ncsa/standalone-smm-smile-graphql/issues/4)
Expand Down
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ if (process.env.DOCKERIZED==='true') {
TWITTER_CONSUMER_SECRET = process.env.TWITTER_CONSUMER_SECRET;
FLICKR_CONSUMER_KEY = process.env.FLICKR_CONSUMER_KEY;
FLICKR_CONSUMER_SECRET = process.env.FLICKR_CONSUMER_SECRET;
GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
GOOGLE_CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET;
}
else{
var config = require('./graphql_config.json');
TWITTER_CONSUMER_KEY = config.twitter.client_id;
TWITTER_CONSUMER_SECRET = config.twitter.client_secret;
FLICKR_CONSUMER_KEY = config.flickr.consumer_key;
FLICKR_CONSUMER_SECRET = config.flickr.consumer_secret;
GOOGLE_CLIENT_ID = config.google.client_id;
GOOGLE_CLIENT_SECRET = config.google.client_secret;
}

// view engine setup
Expand Down
5 changes: 5 additions & 0 deletions data/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var {

const twitterQueryType = require('./twitterSchema');
const redditQueryType = require('./redditSchema');
const youtubeQueryType = require('./youtubeSchema');

function wrapper(){
return {}
Expand All @@ -22,6 +23,10 @@ const Query = new GraphQLObjectType({
type: redditQueryType,
resolve:() => wrapper()
},
youtube:{
type: youtubeQueryType,
resolve:() => wrapper()
},
})
});

Expand Down
Loading

0 comments on commit 375c008

Please sign in to comment.