Digital Cabin

log whoami uses code feeds

You can use RSS with YouTube!

by dweller - 2024-11-09

#util #sh #rss


I am a big proponent of web syndication like RSS or Atom (which, BTW, I use for my feeds.) I think it’s a better way to get updates for all sorts of things than something like push notifications. Chief reason for this is that BOTH publishers and subscribers have more control over how and when to publish or get updates. I am sure you’ve seen these icons at least a few times while “surfing” (that’s how “browsing” was called by some back in the day) online:

Syndication was popular with many techy people back in the early-ish days of the web, but it never really took off with general population, which is a shame. It did become popular with podcasters and virtually all podcasts have feeds you can subscribe with whatever podcast software you use. Of course, even that is getting shoehorned into proprietary “cloud” apps nowadays.

I am saying all of this because there are vestiges of syndication in places you won’t expect them to be. Like major social media sites, or why I am writing this post, YouTube. I don’t know how long they will keep this around, but it is really useful! It allows you to subscribe to YouTube channels without having a Google account!

In order to do this you need to find a channel_id of the, well, channel you want to subscribe to and just append it to https://www.youtube.com/feeds/videos.xml?channel_id=. You can find the channel’s channel_id (this is getting to be a mouthful) by going into the HTML source of the channel’s page. Yea… YouTube doesn’t want you to just click on a cute little icon, they want you to make an account with them, get into their ecosystem and watch ads (more on that later.)

In order to make it easier on you, dear reader, I will share my little scripts I made to just scrape the ids semi-automatically and create the URLs you can feed into your feed reader. I am using newsboat, which just expects a URL and then, optionally, name and tags that you can use for sorting, searching and defining custom queries. It’s a great tool, and CLI too, just like I like them! ;) (You, of course, can use whatever you like.)

So alright, alright, let’s cut to the chase! If you already have an account with YouTube you can go to your subscriptions page and run this piece of Ja- sorry, JavaS- ehem JavaScript 🤮 uuugh, my bad… Anyways run this piece of code in your browser’s developer tools’ console (usually the shortcut is F12):

(Oh and make sure to load the page fully, it’s lazy loading, so scroll all the way down.)

var arr = Array.from(document.getElementsByClassName("channel-link"));
var uarr = [...new Map(arr.map(x => [x.href, x])).values()];
var chans = uarr.map(x => [x.href, x.getElementsByTagName("yt-formatted-string")[0].innerHTML]); 

txt = ""
for(i = 0; i < chans.length; i++)
{
    txt += chans[i][0] + " \"~" + chans[i][1] + "\" yt video" + "\n"
}

console.log(txt)

This will just print all of the channel URLs (chans[i][0]), their names (chans[i][1]), as well as, some tags I use for newsboat, you can remove or change them however you like.

If you don’t have an account with YouTube/Google, you’ll have to manually create a list of channel URLs, one per line.

Once you have all the channels you want to subscribe to via RSS saved in one file, you can run this shell script to fetch the channel_id and create a list of feed URLs:

#!/bin/sh
set -e

# NOTE:
# expets each channel link on a separate line
# expects you to redirect stdout to whatever file you want. stderr is for "progress"


if [ $# -lt 1 ]; then
    echo "Usage: $(basename $0) channel_list" >&2
    exit 1
fi

while read chan; do
    url=$(echo "$chan" | cut -d' ' -f1)
    tags=$(echo "$chan" | cut -d' ' -f2-)

    chanid="$(curl -Ls "$url" | grep -Po '"browse_id","value":"\K[^"]*' | head -1)"

    >&2 printf "%-32s -> %s\n" "$(echo "$url" | cut -d'/' -f4)" "$chanid"
    printf "https://www.youtube.com/feeds/videos.xml?channel_id=%s %s\n" "$chanid" "$tags"
done < "$1"

All it does it curls the channel URL, finds the channel_id which is browse_id in the channel’s HTML and combine the name and tags extracted before to make a newsboat formatted URL entry. You can just not append $tags if you want purely feed URLs.

And voilà! It’s done. Now you can add these feeds into your feed reader of choice and off you go! Hopefully YouTube won’t remove or just change it for no reason in the future, but we both know they probably will…

I use mpv as my video player, and it supports yt-dlp so I can just open YouTube video URLs in mpv and watch them with my system’s video player. In newsboat I added a macro to open feed links in mpv and other stuff:

browser "xdg-open %u"

macro w set browser "w3m %u"; open-in-browser; set browser "xdg-open %u"
macro v set browser "mpv %u"; open-in-browser; set browser "xdg-open %u"
macro i set browser "feh %u"; open-in-browser; set browser "xdg-open %u"

I also created custom query in the urls file to have an equivalent of a subscription feed:

"query:?? YouTube Subs:(tags # \"yt\") and (unread = \"yes\")"

So not only do I get a Google account free YouTube subscriptions, I also get no ads :P Well, unless the creator adds a sponsored bit into the video, but that’s fine. Or until YouTube starts adding ads into the video stream, then it’s back to TV days… At that point might as well just stick to reading blogs :P

Hope this was useful for ya,
– dweller


[Valid Atom 1.0] More…
If you can spare some $$$... Help Ukraine: Or me :P