Automatically adding videos to a YouTube playlist

Have you ever found yourself wishing that all of the new videos posted by your YouTube subscriptions could automatically get added to a YouTube playlist of your choice? No? Well, then this post will be of limited interest to you. But for the rest of you, who likely found this after some convoluted set of searches, read on and see if the solution I came to for my particular problem can be of use to you.

First, some background. YouTube, with the recent launch of YouTube Red, added a feature for offline sync of videos. Individual videos or entire playlists can be downloaded for later viewing when a solid data connection may not be available. In my case, this was the bus commute. During rush hour in Chicago, T-Mobile is spotty at best. So I figured, why not sync my viewing material beforehand so I wouldn’t have to suffer through interminable buffering stutters?

Well, it turns out that while you can sync videos, it was a very manual process to get new videos from my subscriptions downloaded to my phone. I’d either have to sync them individually or add them to a playlist that I had set to save offline and then refresh the playlist. There was no “cache the most recent videos” option anywhere to be found.

After a bit of Googling, I landed upon a script from Reddit user DiRaven that appeared to do just what I wanted. Alighieri_Dante further built upon the work. Using Google Script, the Gmail and YouTube APIs, the script would scrape your Gmail account for any notifications from YouTube about new videos and add the video to your Watch Later playlist. So, turn on notifications for the subscriptions you want and away you go, run the script to grant permissions and set an hourly trigger, and away you go.

But of course, things weren’t quite done for me. While their scripts would get videos into a playlist for me, it didn’t offer much control beyond that. I also thought it would be helpful if videos could be set to remove themselves after a day or two, as my use case wasn’t as much about guaranteeing I saw each video as making sure I had something interesting to watch. So I added a customizable expiration date.

My offline settings

My offline settings, for now.

This keeps my phone’s storage from being overwhelmed. You can check your offline YouTube data storage in the settings of the app. I’ll have to fine tune the number of subscriptions I have and the number of days to hold onto to keep the storage usage in check, but for now it seems to be reasonable.

Then, being me, a couple of more options presented themselves. I wanted to be able to ignore announcements about upcoming live streams. Also, I didn’t want to download hour long videos, so I added an optional minimum and maximum length. Then I added the ability to use a specific playlist in case I wanted to keep the default Watch Later for its traditional use. And finally, the ability to easily add additional Gmail queries would let me set up multiple copies of the script to build multiple playlists for different subscriptions.

And of course, along the way I wanted to make sure it was all working. So an optional logging spreadsheet helps keep track off all the videos added and removed. It turned out that Alighieri_Dante’s script already did this, but I hadn’t noticed. Reinventing the wheel for the win!

The end result

I have the script set to run hourly, and it seems to work just fine. The only wrinkle? For now, it doesn’t seem there is a way to get YouTube to sync a playlist when it changes, but maybe someone else has figured that out. Or, knowing Google, we’ll eventually see a “keep recent videos synced” option. In the meantime, I’ll fire up YouTube a bit before heading out and hit refresh on the playlist.

So if you think this would be helpful to you, you can grab a copy of the script from my Google drive. I’ve got some instructions for use in the file, but basically you need to copy it to your drive, change the settings to your preferences, and run it at least once manually to set the permissions. Oh, and you need to make sure Google Advanced Services is enabled for your account. I’ve got instructions on that in the script comments too. If it works for you or you have questions, let me know.

Playlist Builder Google Script

Update – 4/29/2016

For some strange reason, I had to reauthorize the script to run this morning. So, if you notice you’re not getting new videos, open up the script and try running it manually to see if you need to do the same.

Update #2 – 9/28/2016

Thanks to some investigation by Steve (in the comments), I found a bug in how I was handling multiple search queries. Hopefully I fixed it. While I was in there, I corrected how I was handling the removal of old videos and the email searches to better respect the ‘daysToSave’ variable.