Last active
March 25, 2023 14:44
-
-
Save tallcoleman/595aef9b7b97ac9845aa71d42ce185ee to your computer and use it in GitHub Desktop.
Youtube playlists - email notification when new videos are posted
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
// INSTRUCTIONS | |
// 1. Go to https://script.google.com/ and make a new project | |
// (optional: rename it too, so you remember what it is later...) | |
// 2. Paste in this script and add your email and playlist IDs below (see note [1]) | |
// 3. Save the script and do a test run to set up the permissions | |
// 4. Set up a trigger to run the check_playlists_youtube function every day | |
function check_playlists_youtube() { | |
// OPTIONS: specify playlists to check and email to notify | |
const PLAYLISTS = { | |
'Joanna Stern WSJ': "PLqQNt9DP_BNAU8c1KroIe-PAmrhu-7Xcn" | |
}; // format: 'your title': 'playlist id' | |
const EMAIL_ADDRESS = "[email protected]"; // put your email here | |
const SAFETY_BUFFER = 1; // overlap between checks, in hours (see note [2]) | |
// check playlists | |
const MAX_RESULTS = 50; | |
let new_videos = []; | |
for (let [playlist_name, playlist_id] of Object.entries(PLAYLISTS)) { | |
// get recent playlist entries | |
let playlist_items = YouTube.PlaylistItems.list("snippet", { | |
playlistId: playlist_id, | |
maxResults: MAX_RESULTS | |
}); | |
// check if there are any new videos from the last 24h | |
let newest_video_date = new Date(playlist_items.items[0].snippet.publishedAt); | |
if ((new Date() - newest_video_date)/1000/60/60 < (24 + SAFETY_BUFFER)) { | |
new_videos.push({ | |
'playlist_name': playlist_name, | |
'video_title': playlist_items.items[0].snippet.title, | |
'video_link': "https://www.youtube.com/watch?v=" + | |
playlist_items.items[0].snippet.resourceId.videoId | |
}); | |
} | |
} | |
// send email alert if there are new videos | |
const RE_HTML = /<\/?([bp]|br)>/gi | |
if (new_videos.length > 0) { | |
let video_noun = new_videos.length > 1 ? 'videos' : 'video'; | |
let email_subject = `${new_videos.length} new playlist ${video_noun}: ` + | |
Array.from(new Set(new_videos.map(x => x.playlist_name))).join(", "); | |
let email_body_lines = [`<p><b>New playlist ${video_noun}:</b></p>`]; | |
for (let video of new_videos) { | |
email_body_lines.push(`<p><b>${video.playlist_name}</b><br>\n` + | |
`Title: ${video.video_title}<br>\n` + `Link: ${video.video_link}`); | |
} | |
let email_body = email_body_lines.join(`\n\n`); | |
GmailApp.sendEmail(EMAIL_ADDRESS, email_subject, | |
email_body.replace(RE_HTML, ''), { htmlBody: email_body }); | |
} | |
} | |
/* | |
NOTES | |
----- | |
[1]: If you don't know how to get the id for your playlist, it's the 'list' parameter | |
in the URL of the playlist page. For example, the URL for "Tech Things with Joanna | |
Stern" is https://www.youtube.com/playlist?list=PLqQNt9DP_BNAU8c1KroIe-PAmrhu-7Xcn | |
and the playlist id is 'PLqQNt9DP_BNAU8c1KroIe-PAmrhu-7Xcn'. | |
You can check multiple playlists by setting up your list like this: | |
const PLAYLISTS = { | |
'Your Title A': "long_id_A", | |
'Your Title B': "long_id_B", | |
... | |
'Your Title Z': "long_id_Z" | |
}; | |
[2]: Daily triggers run at some point within in a one-hour window each day, | |
so adding a safety buffer of one hour makes sure you don't miss any videos | |
if the trigger runs early one day and late the next. You may get two notifications | |
for the same video if that video was published during the hour your trigger runs, | |
but this shouldn't be too common. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment