38 lines
1003 B
Python
38 lines
1003 B
Python
import subprocess
|
|
import os
|
|
from config import settings
|
|
import requests
|
|
|
|
def download_song(query: str):
|
|
output_template = os.path.join(settings.MUSIC_DIR, "%(artist)s/%(album)s/%(title)s.%(ext)s")
|
|
|
|
|
|
command = [
|
|
"yt-dlp",
|
|
f"ytsearch1:{query}",
|
|
"--extract-audio",
|
|
"--audio-format", "mp3",
|
|
"--output", output_template,
|
|
"--no-playlist",
|
|
"--quiet"
|
|
]
|
|
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
|
|
if result.returncode != 0:
|
|
raise Exception(f"❌ Download failed:\n{result.stderr}")
|
|
|
|
# Optional: trigger Navidrome rescan
|
|
if settings.NAVIDROME_SCAN_URL:
|
|
try:
|
|
res = requests.get(settings.NAVIDROME_SCAN_URL, timeout=5)
|
|
res.raise_for_status()
|
|
except Exception as e:
|
|
print(f"⚠️ Failed to trigger Navidrome rescan: {e}")
|
|
|
|
return {
|
|
"status": "success",
|
|
"query": query,
|
|
"log": result.stdout + "\n" + result.stderr
|
|
}
|