from fastapi import FastAPI, BackgroundTasks, HTTPException from fastapi.staticfiles import StaticFiles from pydantic import BaseModel from typing import List import os from .library import scan_library from .cast_logic import cast_manager from .utils import get_logger, VIDEOS_DIR, THUMBNAILS_DIR, get_host_ip, API_PORT logger = get_logger(__name__) app = FastAPI(title="Boys_Streaming API") # Serve videos and thumbnails app.mount("/media", StaticFiles(directory=str(VIDEOS_DIR)), name="media") app.mount("/thumbs", StaticFiles(directory=str(THUMBNAILS_DIR)), name="thumbs") class CastRequest(BaseModel): video_paths: List[str] @app.get("/library") async def get_library(): return scan_library() @app.post("/cast") async def start_casting(request: CastRequest, background_tasks: BackgroundTasks): host_ip = get_host_ip() urls = [f"http://{host_ip}:{API_PORT}/media/{path}" for path in request.video_paths] logger.info(f"Received cast request for {len(urls)} videos") cast_manager.play_queue(urls) return {"status": "started", "queue_len": len(urls)} @app.get("/status") async def get_status(): return cast_manager.get_status() @app.post("/stop") async def stop_casting(): cast_manager.stop() return {"status": "stopped"} @app.on_event("startup") async def startup_event(): # Pre-discover Chromecast on startup to be ready logger.info("Application starting up...") # Run discovery in background to not block startup import threading threading.Thread(target=cast_manager.discover, daemon=True).start()