Apply saved watchlist naming to pending downloads and isolate tests

This commit is contained in:
Dymas committed 2026-10-12 21:46:55 +02:00
1 parent fec17577e3
commit cae63ae112
8 files changed
+154 -10

No files matched your search

+8
View File
@@ -1,5 +1,13 @@
# Changelog
## 0.53.15 - 2026-10-12
- Saving watchlist auto-download naming now updates unstarted pending downloads for that anime, including the custom library name, season, and episode offset. Workers reload and claim the saved job before choosing its output folder so an already-fetched job cannot use stale naming.
- Preserved custom names when adding a watchlist item from Search and when making partial auto-download settings updates. Omitted mode and quality fields now retain the saved choices too.
- Kept running jobs, completed history, and retries with completed episodes on their existing layout; existing library files are not renamed or moved by a settings save.
- Fixed regression-test state isolation by setting the temporary state root before importing application paths. Earlier test runs could use the checkout's `.kaizoku/` directory; added an explicit path-isolation regression.
- Checked all three anime providers against upstream; no updates were available.
## 0.53.14 - 2026-10-12
- Fixed custom Docker host ports by keeping the application on container port 8421.
+1 -1
View File
@@ -40,6 +40,6 @@ The dependency audit identified undici advisories [GHSA-3wwx-pv8p-q78v](https://
- Node syntax checks pass for all four inline page scripts and all four provider/bridge JavaScript files.
- Shell syntax checks and `git diff --check` pass.
- `KAIZOKU_PORT=8080 docker compose config --format json` confirms host 8080 maps to container 8421 and the application listens on 8421.
- Tests use temporary state and fixture files. No production data migration, service restart, or deployment was performed.
- Correction in 0.53.15: the earlier test harness selected its temporary state root after importing application paths, and fixtures from the 0.53.14 runs were found in the checkout's `.kaizoku/` database. The earlier isolation claim was incorrect; the available evidence does not establish whether pre-existing local records were affected. The harness now sets the environment before imports and verifies all state paths are temporary. No production deployment or service restart was performed.
Live provider playback, a Docker image build, and an actual Jellyfin server transfer were not exercised. Network checks verified upstream source versions and dependency advisories; provider behavior and file-move failures were tested with fixtures. This review does not establish the absence of all defects or cover unsupported HLS formats.
+2
View File
@@ -113,6 +113,8 @@ Keep `./.kaizoku` mounted for production instances. That directory contains the
## Download Flow
The Watchlist **Auto-download** dialog's custom name and season control the final library path: for example, name `My Series` and season `3` produce `tv/My Series/Season 03/My Series - S03E01.mp4`. The source title continues identifying the provider anime. Saving these settings also updates unstarted pending downloads linked to that anime; save again after upgrading to apply them to existing pending jobs. Running jobs and retries with completed episodes keep their original layout, and existing files are not moved or renamed automatically. A custom library name entered in Search is retained when adding the result to the watchlist.
Anikoto searches use the site's filter endpoint, which supports paginated queries. When Anikoto returns HTTP 500 for a query, Kaizoku retries with the lowercase phrase and then its first meaningful word. The fallback results may be broader than the original phrase. If a provider remains unavailable, Search shows a temporary-unavailability message instead of an unexpected application error. AnimePahe checks each of its configured mirrors once per search, so a blocked API does not cause repeated wait cycles.
Kaizoku stores provider-backed show IDs as `provider:id`, for example `anikoto:some-show-slug`. Queue jobs resolve the episode source through `providers/bridge.js`, then `provider_downloader.py` downloads the media with `ffmpeg` into a staging directory. If a provider returns multiple servers for the requested sub or dub mode, Kaizoku tries every same-mode server source in quality order before falling back to another provider, including when resolving one server's playlist raises an HTTP error. It does not use sub sources for dub downloads, or dub sources for sub downloads. If a provider returns a master HLS playlist, Kaizoku selects the highest-bandwidth variant before starting `ffmpeg`. When a media playlist uses extensionless, SnapCDN, or disguised CDN segments, Kaizoku skips direct ffmpeg and uses a StrawVerse-style segment downloader; if HLS preflight fails, it stays on that native path instead of falling through to ffmpeg. The native downloader fetches the media playlist, preserves provider CDN session cookies across playlist, segment, and encryption-key requests, downloads and concatenates segments itself with browser-like media headers and the provider referer, can fall back to curl-impersonate browser wrappers after protected CDN `HTTP 403` responses, strips short PNG wrappers when present, retries temporary HTTP failures such as `429 Too Many Requests`, optionally paces requests with `KAIZOKU_SEGMENT_DOWNLOAD_DELAY`, then remuxes the local transport stream to MP4. Direct ffmpeg attempts also have a timeout guard so stalled HLS inputs can fall back cleanly. Each episode is written as a temporary `.mp4.part` file and moved into the downloads library after that episode succeeds, so already-finished episodes from a larger batch survive if a later episode fails. Retrying that failed queue job requests only the remaining episodes while keeping the original episode range for display and watchlist sync. Finalization preserves episode numbers from staged `SxxEyy` or `Episode yy` filenames before applying configured season/episode offsets.
+1 -1
View File
@@ -1 +1 @@
0.53.14
0.53.15
+15 -5
View File
@@ -2101,6 +2101,9 @@ class WatchlistStore:
if requested_series is None or str(requested_series).strip() == "":
requested_series = item.get("auto_download_series")
item["auto_download_series"] = normalize_season(requested_series)
item["auto_download_name"] = sanitize_path_component(
payload.get("auto_download_name") or item["auto_download_name"], item["auto_download_name"]
)
requested_mode = str(payload.get("auto_download_mode") or item.get("auto_download_mode") or "").strip().lower()
if requested_mode in MODE_CHOICES:
item["auto_download_mode"] = requested_mode
@@ -2640,13 +2643,16 @@ class WatchlistStore:
def update_auto_download_settings(self, show_id, mode=None, quality=None, name=None, source_name=None, series=None, episode_offset=None):
existing = self.get(show_id)
defaults = self._auto_download_defaults(existing["title"])
normalized_mode = str(mode or defaults["auto_download_mode"]).strip().lower()
normalized_quality = str(quality or defaults["auto_download_quality"]).strip().lower()
normalized_mode = str(mode or existing["auto_download_mode"]).strip().lower()
normalized_quality = str(quality or existing["auto_download_quality"]).strip().lower()
if normalized_mode not in MODE_CHOICES:
raise ValueError("Auto-download mode must be sub or dub.")
if normalized_quality not in QUALITY_CHOICES:
raise ValueError("Unknown auto-download quality.")
normalized_name = sanitize_path_component(name or defaults["auto_download_name"], defaults["auto_download_name"])
normalized_name = sanitize_path_component(
existing["auto_download_name"] if name is None else name,
defaults["auto_download_name"],
)
normalized_source_name = str(source_name or existing.get("auto_download_source_name") or defaults["auto_download_source_name"]).strip()
if not normalized_source_name:
normalized_source_name = defaults["auto_download_source_name"]
@@ -3078,7 +3084,7 @@ def update_watchlist_media_type(show_id, media_type):
def update_watchlist_auto_download(show_id, mode=None, quality=None, name=None, source_name=None, series=None, episode_offset=None):
ensure_runtime()
runtime = ensure_runtime()
item = WATCHLIST.update_auto_download_settings(
show_id,
mode=mode,
@@ -3088,7 +3094,11 @@ def update_watchlist_auto_download(show_id, mode=None, quality=None, name=None,
series=series,
episode_offset=episode_offset,
)
return {"message": f"Saved auto-download settings for {item['title']}.", "item": item}
updated_jobs = runtime["download_queue"].update_watchlist_output_settings(item)
message = f"Saved auto-download settings for {item['title']}."
if updated_jobs:
message += f" Updated output naming for {updated_jobs} pending download(s)."
return {"message": message, "item": item, "updated_jobs": updated_jobs}
def update_all_watchlist_statuses():
+36
View File
@@ -1710,6 +1710,29 @@ class DownloadQueue:
self.current_job["updated_at"] = now_iso()
return {"ok": True, "count": count}
def update_watchlist_output_settings(self, item):
"""Apply saved naming to unstarted jobs without splitting a running batch."""
count = 0
with self.lock, self._connect() as conn:
rows = conn.execute(
"SELECT * FROM jobs WHERE show_id = ? AND status = 'pending'",
(item["show_id"],),
).fetchall()
for row in rows:
job = self._job_from_row(row)
if job.get("started_at") or job.get("completed_episodes") or job.get("skip_watchlist_sync"):
continue
job.update(
anime_name=item["auto_download_name"],
season=item["auto_download_series"],
episode_offset=item.get("auto_download_offset"),
updated_at=now_iso(),
)
job["target_dir"] = str(job_output_dir(job))
self._upsert_job_conn(conn, job)
count += 1
return count
def retry(self, job_id):
with self.lock:
job = self._find(job_id)
@@ -1973,6 +1996,19 @@ class DownloadQueue:
self._run_job(job)
def _run_job(self, job):
# Claim and reload together: settings can change after _next_pending()
# returns, and must not change the output layout once the job starts.
with self.lock:
try:
stored = self._find(job["id"])
except KeyError:
return
if stored["status"] != "pending":
return
job.update(stored)
job["status"] = "running"
job["started_at"] = now_iso()
self._save_job_locked(job)
staging_dir = job_staging_dir(job)
target_dir = job_output_dir(job)
try:
+1
View File
@@ -766,6 +766,7 @@ INDEX_HTML = r"""<!doctype html>
category: $("trackCategory").value,
media_type: $("trackMediaType").value,
auto_download_mode: $("selectionMode").value,
auto_download_name: $("animeNameInput").value || state.selected.title,
auto_download_series: $("seasonInput").value || "1"
})
});
+90 -3
View File
@@ -25,6 +25,10 @@ TEMP_STATE = tempfile.TemporaryDirectory()
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
# State paths are resolved when app_support is imported.
os.environ["KAIZOKU_DISABLE_WORKER"] = "1"
os.environ["KAIZOKU_STATE_ROOT"] = TEMP_STATE.name
import app_support
import queue_jobs
import http_handler
@@ -33,9 +37,6 @@ import provider_downloader
import provider_updates
import template_helpers
os.environ.setdefault("KAIZOKU_DISABLE_WORKER", "1")
os.environ["KAIZOKU_STATE_ROOT"] = TEMP_STATE.name
SPEC = importlib.util.spec_from_file_location("kaizoku_app", MODULE_PATH)
APP = importlib.util.module_from_spec(SPEC)
assert SPEC.loader is not None
@@ -3614,6 +3615,86 @@ class DiscordWebhookFormattingTests(unittest.TestCase):
class AutoDownloadQueueTests(unittest.TestCase):
def test_saved_naming_updates_pending_job_and_final_library_path(self):
with tempfile.TemporaryDirectory() as directory:
config = {**APP.DEFAULT_CONFIG, "download_dir": directory, "auto_download_enabled": True}
queue = APP.DownloadQueue(lambda: config, start_worker=False)
show_id = "anikoto:naming-pending-regression"
item = APP.WATCHLIST.add({"show_id": show_id, "title": "Provider Season Title"})["item"]
item["dub_episode_values"] = ["1"]
runtime = {"config": config, "download_queue": queue, "watchlist": APP.WATCHLIST}
with mock.patch.object(APP, "ensure_runtime", return_value=runtime):
job = APP.queue_watchlist_auto_download(item)["jobs"][0]
self.assertEqual(job["anime_name"], "Provider Season Title")
self.assertEqual(job["season"], "1")
saved = APP.update_watchlist_auto_download(show_id, name="My Custom Library", series="3")
self.assertEqual(saved["updated_jobs"], 1)
stored = queue.get(job["id"])
self.assertEqual(stored["anime_name"], "My Custom Library")
self.assertEqual(stored["season"], "3")
self.assertEqual(stored["show_id"], show_id)
staging = queue_jobs.job_staging_dir(job)
staging.mkdir(parents=True, exist_ok=True)
(staging / "Provider Season Title - S01E01.mp4").write_bytes(b"video")
(staging / "Provider Season Title - S01E01.eng.srt").write_bytes(b"subtitle")
process = mock.Mock(pid=4321, stdout=[])
process.wait.return_value = 0
with mock.patch.object(queue_jobs.subprocess, "Popen", return_value=process):
# Pass the stale job already fetched by the worker before Save.
queue._run_job(job)
self.assertEqual(queue.get(job["id"])["status"], "done")
target = Path(directory) / "tv" / "My Custom Library" / "Season 03"
self.assertEqual((target / "My Custom Library - S03E01.mp4").read_bytes(), b"video")
self.assertEqual((target / "My Custom Library - S03E01.eng.srt").read_bytes(), b"subtitle")
self.assertFalse((Path(directory) / "tv" / "Provider Season Title").exists())
def test_saved_naming_leaves_running_finished_and_partial_jobs_unchanged(self):
with tempfile.TemporaryDirectory() as directory:
config = {**APP.DEFAULT_CONFIG, "download_dir": directory}
queue = APP.DownloadQueue(lambda: config, start_worker=False)
ids = []
for status, completed in (("running", []), ("done", []), ("pending", ["1"])):
job = queue.add({"show_id": "anikoto:naming-status", "query": "Source", "episodes": "1-2"})
job.update(status=status, completed_episodes=completed)
with queue.lock:
queue._save_job_locked(job)
ids.append(job["id"])
count = queue.update_watchlist_output_settings({
"show_id": "anikoto:naming-status", "auto_download_name": "Custom", "auto_download_series": "3",
})
self.assertEqual(count, 0)
for job_id in ids:
self.assertEqual(queue.get(job_id)["anime_name"], "Source")
self.assertEqual(queue.get(job_id)["season"], "1")
def test_worker_does_not_start_job_canceled_or_removed_after_selection(self):
with tempfile.TemporaryDirectory() as directory:
queue = APP.DownloadQueue(lambda: {**APP.DEFAULT_CONFIG, "download_dir": directory}, start_worker=False)
for remove in (False, True):
job = queue.add({"query": "Canceled show", "episodes": "1"})
queue.cancel(job["id"])
if remove:
queue.remove(job["id"])
with mock.patch.object(queue_jobs.subprocess, "Popen") as launch:
queue._run_job(job)
launch.assert_not_called()
def test_watchlist_add_and_partial_update_preserve_custom_naming(self):
show_id = "anikoto:naming-add-regression"
item = APP.WATCHLIST.add({
"show_id": show_id, "title": "Provider Name", "auto_download_name": "Custom Name", "auto_download_series": "3",
})["item"]
self.assertEqual(item["auto_download_name"], "Custom Name")
self.assertEqual(item["auto_download_series"], "3")
item = APP.WATCHLIST.update_auto_download_settings(show_id, mode="sub", quality="720")
self.assertEqual(item["auto_download_name"], "Custom Name")
self.assertEqual(item["auto_download_series"], "3")
item = APP.WATCHLIST.update_auto_download_settings(show_id, series="0")
self.assertEqual(item["auto_download_name"], "Custom Name")
self.assertEqual(item["auto_download_series"], "0")
self.assertEqual(item["auto_download_mode"], "sub")
self.assertEqual(item["auto_download_quality"], "720")
def test_queue_watchlist_auto_download_skips_initial_refresh(self):
item = {
"show_id": "show-1",
@@ -3892,6 +3973,12 @@ class AutoDownloadQueueTests(unittest.TestCase):
)
class StartupBehaviorTests(unittest.TestCase):
def test_regression_suite_uses_temporary_state_paths(self):
root = Path(TEMP_STATE.name)
for path in (APP.STATE_DB_PATH, queue_jobs.STATE_DB_PATH, app_support.STATE_DB_PATH,
app_support.CONFIG_PATH, app_support.STAGING_ROOT, app_support.THUMBNAIL_ROOT):
self.assertTrue(path.is_relative_to(root), str(path))
def test_main_does_not_eagerly_initialize_runtime(self):
server = mock.Mock()
server.serve_forever.side_effect = KeyboardInterrupt()