Last active
March 15, 2017 14:07
-
-
Save beaucarnes/93dfd012619bdb4244c1f1ccb1417234 to your computer and use it in GitHub Desktop.
Google App Script to get a daily e-mail of all new comments from a specific YouTube playlist.
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 myFunction() { | |
var playlistID = "[PLALIST ID GOES HERE]" | |
var APIKey = "[GET AN API KEY FROM GOOGLE TO GO HERE]" | |
var toEmail = "[E-MAIL OF COMMENTS WILL BE SENT TO THIS ADDRESS]" | |
var channelIds = []; | |
var message = ""; | |
var totalPlaylistViews = 0; | |
var TIME_LENGTH = 60 * 60 * 1000 * 24.5; // Will get all comments from the last 24.5 hours | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
// These next few variables are to help determine how many views per day | |
var firstDayOfVideos = new Date(2017, 1, 8); | |
var today = new Date(); | |
var numberOfDaysSinceFirstDay = Math.floor((today.getTime() - firstDayOfVideos.getTime()) / (1000 * 60 * 60 * 24)); | |
// Get list of videos on playlist | |
var playlist = UrlFetchApp.fetch("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=50&playlistId=" + playlistID + "&key=" + APIKey); | |
var playlistDetails = JSON.parse(playlist).items | |
// Get comments on each video on the playlist | |
playlistDetails.forEach(function(element) { | |
var videoID = element.snippet.resourceId.videoId; | |
var videoTitle = element.snippet.title; | |
var commentList = UrlFetchApp.fetch("https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&maxResults=20&videoId="+ videoID +"&key=" + APIKey); | |
var commentDetails = JSON.parse(commentList).items | |
// Get view count on each video | |
var videoStatistics = UrlFetchApp.fetch("https://www.googleapis.com/youtube/v3/videos?part=statistics&id="+ videoID +"&key=" + APIKey); | |
totalPlaylistViews += parseInt(JSON.parse(videoStatistics).items[0].statistics.viewCount) | |
// Add each comment to the message body of the e-mail | |
commentDetails.forEach(function(commentElement) { | |
var commentTime = new Date(commentElement.snippet.topLevelComment.snippet.updatedAt); | |
var commentElement = commentElement | |
if (((new Date) - commentTime) < TIME_LENGTH) { | |
var commentText = commentElement.snippet.topLevelComment.snippet.textDisplay; | |
message += commentText + "<br><i>From video <a href='https://www.youtube.com/watch?v=" + videoID + "'>" + videoTitle + "</a></i><br>-----------------------<br><br>"; | |
}; | |
}); | |
}); | |
// Add view counts at the end of the message | |
var viewsSinceYesterday = totalPlaylistViews - (scriptProperties.getProperty('YESTERDAY_VIEWS')); | |
message += "Total Playlist Views: " + totalPlaylistViews + | |
"<br>Views in last 24 hours: " + viewsSinceYesterday + | |
"<br>Average views per day since beginning: " + Math.floor((totalPlaylistViews / numberOfDaysSinceFirstDay)); | |
scriptProperties.setProperty('YESTERDAY_VIEWS', totalPlaylistViews); // Set YESTERDAY_VIEWS for use when script runs tomorrow | |
// Send e-mail | |
MailApp.sendEmail({ | |
to: toEmail, | |
subject: "Daily Comments", | |
htmlBody: message, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment