Skip to content

Instantly share code, notes, and snippets.

@arran4
Last active March 16, 2018 02:23
Show Gist options
  • Save arran4/8597ad91491f1163b070cb6a8b29399c to your computer and use it in GitHub Desktop.
Save arran4/8597ad91491f1163b070cb6a8b29399c to your computer and use it in GitHub Desktop.
// http://www.reddit.com/user/DiRaven
// 11/01/2016
// Run this once to grant script access and add trigger automatically
function AddAddNewVideosToWatchLaterTrigger()
{
ScriptApp.newTrigger('AddNewVideosToWatchLater')
.timeBased()
.everyHours(24)
.create();
}
// Automatically adds new subscription videos from youtube to watch later list (if you have email notifications for those turned on)
function AddNewVideosToWatchLater()
{
var regexp = /\/watch(?:\?|%3F)v%3D([^%]+)/i;
var threads = GmailApp.search('label:"YouTube" is:unread');
Logger.log("Found " + threads.length + " threads");
for (var i in threads) {
try {
var messages = threads[i].getMessages()
Logger.log("Thread " + i + " has " + messages.length + " messages");
for (var k in messages)
{
var msg = messages[k];
Logger.log("Processing " + msg.getSubject());
if (msg.isUnread())
{
Logger.log("Is unread");
var body = msg.getBody()
var video_id = regexp.exec(body);
if (video_id != null) {
Logger.log("Found video id: " + video_id);
video_id = video_id[1];
var part= 'id';
var details = {
videoId: video_id,
kind: 'youtube#video'
};
var resource = {
snippet: {
playlistId: 'WL',
resourceId: details
}
};
YouTube.PlaylistItems.insert(resource, 'snippet');
Logger.log("Is now in watch list");
msg.markRead()
Logger.log("Is now read");
}
}
}
// threads[i].moveToArchive();
}
catch(e) {
Logger.log("Error!!");
Logger.log(e.message);
switch (e.message) {
case "Video not found.":
case "Video already in playlist.":
Logger.log("Marking as read");
msg.markRead();
break;
}
}
}
var date = new Date();
var yyyymmdd = Utilities.formatDate(date, "Australia/Melbourne", "yyyy-MM-dd EE");
var email = Session.getActiveUser().getEmail();
Logger.log("Sending email to: " + email);
GmailApp.sendEmail(email, "Youtube watch later " + yyyymmdd, "Log:\n" + Logger.getLog());
}
function test() {
var regexp = /\/watch\?v%3D([^%]+)"/i;
result = regexp.exec("/watch/v%3Dfdasasdf");
Logger.log(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment