Automating YouTube Playlists generation with YouTube API

Each week, my wife Jen and I enjoy watching our favorite YouTube channels together. Using a Python script generated by ChatGPT, I automated the process of curating a playlist of unwatched videos. Now, our custom playlist updates every Sunday, making our viewing routine seamless and hassle-free.

Automating YouTube Playlists generation with YouTube API

My wife Jen and I each week sit down in front of the TV in our living room and watch YouTube together. We have about 8 channels we watch and each week on sunday these channel put out new content to watch. Some of these we've been watching for years to its fun to sit down and watch them together.

I made this process pretty easy for myself by adding videos from each channel to a playlist early sunday and by the time we sit down to watch them I have all the videos lined up and ready to go so we can burn through them one by one. Since it's a playlist they just keep playing until were done which is nice and easy to watch without digging into each YouTube channel to find the latest content.

I wanted to automate this process by providing a script a list of YouTube channel IDs and have it scan the channel list and pluck out the videos we havent watched yet that are on this list and at it to the Sunday YouTube playlist for us to watch. Most of the time we end up watching all of the videos but sometimes we watch a few and then may wait until the following sunday or a few days later to inbox zero them and get everything watched. I approached this problem by using ChatGPT 4o to geneate a Python Script to make this happen. I have a linux box that I use for utilities such as this and it just runs scripts all the time doing by bidding. Here is what I asked ChatGPT to do

💡
I have a collection of youtube channels that I watch their latest content each week and would like you to write a python script that would look for the latest unwatched video from each channel and add it to my watch later playlist

It generated the command to install some python modules and then wrote the script I needed to do the basics of talking to the YouTube API

It then explained how I needed to open then Google Developer Console and setup the API, the scope for accessing the API and setting up the Oauth 2.0 creds to make it all happen. We also needed to setup some python modules to interact with the API. A few copy and pastes and we had a working setup. If I ran into any issues I'd just copy and paste the error and let ChatGPT solve it for me so we could get to a working result.

Problems with Watch Later and using a custom playlist

Above I mentioned we use a custom playlist but inactualiy we were using the Watch Later built in playlist from YouTube each week. We (ChatGPT and I) found out that the API we were using didnt suppose using the Watch Later playlist so I made a new one in YouTube, got the ID for the playlist and asked ChatGPT to add the functionality.

💡
Can you use a custom playlist with ID: PLT4hI3SgCr16yyLYKqFvxwtku772FhI and remove any watched videos from this playlist when you add new ones and be sure there are no duplicates.

With its new marching orders it generated the code for me. I then wanted to have it run this script weekly on sunday at 7pm just before Jen and I sitdown and watch TV together. It let me know it needed to add a Shebang at the beginning of the script so it can be run directly and know the path where the verison of python was run it from cron.

💡
can we make a cron job in linux to run this script each sunday at 7pm?

It explained how to make the script executible using chmod, how to edit crontab and it made the crontab entry for me to copy and paste. I ran this a few times and it worked well so I then asked it if we could add pushcut support to it and it generated the code to do so.

💡
can we add to the script to send a message to Pushover indicating that the script was ran, what videos were added and how many there were?

It had me run a pip command to add "requests" module and then updated the code to include that module and added the logic to do what I asked. It then indicated "This will ensure your script runs automatically at the specified time each week, cleans out the custom playlist, removes any duplicates, adds new videos, and sends a Pushover notification with the details."

What if you don't want to run this with Python? you could do this with Google's code implementation with Google Sheets or using any other programming language, I just know that Python has a lot that it can do so I chose that language to do this script.

Let's try Ruby

Just for giggles I asked ChatGPT to refactor this code and write it in ruby.

Pick your scripting language, add the script to cron and let'er rip.

What did I learn?

I learned that it's really quick to spin up a script if you know what to ask ChatGPT to make it happen. I also learned that WOW you can blow through your 10,000 API calls very quick with some testing. I ran the script a few times and by the end of it i exhausted the API limit really fast. There is some expensive processes happening in this script. I asked it at once point to do some optimization and to also include some checks on how much of the API calls it was using and it attempted to do so using some predefined values

search_list: 100, # Cost for each search.list request
playlist_items_list: 1, # Cost for each playlistItems.list request
playlist_items_insert: 50, # Cost for each playlistItems.insert request
playlist_items_delete: 50, # Cost for each playlistItems.delete request

I think this was a good learning experience in just how fast you can spend your API calls when using a free account and just doing some basic testing. For the 8 youtube channels we follow each week and each channel typically puts out 1-3 videos in that week (with shorts and full videos included in this) it shouldnt run into too many issues.

What's next?

I'll run some test this week and will let you know how it goes this sunday. What would you do different? How would you have ChatGPT approach this problem or if you are a coder in Python or Ruby what would you do different when approaching this problem?