Add selectable animdl download fallback

This commit is contained in:
Dymas
2026-07-19 14:17:13 +02:00
parent 6bec0e068a
commit 6b3745c7e9
10 changed files with 282 additions and 65 deletions
+62 -38
View File
@@ -13,7 +13,10 @@ import time
import re
from app_support import (
ANIMDL,
ANIPY_CLI,
ANI_CLI,
DOWNLOAD_METHOD_CHOICES,
MAX_LOG_LINES,
PROJECT_ROOT,
QUEUE_PATH,
@@ -37,6 +40,11 @@ from app_support import (
LOG_PERSIST_INTERVAL_SECONDS = 0.75
LOG_PERSIST_LINE_BATCH = 8
DOWNLOAD_BACKEND_COMMANDS = {
"ani-cli": ANI_CLI,
"anipy-cli": ANIPY_CLI,
"animdl": ANIMDL,
}
JOB_STRUCTURED_COLUMNS = (
"show_id",
"title",
@@ -1490,34 +1498,46 @@ class DownloadQueue:
self._cleanup_staging_dir(job)
def _command_attempts(self, job, staging_dir):
config = normalize_config(self.config_getter() or {})
attempts = [
{
"backend": "ani-cli",
"command": command_for_job(job, backend="ani-cli"),
"env": {
**os.environ.copy(),
"ANI_CLI_DOWNLOAD_DIR": str(staging_dir),
"ANI_CLI_MODE": job["mode"],
"ANI_CLI_QUALITY": job["quality"],
"TERM": os.environ.get("TERM", "xterm-256color"),
},
}
]
fallback_enabled = bool(config.get("anipy_cli_fallback_enabled"))
fallback_available = cli_executable_exists(ANIPY_CLI)
if fallback_enabled and fallback_available:
attempts.append(
{
"backend": "anipy-cli",
"command": command_for_job(job, backend="anipy-cli", download_path=staging_dir),
def attempt_for(method):
if method == "ani-cli":
return {
"backend": "ani-cli",
"command": command_for_job(job, backend="ani-cli"),
"env": {
**os.environ.copy(),
"ANI_CLI_DOWNLOAD_DIR": str(staging_dir),
"ANI_CLI_MODE": job["mode"],
"ANI_CLI_QUALITY": job["quality"],
"TERM": os.environ.get("TERM", "xterm-256color"),
},
}
)
return attempts, fallback_enabled, fallback_available
return {
"backend": method,
"command": command_for_job(job, backend=method, download_path=staging_dir),
"env": {
**os.environ.copy(),
"TERM": os.environ.get("TERM", "xterm-256color"),
},
}
config = normalize_config(self.config_getter() or {})
selected_methods = [
method
for method in config.get("download_methods") or ["ani-cli"]
if method in DOWNLOAD_METHOD_CHOICES
]
if not selected_methods:
selected_methods = ["ani-cli"]
attempts = []
unavailable_methods = []
for method in selected_methods:
if not cli_executable_exists(DOWNLOAD_BACKEND_COMMANDS.get(method)):
unavailable_methods.append(method)
continue
attempts.append(attempt_for(method))
if not attempts:
attempts.append(attempt_for(selected_methods[0]))
return attempts, selected_methods, unavailable_methods
def _run(self):
while not self.stop_event.is_set():
@@ -1533,7 +1553,7 @@ class DownloadQueue:
target_dir = job_output_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
target_dir.mkdir(parents=True, exist_ok=True)
attempts, fallback_enabled, fallback_available = self._command_attempts(job, staging_dir)
attempts, selected_methods, unavailable_methods = self._command_attempts(job, staging_dir)
primary_command = attempts[0]["command"]
with self.lock:
@@ -1543,17 +1563,21 @@ class DownloadQueue:
job["command"] = printable_command(primary_command)
job["staging_dir"] = str(staging_dir)
job["target_dir"] = str(target_dir)
job["download_backend"] = "ani-cli"
job["download_backend"] = attempts[0]["backend"]
job["fallback_used"] = False
job["log"] = [
f"Starting: {job['command']}",
f"Staging in: {job['staging_dir']}",
f"Final folder: {job['target_dir']}",
f"Download methods: {', '.join(selected_methods)}",
]
if fallback_enabled and fallback_available:
job["log"].append("anipy-cli fallback is enabled and will retry automatically if ani-cli fails.")
elif fallback_enabled:
job["log"].append("anipy-cli fallback is enabled, but anipy-cli is not installed or not executable.")
if len(attempts) > 1:
job["log"].append("Fallback retry is enabled for the selected download methods.")
if unavailable_methods:
job["log"].append(
"Skipped unavailable download method"
f"{'s' if len(unavailable_methods) != 1 else ''}: {', '.join(unavailable_methods)}."
)
job["cancel_requested"] = False
job["_dirty_log_lines"] = 0
self._save_job_locked(job)
@@ -1593,8 +1617,8 @@ class DownloadQueue:
)
except Exception as exc:
if should_fallback:
self._append_log(job, f"ani-cli could not start: {exc}")
self._append_log(job, "Retrying with anipy-cli fallback.")
self._append_log(job, f"{backend} could not start: {exc}")
self._append_log(job, f"Retrying with {attempts[index + 1]['backend']} fallback.")
self._cleanup_staging_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
continue
@@ -1621,9 +1645,9 @@ class DownloadQueue:
if job.get("cancel_requested"):
break
if backend == "ani-cli" and exit_code != 0 and should_fallback:
self._append_log(job, f"ani-cli failed with exit code {exit_code}.")
self._append_log(job, "Retrying with anipy-cli fallback.")
if exit_code != 0 and should_fallback:
self._append_log(job, f"{backend} failed with exit code {exit_code}.")
self._append_log(job, f"Retrying with {attempts[index + 1]['backend']} fallback.")
self._cleanup_staging_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
continue
@@ -1634,10 +1658,10 @@ class DownloadQueue:
except Exception as exc:
finalize_error = exc
no_files_downloaded = "No downloaded files were found in the staging folder" in str(exc)
if backend == "ani-cli" and no_files_downloaded and should_fallback:
if no_files_downloaded and should_fallback:
self._append_log(
job,
"ani-cli exited without downloading any files. Retrying with anipy-cli fallback.",
f"{backend} exited without downloading any files. Retrying with {attempts[index + 1]['backend']} fallback.",
)
self._cleanup_staging_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
@@ -1674,8 +1698,8 @@ class DownloadQueue:
cleanup_staging = True
elif exit_code == 0:
job["status"] = "done"
if successful_backend == "anipy-cli":
job.setdefault("log", []).append("Fallback download completed with anipy-cli.")
if successful_backend and successful_backend != attempts[0]["backend"]:
job.setdefault("log", []).append(f"Fallback download completed with {successful_backend}.")
for path in moved_files:
job.setdefault("log", []).append(f"Saved: {path}")
job.setdefault("log", []).append("Download completed.")