feat: implemented Monochrome downloading
This commit is contained in:
64
app.py
64
app.py
@@ -259,6 +259,70 @@ def start_download():
|
||||
return jsonify({"job_id": job_id})
|
||||
|
||||
|
||||
def run_monochrome_download(job_id: str, url: str, quality: str):
|
||||
with jobs_lock:
|
||||
jobs[job_id]["status"] = "running"
|
||||
|
||||
def log(msg):
|
||||
with jobs_lock:
|
||||
jobs[job_id]["output"] = jobs[job_id].get("output", [])[-500:] + [msg]
|
||||
|
||||
def is_cancelled():
|
||||
with jobs_lock:
|
||||
return jobs[job_id]["status"] == "cancelled"
|
||||
|
||||
try:
|
||||
from monochrome.api import download_spotify_url
|
||||
success, total = download_spotify_url(
|
||||
spotify_url=url,
|
||||
quality=quality,
|
||||
output_dir=str(DOWNLOADS_DIR),
|
||||
log=log,
|
||||
cancel_check=is_cancelled,
|
||||
)
|
||||
with jobs_lock:
|
||||
if jobs[job_id]["status"] != "cancelled":
|
||||
jobs[job_id]["status"] = "completed" if success > 0 else "failed"
|
||||
jobs[job_id]["return_code"] = 0 if success > 0 else 1
|
||||
except Exception as e:
|
||||
with jobs_lock:
|
||||
jobs[job_id]["status"] = "failed"
|
||||
jobs[job_id]["output"] = jobs[job_id].get("output", []) + [f"[error] {e}"]
|
||||
jobs[job_id]["return_code"] = 1
|
||||
|
||||
|
||||
@app.route("/api/monochrome/download", methods=["POST"])
|
||||
def start_monochrome_download():
|
||||
data = request.json
|
||||
url = data.get("url", "").strip()
|
||||
quality = data.get("quality", "HI_RES_LOSSLESS")
|
||||
|
||||
if not url:
|
||||
return jsonify({"error": "No URL provided"}), 400
|
||||
|
||||
valid_qualities = ["HI_RES_LOSSLESS", "LOSSLESS", "HIGH", "LOW", "MP3_320"]
|
||||
if quality not in valid_qualities:
|
||||
return jsonify({"error": f"Invalid quality. Choose from: {valid_qualities}"}), 400
|
||||
|
||||
job_id = str(uuid.uuid4())[:8]
|
||||
with jobs_lock:
|
||||
jobs[job_id] = {
|
||||
"id": job_id,
|
||||
"urls": [url],
|
||||
"options": {"quality": quality, "source": "monochrome"},
|
||||
"status": "queued",
|
||||
"output": [],
|
||||
"created_at": time.time(),
|
||||
}
|
||||
|
||||
thread = threading.Thread(
|
||||
target=run_monochrome_download, args=(job_id, url, quality), daemon=True
|
||||
)
|
||||
thread.start()
|
||||
|
||||
return jsonify({"job_id": job_id})
|
||||
|
||||
|
||||
def job_to_dict(job):
|
||||
return {k: v for k, v in job.items() if k != "process"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user