import base64 from datetime import datetime, timedelta, timezone from typing import Literal from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import func, or_, select from sqlalchemy.orm import Session from app.config import settings from app.core.auth_dependency import require_session from app.db import get_db from app.models.channel import Channel from app.models.channel_category import channel_categories from app.models.download_job import DownloadJob from app.models.video import Video from app.services.download_jobs import latest_jobs_map from app.services.video_presentation import channel_categories_map, serialize_video router = APIRouter(dependencies=[Depends(require_session)]) DEFAULT_LIMIT = 30 MAX_LIMIT = 100 def _encode_cursor(published_at: datetime, video_id: int) -> str: raw = f"{published_at.isoformat()}|{video_id}" return base64.urlsafe_b64encode(raw.encode()).decode() def _decode_cursor(cursor: str) -> tuple[datetime, int]: try: raw = base64.urlsafe_b64decode(cursor.encode()).decode() published_at_raw, video_id_raw = raw.rsplit("|", 1) published_at = datetime.fromisoformat(published_at_raw) if published_at.tzinfo is None: published_at = published_at.replace(tzinfo=timezone.utc) return published_at, int(video_id_raw) except Exception: raise HTTPException(status_code=400, detail="Invalid cursor") def _completed_video_ids(): # A video may have several download_jobs over time (retries, # redownload after delete); only its most recent one counts. latest_job_ids = select(func.max(DownloadJob.id)).group_by(DownloadJob.video_id) return select(DownloadJob.video_id).where( DownloadJob.id.in_(latest_job_ids), DownloadJob.status == "completed" ) @router.get("/feed/saved-counts") def get_saved_counts(db: Session = Depends(get_db)) -> dict: """Per-category counts of saved (downloaded) videos, for the Saved page's sidebar -- distinct from /api/categories' channel_count.""" completed_video_ids = _completed_video_ids() all_count = db.query(Video).filter(Video.id.in_(completed_video_ids)).count() categorized_channel_ids = select(channel_categories.c.channel_id) uncategorized_count = ( db.query(Video) .filter(Video.id.in_(completed_video_ids), ~Video.channel_id.in_(categorized_channel_ids)) .count() ) rows = ( db.query(channel_categories.c.category_id, func.count(func.distinct(Video.id))) .join(Video, Video.channel_id == channel_categories.c.channel_id) .filter(Video.id.in_(completed_video_ids)) .group_by(channel_categories.c.category_id) .all() ) categories = {str(category_id): count for category_id, count in rows} return {"all": all_count, "uncategorized": uncategorized_count, "categories": categories} @router.get("/feed") def get_feed( category_id: int | None = None, uncategorized: bool = False, channel_id: int | None = None, downloaded: bool = False, search: str | None = Query(None, max_length=200), new_only: bool = False, video_type: Literal["all", "long", "short"] = Query("all", alias="type"), anchor: str | None = None, limit: int = Query(DEFAULT_LIMIT, ge=1, le=MAX_LIMIT), cursor: str | None = None, db: Session = Depends(get_db), ) -> dict: query = db.query(Video) if channel_id is not None: query = query.filter(Video.channel_id == channel_id) elif uncategorized: categorized_channel_ids = select(channel_categories.c.channel_id) query = query.filter(~Video.channel_id.in_(categorized_channel_ids)) elif category_id is not None: channel_ids_in_category = select(channel_categories.c.channel_id).where( channel_categories.c.category_id == category_id ) query = query.filter(Video.channel_id.in_(channel_ids_in_category)) if new_only: since = datetime.now(timezone.utc) - timedelta(days=settings.new_videos_window_days) query = query.filter(Video.published_at >= since) # Align with the sidebar badge count (categories.py), which only # counts subscribed channels: after unsubscribing, a channel's # fresh videos must disappear from "new only" too. subscribed_channel_ids = select(Channel.id).where(Channel.subscribed.is_(True)) query = query.filter(Video.channel_id.in_(subscribed_channel_ids)) if downloaded: query = query.filter(Video.id.in_(_completed_video_ids())) if search and search.strip(): escaped = search.strip().replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") needle = f"%{escaped}%" query = query.join(Channel, Channel.id == Video.channel_id).filter( or_(Video.title.ilike(needle, escape="\\"), Channel.title.ilike(needle, escape="\\")) ) if video_type == "short": query = query.filter( Video.duration_seconds.is_not(None), Video.duration_seconds <= settings.shorts_max_duration_seconds, ) elif video_type == "long": query = query.filter( or_( Video.duration_seconds.is_(None), Video.duration_seconds > settings.shorts_max_duration_seconds, ) ) # The anchor is a one-off starting point for the Shorts feed: only the # first page (no cursor) is shifted to start at that video and go older. # Unknown anchors degrade silently to a regular first page. if anchor and not cursor: anchor_video = db.query(Video).filter(Video.youtube_video_id == anchor).first() if anchor_video is not None: query = query.filter( (Video.published_at < anchor_video.published_at) | ( (Video.published_at == anchor_video.published_at) & (Video.id <= anchor_video.id) ) ) if cursor: cursor_published_at, cursor_id = _decode_cursor(cursor) query = query.filter( (Video.published_at < cursor_published_at) | ((Video.published_at == cursor_published_at) & (Video.id < cursor_id)) ) query = query.order_by(Video.published_at.desc(), Video.id.desc()) rows = query.limit(limit + 1).all() next_cursor = None if len(rows) > limit: last_kept = rows[limit - 1] next_cursor = _encode_cursor(last_kept.published_at, last_kept.id) rows = rows[:limit] channel_ids = list({v.channel_id for v in rows}) channels = {c.id: c for c in db.query(Channel).filter(Channel.id.in_(channel_ids)).all()} categories_map = channel_categories_map(db, channel_ids) jobs_map = latest_jobs_map(db, [v.id for v in rows]) items = [] for video in rows: channel = channels.get(video.channel_id) if channel is None: continue items.append( serialize_video(video, channel, categories_map.get(channel.id, []), jobs_map.get(video.id)) ) return {"items": items, "next_cursor": next_cursor}