Restore "Most Views" download order

yt-dlp 2026.7.4 (via upstream PR #16965) again extracts view_count from
YouTube's lockupViewModel channel/playlist listings, so the 'views'
download order deprecated in fcb64fed can come back — verified live
against a real channel listing.

Unlike the old implementation, the sort degrades gracefully:

- Entries missing view_count sort last instead of raising KeyError.
- If NO entry has a view_count (yt-dlp older than ~2026.07), the
  download fails cleanly with UnrecoverableDownloadError telling the
  user to update yt-dlp or switch to Newest/Oldest — no retry loop.

Listing counts are approximate ("3K" -> 3000), which is fine for
ordering. No requirements.txt change needed; the >=2026.03.17 range
picks up 2026.7.4 automatically.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-07 15:40:37 -06:00
parent 6730235b7d
commit f65f0aca6b
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
5 changed files with 40 additions and 24 deletions

View file

@ -101,7 +101,7 @@ export const validUrlRegex = /^(http|https):\/\/[^ "]+$/;
export const downloadOrderOptions = [
{key: 'newest', text: 'Newest', value: 'newest'},
{key: 'oldest', text: 'Oldest', value: 'oldest'},
// 'views'/'Most Views' was removed: YouTube no longer provides view counts in channel/playlist listings.
{key: 'views', text: 'Most Views', value: 'views'},
];
export const downloadResolutionOptions = [

View file

@ -720,12 +720,15 @@ class ChannelDownloader(Downloader, ABC):
logger.debug(f'Downloading oldest videos from {download.url}')
downloads = downloads.reverse()
elif sort_key == 'views':
# YouTube no longer exposes `view_count` in flat channel/playlist listings, so we can no longer
# sort by views without fetching every video individually. This may become possible again if we
# integrate the YouTube Data API.
raise UnrecoverableDownloadError(
'Downloading by "Most Views" is no longer supported because YouTube no longer provides view '
'counts in channel/playlist listings. Please change this download to "Newest" or "Oldest".')
# Listing view counts are approximate ("3K" -> 3000); good enough for ordering.
logger.debug(f'Downloading most viewed videos from {download.url}')
if not any(i.get('view_count') is not None for i in downloads):
# Old yt-dlp (< 2026.07) does not extract view_count from YouTube's lockupViewModel
# listings. Fail cleanly rather than downloading in arbitrary order.
raise UnrecoverableDownloadError(
'yt-dlp did not report view counts for this channel/playlist. Update yt-dlp '
'(or change this download to "Newest"/"Oldest").')
downloads = sorted(downloads, key=lambda i: i.get('view_count') or 0, reverse=True)
# Limit the videos that will be downloads (this allows the user to download the "top 100" videos of a Channel)
if video_count_limit := settings.get('video_count_limit'):

View file

@ -708,12 +708,34 @@ async def test_download_playlist(test_session, test_directory, mock_video_extrac
}
def test_get_missing_videos_views_order_deprecated(test_session):
"""The 'views' download order was deprecated. YouTube no longer provides `view_count` in flat
channel/playlist listings, so a download configured for 'views' raises an unrecoverable error rather
than crashing with a KeyError or silently downloading in the wrong order."""
def test_get_missing_videos_views_order(test_session):
"""The 'views' download order sorts videos most-viewed-first. Entries missing a `view_count`
(some listings omit it) sort last rather than raising a KeyError."""
from modules.videos.downloader import channel_downloader
download = Download(
url='https://www.youtube.com/@example/videos',
settings={'download_order': 'views'},
info_json={'entries': [
{'_type': 'url', 'id': 'a', 'title': 'a', 'url': 'https://youtube.com/watch?v=a', 'view_count': 918},
{'_type': 'url', 'id': 'b', 'title': 'b', 'url': 'https://youtube.com/watch?v=b'}, # No view_count.
{'_type': 'url', 'id': 'c', 'title': 'c', 'url': 'https://youtube.com/watch?v=c', 'view_count': 3000},
]},
)
test_session.add(download)
test_session.commit()
missing_videos = channel_downloader.get_missing_videos(test_session, download)
assert missing_videos == [
'https://youtube.com/watch?v=c',
'https://youtube.com/watch?v=a',
'https://youtube.com/watch?v=b',
]
def test_get_missing_videos_views_order_no_view_counts(test_session):
"""When no entry has a `view_count` (old yt-dlp does not extract it from YouTube's lockupViewModel
listings), the 'views' download order fails cleanly rather than downloading in arbitrary order."""
from modules.videos.downloader import channel_downloader
# Flat entries no longer contain a `view_count` key (matches current yt-dlp behavior).
download = Download(
url='https://www.youtube.com/@example/videos',
settings={'download_order': 'views'},

View file

@ -301,9 +301,8 @@ class DownloadSettings:
if not self.download_metadata_only:
del self.download_metadata_only
# 'views' was deprecated: YouTube no longer provides view counts in channel/playlist listings.
if self.download_order not in (None, 'newest', 'oldest'):
raise ValidationError(f'Download order must be one of newest, oldest, or null.')
if self.download_order not in (None, 'newest', 'oldest', 'views'):
raise ValidationError(f'Download order must be one of newest, oldest, views, or null.')
if self.video_format not in (None, 'mp4', 'mkv', 'ogg', 'webm'):
raise ValidationError(f'Video format must be one of mp4, mkv, ogg, webm, or null.')

View file

@ -914,20 +914,12 @@ def test_download_settings_coerces_numeric_strings():
assert 'max_pages' not in req.settings
@pytest.mark.parametrize('order', ['newest', 'oldest', None])
@pytest.mark.parametrize('order', ['newest', 'oldest', 'views', None])
def test_download_settings_accepts_valid_download_order(order):
from wrolpi.schema import DownloadSettings
DownloadSettings(download_order=order)
def test_download_settings_rejects_deprecated_views_order():
"""'views' download order was deprecated; YouTube no longer provides view counts in listings."""
from wrolpi.schema import DownloadSettings
from wrolpi.errors import ValidationError
with pytest.raises(ValidationError):
DownloadSettings(download_order='views')
@pytest.mark.asyncio
async def test_downloads_config(test_session, test_download_manager, test_download_manager_config,
test_downloader, assert_downloads, tag_factory, await_switches):