Compare commits

..

1 Commits

Author SHA1 Message Date
9376fae60b Fix md5 directory permissions fallback 2025-12-31 18:53:56 +01:00
2 changed files with 32 additions and 166 deletions

View File

@@ -117,8 +117,6 @@ class Job:
library: str # movies|series|auto library: str # movies|series|auto
status: str # queued|collecting|downloading|upload|finished|failed status: str # queued|collecting|downloading|upload|finished|failed
message: str message: str
progress: float = 0.0
cancel_requested: bool = False
jobs: Dict[str, Job] = {} jobs: Dict[str, Job] = {}
lock = threading.Lock() lock = threading.Lock()
@@ -206,25 +204,39 @@ def md5_file(path: str) -> str:
h.update(chunk) h.update(chunk)
return h.hexdigest() return h.hexdigest()
def write_md5_sidecar(file_path: str, md5_hex: str) -> str: _md5_dir_cache: Optional[str] = None
base = os.path.basename(file_path)
candidates = [MD5_DIR, "/tmp/md5"]
last_err: Optional[Exception] = None
for target in candidates: def pick_md5_dir() -> str:
global _md5_dir_cache
if _md5_dir_cache:
return _md5_dir_cache
candidates = [
MD5_DIR,
os.path.join(JD_OUTPUT_PATH, ".md5"),
"/tmp/jd-md5",
]
for candidate in candidates:
try: try:
os.makedirs(target, exist_ok=True) os.makedirs(candidate, exist_ok=True)
md5_path = os.path.join(target, base + ".md5") except Exception:
with open(md5_path, "w", encoding="utf-8") as f:
f.write(f"{md5_hex} {base}\n")
return md5_path
except PermissionError as exc:
last_err = exc
continue continue
if os.access(candidate, os.W_OK):
_md5_dir_cache = candidate
return candidate
if last_err: raise RuntimeError(
raise last_err "Kein beschreibbares MD5-Verzeichnis gefunden (MD5_DIR, /output/.md5, /tmp/jd-md5)."
raise RuntimeError("Failed to write MD5 sidecar file.") )
def write_md5_sidecar(file_path: str, md5_hex: str) -> str:
md5_dir = pick_md5_dir()
base = os.path.basename(file_path)
md5_path = os.path.join(md5_dir, base + ".md5")
with open(md5_path, "w", encoding="utf-8") as f:
f.write(f"{md5_hex} {base}\n")
return md5_path
def ffprobe_ok(path: str) -> bool: def ffprobe_ok(path: str) -> bool:
try: try:
@@ -297,10 +309,7 @@ def tmdb_search_movie(query: str) -> Optional[Dict[str, Any]]:
return None return None
q = urllib.parse.quote(query.strip()) q = urllib.parse.quote(query.strip())
url = f"https://api.themoviedb.org/3/search/movie?api_key={TMDB_API_KEY}&language={urllib.parse.quote(TMDB_LANGUAGE)}&query={q}" url = f"https://api.themoviedb.org/3/search/movie?api_key={TMDB_API_KEY}&language={urllib.parse.quote(TMDB_LANGUAGE)}&query={q}"
try: data = _http_get_json(url)
data = _http_get_json(url)
except Exception:
return None
results = data.get("results") or [] results = data.get("results") or []
return results[0] if results else None return results[0] if results else None
@@ -309,10 +318,7 @@ def tmdb_search_tv(query: str) -> Optional[Dict[str, Any]]:
return None return None
q = urllib.parse.quote(query.strip()) q = urllib.parse.quote(query.strip())
url = f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API_KEY}&language={urllib.parse.quote(TMDB_LANGUAGE)}&query={q}" url = f"https://api.themoviedb.org/3/search/tv?api_key={TMDB_API_KEY}&language={urllib.parse.quote(TMDB_LANGUAGE)}&query={q}"
try: data = _http_get_json(url)
data = _http_get_json(url)
except Exception:
return None
results = data.get("results") or [] results = data.get("results") or []
return results[0] if results else None return results[0] if results else None
@@ -409,10 +415,6 @@ def query_links_and_packages(dev, jobid: str) -> Tuple[List[Dict[str, Any]], Dic
"name": True, "name": True,
"finished": True, "finished": True,
"running": True, "running": True,
"bytesLoaded": True,
"bytesTotal": True,
"bytes": True,
"totalBytes": True,
"status": True, "status": True,
"packageUUID": True, "packageUUID": True,
"uuid": True, "uuid": True,
@@ -485,86 +487,6 @@ def try_remove_from_jd(dev, links: List[Dict[str, Any]], pkg_map: Dict[Any, Dict
return "JDownloader-API: Paket/Links konnten nicht entfernt werden (Wrapper-Methoden nicht vorhanden)." return "JDownloader-API: Paket/Links konnten nicht entfernt werden (Wrapper-Methoden nicht vorhanden)."
def try_cancel_from_jd(dev, links: List[Dict[str, Any]], pkg_map: Dict[Any, Dict[str, Any]]) -> Optional[str]:
link_ids = [l.get("uuid") for l in links if l.get("uuid") is not None]
pkg_ids = list(pkg_map.keys())
candidates = [
("downloads", "removeLinks"),
("downloads", "remove_links"),
("downloads", "deleteLinks"),
("downloads", "delete_links"),
("downloadcontroller", "removeLinks"),
("downloadcontroller", "remove_links"),
]
payloads = [
{"linkUUIDs": link_ids, "packageUUIDs": pkg_ids, "deleteFiles": True},
{"linkIds": link_ids, "packageIds": pkg_ids, "deleteFiles": True},
{"linkUUIDs": link_ids, "deleteFiles": True},
{"packageUUIDs": pkg_ids, "deleteFiles": True},
{"linkUUIDs": link_ids, "packageUUIDs": pkg_ids, "removeFiles": True},
{"linkIds": link_ids, "packageIds": pkg_ids, "removeFiles": True},
]
for ns, fn in candidates:
obj = getattr(dev, ns, None)
if obj is None:
continue
meth = getattr(obj, fn, None)
if meth is None:
continue
for payload in payloads:
try:
meth([payload])
return None
except Exception:
continue
return "JDownloader-API: Abbrechen fehlgeschlagen (Wrapper-Methoden nicht vorhanden)."
def cancel_job(dev, jobid: str) -> Optional[str]:
links, pkg_map = query_links_and_packages(dev, jobid)
local_paths = local_paths_from_links(links, pkg_map)
for path in local_paths:
try:
if os.path.isfile(path):
os.remove(path)
except Exception:
pass
try:
sidecar = os.path.join(MD5_DIR, os.path.basename(path) + ".md5")
if os.path.isfile(sidecar):
os.remove(sidecar)
except Exception:
pass
return try_cancel_from_jd(dev, links, pkg_map)
def calculate_progress(links: List[Dict[str, Any]]) -> float:
total = 0
loaded = 0
for link in links:
bytes_total = link.get("bytesTotal")
if bytes_total is None:
bytes_total = link.get("totalBytes")
if bytes_total is None:
bytes_total = link.get("bytes")
bytes_loaded = link.get("bytesLoaded")
if bytes_total is None or bytes_loaded is None:
continue
try:
bytes_total = int(bytes_total)
bytes_loaded = int(bytes_loaded)
except (TypeError, ValueError):
continue
if bytes_total <= 0:
continue
total += bytes_total
loaded += min(bytes_loaded, bytes_total)
if total <= 0:
return 0.0
return max(0.0, min(100.0, (loaded / total) * 100.0))
# ============================================================ # ============================================================
# Worker # Worker
# ============================================================ # ============================================================
@@ -578,13 +500,6 @@ def worker(jobid: str):
job = jobs.get(jobid) job = jobs.get(jobid)
if not job: if not job:
return return
if job.cancel_requested:
cancel_msg = cancel_job(dev, jobid)
with lock:
job.status = "canceled"
job.message = cancel_msg or "Download abgebrochen und Dateien entfernt."
job.progress = 0.0
return
links, pkg_map = query_links_and_packages(dev, jobid) links, pkg_map = query_links_and_packages(dev, jobid)
@@ -592,18 +507,15 @@ def worker(jobid: str):
with lock: with lock:
job.status = "collecting" job.status = "collecting"
job.message = "Warte auf Link-Crawler…" job.message = "Warte auf Link-Crawler…"
job.progress = 0.0
time.sleep(POLL_SECONDS) time.sleep(POLL_SECONDS)
continue continue
all_finished = all(bool(l.get("finished")) for l in links) all_finished = all(bool(l.get("finished")) for l in links)
if not all_finished: if not all_finished:
progress = calculate_progress(links)
with lock: with lock:
job.status = "downloading" job.status = "downloading"
done = sum(1 for l in links if l.get("finished")) done = sum(1 for l in links if l.get("finished"))
job.message = f"Download läuft… ({done}/{len(links)} fertig)" job.message = f"Download läuft… ({done}/{len(links)} fertig)"
job.progress = progress
time.sleep(POLL_SECONDS) time.sleep(POLL_SECONDS)
continue continue
@@ -614,7 +526,6 @@ def worker(jobid: str):
with lock: with lock:
job.status = "failed" job.status = "failed"
job.message = "Keine Video-Datei gefunden (Whitelist)." job.message = "Keine Video-Datei gefunden (Whitelist)."
job.progress = 0.0
return return
valid_videos = [p for p in video_files if ffprobe_ok(p)] valid_videos = [p for p in video_files if ffprobe_ok(p)]
@@ -622,13 +533,11 @@ def worker(jobid: str):
with lock: with lock:
job.status = "failed" job.status = "failed"
job.message = "ffprobe: keine gültige Video-Datei." job.message = "ffprobe: keine gültige Video-Datei."
job.progress = 0.0
return return
with lock: with lock:
job.status = "upload" job.status = "upload"
job.message = f"Download fertig. MD5/Upload/Verify für {len(valid_videos)} Datei(en)…" job.message = f"Download fertig. MD5/Upload/Verify für {len(valid_videos)} Datei(en)…"
job.progress = 100.0
ssh = ssh_connect() ssh = ssh_connect()
try: try:
@@ -668,7 +577,6 @@ def worker(jobid: str):
with lock: with lock:
job.status = "finished" job.status = "finished"
job.message = "Upload + MD5 OK. " + (jd_cleanup_msg or "JDownloader: Paket/Links entfernt.") job.message = "Upload + MD5 OK. " + (jd_cleanup_msg or "JDownloader: Paket/Links entfernt.")
job.progress = 100.0
return return
except Exception as e: except Exception as e:
@@ -677,7 +585,6 @@ def worker(jobid: str):
if job: if job:
job.status = "failed" job.status = "failed"
job.message = str(e) job.message = str(e)
job.progress = 0.0
# ============================================================ # ============================================================
# Web # Web
@@ -692,27 +599,13 @@ def render_page(error: str = "") -> str:
job_list = list(jobs.values())[::-1] job_list = list(jobs.values())[::-1]
for j in job_list: for j in job_list:
progress_pct = f"{j.progress:.1f}%"
progress_html = (
f"<div class='progress-row'>"
f"<progress value='{j.progress:.1f}' max='100'></progress>"
f"<span class='progress-text'>{progress_pct}</span>"
f"</div>"
)
cancel_html = ""
if j.status not in {"finished", "failed", "canceled"}:
cancel_html = (
f"<form method='post' action='/cancel/{j.id}' class='inline-form'>"
f"<button type='submit' class='danger'>Abbrechen</button>"
f"</form>"
)
rows += ( rows += (
f"<tr>" f"<tr>"
f"<td><code>{j.id}</code></td>" f"<td><code>{j.id}</code></td>"
f"<td style='max-width:560px; word-break:break-all;'>{j.url}</td>" f"<td style='max-width:560px; word-break:break-all;'>{j.url}</td>"
f"<td>{j.package_name}</td>" f"<td>{j.package_name}</td>"
f"<td>{j.library}</td>" f"<td>{j.library}</td>"
f"<td><b>{j.status}</b><br/><small>{j.message}</small>{progress_html}{cancel_html}</td>" f"<td><b>{j.status}</b><br/><small>{j.message}</small></td>"
f"</tr>" f"</tr>"
) )
@@ -724,12 +617,6 @@ def render_page(error: str = "") -> str:
<link rel="stylesheet" href="/static/style.css"> <link rel="stylesheet" href="/static/style.css">
<meta charset="utf-8"> <meta charset="utf-8">
<title>JD → Jellyfin</title> <title>JD → Jellyfin</title>
<script>
setInterval(() => {{
if (document.hidden) return;
window.location.reload();
}}, 5000);
</script>
</head> </head>
<body> <body>
<h1>JD → Jellyfin</h1> <h1>JD → Jellyfin</h1>
@@ -811,22 +698,9 @@ def submit(url: str = Form(...), package_name: str = Form(""), library: str = Fo
library=library, library=library,
status="queued", status="queued",
message="Download gestartet", message="Download gestartet",
progress=0.0,
) )
t = threading.Thread(target=worker, args=(jobid,), daemon=True) t = threading.Thread(target=worker, args=(jobid,), daemon=True)
t.start() t.start()
return RedirectResponse(url="/", status_code=303) return RedirectResponse(url="/", status_code=303)
@app.post("/cancel/{jobid}")
def cancel(jobid: str):
with lock:
job = jobs.get(jobid)
if not job:
return RedirectResponse(url="/", status_code=303)
if job.status in {"finished", "failed", "canceled"}:
return RedirectResponse(url="/", status_code=303)
job.cancel_requested = True
job.message = "Abbruch angefordert…"
return RedirectResponse(url="/", status_code=303)

View File

@@ -4,17 +4,9 @@ form { background:#fff; border:1px solid #e5e5e5; border-radius:10px; padding:14
.row { margin-bottom: 10px; } .row { margin-bottom: 10px; }
input, select { padding:10px; border:1px solid #ccc; border-radius:8px; font-size:14px; width: 100%; max-width: 860px; } input, select { padding:10px; border:1px solid #ccc; border-radius:8px; font-size:14px; width: 100%; max-width: 860px; }
button { padding:10px 14px; border:0; border-radius:8px; font-weight:600; cursor:pointer; } button { padding:10px 14px; border:0; border-radius:8px; font-weight:600; cursor:pointer; }
button.danger { background:#b00020; color:#fff; }
progress { width: 100%; height: 12px; }
progress::-webkit-progress-bar { background:#f0f0f0; border-radius:8px; }
progress::-webkit-progress-value { background:#1b7f3a; border-radius:8px; }
progress::-moz-progress-bar { background:#1b7f3a; border-radius:8px; }
table { margin-top:16px; width:100%; border-collapse: collapse; background:#fff; border:1px solid #e5e5e5; border-radius:10px; overflow:hidden; } table { margin-top:16px; width:100%; border-collapse: collapse; background:#fff; border:1px solid #e5e5e5; border-radius:10px; overflow:hidden; }
th, td { border-top:1px solid #eee; padding:10px; vertical-align: top; font-size:14px; } th, td { border-top:1px solid #eee; padding:10px; vertical-align: top; font-size:14px; }
th { background:#fbfbfb; text-align:left; } th { background:#fbfbfb; text-align:left; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; background:#f2f2f2; padding:2px 4px; border-radius:4px; } code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; background:#f2f2f2; padding:2px 4px; border-radius:4px; }
.hint { color:#555; font-size: 12px; margin-top: 10px; } .hint { color:#555; font-size: 12px; margin-top: 10px; }
.error { color:#b00020; font-weight: 700; } .error { color:#b00020; font-weight: 700; }
.progress-row { display:flex; align-items:center; gap:8px; margin-top:6px; }
.progress-text { font-size:12px; color:#333; min-width:48px; }
.inline-form { margin-top:6px; }