33 lines
817 B
Python
33 lines
817 B
Python
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
|
|
} |