Created
April 11, 2019 18:06
-
-
Save woodwardtw/0fa8d8672d4ef7ad3f38b4afb67032dc to your computer and use it in GitHub Desktop.
for Google Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getVideos (){ | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = ss.getSheetByName('videos'); | |
var last = sheet.getLastRow(); | |
var videos = sheet.getRange("A1:A"+last).getValues(); | |
for (var i = 0; i < videos.length; i++){ | |
var vidId = getVideoId(videos[i][0]); | |
getYTdata(vidId); | |
} | |
} | |
function getYTdata(vidId) { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("data"); | |
var apiKey = 'AIzaSyBAPaX4HvWBniJ5F6vjUUZ-VsdxPLPzWzE'; | |
var url = 'https://www.googleapis.com/youtube/v3/videos?id=' + vidId + '&key=' + apiKey + '&part=snippet,contentDetails,statistics,status'; | |
Logger.log(url); | |
var currentTime = new Date(); | |
var response = UrlFetchApp.fetch(url); // get feed | |
var json = response.getContentText(); // | |
var data = JSON.parse(json); | |
var stats = []; | |
if(data.pageInfo.totalResults > 0){ | |
stats.push(currentTime); | |
stats.push(data.items[0].snippet.title); | |
stats.push(data.items[0].statistics.viewCount); | |
stats.push(data.items[0].statistics.likeCount); | |
stats.push(data.items[0].statistics.dislikeCount); | |
stats.push(data.items[0].statistics.commentCount); | |
stats.push(data.items[0].snippet.publishedAt); | |
stats.push(data.items[0].snippet.thumbnails.high.url); | |
stats.push('https://www.youtube.com/watch?v=' + vidId); | |
sheet.appendRow(stats); | |
} | |
} | |
function getVideoId(url){ | |
var start = url.indexOf("?v=")+3; | |
var end = findEnd(url); | |
var id = url.substring(start,end); | |
return id; | |
} | |
function findEnd(url){ | |
var end = url.indexOf("&"); | |
if (end > 0) { | |
return end; | |
} else { | |
return url.length; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment