Skip to content

Instantly share code, notes, and snippets.

@yyoshiki41
Last active February 18, 2017 11:57
Show Gist options
  • Select an option

  • Save yyoshiki41/c7b5b839f1ab8872c52c1c7641b90910 to your computer and use it in GitHub Desktop.

Select an option

Save yyoshiki41/c7b5b839f1ab8872c52c1c7641b90910 to your computer and use it in GitHub Desktop.
var API_KEY = '';
var VIDEO_ID = '';
function trigger() {
getStats();
}
function getStats() {
url = 'https://www.googleapis.com/youtube/v3/videos?id=' + VIDEO_ID + '&key=' + API_KEY + '&fields=items(statistics)&part=statistics'
var response = UrlFetchApp.fetch(url);
if (response.getResponseCode() != 200) {
Logger.log("Error %s:", response);
return;
}
var json = JSON.parse(response.getContentText());
var stats = json['items'][0]['statistics'];
as = SpreadsheetApp.getActiveSheet();
lastRow = as.getLastRow();
cell = as.getRange('A1');
var d = new Date();
now = Utilities.formatDate(d, 'JST', 'M/d HH:mm');
cell.offset(lastRow, 0).setValue(now);
cell.offset(lastRow, 1).setValue(stats['viewCount']);
cell.offset(lastRow, 2).setValue(stats['likeCount']);
cell.offset(lastRow, 3).setValue(stats['dislikeCount']);
cell.offset(lastRow, 4).setValue(stats['favoriteCount']);
cell.offset(lastRow, 5).setValue(stats['commentCount']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment