Building a dynamic link forwarder
If you've ever looked at a YouTuber's social media profile (especially on Instagram), you'll have noticed that some of them like to link to their most recent video.
While I completely understand the motivation behind this, I'm worried that the vast majority of them updates these links manually in lack of a dynamic solution.
Let's fix that.
The goal was to have a fixed domain, which automatically redirects to the most recent video of the channel given in the url.
So a YouTube channel with the username dailygiraffevideos (I wish that channel existed) could use mylatest.video/dailygiraffevideos and it would redirect accordingly.
So far, so good. I've been playing around with HTML/CSS/JS recently (I built tastytunes to share songs with my friends who have similar taste in music), but those sites are all static. In order to redirect to every YouTube channel, I need a dynamic solution.
After a bit of googling, Express running on Node.js seemed like just what I needed. The routing examples in their documentation pretty much gave me everything I needed for the handling of dynamic requests. So using Express I could get the requested channel name as parameter, which I then need to feed into the YouTube API to get the latest video.
This splits up into two API calls: Using the channelName I can get the Playlist ID of the channel's uploads. A second request gives me the first n videos for that playlist ID, here we only care about the first one. (More on the YouTube API here)
If those two API requests succeed - meaning the channel exists and has uploaded at least 1 video, we get the video ID of the most recent video back. Nice!
Then the only thing left is to put that video ID in a youtube-url and redirect the page. And that's it!
You may peek at the code here - it's not very nice to look at since I lazied it out on the request callbacks, but maybe it'll help someone out there!
Other than that, feel free to try it out in action at http://mylatest.video/ and make sure to share the site next time you see some poor fella with hand-pasted links in their social media profile. :)