import asyncio import threading from pathlib import Path from typing import Optional from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Query from fastapi.responses import FileResponse, StreamingResponse from sqlalchemy.orm import Session from database import get_db from schemas import CleanupRequest, ProfileResponse, VideoCreate, VideoResponse from services import video_service from services.download_service import download_video from services.stream_service import stream_video_live from services.video_service import update_file_path router = APIRouter(prefix="/videos", tags=["videos"]) @router.post("", response_model=list[VideoResponse]) async def create_videos(videos_data: list[VideoCreate], db: Session = Depends(get_db)): created_ids = [] profile_ids = set() for video_data in reversed(videos_data): video_id_match = video_data.youtube_url.split("v=")[-1].split("&")[0] video_service.delete_by_youtube_id(db, video_id_match) video = video_service.create_video(db, video_data) created_ids.append(video.id) if video_data.profile_id: profile_ids.add(video_data.profile_id) videos = [video_service.get_video(db, vid) for vid in created_ids] if profile_ids: from main import notify_clients await notify_clients(list(profile_ids)) return [VideoResponse.from_model(v) for v in videos if v] @router.get("", response_model=list[VideoResponse]) def get_all_videos(profile_id: Optional[int] = Query(None), db: Session = Depends(get_db)): videos = video_service.get_all_videos(db, profile_id=profile_id) return [VideoResponse.from_model(v) for v in videos] @router.get("/downloaded", response_model=list[VideoResponse]) def get_downloaded_videos(profile_id: Optional[int] = Query(None), db: Session = Depends(get_db)): videos = video_service.get_downloaded_videos(db, profile_id=profile_id) return [VideoResponse.from_model(v) for v in videos] @router.post("/cleanup") def cleanup_videos(request: CleanupRequest, db: Session = Depends(get_db)): count = video_service.delete_not_downloaded(db, request.profile_id, request.exclude_ids or None) return {"deleted": count} @router.post("/{video_id}/download") def trigger_download(video_id: int, db: Session = Depends(get_db)): video = video_service.get_video(db, video_id) if not video: raise HTTPException(status_code=404, detail="Video nicht gefunden") if video.file_path: return {"status": "already_downloaded"} thread = threading.Thread(target=download_video, args=(video.id, video.youtube_url)) thread.start() return {"status": "download_started"} @router.get("/{video_id}/stream") def stream_video(video_id: int, db: Session = Depends(get_db)): video = video_service.get_video(db, video_id) if not video: raise HTTPException(status_code=404, detail="Video nicht gefunden") if not video.file_path: def stream_and_save(): output_path = f"/videos/{video_id}.mp4" yield from stream_video_live(video_id, video.youtube_url) if Path(output_path).exists(): sdb = __import__("database").SessionLocal() try: update_file_path(sdb, video_id, output_path) finally: sdb.close() return StreamingResponse(stream_and_save(), media_type="video/mp4") path = Path(video.file_path) if not path.exists(): raise HTTPException(status_code=404, detail="Videodatei nicht gefunden") return FileResponse(path, media_type="video/mp4") @router.get("/{video_id}/file") def download_file(video_id: int, db: Session = Depends(get_db)): video = video_service.get_video(db, video_id) if not video: raise HTTPException(status_code=404, detail="Video nicht gefunden") if not video.file_path: raise HTTPException(status_code=404, detail="Video noch nicht heruntergeladen") path = Path(video.file_path) if not path.exists(): raise HTTPException(status_code=404, detail="Videodatei nicht gefunden") return FileResponse(path, media_type="video/mp4", filename=f"{video.title}.mp4") @router.delete("/{video_id}/file") def delete_server_file(video_id: int, db: Session = Depends(get_db)): video = video_service.get_video(db, video_id) if not video: raise HTTPException(status_code=404, detail="Video nicht gefunden") if video.file_path: path = Path(video.file_path) if path.exists(): path.unlink() video_service.update_file_path(db, video_id, None) return {"status": "deleted"} profiles_router = APIRouter(prefix="/profiles", tags=["profiles"]) @profiles_router.get("", response_model=list[ProfileResponse]) def get_profiles(db: Session = Depends(get_db)): return video_service.get_all_profiles(db)