Add Popular tab to channel page
- YouTube sort=p fetch: indexes top 100 most-viewed videos from a channel, storing view_count in the DB - Popular tab on channel page shows videos sorted by view_count DESC - Videos/Popular tab switcher with context-appropriate fetch buttons - Expose view_count in VideoOut; add 'popular' sort to channel videos endpoint Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -62,6 +62,7 @@ class VideoOut(BaseModel):
|
||||
is_downloaded: bool = False
|
||||
is_watched: bool = False
|
||||
queued: bool = False
|
||||
view_count: Optional[int] = None
|
||||
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
@@ -608,10 +609,11 @@ def get_channel_videos(
|
||||
):
|
||||
_get_channel_or_404(db, channel_id)
|
||||
order = {
|
||||
"newest": "v.published_at DESC NULLS LAST",
|
||||
"oldest": "v.published_at ASC NULLS LAST",
|
||||
"title": "v.title ASC",
|
||||
"unwatched": "COALESCE(uv.watched, 0) ASC, v.published_at DESC NULLS LAST",
|
||||
"newest": "v.published_at DESC NULLS LAST",
|
||||
"oldest": "v.published_at ASC NULLS LAST",
|
||||
"title": "v.title ASC",
|
||||
"unwatched":"COALESCE(uv.watched, 0) ASC, v.published_at DESC NULLS LAST",
|
||||
"popular": "v.view_count DESC NULLS LAST",
|
||||
}.get(sort, "v.published_at DESC NULLS LAST")
|
||||
params: dict = {"user_id": current_user.id, "channel_id": channel_id, "limit": limit, "offset": offset}
|
||||
q_clause = ""
|
||||
@@ -621,7 +623,7 @@ def get_channel_videos(
|
||||
rows = db.execute(
|
||||
text(f"""
|
||||
SELECT v.id, v.youtube_video_id, v.title, v.thumbnail_url,
|
||||
v.duration_seconds, v.published_at,
|
||||
v.duration_seconds, v.published_at, v.view_count,
|
||||
COALESCE(uv.downloaded, 0) AS is_downloaded,
|
||||
COALESCE(uv.watched, 0) AS is_watched
|
||||
FROM videos v
|
||||
@@ -635,6 +637,77 @@ def get_channel_videos(
|
||||
return [VideoOut(**dict(r)) for r in rows]
|
||||
|
||||
|
||||
@router.post("/{channel_id}/fetch-popular", status_code=status.HTTP_202_ACCEPTED)
|
||||
def fetch_popular_videos(
|
||||
channel_id: int,
|
||||
background_tasks: BackgroundTasks,
|
||||
db: Session = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""Fetch the channel's most popular videos from YouTube and index them."""
|
||||
channel = _get_channel_or_404(db, channel_id)
|
||||
background_tasks.add_task(_fetch_popular_task, channel_id, channel.youtube_channel_id)
|
||||
return {"detail": "Fetching popular videos"}
|
||||
|
||||
|
||||
def _fetch_popular_task(channel_id: int, youtube_channel_id: str):
|
||||
from ..database import SessionLocal
|
||||
db = SessionLocal()
|
||||
try:
|
||||
if youtube_channel_id.startswith("@"):
|
||||
url = f"https://www.youtube.com/{youtube_channel_id}/videos?sort=p"
|
||||
else:
|
||||
url = f"https://www.youtube.com/channel/{youtube_channel_id}/videos?sort=p"
|
||||
|
||||
stdout, _, code = ytdlp._run([
|
||||
"yt-dlp", url,
|
||||
"--dump-json", "--flat-playlist",
|
||||
"--playlist-end", "100",
|
||||
"--quiet",
|
||||
*ytdlp._cookie_args(),
|
||||
], timeout=120)
|
||||
|
||||
channel = db.query(Channel).filter_by(id=channel_id).first()
|
||||
if not channel:
|
||||
return
|
||||
|
||||
for line in stdout.splitlines():
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
try:
|
||||
info = json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
yt_id = info.get("id")
|
||||
if not yt_id:
|
||||
continue
|
||||
existing = db.query(Video).filter_by(youtube_video_id=yt_id).first()
|
||||
view_count = info.get("view_count")
|
||||
published_at = ytdlp._parse_published(info)
|
||||
if existing:
|
||||
if view_count is not None:
|
||||
existing.view_count = view_count
|
||||
if published_at and not existing.published_at:
|
||||
existing.published_at = published_at
|
||||
else:
|
||||
db.add(Video(
|
||||
youtube_video_id=yt_id,
|
||||
channel_id=channel.id,
|
||||
title=info.get("title", ""),
|
||||
thumbnail_url=ytdlp._stable_thumbnail(yt_id),
|
||||
duration_seconds=info.get("duration"),
|
||||
published_at=published_at,
|
||||
tags=json.dumps(info.get("tags") or []),
|
||||
view_count=view_count,
|
||||
))
|
||||
db.commit()
|
||||
except Exception:
|
||||
db.rollback()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.post("/{channel_id}/search", status_code=status.HTTP_202_ACCEPTED)
|
||||
def search_channel_youtube(
|
||||
channel_id: int,
|
||||
|
||||
Reference in New Issue
Block a user