refactor: improve error handling and response formatting in API endpoints
Some checks failed
Code Check - Quality and Syntax / syntax-lint (3.14) (push) Has been cancelled
Code Check - Quality and Syntax / syntax-lint (3.12) (push) Has been cancelled
Code Check - Quality and Syntax / syntax-lint (3.11) (push) Has been cancelled
Code Check - Quality and Syntax / syntax-lint (3.13) (push) Has been cancelled

This commit is contained in:
Space-Banane
2026-04-06 23:09:06 +02:00
parent 002533044a
commit f34c90b999
2 changed files with 92 additions and 46 deletions

View File

@@ -71,15 +71,17 @@ def get_info():
cmd, capture_output=True, text=True, timeout=30, cwd=DOWNLOAD_DIR
)
if result.returncode != 0:
return jsonify({"error": result.stderr or "Failed to fetch info"}), 400
msg = result.stderr or "Failed to fetch info"
return jsonify({"error": msg}), 400
info = json.loads(result.stdout)
formats = []
for f in info.get("formats", []):
res = f.get("resolution") or f.get("format_note") or ""
formats.append(
{
"id": f.get("format_id"),
"ext": f.get("ext"),
"resolution": f.get("resolution") or f.get("format_note") or "",
"resolution": res,
"vcodec": f.get("vcodec", "none"),
"acodec": f.get("acodec", "none"),
"filesize": f.get("filesize") or f.get("filesize_approx"),
@@ -125,7 +127,11 @@ def start_download():
cmd.append(url)
job_id = str(uuid.uuid4())
thread = threading.Thread(target=run_ytdlp, args=(job_id, cmd), daemon=True)
thread = threading.Thread(
target=run_ytdlp,
args=(job_id, cmd),
daemon=True,
)
thread.start()
return jsonify({"job_id": job_id})
@@ -139,14 +145,17 @@ def job_status(job_id):
with jobs_lock:
job = jobs.get(job_id)
if not job:
yield f"data: {json.dumps({'error': 'Job not found'})}\n\n"
payload = json.dumps({"error": "Job not found"})
yield "data: " + payload + "\n\n"
break
lines = job["lines"]
while sent < len(lines):
yield f"data: {json.dumps({'line': lines[sent]})}\n\n"
payload = json.dumps({"line": lines[sent]})
yield "data: " + payload + "\n\n"
sent += 1
if job["done"]:
yield f"data: {json.dumps({'done': True, 'error': job['error']})}\n\n"
payload = json.dumps({"done": True, "error": job["error"]})
yield "data: " + payload + "\n\n"
break
time.sleep(0.2)
@@ -158,6 +167,6 @@ def job_status(job_id):
if __name__ == "__main__":
print(f"[yt-dlp UI] Serving on http://localhost:5000")
print(f"[yt-dlp UI] Download directory: {DOWNLOAD_DIR}")
print("[yt-dlp UI] Serving on http://localhost:5000")
print("[yt-dlp UI] Download directory: {}".format(DOWNLOAD_DIR))
app.run(debug=False, host="0.0.0.0", port=5000, threaded=True)