Skip to content

Instantly share code, notes, and snippets.

@godfreyd
Last active April 17, 2018 08:40
Show Gist options
  • Save godfreyd/e103013e1fe480965cd84b3e7040d04b to your computer and use it in GitHub Desktop.
Save godfreyd/e103013e1fe480965cd84b3e7040d04b to your computer and use it in GitHub Desktop.
var google = require('googleapis'),
OAuth2 = google.auth.OAuth2;
function GoogleYoutube(credentials) {
this.oauth2Client = new OAuth2(credentials.client_id, credentials.client_secret, credentials.redirect_url);
};
GoogleYoutube.prototype.searchList = function(user, params, callback) {
this.oauth2Client.setCredentials({
access_token: user.token,
refresh_token: user.refreshtoken
});
var youtube = google.youtube({
version: 'v3',
auth: this.oauth2Client
});
youtube.search.list(params, function(err, response) {
err ? callback(err, null) : callback(null, response);
});
};
module.exports = function(config, user, params) {
return new Promise(function(resolve, reject) {
(new GoogleYoutube(config)).searchList(user, params, function(err, data) {
if (err) return reject(err);
if (!data.items.length) return resolve({});
resolve({
nextPageId: data.nextPageToken,
videos: data.items.map(function(item) {
return {
name: item.snippet.channelTitle,
time: item.snippet.publishedAt, // RFC 3339 formatted date-time
q: params.q,
nextpage: data.nextPageToken,
url: 'https://www.youtube.com/embed/' + item.id.videoId,
service: 'youtube'
};
})
});
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment