"""Channel API routes.""" from fastapi import APIRouter, HTTPException from pydantic import BaseModel from core import youtube router = APIRouter() class ChannelCreate(BaseModel): channel_id: str channel_name: str @router.get("") def list_channels(): return youtube.get_active_channels() @router.post("", status_code=201) def create_channel(body: ChannelCreate): try: row_id = youtube.add_channel(body.channel_id, body.channel_name) return {"id": row_id, "channel_id": body.channel_id} except Exception as e: if "UQ_CHANNELS_CID" in str(e).upper(): raise HTTPException(409, "Channel already exists") raise @router.post("/{channel_id}/scan") def scan_channel(channel_id: str): """Trigger a scan for new videos from this channel.""" channels = youtube.get_active_channels() ch = next((c for c in channels if c["channel_id"] == channel_id), None) if not ch: raise HTTPException(404, "Channel not found") videos = youtube.fetch_channel_videos(channel_id, max_results=50) new_count = 0 for v in videos: row_id = youtube.save_video(ch["id"], v) if row_id: new_count += 1 return {"total_fetched": len(videos), "new_videos": new_count}