Skip to content

Instantly share code, notes, and snippets.

@brentschooley
Created January 20, 2020 07:11
Show Gist options
  • Save brentschooley/b5e39589360fcd854890138c281c3133 to your computer and use it in GitHub Desktop.
Save brentschooley/b5e39589360fcd854890138c281c3133 to your computer and use it in GitHub Desktop.
Code for saving YouTube Data API info to a Google Sheet (https://youtu.be/DZQWcQ2Z1Tw)
const got = require('got');
const GoogleSpreadsheet = require('google-spreadsheet');
const { promisify } = require('util');
const creds = require('./client_secret.json');
const doc = new GoogleSpreadsheet(process.env.SPREADSHEET_ID);
(async () => {
await promisify(doc.useServiceAccountAuth)(creds);
const info = await promisify(doc.getInfo)();
const sheet = info.worksheets[0];
const rows = await promisify(sheet.getRows)();
const ids = rows.map(r => r.id).join(',');
const response = await got(
`https://www.googleapis.com/youtube/v3/videos?part=snippet,statistics&id=${ids}&key=${process.env.YOUTUBE_API_KEY}`
);
items = JSON.parse(response.body).items;
console.log(items);
let index = 0;
for(const item of items) {
rows[index].title = item.snippet.title;
rows[index].views = item.statistics.viewCount;
rows[index].likes = item.statistics.likeCount;
rows[index].dislikes = item.statistics.dislikeCount;
rows[index].comments = item.statistics.commentCount;
await promisify(rows[index].save)();
index++;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment