Plugin for subscribing to YouTube channel feed?

I know how to manually build a URL for a YouTube channel’s feed based on the HTML source of one of the feed’s videos, but… I’m getting kind of tired of actually doing it. I remember reading (almost certainly here on these forums) about a plugin that will build the feed URL automatically based on the HTML URL (so you can just “subscribe to feed” in TT-RSS for a YouTube feed just like you can for typical feeds). So, I just came back here a few minutes ago to look for that plugin. Unfortunately, I haven’t been able to find it.

Does anyone know where to get that plugin? Thanks.

Here’s one you can try (plugins.local/youtube_subscribe/init.php):

<?php
class Youtube_Subscribe extends Plugin {
	public function about() {
		return [
			null, // version
			'Subscribe to YouTube channels', // description
			'wn', // author
			false, // is system
			'', // more info URL
		];
	}

	public function api_version() {
		return 2;
	}

	/**
	 * @param PluginHost $host
	 * @return void
	 **/
	public function init($host) {
		$host->add_hook($host::HOOK_PRE_SUBSCRIBE, $this);
	}

	function hook_pre_subscribe(&$url, $auth_login, $auth_pass) {
		if (self::should_handle($url)) {
			$content = UrlHelper::fetch(['url' => $url]);

			if ($content) {
				$doc = new DOMDocument();
				if ($doc->loadHTML($content)) {
					$xpath = new DOMXPath($doc);

					/** @var DOMElement|null */
					$alt_link = ($xpath->query('/html/body/link[@rel="alternate" and @type="application/rss+xml"]') ?: null)?->item(0);

					if ($alt_link?->getAttribute('href')) {
						$url = $alt_link->getAttribute('href');
						return true;
					}
				}
			}
		}
		return false;
	}

	private static function should_handle(string $url): bool {
		$url_host = parse_url($url, PHP_URL_HOST);
		return in_array($url_host, ['www.youtube.com', 'youtube.com'])
			&& !str_contains($url, '/feeds/videos.xml');
	}
}
2 Likes

Thanks very much! It’s working great.

Looked into this a bit more. With https://gitlab.tt-rss.org/tt-rss/tt-rss/-/commit/7a5ea2a2b9ea9e1d9ff5134d626c9a0c4a905e0e (in the Docker image from a few hours ago) you should no longer need that plugin.

3 Likes