From 1f42c53dfec58533221ef0e0213459bbf98f3b15 Mon Sep 17 00:00:00 2001 From: Roland Date: Wed, 6 May 2026 16:57:57 -0600 Subject: [PATCH] Fixing bug where Channel Downloader would create empty videos.xml channel if RSS feed was sent to it. --- modules/videos/downloader.py | 18 ++++++++++ modules/videos/test/test_downloader.py | 48 +++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/modules/videos/downloader.py b/modules/videos/downloader.py index 9d1c8770..880779ed 100755 --- a/modules/videos/downloader.py +++ b/modules/videos/downloader.py @@ -388,6 +388,17 @@ async def fetch_video_duration(url: str) -> int: return duration +YOUTUBE_RSS_FEED_URL_RE = re.compile( + r'^https?://([\w.-]+\.)?youtube\.com/feeds/videos\.xml(\?|/|$)', + re.IGNORECASE, +) + + +def is_youtube_rss_feed_url(url: str) -> bool: + """Return True if `url` is a YouTube RSS feed URL (e.g. /feeds/videos.xml?channel_id=...).""" + return bool(YOUTUBE_RSS_FEED_URL_RE.match(url or '')) + + class ChannelDownloader(Downloader, ABC): """Handles downloading of videos in a Channel or Playlist.""" name = 'video_channel' @@ -405,6 +416,13 @@ class ChannelDownloader(Downloader, ABC): async def do_download(self, download: Download) -> DownloadResult: """Update a Channel's catalog, then schedule downloads of every missing video.""" _log_ytdlp_versions() + if is_youtube_rss_feed_url(download.url): + # yt-dlp returns no `uploader` or `channel_id` for an RSS feed URL, so falling through + # would create a junk channel named after `webpage_url_basename` ("videos.xml"). + raise UnrecoverableDownloadError( + f'Refusing to handle RSS feed URL with ChannelDownloader: {download.url}.' + f' Use the RSS Downloader (sub_downloader=video) instead.' + ) try: info = extract_info(download.url, process=False) except Exception as e: diff --git a/modules/videos/test/test_downloader.py b/modules/videos/test/test_downloader.py index a50af01c..3b68f6e0 100644 --- a/modules/videos/test/test_downloader.py +++ b/modules/videos/test/test_downloader.py @@ -10,7 +10,7 @@ import pytest from modules.videos.downloader import VideoDownloader, \ get_or_create_channel, channel_downloader, video_downloader, preview_filename, \ prepare_filename, convert_wrolpi_filename_format, _bot_blocked, _skip_download, \ - parse_ytdlp_progress + parse_ytdlp_progress, is_youtube_rss_feed_url from modules.videos.lib import get_videos_downloader_config from modules.videos.models import Channel, Video from wrolpi.conftest import test_directory, await_switches @@ -443,6 +443,52 @@ def test_bad_downloader(test_session, video_download_manager): video_download_manager.create_download(test_session, 'https://example.com', downloader_name='bad downloader') +@pytest.mark.parametrize('url,expected', [ + ('https://www.youtube.com/feeds/videos.xml?channel_id=UC2JGH5ZBtWQJuTYCyfI3M3Q', True), + ('http://youtube.com/feeds/videos.xml?channel_id=UC123', True), + ('https://www.youtube.com/feeds/videos.xml', True), + ('https://www.youtube.com/@SomeChannel/videos', False), + ('https://www.youtube.com/watch?v=abc123', False), + ('https://example.com/feeds/videos.xml', False), + ('', False), + (None, False), +]) +def test_is_youtube_rss_feed_url(url, expected): + """Detect YouTube RSS feed URLs so ChannelDownloader can refuse to process them.""" + assert is_youtube_rss_feed_url(url) is expected + + +@pytest.mark.asyncio +async def test_channel_downloader_rejects_rss_feed_url(test_session, mock_video_extract_info): + """ChannelDownloader must refuse YouTube RSS feed URLs. + + Previously, yt-dlp would return no `uploader`/`channel_id` for an RSS feed, so + `name = info.get('uploader') or info.get('webpage_url_basename')` fell back to + 'videos.xml' and `source_id` fell back to 'videos', creating a junk channel named + 'videos.xml' in directory 'videos/videos.xml' that never received any videos. + """ + download = Download( + url='https://www.youtube.com/feeds/videos.xml?channel_id=UC2JGH5ZBtWQJuTYCyfI3M3Q', + downloader=channel_downloader.name, + ) + test_session.add(download) + test_session.commit() + + # If the rejection was missing, ChannelDownloader would call extract_info and create a channel. + mock_video_extract_info.return_value = { + 'id': 'videos', + 'webpage_url_basename': 'videos.xml', + 'entries': [], + } + + with pytest.raises(UnrecoverableDownloadError): + await channel_downloader.do_download(download) + + # No Channel should have been created from the RSS URL. + assert test_session.query(Channel).count() == 0 + mock_video_extract_info.assert_not_called() + + @pytest.mark.asyncio async def test_video_download(test_session, test_directory, mock_video_extract_info, simple_channel, await_switches, video_download_manager, mock_video_process_runner, image_file):