fix: strip ?si= from Spotify URL, improve spotdl error logging
spotdl doesn't handle tracking params in URLs. Also separate stdout/stderr in Python script and always try parsing stdout before checking exit code. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
3c85deab40
commit
9b197e119a
2 files changed
+32
-16
No files matched your search
@@ -2,7 +2,7 @@
|
||||
"""
|
||||
Resolves a Spotify URL to track metadata using spotdl (TzurSoffer fork).
|
||||
Usage: python3 spotify_resolve.py <spotify_url>
|
||||
Outputs JSON to stdout.
|
||||
Outputs JSON to stdout. Errors go to stderr for clean separation.
|
||||
"""
|
||||
import sys
|
||||
import json
|
||||
@@ -10,36 +10,47 @@ import os
|
||||
import tempfile
|
||||
import subprocess
|
||||
|
||||
def eprint(*args):
|
||||
print(*args, file=sys.stderr)
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(json.dumps({"error": "No URL provided"}))
|
||||
eprint("No URL provided")
|
||||
sys.exit(1)
|
||||
|
||||
url = sys.argv[1]
|
||||
tmp = tempfile.mktemp(suffix=".spotdl")
|
||||
|
||||
try:
|
||||
eprint(f"[spotdl] running: spotdl save {url}")
|
||||
result = subprocess.run(
|
||||
["spotdl", "save", url, "--save-file", tmp, "--log-level", "ERROR"],
|
||||
capture_output=True, text=True, timeout=60
|
||||
)
|
||||
eprint(f"[spotdl] exit={result.returncode}")
|
||||
if result.stdout.strip():
|
||||
eprint(f"[spotdl] stdout: {result.stdout.strip()[:300]}")
|
||||
if result.stderr.strip():
|
||||
eprint(f"[spotdl] stderr: {result.stderr.strip()[:300]}")
|
||||
|
||||
if result.returncode != 0:
|
||||
err = result.stderr.strip() or result.stdout.strip() or "spotdl failed"
|
||||
print(json.dumps({"error": err}))
|
||||
err = result.stderr.strip() or result.stdout.strip() or "spotdl exited non-zero"
|
||||
eprint(f"[spotdl] FAILED: {err}")
|
||||
sys.exit(1)
|
||||
|
||||
if not os.path.exists(tmp):
|
||||
print(json.dumps({"error": "spotdl produced no output file"}))
|
||||
eprint("[spotdl] no output file produced")
|
||||
sys.exit(1)
|
||||
|
||||
with open(tmp, "r", encoding="utf-8") as f:
|
||||
songs = json.load(f)
|
||||
|
||||
if not songs:
|
||||
print(json.dumps({"error": "No tracks found"}))
|
||||
eprint("[spotdl] empty track list")
|
||||
sys.exit(1)
|
||||
|
||||
# Transform spotdl Song format to NovaSonic format
|
||||
eprint(f"[spotdl] got {len(songs)} tracks")
|
||||
|
||||
def fmt_track(s):
|
||||
artists = s.get("artists") or []
|
||||
artist_str = ", ".join(artists) if artists else s.get("artist", "")
|
||||
@@ -60,7 +71,6 @@ def main():
|
||||
artists0 = first.get("artists") or []
|
||||
artist0 = ", ".join(artists0) if artists0 else first.get("artist", "")
|
||||
|
||||
# Determine type from URL
|
||||
if "/track/" in url:
|
||||
out = {
|
||||
"type": "track",
|
||||
|
||||
Reference in new issue
Block a user