26 lines
933 B
Python
26 lines
933 B
Python
import subprocess
|
|
from pathlib import Path
|
|
from config import settings
|
|
|
|
def download_song(query: str):
|
|
Path(settings.MUSIC_DIR).mkdir(parents=True, exist_ok=True)
|
|
output_template = f"{settings.MUSIC_DIR}/%(title)s.%(ext)s"
|
|
command = [
|
|
"yt-dlp",
|
|
f"ytsearch1:{query}",
|
|
"--extract-audio",
|
|
"--audio-format", "mp3",
|
|
"--add-metadata",
|
|
"--output", output_template,
|
|
"--no-playlist",
|
|
"--verbose"
|
|
]
|
|
print("Running command:", " ".join(command))
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
print("STDOUT:", result.stdout)
|
|
print("STDERR:", result.stderr)
|
|
if result.returncode != 0:
|
|
raise Exception(f"yt-dlp failed: {result.stderr}")
|
|
# Try to find the actual filename
|
|
# You can parse result.stdout or list the directory for the latest file
|
|
return {"downloaded": "Check music folder for artist - title.mp3"} |