from fastapi import FastAPI, HTTPException, Query from fastapi.middleware.cors import CORSMiddleware from typing import List import os from models import Base, Video from schemas import VideoCreate, VideoInDB from database import engine, get_db from video_scanner import scan_video_directory import json # Create database tables Base.metadata.create_all(bind=engine) app = FastAPI(title="Video Organization API", version="0.1.0") # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # In-memory storage for video paths (in a real app, this would be in a database) video_paths = [] @app.get("/") def read_root(): return {"message": "Welcome to Video Organization API"} from fastapi import FastAPI, HTTPException, Query, Depends from fastapi.middleware.cors import CORSMiddleware from typing import List import os from sqlalchemy.orm import Session from models import Base, Video from schemas import VideoCreate, VideoInDB from database import engine, get_db from video_scanner import scan_video_directory import json # Create database tables Base.metadata.create_all(bind=engine) app = FastAPI(title="Video Organization API", version="0.1.0") # Add CORS middleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # In-memory storage for video paths (in a real app, this would be in a database) video_paths = [] @app.get("/") def read_root(): return {"message": "Welcome to Video Organization API"} @app.post("/video-paths/") def add_video_path(path: str = Query(...)): """ Add a video path to the library """ if not os.path.exists(path): raise HTTPException(status_code=404, detail="Path does not exist") if path not in video_paths: video_paths.append(path) return {"message": f"Path {path} added successfully", "paths": video_paths} else: return {"message": f"Path {path} already exists", "paths": video_paths} @app.get("/video-paths/") def get_video_paths(): """ Get all video paths in the library """ return {"paths": video_paths} @app.post("/scan-videos/") def scan_videos(db: Session = Depends(get_db)): """ Scan all video paths and save the videos found to the database """ all_videos = [] for path in video_paths: try: videos = scan_video_directory(path) all_videos.extend(videos) except FileNotFoundError as e: raise HTTPException(status_code=404, detail=str(e)) added_count = 0 for video_data in all_videos: existing_video = db.query(Video).filter(Video.path == video_data['path']).first() if not existing_video: video = Video(**video_data) db.add(video) added_count += 1 db.commit() return {"message": f"Scan complete. Added {added_count} new videos.", "count": added_count} @app.get("/videos/", response_model=List[VideoInDB]) def get_videos(db: Session = Depends(get_db)): """ Get all videos in the library """ videos = db.query(Video).all() return videos if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) @app.get("/video-paths/") def get_video_paths(): """ Get all video paths in the library """ return {"paths": video_paths} @app.post("/scan-videos/") def scan_videos(): """ Scan all video paths and return the videos found """ all_videos = [] for path in video_paths: try: videos = scan_video_directory(path) all_videos.extend(videos) except FileNotFoundError as e: raise HTTPException(status_code=404, detail=str(e)) return {"videos": all_videos, "count": len(all_videos)} @app.get("/videos/") def get_videos(): """ Get all videos in the library """ # This is a placeholder - in a real implementation, we would query the database return {"message": "List of videos"} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)