From be88d70935e98edb22eeff14797f72bee4aeb090 Mon Sep 17 00:00:00 2001 From: Mattias Thall Date: Tue, 26 May 2026 22:42:58 +0200 Subject: [PATCH] Fetch first video thumbnail for playlists with no thumbnail When yt-dlp returns no thumbnail for a playlist entry, fetch the playlist's first video (max_videos=1) and derive a stable thumbnail URL from its video ID. Applied during both the initial fetch and on index (already done on index in previous commit). Co-Authored-By: Claude Sonnet 4.6 --- backend/routers/playlists.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/routers/playlists.py b/backend/routers/playlists.py index dde2b2b..026bb35 100644 --- a/backend/routers/playlists.py +++ b/backend/routers/playlists.py @@ -68,6 +68,20 @@ def fetch_channel_playlists( return {"detail": "Fetching playlists"} +def _first_video_thumbnail(youtube_playlist_id: str) -> Optional[str]: + """Fetch just the first video of a playlist and return a stable thumbnail URL.""" + try: + videos = ytdlp.fetch_playlist_videos(youtube_playlist_id, max_videos=1) + if videos: + vid_id = videos[0].get("youtube_video_id") + if vid_id: + from ..services.ytdlp import _stable_thumbnail + return _stable_thumbnail(vid_id) + except Exception: + pass + return None + + def _fetch_playlists_task(channel_id: int, youtube_channel_id: str): from ..database import SessionLocal db = SessionLocal() @@ -75,20 +89,21 @@ def _fetch_playlists_task(channel_id: int, youtube_channel_id: str): playlists = ytdlp.fetch_channel_playlists(youtube_channel_id) for pl in playlists: pl_id = pl["youtube_playlist_id"] + thumb = pl.get("thumbnail_url") or _first_video_thumbnail(pl_id) existing = db.query(Playlist).filter_by(youtube_playlist_id=pl_id).first() if existing: existing.title = pl["title"] or existing.title if pl.get("video_count"): existing.video_count = pl["video_count"] - if pl.get("thumbnail_url") and not existing.thumbnail_url: - existing.thumbnail_url = pl["thumbnail_url"] + if thumb and not existing.thumbnail_url: + existing.thumbnail_url = thumb else: db.add(Playlist( youtube_playlist_id=pl_id, channel_id=channel_id, title=pl["title"], description=pl.get("description"), - thumbnail_url=pl.get("thumbnail_url"), + thumbnail_url=thumb, video_count=pl.get("video_count") or 0, )) db.commit()