Sometime last Spring YouTube dropped a feature I was using called Collections. Essentially, Collections were just subgroups within your subscriptions. This was a cool feature if you subscribe to a bunch of music channels. Many electronic music labels and artists will throw their newest tracks on YouTube with a simple backdrop for the video. I could jump into my Music Collection and play new videos restricted to the ten or so music channels I liked.
After they removed the feature, I’ve been struggling to keep up with those channels. It was quite a bit harder to just surf through the full list of new videos from my subscriptions and pick out the music videos. You also end up having to click on each one. I couldn’t just turn it on and let it play through at work.
After screwing around with RSS feeds of each channel and attempting to setup a special category in my feed reader, I decided to write a script to do what I wanted.
The script I came up with is written in Ruby and uses a gem called Yt. The idea was to pull the latest videos from a select few channels and make a weekly mixtape playlist for my user account.
Overall, the script that resulted is pretty simple. Check it out on Github
Since Google uses OAuth to manage authentication for anything user related (eg. creating a new playlist) things become slightly more complicated, than they may first seem. A sloppy summarization of what you’ll need to setup to run a script against your own YouTube account is as follows:
Obtain a Client ID, Secret, and API Key from Google. You’ll need access to the YouTube Data API.
Determine the scopes you will need. For my script
Use your Client ID, Client Secret, and scope values to hit an authentication URL on Google. This is where you grant your script access to do the actions requested by your scope values.
From step 3, you will end up with an Authorization Code. This is usually part of the URL that you are sent to after you grant access. For the return URL I generally just used
Use the Authorization Code to request a Refresh Token (long lived). This token is what you will save for use with your script.
Once you have all of those chunks of user data you can add them as bash/zsh Environment variables or use a runner shell script to set them for you. It will depend on how you are planning to run the script, but I’ve found that using a little runner shell script is pretty versatile. Mine simple sets the ruby version, sets the Environment variables, and runs the script.
My ultimate goal was to make something that was automated, so I wanted this script to be run once a week. Instead of using cron or launchd, I decided to use Keyboard Maestro. This might seem strange, but it does have a couple advantages. If you are using a laptop, having something reliably run at the same time every week can be a little dicey. What if the laptop is powered down or sleeping at the time? Also, I wanted to get a notification that a new mixtape had been generated. Keyboard Maestro allows me to handle all of that pretty easily. I was able to get it setup so that on Monday morning, the first time my computer comes out of sleep this script gets kicked off and then automatically opened in my browser.
I’ve been running the script for a couple weeks now, and I am happy with the results.