Skip to content

Instantly share code, notes, and snippets.

@FirePanther
Last active November 17, 2016 04:10
Show Gist options
  • Save FirePanther/c4279d48c8a2280e9176 to your computer and use it in GitHub Desktop.
Save FirePanther/c4279d48c8a2280e9176 to your computer and use it in GitHub Desktop.
Parses YouTube Mails, sende them to a Server to Download the videos
function myFunction() {
var labelName = "YouTube",
label = GmailApp.getUserLabelByName(labelName),
threads = label.getThreads(),
msg, match, date, now = new Date(), afterTime = 1000 * 60 * 60;
Logger.log("Threads: " + threads.length);
for (var x in threads) {
Logger.log("Found thread: " + threads[x].getFirstMessageSubject());
msg = threads[x].getMessages();
match = msg[0].getPlainBody().match(/watch\?v=(.*?)\&/);
if (match !== null) {
Logger.log("Match found: " + match[1]);
date = msg[0].getDate();
if (date.getTime() < now.getTime() - afterTime && fetchYTDownload(match[1])) {
Logger.log("Downloaded video: " + msg[0].getSubject());
msg[0].markRead().moveToTrash();
msg[0].getThread().removeLabel(label);
}
}
}
}
function fetchYTDownload(v) {
Logger.log("Requesting: http://YOUR-URL/youtube.php?p=???&v=" + encodeURIComponent(v));
var response = UrlFetchApp.fetch("http://YOUR-URL/youtube.php?p=" + encodeURIComponent("YOUR-PW") + "&v=" + encodeURIComponent(v));
Logger.log("Response:");
Logger.log("-> " + response);
return response.getContentText().indexOf("success") !== -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment