Fix config.py and downloader.py

This commit is contained in:
dvirlabs 2025-07-10 16:21:17 +03:00
parent d15223f2cb
commit e5a151ce99
2 changed files with 16 additions and 11 deletions

View File

@ -1,10 +1,12 @@
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
from pydantic import Field
class Settings(BaseSettings): class Settings(BaseSettings):
MUSIC_DIR: str = "/music" MUSIC_DIR: str = Field(default="/music", description="Path where songs are saved")
NAVIDROME_SCAN_URL: str = "" NAVIDROME_SCAN_URL: str = Field(default="", description="URL to trigger Navidrome rescan")
class Config: class Config:
env_file = ".env" env_file = ".env"
env_file_encoding = "utf-8"
settings = Settings() settings = Settings()

View File

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