from fastapi import FastAPI, Query, HTTPException from fastapi.responses import JSONResponse from fastapi.middleware.cors import CORSMiddleware import uvicorn import os from downloader import download_song from config import settings # Ensure the music directory exists os.makedirs(settings.MUSIC_DIR, exist_ok=True) print(f"🎯 Music will be saved to: {os.path.join(settings.MUSIC_DIR, '%(artist)s - %(title)s.%(ext)s')}") # List existing files for debug existing_files = os.listdir(settings.MUSIC_DIR) print(f"📂 Existing files: {existing_files}") app = FastAPI( title="Tunedrop", description="🎵 Download music using yt-dlp and stream with Navidrome", version="1.0.0" ) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"], ) @app.get("/download", summary="Download a song or playlist") def download( query: str = Query(..., min_length=2, description="Artist name, song title, or YouTube/playlist link"), single: bool = Query(False, description="Set to true to download only a single matching song (for search queries)") ): if not query.strip(): raise HTTPException(status_code=400, detail="Query cannot be empty") try: result = download_song(query, single=single) return JSONResponse(content={"status": "success", **result}) except Exception as e: raise HTTPException(status_code=500, detail=f"Download failed: {str(e)}") if __name__ == "__main__": uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)