diff --git a/backend/downloader.py b/backend/downloader.py new file mode 100644 index 0000000..7989718 --- /dev/null +++ b/backend/downloader.py @@ -0,0 +1,33 @@ +import subprocess +import os +from config import settings +import uuid +import requests + +def download_song(query: str): + output_template = os.path.join(settings.MUSIC_DIR, "%(title)s.%(ext)s") + + command = [ + "yt-dlp", + f"ytsearch1:{query}", + "--extract-audio", + "--audio-format", "mp3", + "--output", output_template + ] + + result = subprocess.run(command, capture_output=True, text=True) + + if result.returncode != 0: + raise Exception(f"Download failed: {result.stderr}") + + # Optional: trigger navidrome scan + if settings.NAVIDROME_SCAN_URL: + try: + requests.post(settings.NAVIDROME_SCAN_URL, timeout=5) + except Exception as e: + pass # non-critical + + return { + "query": query, + "log": result.stdout + } \ No newline at end of file diff --git a/backend/main.py b/backend/main.py index 69266a7..92755b8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -12,38 +12,3 @@ def download(query: str = Query(..., description="Song name or YouTube search te return JSONResponse(content={"status": "success", **result}) except Exception as e: raise HTTPException(status_code=500, detail=str(e)) - -# downloader.py -import subprocess -import os -from config import settings -import uuid -import requests - -def download_song(query: str): - output_template = os.path.join(settings.MUSIC_DIR, "%(title)s.%(ext)s") - - command = [ - "yt-dlp", - f"ytsearch1:{query}", - "--extract-audio", - "--audio-format", "mp3", - "--output", output_template - ] - - result = subprocess.run(command, capture_output=True, text=True) - - if result.returncode != 0: - raise Exception(f"Download failed: {result.stderr}") - - # Optional: trigger navidrome scan - if settings.NAVIDROME_SCAN_URL: - try: - requests.post(settings.NAVIDROME_SCAN_URL, timeout=5) - except Exception as e: - pass # non-critical - - return { - "query": query, - "log": result.stdout - } \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index cb359d4..0ea73f9 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -2,6 +2,3 @@ fastapi uvicorn pydantic requests -downloader -config -urllib2 \ No newline at end of file