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 <noreply@anthropic.com>
This commit is contained in:
2026-05-26 22:42:58 +02:00
parent 6cfaca382c
commit be88d70935

View File

@@ -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()