Skip to content

Instantly share code, notes, and snippets.

@lakshaygupta21
Created June 19, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lakshaygupta21/75b63fab55592b63b34413c61a981171 to your computer and use it in GitHub Desktop.
Save lakshaygupta21/75b63fab55592b63b34413c61a981171 to your computer and use it in GitHub Desktop.
require('dotenv').config()
var {
google
} = require('googleapis');
var OAuth2 = google.auth.OAuth2;
const VIDEO_ID = process.env.VIDEO_ID
//main()
async function main() {
try {
const auth = authorize()
const videoViews = await getVideoViews(auth)
const videoTitle = await updateVideoTitle(auth, videoViews)
console.log(videoTitle)
} catch (e) {
console.error(e)
}
}
function authorize() {
const credentials = JSON.parse(process.env.CLIENT_SECRET)
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var oauth2Client = new OAuth2(clientId, clientSecret, redirectUrl);
oauth2Client.credentials = JSON.parse(process.env.OAUTH_TOKEN);
return oauth2Client
}
function getVideoViews(auth) {
const service = google.youtube('v3')
return new Promise((resolve, reject) => {
service.videos.list({
auth: auth,
part: 'statistics',
id: VIDEO_ID
}, function(err, response) {
if (err) return reject(err)
resolve(response.data.items[0].statistics.viewCount)
})
})
}
function updateVideoTitle(auth, views) {
const service = google.youtube('v3')
return new Promise((resolve, reject) => {
service.videos.update({
auth: auth,
part: 'snippet',
resource: {
id: VIDEO_ID,
snippet: {
title: `This Video Has ${new Intl.NumberFormat('en-US').format(views)} Views`,
description: `Happy Coding! | ${new Intl.NumberFormat('en-US').format(views)} Views`,
categoryId: 27
}
}
}, function(err, response) {
if (err) return reject(err)
resolve(response.data.snippet.title)
})
})
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment