Compare commits

..
1 Commits
Author SHA1 Message Date
Dymas 4dd56b72f6 Add anikoto backup downloader
- add a config toggle for backup downloads and retry failed ani-cli jobs with anikoto-cli
- install anikoto-cli systemwide in the container with its required dependencies
- bump the project version to 0.46.1 and publish the release notes
2026-07-09 12:54:14 +02:00
18 changed files with 272 additions and 2125 deletions
-1
View File
@@ -10,4 +10,3 @@ __pycache__
venv venv
env env
*.log *.log
_backup_/
-1
View File
@@ -9,4 +9,3 @@ __pycache__/
venv/ venv/
env/ env/
*.log *.log
_backup_/
-7
View File
@@ -1,7 +0,0 @@
- always bump up the VERSION unless its changes to Dockerfile, *.md files
- always document changes in simple CHANGELOG.md unless modifiing Dockerfile, *.md, VERSION
- when needed you may install any tools using pip if the project reqires it
- you can freely acces any LAN URL and github.com URL.
- always update README.md to reflect current project.
- always commit changes with commentary and push
- always make sure that update wont wipe ani data in a previosly running instance like configuration or watchlist content
+4 -92
View File
@@ -1,98 +1,10 @@
# Changelog # Changelog
## 0.50.2 - 2026-08-01 ## 0.46.1 - 2026-07-09
- Re-ran queue job preparation when retrying failed or canceled downloads, letting retries repair missing `ani-cli` result selections before launching. - Added a Config-page backup download toggle that lets queue jobs retry failed `ani-cli` downloads automatically with `anikoto-cli`.
- Added result-index inference for older or watchlist-created jobs by matching the stored show ID, or a single confident title match, against current provider search results. - Installed `anikoto-cli` system-wide in the container image alongside `ani-cli`, and added the extra `jq` and `mpv` packages it currently needs even for download-only runs.
- Exposed `anikoto-cli` in Runtime info and dependency checks so it is easier to verify that the backup downloader is available.
## 0.50.1 - 2026-08-01
- Passed the selected Search result number through queued `ani-cli` jobs as `-S`, so downloads avoid the interactive series picker when multiple similar titles are returned.
- Preserved the selected result index in queued jobs for more reliable watchlist identity sync after ambiguous downloads.
## 0.50.0 - 2026-08-01
- Added `curl-impersonate` to Docker images using the maintained `lexiforest/curl-impersonate` image so `ani-cli` v5 can use browser-like curl wrappers before falling back to plain `curl`.
- Exposed `curl-impersonate` availability in the Config page dependency status.
- Updated Docker/runtime documentation for the new `ani-cli` v5 Cloudflare workaround.
## 0.49.9 - 2026-07-26
- Changed the Config page `ani-cli` version probe to run with `ANI_CLI_PLAYER=download`, avoiding false missing-player failures on headless installs without `mpv` or `vlc`.
- Added regression coverage for the player-safe `ani-cli --version` environment.
## 0.49.8 - 2026-07-21
- Forced queued `ani-cli` attempts into explicit download-player mode with `ANI_CLI_PLAYER=download`, preventing headless Docker jobs from failing the upstream player check when `mpv` or `vlc` are not installed.
- Expanded fallback regression coverage for the `ani-cli` download-mode environment.
## 0.49.7 - 2026-07-21
- Ignored the local `_backup_/` folder in git and Docker build contexts so temporary backups do not get committed or copied into images.
- Added an agent note to preserve existing ani-cli-web configuration and watchlist data during future updates.
## 0.49.6 - 2026-07-21
- Added Config page watchlist export and import actions for full watchlist database backups.
- Backup files include raw `watchlist` rows plus `watchlist_removals`, preserving tracked shows, downloaded state, metadata, auto-download settings, thumbnails, and removed-show history.
- Expanded regression coverage for backup export/import behavior and the new HTTP routes.
## 0.49.5 - 2026-07-21
- Added a manual Config page action that reconciles watchlist downloaded episode flags against the current filesystem library.
- The new sync removes downloaded flags for missing files, adds flags for episodes already present on disk for the watchlist's active download mode, and updates movie entries when their library folder exists again.
- Expanded regression coverage for the new filesystem reconciliation flow and Config route wiring.
## 0.49.4 - 2026-07-19
- Added optional download job stdout mirroring with `ANI_CLI_WEB_JOB_STDOUT`, enabled by default in Docker and Docker Compose so container log collectors can capture downloader output.
## 0.49.3 - 2026-07-19
- Changed Docker builds to copy the current local `ani-cli-web` checkout into the image instead of cloning the configured remote branch, so rebuilt images include local Docker/runtime fixes immediately.
- Installed Docker `anipy-cli` and `animdl` in separate virtualenvs, with `animdl` pinned under Python 3.11 so its older native dependency pins resolve and remain executable.
- Removed the unused `ANI_CLI_WEB` compose build argument and updated Docker documentation to match the local-checkout build flow.
## 0.49.2 - 2026-07-19
- Fixed the Config page dependency check so Docker-installed fallback tools are detected from `/usr/local/bin` and `/usr/bin`, matching the runtime command lookup.
- Set the Docker image `PATH` explicitly to include system binary directories before the app starts.
## 0.49.1 - 2026-07-19
- Fixed Docker fallback tool discovery by exposing `anipy-cli` and `animdl` from fixed system paths and checking common system install locations at runtime.
- Changed queue jobs to fail immediately with a clear message when every selected download method is unavailable, instead of trying to launch an unavailable command.
## 0.49.0 - 2026-07-19
- Added `animdl` as a third queue download backend and Docker-installed fallback tool.
- Replaced the single `anipy-cli` fallback toggle with selectable download methods for `ani-cli`, `anipy-cli`, and `animdl`; queue jobs try the checked methods in that order.
- Exposed `animdl` availability and version in the Config page runtime checks.
## 0.48.3 - 2026-07-17
- Added Jellyfin handoff jobs to the Queue page so manual copy jobs appear alongside downloads with live progress, moved-file counts, and recent handoff activity.
## 0.48.2 - 2026-07-09
- Added support for a checked-in `favicon.png`, serving it from the app and wiring it into the website pages so browsers show the custom favicon.
## 0.48.1 - 2026-07-09
- Changed queue inserts so adding a download that exactly matches an existing failed job now reuses that failed entry and resets it to pending instead of creating another duplicate failed row.
## 0.48.0 - 2026-07-09
- Added a `Clear failed` bulk action to the Queue page so failed jobs can be removed without touching pending, running, finished, or canceled downloads.
## 0.47.1 - 2026-07-09
- Fixed `anipy-cli` fallback renaming for queued TV downloads so retried jobs now keep the requested episode numbers from the queue, avoiding cases like a retried `S02E11` being renamed as `S02E01`.
## 0.47.0 - 2026-07-09
- Added an optional `anipy-cli` fallback toggle on the Config page so failed `ani-cli` queue jobs can automatically retry once with `anipy-cli`.
- Exposed `anipy-cli` availability and version in Config page runtime checks, and updated queue processing so fallback downloads still finalize into the same TV or movie library layout.
## 0.46.0 - 2026-06-14 ## 0.46.0 - 2026-06-14
+25 -39
View File
@@ -1,22 +1,23 @@
FROM lexiforest/curl-impersonate:latest AS curl-impersonate FROM python:3.12-slim
FROM python:3.12-slim-bookworm
ARG ANI_CLI=https://github.com/pystardust/ani-cli.git ARG ANI_CLI=https://github.com/pystardust/ani-cli.git
ARG ANI_CLI_BRANCH=master ARG ANI_CLI_BRANCH=master
ARG ANIKOTO_CLI=https://github.com/Slimyslushy/anikoto-cli.git
ARG ANIKOTO_CLI_BRANCH=main
ARG ANI_CLI_WEB=https://gitea.coreplay.eu/Dymas/ani-cli-web.git
ARG YT_DLP_RELEASE_URL=https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp ARG YT_DLP_RELEASE_URL=https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp
ENV DEBIAN_FRONTEND=noninteractive \ ENV DEBIAN_FRONTEND=noninteractive \
ANI_CLI_REPO=${ANI_CLI} \ ANI_CLI_REPO=${ANI_CLI} \
ANI_CLI_BRANCH=${ANI_CLI_BRANCH} \ ANI_CLI_BRANCH=${ANI_CLI_BRANCH} \
PATH=/usr/local/bin:/usr/bin:/bin \ ANIKOTO_CLI_REPO=${ANIKOTO_CLI} \
ANIKOTO_CLI_BRANCH=${ANIKOTO_CLI_BRANCH} \
ANI_CLI_WEB_REPO=${ANI_CLI_WEB} \
ANI_CLI_BIN=ani-cli \ ANI_CLI_BIN=ani-cli \
ANIPY_CLI_BIN=/usr/local/bin/anipy-cli \ ANIKOTO_CLI_BIN=anikoto-cli \
ANIMDL_BIN=/usr/local/bin/animdl \
ANI_CLI_DOWNLOAD_DIR=/downloads \ ANI_CLI_DOWNLOAD_DIR=/downloads \
ANI_CLI_WEB_HOST=0.0.0.0 \ ANI_CLI_WEB_HOST=0.0.0.0 \
ANI_CLI_WEB_PORT=8421 \ ANI_CLI_WEB_PORT=8421 \
ANI_CLI_WEB_JOB_STDOUT=true \
UPDATE_ON_START=false \ UPDATE_ON_START=false \
USER_UID= \ USER_UID= \
USER_GID= USER_GID=
@@ -24,32 +25,21 @@ ENV DEBIAN_FRONTEND=noninteractive \
RUN apt-get update \ RUN apt-get update \
&& apt-get install -y --no-install-recommends \ && apt-get install -y --no-install-recommends \
aria2 \ aria2 \
bash \
botan \
ca-certificates \ ca-certificates \
curl \ curl \
ffmpeg \ ffmpeg \
fzf \ fzf \
build-essential \
git \ git \
grep \ grep \
libxml2-dev \ jq \
libxslt1-dev \ mpv \
libstdc++6 \
openssl \ openssl \
patch \ patch \
python3.11 \
python3.11-venv \
sed \ sed \
sudo \ sudo \
util-linux \ util-linux \
&& rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/apt/lists/*
COPY --from=curl-impersonate /usr/local /usr/local
RUN curl_firefox135 --version \
&& curl_chrome136 --version
RUN git clone --depth 1 --branch "${ANI_CLI_BRANCH}" "${ANI_CLI}" /tmp/ani-cli-src \ RUN git clone --depth 1 --branch "${ANI_CLI_BRANCH}" "${ANI_CLI}" /tmp/ani-cli-src \
&& install -m 0755 /tmp/ani-cli-src/ani-cli /usr/local/bin/ani-cli \ && install -m 0755 /tmp/ani-cli-src/ani-cli /usr/local/bin/ani-cli \
&& ani_cli_raw_repo="$(printf '%s' "${ANI_CLI}" | sed -E 's#^https://github.com/##; s#\\.git$##')" \ && ani_cli_raw_repo="$(printf '%s' "${ANI_CLI}" | sed -E 's#^https://github.com/##; s#\\.git$##')" \
@@ -59,29 +49,25 @@ RUN git clone --depth 1 --branch "${ANI_CLI_BRANCH}" "${ANI_CLI}" /tmp/ani-cli-s
esac \ esac \
&& rm -rf /tmp/ani-cli-src && rm -rf /tmp/ani-cli-src
RUN git clone --depth 1 --branch "${ANIKOTO_CLI_BRANCH}" "${ANIKOTO_CLI}" /tmp/anikoto-cli-src \
&& install -m 0755 /tmp/anikoto-cli-src/anikoto-cli /usr/local/bin/anikoto-cli \
&& rm -rf /tmp/anikoto-cli-src
RUN curl -fL "${YT_DLP_RELEASE_URL}" -o /usr/local/bin/yt-dlp \ RUN curl -fL "${YT_DLP_RELEASE_URL}" -o /usr/local/bin/yt-dlp \
&& chmod 0755 /usr/local/bin/yt-dlp \ && chmod 0755 /usr/local/bin/yt-dlp \
&& yt-dlp --version && yt-dlp --version
RUN python3 -m venv /opt/anipy-cli \ RUN git clone --depth 1 "${ANI_CLI_WEB}" /app \
&& /opt/anipy-cli/bin/pip install --no-cache-dir --upgrade pip \ && rm -rf /app/.git \
&& /opt/anipy-cli/bin/pip install --no-cache-dir anipy-cli \ && rm -f \
&& python3.11 -m venv /opt/animdl \ /app/.dockerignore \
&& /opt/animdl/bin/pip install --no-cache-dir --upgrade pip \ /app/.gitignore \
&& /opt/animdl/bin/pip install --no-cache-dir animdl==1.7.27 \ /app/Dockerfile \
&& test -x /opt/anipy-cli/bin/anipy-cli \ /app/README.md \
&& test -x /opt/animdl/bin/animdl \ /app/ani-cli-web \
&& printf '%s\n' '#!/bin/sh' 'exec /opt/anipy-cli/bin/anipy-cli "$@"' > /usr/local/bin/anipy-cli \ /app/docker-compose.yaml \
&& printf '%s\n' '#!/bin/sh' 'exec /opt/animdl/bin/animdl "$@"' > /usr/local/bin/animdl \ /app/docker-entrypoint.sh \
&& chmod 0755 /usr/local/bin/anipy-cli /usr/local/bin/animdl \ /app/test_app.py
&& ln -sf /usr/local/bin/anipy-cli /usr/bin/anipy-cli \
&& ln -sf /usr/local/bin/animdl /usr/bin/animdl \
&& /usr/local/bin/anipy-cli --version \
&& /usr/local/bin/animdl --version
RUN mkdir -p /app
COPY app.py app_support.py config_page.py http_handler.py queue_jobs.py queue_page.py search_page.py template_helpers.py title_matching.py watchlist_identity.py watchlist_page.py web_templates.py VERSION CHANGELOG.md favicon.png /app/
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
+15 -42
View File
@@ -2,35 +2,34 @@
Local web UI for a system-wide `ani-cli` install. Local web UI for a system-wide `ani-cli` install.
Current version: `0.50.2` Current version: `0.46.1`
## What it does ## What it does
- Search anime in `sub` or `dub`, with poster-style results shown below the selection panel and the first English alternate title when available - Search anime in `sub` or `dub`, with poster-style results shown below the selection panel and the first English alternate title when available
- Queue downloads with folder, quality, media type, season, episode controls, and the selected upstream search result when a Search card is used - Queue downloads with folder, quality, media type, season, and episode controls
- Choose which download backends queue jobs may use: `ani-cli`, `anipy-cli`, and `animdl` - Optionally retry failed `ani-cli` downloads with system-wide `anikoto-cli` as a backup downloader
- Track shows in a watchlist with `Watching`, `Planned`, `Finished`, and `Dropped` categories - Track shows in a watchlist with `Watching`, `Planned`, `Finished`, and `Dropped` categories
- Expose a small JSON summary endpoint for Homepage dashboard widgets - Expose a small JSON summary endpoint for Homepage dashboard widgets
- Refresh watchlist episode counts manually or on a schedule - Refresh watchlist episode counts manually or on a schedule
- Auto-download missing episodes for `Watching` entries after later refreshes, with a per-show `Source name` override for `ani-cli` search - Auto-download missing episodes for `Watching` entries after later refreshes, with a per-show `Source name` override for `ani-cli` search
- Move fully completed libraries into Jellyfin TV or movie folders with tracked background handoff jobs that also appear in the Queue page - Move fully completed libraries into Jellyfin TV or movie folders with a tracked background handoff job
- Send optional Discord webhook notifications - Send optional Discord webhook notifications
- Serve a checked-in `favicon.png` as the site favicon
## Pages ## Pages
- `/`: Search and queue downloads, with poster-card search results below the selection box and alternate English titles under the main name when available - `/`: Search and queue downloads, with poster-card search results below the selection box and alternate English titles under the main name when available
- `/queue`: Monitor downloads and Jellyfin handoff jobs, inspect logs, retry failed downloads, avoid duplicate re-queues for the same failed download, clear failed jobs, and remove finished jobs - `/queue`: Monitor jobs, inspect logs, retry failed jobs, remove finished jobs
- `/watchlist`: Track shows, refresh them, download all available episodes, and manage auto-download settings such as `Source name`, library `Name`, season, and episode offset - `/watchlist`: Track shows, refresh them, download all available episodes, and manage auto-download settings such as `Source name`, library `Name`, season, and episode offset
- `/config`: Save defaults, choose queue download methods and fallback retries, schedule refreshes, configure Jellyfin and Discord, export/import watchlist backups, manually reconcile watchlist download flags against the filesystem, monitor Jellyfin handoff progress, and view runtime info and the changelog - `/config`: Save defaults, schedule refreshes, configure Jellyfin and Discord, monitor Jellyfin handoff progress, and view runtime info and the changelog
## Quick start ## Quick start
Requirements: Requirements:
- `ani-cli` installed and available in `PATH` - `ani-cli` installed and available in `PATH`
- Optional: `anipy-cli` and/or `animdl` installed and available in `PATH` if you want automatic fallback retries outside Docker - Optional: `anikoto-cli` installed in `PATH` if you want backup downloads
- Common runtime tools used by `ani-cli`, especially `curl` or `curl-impersonate`, `ffmpeg`, `fzf`, `grep`, `openssl`, `sed`, `aria2c`, `botan`, and `yt-dlp` - Common runtime tools used by `ani-cli` and `anikoto-cli`, especially `curl`, `ffmpeg`, `fzf`, `grep`, `jq`, `mpv`, `openssl`, `sed`, `aria2c`, and `yt-dlp`
Run locally: Run locally:
@@ -49,10 +48,7 @@ Useful flags and env:
- `./ani-cli-web --debug` - `./ani-cli-web --debug`
- `ANI_CLI_WEB_HOST=0.0.0.0` - `ANI_CLI_WEB_HOST=0.0.0.0`
- `ANI_CLI_WEB_PORT=8421` - `ANI_CLI_WEB_PORT=8421`
- `ANI_CLI_WEB_JOB_STDOUT=true`
- `ANI_CLI_BIN=/path/to/ani-cli` - `ANI_CLI_BIN=/path/to/ani-cli`
- `ANIPY_CLI_BIN=/path/to/anipy-cli`
- `ANIMDL_BIN=/path/to/animdl`
## Docker ## Docker
@@ -62,8 +58,6 @@ Build:
docker build -t ani-cli-web . docker build -t ani-cli-web .
``` ```
Docker builds the app from the current local checkout.
Run: Run:
```sh ```sh
@@ -89,10 +83,7 @@ Docker notes:
- App state is stored under `/app/.ani-cli-web` - App state is stored under `/app/.ani-cli-web`
- `USER_UID` and `USER_GID` help keep mounted files owned by your host user - `USER_UID` and `USER_GID` help keep mounted files owned by your host user
- `UPDATE_ON_START=true` runs `ani-cli --update` before startup - `UPDATE_ON_START=true` runs `ani-cli --update` before startup
- `ANI_CLI_WEB_JOB_STDOUT=true` mirrors download job output to container stdout for log collectors; set it to `false` to keep job output only in the Queue page - The image installs both `ani-cli` and `anikoto-cli` system-wide under `/usr/local/bin`
- `curl-impersonate` is installed inside the container and exposes the browser wrapper commands used by `ani-cli` v5, such as `curl_firefox135` and `curl_chrome136`, so provider requests can avoid Cloudflare blocks that affect plain `curl`
- `botan` is installed inside the container for providers or scripts that need Botan cryptography tooling
- `anipy-cli` and `animdl` are installed systemwide inside the container at `/usr/local/bin`, linked from `/usr/bin`, and included in the runtime `PATH`; `animdl` is isolated in a Python 3.11 virtualenv so its pinned dependencies stay executable
## Key behavior ## Key behavior
@@ -104,7 +95,6 @@ Docker notes:
- Successful downloads sync back into the watchlist - Successful downloads sync back into the watchlist
- TV entries move to `Finished` when the selected downloaded mode reaches the expected episode count - TV entries move to `Finished` when the selected downloaded mode reaches the expected episode count
- Movie entries can be treated as complete after download and routed to movie storage - Movie entries can be treated as complete after download and routed to movie storage
- Watchlist backups exported from Config include the full watchlist database rows and removed-show history for restore/import
### Auto-download ### Auto-download
@@ -112,23 +102,11 @@ Docker notes:
- Uses the per-show language, quality, `Source name` search override, season, library name, and optional episode offset settings - Uses the per-show language, quality, `Source name` search override, season, library name, and optional episode offset settings
- Can queue backlog episodes that were already available before the show was first tracked - Can queue backlog episodes that were already available before the show was first tracked
### Download fallback ### Backup downloads
- Optional and configured from `/config` - Configure the global backup toggle from `/config`
- Queue jobs can use any checked combination of `ani-cli`, `anipy-cli`, and `animdl` - When enabled, any failed `ani-cli` queue job automatically retries with `anikoto-cli`
- Checked methods are tried in this order: `ani-cli`, `anipy-cli`, then `animdl` - `anikoto-cli` ignores the saved quality setting, so backup attempts use the source's default stream quality
- Selecting one method disables fallback retry; selecting multiple methods makes later methods automatic fallbacks
- Queue `ani-cli` downloads explicitly run with `ANI_CLI_PLAYER=download` so headless Docker installs do not need `mpv` or `vlc` just to save files
- Docker images include `curl-impersonate` wrappers before plain `curl` on `PATH`, matching the preference order used by `ani-cli` v5
- Queue jobs created from Search pass the selected result number to `ani-cli -S`, avoiding the interactive series picker when the provider returns multiple similar titles
- Retried failed jobs are prepared again before running, so older or watchlist-created jobs can infer and persist an `ani-cli -S` result when the stored show ID or title maps cleanly to a provider search result
- Fallback retries keep the queued episode numbers when fallback downloader filenames are renumbered from `1`
- Docker images install `anipy-cli` and `animdl` automatically; non-Docker installs should provide the selected tools in `PATH` themselves
### Queue retries
- Adding a download that exactly matches an existing failed queue job reuses that failed entry and sets it back to `pending`
- This avoids stacking multiple failed rows for the same episode when you queue the same job again
### Jellyfin handoff ### Jellyfin handoff
@@ -136,7 +114,6 @@ Docker notes:
- TV libraries move only after the show is finished and download-complete - TV libraries move only after the show is finished and download-complete
- Movie libraries move after download completion - Movie libraries move after download completion
- Manual `Run now` scans the watchlist in a background job and shows live progress for processed entries, moved files, and the current destination path - Manual `Run now` scans the watchlist in a background job and shows live progress for processed entries, moved files, and the current destination path
- Manual watchlist filesystem sync rechecks downloaded episode flags against the files currently present under the download library
- Recent Jellyfin handoff jobs survive page reloads and show their latest status after app restarts - Recent Jellyfin handoff jobs survive page reloads and show their latest status after app restarts
## Homepage widget API ## Homepage widget API
@@ -166,8 +143,7 @@ This endpoint is intentionally small so Homepage can map the returned values dir
The Config page runtime panel shows: The Config page runtime panel shows:
- `ani-cli-web` version loaded from `VERSION` - `ani-cli-web` version loaded from `VERSION`
- Installed `ani-cli` version, checked with download-player mode so headless installs do not need `mpv` or `vlc` for runtime info - Installed `ani-cli` version
- Installed `anipy-cli` and `animdl` versions and dependency status
- A `Changelog` button that opens a scrollable viewer backed by `CHANGELOG.md` - A `Changelog` button that opens a scrollable viewer backed by `CHANGELOG.md`
## Remote access ## Remote access
@@ -203,8 +179,6 @@ Main contents:
- `state.sqlite3` - `state.sqlite3`
- `thumbnails/` - `thumbnails/`
Local `_backup_/` folders are ignored by git and Docker builds so temporary backup copies stay outside release artifacts.
Downloaded files are organized like this: Downloaded files are organized like this:
```text ```text
@@ -217,8 +191,7 @@ ANI_CLI_DOWNLOAD_DIR/movie/Movie Name/Movie Name.mp4
Most users only need these environment variables: Most users only need these environment variables:
- `ANI_CLI_BIN` - `ANI_CLI_BIN`
- `ANIPY_CLI_BIN` - `ANIKOTO_CLI_BIN`
- `ANIMDL_BIN`
- `ANI_CLI_WEB_HOST` - `ANI_CLI_WEB_HOST`
- `ANI_CLI_WEB_PORT` - `ANI_CLI_WEB_PORT`
- `ANI_CLI_WEB_ALLOW_REMOTE` - `ANI_CLI_WEB_ALLOW_REMOTE`
+1 -1
View File
@@ -1 +1 @@
0.50.2 0.46.1
+1 -314
View File
@@ -58,7 +58,6 @@ from app_support import (
normalize_config, normalize_config,
normalize_episode_offset, normalize_episode_offset,
normalize_media_type, normalize_media_type,
normalize_result_index,
now_iso, now_iso,
remote_access_session_fingerprint, remote_access_session_fingerprint,
sanitize_path_component, sanitize_path_component,
@@ -75,10 +74,7 @@ from title_matching import (
title_lookup_queries, title_lookup_queries,
title_match_variants, title_match_variants,
) )
from watchlist_identity import ( from watchlist_identity import resolve_job_watchlist_identity as resolve_job_watchlist_identity_impl
resolve_job_watchlist_identity as resolve_job_watchlist_identity_impl,
titles_confidently_match,
)
from web_templates import CONFIG_HTML, INDEX_HTML, PAGE_LINKS, QUEUE_HTML, WATCHLIST_HTML, render_page_links, render_sidebar_brand from web_templates import CONFIG_HTML, INDEX_HTML, PAGE_LINKS, QUEUE_HTML, WATCHLIST_HTML, render_page_links, render_sidebar_brand
RUNTIME_LOCK = threading.RLock() RUNTIME_LOCK = threading.RLock()
@@ -97,7 +93,6 @@ DOWNLOAD_QUEUE = None
WATCHLIST_REFRESH = None WATCHLIST_REFRESH = None
WATCHLIST_AUTO_REFRESH = None WATCHLIST_AUTO_REFRESH = None
WATCHLIST_JELLYFIN_SYNC = None WATCHLIST_JELLYFIN_SYNC = None
WATCHLIST_BACKUP_FORMAT_VERSION = 1
def graph_request(query, variables, timeout=25): def graph_request(query, variables, timeout=25):
@@ -992,14 +987,6 @@ def watchlist_source_library_dir(item, config):
return Path(str((config or {}).get("download_dir") or DEFAULT_CONFIG["download_dir"])).expanduser() / media_type / watchlist_download_library_name(item) return Path(str((config or {}).get("download_dir") or DEFAULT_CONFIG["download_dir"])).expanduser() / media_type / watchlist_download_library_name(item)
def watchlist_source_season_dir(item, config):
source_root = watchlist_source_library_dir(item, config)
if normalize_media_type(item.get("media_type")) == "movie":
return source_root
season = normalize_season((item or {}).get("auto_download_series") or "1")
return source_root / f"Season {int(season):02d}"
def jellyfin_target_library_dir(item, config): def jellyfin_target_library_dir(item, config):
media_type = normalize_media_type(item.get("media_type")) media_type = normalize_media_type(item.get("media_type"))
key = "jellyfin_movie_dir" if media_type == "movie" else "jellyfin_tv_dir" key = "jellyfin_movie_dir" if media_type == "movie" else "jellyfin_tv_dir"
@@ -1056,62 +1043,6 @@ def watchlist_item_download_complete(item, mode=""):
return True, "ready" return True, "ready"
def normalize_downloaded_episode_value(value):
text = str(value or "").strip()
if not text:
return ""
match = re.match(r"^0*([0-9]+)(.*)$", text)
if not match:
return text
number = int(match.group(1) or "0", 10)
suffix = str(match.group(2) or "")
return f"{number}{suffix}" if number > 0 else text
def reverse_episode_offset(value, offset):
normalized_offset = normalize_episode_offset(offset)
text = str(value or "").strip()
if not normalized_offset or not text:
return normalize_downloaded_episode_value(text)
match = re.match(r"^0*([0-9]+)(.*)$", text)
if not match:
return normalize_downloaded_episode_value(text)
number = int(match.group(1) or "0", 10)
if number < 1:
return normalize_downloaded_episode_value(text)
logical_number = number - int(normalized_offset, 10) + 1
if logical_number < 1:
return ""
suffix = str(match.group(2) or "")
return f"{logical_number}{suffix}"
def watchlist_downloaded_episode_values_from_filesystem(item, config, mode):
normalized_mode = str(mode or "").strip().lower()
if normalized_mode not in MODE_CHOICES:
return []
season_dir = watchlist_source_season_dir(item, config)
if not season_dir.exists() or not season_dir.is_dir():
return []
season = normalize_season((item or {}).get("auto_download_series") or "1")
pattern = re.compile(rf"\bS{int(season):02d}E([0-9][0-9A-Za-z.\-]*)\b", re.IGNORECASE)
values = []
seen = set()
offset = (item or {}).get("auto_download_offset")
for path in sorted(season_dir.rglob("*")):
if not path.is_file():
continue
match = pattern.search(path.stem)
if not match:
continue
logical_value = reverse_episode_offset(match.group(1), offset)
normalized_value = normalize_downloaded_episode_value(logical_value)
if normalized_value and normalized_value not in seen:
seen.add(normalized_value)
values.append(normalized_value)
return values
def _move_tree_contents(source_dir, target_dir, progress_fn=None): def _move_tree_contents(source_dir, target_dir, progress_fn=None):
moved = [] moved = []
file_paths = [ file_paths = [
@@ -1434,9 +1365,6 @@ class WatchlistStore:
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
return conn return conn
def _table_columns_conn(self, conn, table_name):
return [str(row["name"]) for row in conn.execute(f"PRAGMA table_info({table_name})").fetchall()]
def _exists_conn(self, conn, show_id): def _exists_conn(self, conn, show_id):
row = conn.execute( row = conn.execute(
"SELECT 1 FROM watchlist WHERE show_id = ? LIMIT 1", "SELECT 1 FROM watchlist WHERE show_id = ? LIMIT 1",
@@ -2167,192 +2095,6 @@ class WatchlistStore:
) )
return self.schedule_refresh(existing["show_id"], source=source) return self.schedule_refresh(existing["show_id"], source=source)
def reconcile_downloaded_filesystem(self, config=None):
active_config = normalize_config(config or self.config_getter() or {})
items = []
changed = 0
with self.lock, self._connect() as conn:
rows = conn.execute("SELECT * FROM watchlist ORDER BY title COLLATE NOCASE ASC").fetchall()
for row in rows:
existing_item = self._row_to_item(row)
media_type = normalize_media_type(existing_item.get("media_type"))
if media_type == "movie":
source_dir = watchlist_source_library_dir(existing_item, active_config)
has_files = source_dir.exists() and any(path.is_file() for path in source_dir.rglob("*"))
new_sub_values = []
new_dub_values = []
downloaded = has_files
else:
filesystem_values = watchlist_downloaded_episode_values_from_filesystem(existing_item, active_config, "dub")
if filesystem_values:
preferred_mode = watchlist_preferred_completion_mode(existing_item)
new_sub_values = list(existing_item.get("downloaded_sub_episodes") or [])
new_dub_values = list(existing_item.get("downloaded_dub_episodes") or [])
if preferred_mode == "sub":
new_sub_values = filesystem_values
else:
new_dub_values = filesystem_values
downloaded = bool(new_sub_values or new_dub_values)
else:
new_sub_values = []
new_dub_values = []
downloaded = False
old_sub_values = existing_item.get("downloaded_sub_episodes") or []
old_dub_values = existing_item.get("downloaded_dub_episodes") or []
sub_added = [value for value in new_sub_values if value not in old_sub_values]
sub_removed = [value for value in old_sub_values if value not in new_sub_values]
dub_added = [value for value in new_dub_values if value not in old_dub_values]
dub_removed = [value for value in old_dub_values if value not in new_dub_values]
downloaded_changed = bool(existing_item.get("downloaded")) != downloaded
row_changed = bool(sub_added or sub_removed or dub_added or dub_removed or downloaded_changed)
if row_changed:
conn.execute(
"""
UPDATE watchlist
SET downloaded = ?, downloaded_sub_episodes_json = ?, downloaded_dub_episodes_json = ?, updated_at = ?
WHERE show_id = ?
""",
(
1 if downloaded else 0,
encode_episode_values(new_sub_values) if new_sub_values else None,
encode_episode_values(new_dub_values) if new_dub_values else None,
now_iso(),
existing_item["show_id"],
),
)
changed += 1
items.append(
{
"show_id": existing_item.get("show_id"),
"title": existing_item.get("title"),
"media_type": media_type,
"changed": row_changed,
"downloaded": downloaded,
"sub_added": sub_added,
"sub_removed": sub_removed,
"dub_added": dub_added,
"dub_removed": dub_removed,
}
)
return {
"message": f"Reconciled downloaded watchlist files for {len(items)} entr{'y' if len(items) == 1 else 'ies'}."
+ (f" Updated {changed}." if changed else " No changes were needed."),
"total": len(items),
"changed": changed,
"items": items,
}
def export_backup(self):
with self.lock, self._connect() as conn:
watchlist_columns = self._table_columns_conn(conn, "watchlist")
removal_columns = self._table_columns_conn(conn, "watchlist_removals")
watchlist_rows = [
{column: row[column] for column in watchlist_columns}
for row in conn.execute("SELECT * FROM watchlist ORDER BY title COLLATE NOCASE ASC").fetchall()
]
removal_rows = [
{column: row[column] for column in removal_columns}
for row in conn.execute("SELECT * FROM watchlist_removals ORDER BY removed_at DESC").fetchall()
]
return {
"format": "ani-cli-web-watchlist-backup",
"format_version": WATCHLIST_BACKUP_FORMAT_VERSION,
"app_version": VERSION,
"exported_at": now_iso(),
"tables": {
"watchlist": {
"columns": watchlist_columns,
"rows": watchlist_rows,
},
"watchlist_removals": {
"columns": removal_columns,
"rows": removal_rows,
},
},
}
def import_backup(self, backup):
if not isinstance(backup, dict):
raise ValueError("Watchlist backup must be a JSON object.")
if backup.get("format") != "ani-cli-web-watchlist-backup":
raise ValueError("Backup format is not recognized.")
try:
format_version = int(backup.get("format_version") or 0)
except (TypeError, ValueError) as exc:
raise ValueError("Backup format version is invalid.") from exc
if format_version < 1 or format_version > WATCHLIST_BACKUP_FORMAT_VERSION:
raise ValueError("Backup format version is not supported by this app version.")
tables = backup.get("tables")
if not isinstance(tables, dict):
raise ValueError("Backup is missing table data.")
watchlist_rows = ((tables.get("watchlist") or {}).get("rows") if isinstance(tables.get("watchlist"), dict) else None)
removal_rows = (
(tables.get("watchlist_removals") or {}).get("rows")
if isinstance(tables.get("watchlist_removals"), dict)
else []
)
if not isinstance(watchlist_rows, list):
raise ValueError("Backup is missing watchlist rows.")
if not isinstance(removal_rows, list):
raise ValueError("Backup removal rows must be an array.")
now = now_iso()
with self.lock, self._connect() as conn:
watchlist_columns = self._table_columns_conn(conn, "watchlist")
removal_columns = self._table_columns_conn(conn, "watchlist_removals")
conn.execute("DELETE FROM watchlist")
conn.execute("DELETE FROM watchlist_removals")
imported_watchlist = 0
for row in watchlist_rows:
if not isinstance(row, dict):
raise ValueError("Backup watchlist rows must be JSON objects.")
show_id = str(row.get("show_id") or "").strip()
title = str(row.get("title") or "").strip()
if not show_id or not title:
raise ValueError("Backup watchlist rows must include show_id and title.")
values = {column: row.get(column) for column in watchlist_columns}
values["show_id"] = show_id
values["title"] = title
values["category"] = normalize_watchlist_category(values.get("category"))
values["downloaded"] = 1 if values.get("downloaded") else 0
values["status"] = str(values.get("status") or "tracked")
values["status_message"] = str(values.get("status_message") or "Imported from backup.")
values["created_at"] = str(values.get("created_at") or now)
values["updated_at"] = str(values.get("updated_at") or now)
values["sub_count"] = int(values.get("sub_count") or 0)
values["dub_count"] = int(values.get("dub_count") or 0)
values["expected_count"] = int(values.get("expected_count") or 0)
values["media_type"] = normalize_media_type(values.get("media_type"))
placeholders = ", ".join("?" for _column in watchlist_columns)
column_sql = ", ".join(watchlist_columns)
conn.execute(
f"INSERT INTO watchlist ({column_sql}) VALUES ({placeholders})",
[values.get(column) for column in watchlist_columns],
)
imported_watchlist += 1
imported_removals = 0
for row in removal_rows:
if not isinstance(row, dict):
raise ValueError("Backup removal rows must be JSON objects.")
show_id = str(row.get("show_id") or "").strip()
if not show_id:
continue
values = {column: row.get(column) for column in removal_columns}
values["show_id"] = show_id
values["title"] = str(values.get("title") or "").strip()
values["removed_at"] = str(values.get("removed_at") or now)
placeholders = ", ".join("?" for _column in removal_columns)
column_sql = ", ".join(removal_columns)
conn.execute(
f"INSERT INTO watchlist_removals ({column_sql}) VALUES ({placeholders})",
[values.get(column) for column in removal_columns],
)
imported_removals += 1
return {
"message": f"Imported {imported_watchlist} watchlist entr{'y' if imported_watchlist == 1 else 'ies'} from backup.",
"imported": imported_watchlist,
"removals": imported_removals,
}
def mark_downloaded(self, show_id, title="", mode="", episodes="", media_type=""): def mark_downloaded(self, show_id, title="", mode="", episodes="", media_type=""):
normalized_show_id = str(show_id or "").strip() normalized_show_id = str(show_id or "").strip()
if not normalized_show_id: if not normalized_show_id:
@@ -2911,22 +2653,6 @@ def get_jellyfin_sync_status():
return WATCHLIST_JELLYFIN_SYNC.status() return WATCHLIST_JELLYFIN_SYNC.status()
def export_watchlist_backup():
runtime = ensure_runtime()
return runtime["watchlist"].export_backup()
def import_watchlist_backup(payload):
runtime = ensure_runtime()
return runtime["watchlist"].import_backup(payload)
def reconcile_watchlist_downloaded_files(config=None):
runtime = ensure_runtime()
active_config = normalize_config(config or runtime["config"] or {})
return runtime["watchlist"].reconcile_downloaded_filesystem(active_config)
def update_watchlist_category(show_id, category): def update_watchlist_category(show_id, category):
ensure_runtime() ensure_runtime()
item = WATCHLIST.update_category(show_id, category) item = WATCHLIST.update_category(show_id, category)
@@ -3011,10 +2737,6 @@ def sync_downloaded_job_to_watchlist(job):
def prepare_download_job(job): def prepare_download_job(job):
config = ensure_runtime()["config"] config = ensure_runtime()["config"]
if normalize_result_index(job.get("result_index")) is None:
result_index = infer_download_job_result_index(job, config)
if result_index is not None:
job["result_index"] = result_index
show_id, title, reason = resolve_job_watchlist_identity_impl( show_id, title, reason = resolve_job_watchlist_identity_impl(
job, job,
search_fn=search_anime, search_fn=search_anime,
@@ -3032,36 +2754,6 @@ def prepare_download_job(job):
return job return job
def infer_download_job_result_index(job, config):
query = str(job.get("query") or job.get("title") or job.get("anime_name") or "").strip()
mode = str(job.get("mode") or config.get("mode") or "sub").strip().lower()
if not query or mode not in MODE_CHOICES:
return None
try:
results = search_anime(query, mode)
except Exception as exc:
debug_log("watchlist.prepare_job.result_index_search_failed", job=job, error=exc)
return None
show_id = str(job.get("show_id") or "").strip()
if show_id:
for position, result in enumerate(results, start=1):
if str(result.get("id") or "").strip() == show_id:
return position
title = str(job.get("title") or job.get("anime_name") or query).strip()
matches = []
for position, result in enumerate(results, start=1):
candidate_title = str(result.get("title") or "").strip()
if titles_confidently_match(title, candidate_title, normalize_identity_title_key, base_title_variants):
matches.append(position)
if len(matches) == 1:
return matches[0]
if len(matches) > 1:
debug_log("watchlist.prepare_job.result_index_ambiguous", job=job, matches=matches)
return None
def get_config_snapshot(fallback=None): def get_config_snapshot(fallback=None):
with RUNTIME_LOCK: with RUNTIME_LOCK:
source = CONFIG if CONFIG is not None else fallback source = CONFIG if CONFIG is not None else fallback
@@ -3074,8 +2766,6 @@ def save_runtime_config(config):
current = get_config_snapshot(fallback=DEFAULT_CONFIG) current = get_config_snapshot(fallback=DEFAULT_CONFIG)
merged = dict(current) merged = dict(current)
if isinstance(config, dict): if isinstance(config, dict):
if "anipy_cli_fallback_enabled" in config and "download_methods" not in config:
merged.pop("download_methods", None)
merged.update(config) merged.update(config)
normalized = normalize_config(merged) normalized = normalize_config(merged)
previous_fingerprint = remote_access_session_fingerprint(current) previous_fingerprint = remote_access_session_fingerprint(current)
@@ -3251,7 +2941,6 @@ Handler = build_handler_class(
download_watchlist_item=download_watchlist_item, download_watchlist_item=download_watchlist_item,
ensure_runtime=ensure_runtime, ensure_runtime=ensure_runtime,
episode_list=episode_list, episode_list=episode_list,
export_watchlist_backup=export_watchlist_backup,
get_config_snapshot=get_config_snapshot, get_config_snapshot=get_config_snapshot,
get_watchlist_homepage_summary=get_watchlist_homepage_summary, get_watchlist_homepage_summary=get_watchlist_homepage_summary,
get_jellyfin_sync_status=get_jellyfin_sync_status, get_jellyfin_sync_status=get_jellyfin_sync_status,
@@ -3259,9 +2948,7 @@ Handler = build_handler_class(
get_watchlist_refresh_status=get_watchlist_refresh_status, get_watchlist_refresh_status=get_watchlist_refresh_status,
http_error=HttpError, http_error=HttpError,
index_html=INDEX_HTML, index_html=INDEX_HTML,
import_watchlist_backup=import_watchlist_backup,
queue_html=QUEUE_HTML, queue_html=QUEUE_HTML,
reconcile_watchlist_downloaded_files=reconcile_watchlist_downloaded_files,
remove_from_watchlist=remove_from_watchlist, remove_from_watchlist=remove_from_watchlist,
run_jellyfin_sync=run_jellyfin_sync, run_jellyfin_sync=run_jellyfin_sync,
runtime_state=runtime_state, runtime_state=runtime_state,
+28 -135
View File
@@ -22,24 +22,8 @@ from uuid import uuid4
PROJECT_ROOT = Path(__file__).resolve().parent PROJECT_ROOT = Path(__file__).resolve().parent
ANI_CLI = os.environ.get("ANI_CLI_BIN") or shutil.which("ani-cli") or "ani-cli"
ANIKOTO_CLI = os.environ.get("ANIKOTO_CLI_BIN") or shutil.which("anikoto-cli") or "anikoto-cli"
def configured_cli_command(env_name, executable_name):
configured = str(os.environ.get(env_name) or "").strip()
if configured:
return configured
resolved = shutil.which(executable_name)
if resolved:
return resolved
for candidate in (Path("/usr/local/bin") / executable_name, Path("/usr/bin") / executable_name):
if candidate.exists() and os.access(candidate, os.X_OK):
return str(candidate)
return executable_name
ANI_CLI = configured_cli_command("ANI_CLI_BIN", "ani-cli")
ANIPY_CLI = configured_cli_command("ANIPY_CLI_BIN", "anipy-cli")
ANIMDL = configured_cli_command("ANIMDL_BIN", "animdl")
APP_NAME = "ani-cli-web" APP_NAME = "ani-cli-web"
VERSION_FILE = PROJECT_ROOT / "VERSION" VERSION_FILE = PROJECT_ROOT / "VERSION"
CHANGELOG_FILE = PROJECT_ROOT / "CHANGELOG.md" CHANGELOG_FILE = PROJECT_ROOT / "CHANGELOG.md"
@@ -57,7 +41,6 @@ DEBUG_MODE = False
QUALITY_CHOICES = {"best", "1080", "1080p", "720", "720p", "480", "480p", "360", "360p", "worst"} QUALITY_CHOICES = {"best", "1080", "1080p", "720", "720p", "480", "480p", "360", "360p", "worst"}
MODE_CHOICES = {"sub", "dub"} MODE_CHOICES = {"sub", "dub"}
DOWNLOAD_METHOD_CHOICES = ("ani-cli", "anipy-cli", "animdl")
MEDIA_TYPE_CHOICES = {"tv", "movie"} MEDIA_TYPE_CHOICES = {"tv", "movie"}
WATCHLIST_CATEGORY_LABELS = { WATCHLIST_CATEGORY_LABELS = {
"watching": "Watching", "watching": "Watching",
@@ -271,10 +254,6 @@ def background_workers_enabled():
return not env_flag("ANI_CLI_WEB_DISABLE_WORKER") return not env_flag("ANI_CLI_WEB_DISABLE_WORKER")
def job_stdout_enabled():
return env_flag("ANI_CLI_WEB_JOB_STDOUT")
def client_address_is_local(host): def client_address_is_local(host):
text = str(host or "").strip() text = str(host or "").strip()
if not text: if not text:
@@ -312,8 +291,7 @@ DEFAULT_CONFIG = {
"download_dir": default_download_dir(), "download_dir": default_download_dir(),
"mode": os.environ.get("ANI_CLI_MODE", "sub"), "mode": os.environ.get("ANI_CLI_MODE", "sub"),
"quality": os.environ.get("ANI_CLI_QUALITY", "best"), "quality": os.environ.get("ANI_CLI_QUALITY", "best"),
"anipy_cli_fallback_enabled": False, "download_backup_enabled": False,
"download_methods": ["ani-cli"],
"watchlist_auto_refresh_enabled": False, "watchlist_auto_refresh_enabled": False,
"watchlist_auto_refresh_minutes": WATCHLIST_AUTO_REFRESH_DEFAULT_MINUTES, "watchlist_auto_refresh_minutes": WATCHLIST_AUTO_REFRESH_DEFAULT_MINUTES,
"watchlist_refresh_delay_seconds": WATCHLIST_REFRESH_DELAY_DEFAULT_SECONDS, "watchlist_refresh_delay_seconds": WATCHLIST_REFRESH_DELAY_DEFAULT_SECONDS,
@@ -460,7 +438,6 @@ def write_json(path, data):
def normalize_config(data): def normalize_config(data):
source_has_download_methods = isinstance(data, dict) and "download_methods" in data
config = dict(DEFAULT_CONFIG) config = dict(DEFAULT_CONFIG)
if isinstance(data, dict): if isinstance(data, dict):
config.update({key: data[key] for key in DEFAULT_CONFIG if key in data}) config.update({key: data[key] for key in DEFAULT_CONFIG if key in data})
@@ -470,11 +447,10 @@ def normalize_config(data):
if quality.endswith("p") and quality[:-1].isdigit(): if quality.endswith("p") and quality[:-1].isdigit():
quality = quality[:-1] quality = quality[:-1]
download_dir = str(config.get("download_dir") or DEFAULT_CONFIG["download_dir"]) download_dir = str(config.get("download_dir") or DEFAULT_CONFIG["download_dir"])
anipy_cli_fallback_enabled = config.get("anipy_cli_fallback_enabled")
download_methods = config.get("download_methods")
auto_refresh_enabled = config.get("watchlist_auto_refresh_enabled") auto_refresh_enabled = config.get("watchlist_auto_refresh_enabled")
auto_refresh_minutes = config.get("watchlist_auto_refresh_minutes") auto_refresh_minutes = config.get("watchlist_auto_refresh_minutes")
refresh_delay_seconds = config.get("watchlist_refresh_delay_seconds") refresh_delay_seconds = config.get("watchlist_refresh_delay_seconds")
download_backup_enabled = config.get("download_backup_enabled")
auto_download_enabled = config.get("auto_download_enabled") auto_download_enabled = config.get("auto_download_enabled")
auto_download_mode = str(config.get("auto_download_mode", "dub")).lower() auto_download_mode = str(config.get("auto_download_mode", "dub")).lower()
auto_download_quality = str(config.get("auto_download_quality", "best")).lower() auto_download_quality = str(config.get("auto_download_quality", "best")).lower()
@@ -488,22 +464,6 @@ def normalize_config(data):
auth_username = str(config.get("auth_username") or "").strip() auth_username = str(config.get("auth_username") or "").strip()
auth_password = str(config.get("auth_password") or "").strip() auth_password = str(config.get("auth_password") or "").strip()
if isinstance(anipy_cli_fallback_enabled, str):
anipy_cli_fallback_enabled = anipy_cli_fallback_enabled.strip().lower() in {"1", "true", "yes", "on"}
else:
anipy_cli_fallback_enabled = bool(anipy_cli_fallback_enabled)
if isinstance(download_methods, str):
method_values = [part.strip().lower() for part in re.split(r"[\s,]+", download_methods) if part.strip()]
elif isinstance(download_methods, list):
method_values = [str(part or "").strip().lower() for part in download_methods]
else:
method_values = []
normalized_methods = []
for method in method_values:
if method in DOWNLOAD_METHOD_CHOICES and method not in normalized_methods:
normalized_methods.append(method)
if not normalized_methods or not source_has_download_methods:
normalized_methods = ["ani-cli", "anipy-cli"] if anipy_cli_fallback_enabled else ["ani-cli"]
if isinstance(auto_refresh_enabled, str): if isinstance(auto_refresh_enabled, str):
auto_refresh_enabled = auto_refresh_enabled.strip().lower() in {"1", "true", "yes", "on"} auto_refresh_enabled = auto_refresh_enabled.strip().lower() in {"1", "true", "yes", "on"}
else: else:
@@ -512,6 +472,10 @@ def normalize_config(data):
auto_download_enabled = auto_download_enabled.strip().lower() in {"1", "true", "yes", "on"} auto_download_enabled = auto_download_enabled.strip().lower() in {"1", "true", "yes", "on"}
else: else:
auto_download_enabled = bool(auto_download_enabled) auto_download_enabled = bool(auto_download_enabled)
if isinstance(download_backup_enabled, str):
download_backup_enabled = download_backup_enabled.strip().lower() in {"1", "true", "yes", "on"}
else:
download_backup_enabled = bool(download_backup_enabled)
if isinstance(jellyfin_sync_enabled, str): if isinstance(jellyfin_sync_enabled, str):
jellyfin_sync_enabled = jellyfin_sync_enabled.strip().lower() in {"1", "true", "yes", "on"} jellyfin_sync_enabled = jellyfin_sync_enabled.strip().lower() in {"1", "true", "yes", "on"}
else: else:
@@ -537,8 +501,7 @@ def normalize_config(data):
config["mode"] = mode if mode in MODE_CHOICES else "sub" config["mode"] = mode if mode in MODE_CHOICES else "sub"
config["quality"] = quality if quality in QUALITY_CHOICES else "best" config["quality"] = quality if quality in QUALITY_CHOICES else "best"
config["download_dir"] = str(Path(download_dir).expanduser()) config["download_dir"] = str(Path(download_dir).expanduser())
config["download_methods"] = normalized_methods config["download_backup_enabled"] = download_backup_enabled
config["anipy_cli_fallback_enabled"] = len(normalized_methods) > 1
config["watchlist_auto_refresh_enabled"] = auto_refresh_enabled config["watchlist_auto_refresh_enabled"] = auto_refresh_enabled
config["watchlist_auto_refresh_minutes"] = auto_refresh_minutes config["watchlist_auto_refresh_minutes"] = auto_refresh_minutes
config["watchlist_refresh_delay_seconds"] = refresh_delay_seconds config["watchlist_refresh_delay_seconds"] = refresh_delay_seconds
@@ -1006,17 +969,6 @@ def strip_control(value):
return re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", "", str(value)).replace("\r", "\n") return re.sub(r"\x1b\[[0-9;?]*[A-Za-z]", "", str(value)).replace("\r", "\n")
def cli_executable_exists(command):
text = str(command or "").strip()
if not text:
return False
return bool(shutil.which(text) or (Path(text).exists() and os.access(text, os.X_OK)))
def cli_executable_exists_any(*commands):
return any(cli_executable_exists(command) for command in commands)
def printable_command(command): def printable_command(command):
return " ".join(sh_quote(part) for part in command) return " ".join(sh_quote(part) for part in command)
@@ -1117,26 +1069,6 @@ def extract_episode_from_filename(path):
return match.group(1) if match else None return match.group(1) if match else None
def episode_values_from_spec(value):
spec = str(value or "").strip()
if not spec:
return []
values = []
for part in re.split(r"\s+", spec):
if not part:
continue
if "-" in part:
start_text, end_text = [piece.strip() for piece in part.split("-", 1)]
if start_text.isdigit() and end_text.isdigit():
start = int(start_text, 10)
end = int(end_text, 10)
if start <= end:
values.extend(str(number) for number in range(start, end + 1))
continue
values.append(part)
return values
def unique_destination(path): def unique_destination(path):
if not path.exists(): if not path.exists():
return path return path
@@ -1157,25 +1089,19 @@ def finalize_library_files(job):
episode_offset = normalize_episode_offset(job.get("episode_offset")) episode_offset = normalize_episode_offset(job.get("episode_offset"))
target_dir.mkdir(parents=True, exist_ok=True) target_dir.mkdir(parents=True, exist_ok=True)
files = sorted( files = sorted(
[path for path in staging_dir.rglob("*") if path.is_file()], [path for path in staging_dir.iterdir() if path.is_file()],
key=lambda path: (path.stat().st_mtime, str(path.relative_to(staging_dir)).lower()), key=lambda path: (path.stat().st_mtime, path.name),
) )
if not files: if not files:
raise RuntimeError("No downloaded files were found in the staging folder") raise RuntimeError("No downloaded files were found in the staging folder")
moved = [] moved = []
fallback_episode = 1 fallback_episode = 1
requested_episode_values = episode_values_from_spec(job.get("episodes"))
backend = str(job.get("download_backend") or "").strip().lower()
for source in files: for source in files:
if media_type == "movie": if media_type == "movie":
final_name = f"{library_name}{source.suffix}" final_name = f"{library_name}{source.suffix}"
else: else:
ep = None ep = extract_episode_from_filename(source)
if backend in {"anipy-cli", "animdl"} and requested_episode_values:
ep = requested_episode_values.pop(0)
if ep is None:
ep = extract_episode_from_filename(source)
if ep is None: if ep is None:
ep = str(fallback_episode) ep = str(fallback_episode)
fallback_episode += 1 fallback_episode += 1
@@ -1185,7 +1111,10 @@ def finalize_library_files(job):
shutil.move(str(source), str(destination)) shutil.move(str(source), str(destination))
moved.append(str(destination)) moved.append(str(destination))
shutil.rmtree(staging_dir, ignore_errors=True) try:
staging_dir.rmdir()
except OSError:
pass
return moved return moved
@@ -1216,7 +1145,7 @@ def build_job(payload, config):
"season": normalize_season(payload.get("season")), "season": normalize_season(payload.get("season")),
"episode_offset": normalize_episode_offset(payload.get("episode_offset")), "episode_offset": normalize_episode_offset(payload.get("episode_offset")),
"query": query, "query": query,
"result_index": normalize_result_index(payload.get("result_index")), "result_index": None,
"mode": mode, "mode": mode,
"quality": quality, "quality": quality,
"episodes": validate_episode_spec(payload.get("episodes")), "episodes": validate_episode_spec(payload.get("episodes")),
@@ -1232,54 +1161,21 @@ def build_job(payload, config):
} }
def normalize_result_index(value): def command_for_job(job, downloader="ani-cli"):
if value in (None, ""): if str(downloader or "ani-cli").strip().lower() == "anikoto-cli":
return None
try:
index = int(str(value).strip())
except (TypeError, ValueError):
return None
if index < 1:
return None
return index
def anipy_search_spec(job):
query = re.sub(r"\s+", " ", str(job.get("query") or "").replace(":", " ")).strip()
mode = "dub" if str(job.get("mode") or "").strip().lower() == "dub" else "sub"
return f"{query}:{job['episodes']}:{mode}"
def command_for_job(job, backend="ani-cli", download_path=None):
normalized_backend = str(backend or "ani-cli").strip().lower()
if normalized_backend == "anipy-cli":
command = [ command = [
ANIPY_CLI, ANIKOTO_CLI,
"-D", "-d",
"-s", "-e",
anipy_search_spec(job),
"-q",
job["quality"],
]
if download_path:
command.extend(["-l", str(download_path)])
return command
if normalized_backend == "animdl":
query = str(job.get("query") or "").strip()
command = [
ANIMDL,
"download",
query,
"-r",
job["episodes"], job["episodes"],
"-q",
job["quality"],
"--index",
"1",
] ]
if download_path: if job["mode"] == "dub":
command.extend(["-d", str(download_path)]) command.append("--dub")
else:
command.append("--sub")
command.append(job["query"])
return command return command
command = [ command = [
ANI_CLI, ANI_CLI,
"-d", "-d",
@@ -1288,9 +1184,6 @@ def command_for_job(job, backend="ani-cli", download_path=None):
"-e", "-e",
job["episodes"], job["episodes"],
] ]
result_index = normalize_result_index(job.get("result_index"))
if result_index is not None:
command.extend(["-S", str(result_index)])
if job["mode"] == "dub": if job["mode"] == "dub":
command.append("--dub") command.append("--dub")
command.append(job["query"]) command.append(job["query"])
+13 -137
View File
@@ -21,7 +21,6 @@ CONFIG_HTML = r"""<!doctype html>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>ani-cli web - config</title> <title>ani-cli web - config</title>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png">
<style> <style>
:root { :root {
color-scheme: dark; color-scheme: dark;
@@ -462,8 +461,7 @@ CONFIG_HTML = r"""<!doctype html>
<div class="stack muted"> <div class="stack muted">
<p id="versionLine">Loading version...</p> <p id="versionLine">Loading version...</p>
<p id="aniCliVersionLine">Detecting ani-cli version...</p> <p id="aniCliVersionLine">Detecting ani-cli version...</p>
<p id="anipyCliVersionLine">Detecting anipy-cli fallback...</p> <p id="anikotoCliVersionLine">Detecting anikoto-cli version...</p>
<p id="animdlVersionLine">Detecting animdl fallback...</p>
<p>Saved settings live in `.ani-cli-web/config.json` inside this project.</p> <p>Saved settings live in `.ani-cli-web/config.json` inside this project.</p>
</div> </div>
<button class="ghost wide-button" id="openChangelogBtn" type="button">Changelog</button> <button class="ghost wide-button" id="openChangelogBtn" type="button">Changelog</button>
@@ -505,23 +503,14 @@ CONFIG_HTML = r"""<!doctype html>
<option value="480">480p</option> <option value="480">480p</option>
</select> </select>
</label> </label>
<label>Backup downloader
<select id="downloadBackupEnabled">
<option value="false">Disabled</option>
<option value="true">Enabled</option>
</select>
</label>
</div> </div>
<div class="field-hint">Tip: these are the saved defaults only. The active search form can still be adjusted per download.</div> <div class="field-hint">Tip: these are the saved defaults only. The active search form can still be adjusted per download. When backup downloads are enabled, failed `ani-cli` jobs retry automatically with `anikoto-cli`.</div>
</section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Download fallback</h2>
<p class="muted">Choose the download methods queue jobs may use, in fallback order.</p>
</div>
</div>
<div class="check-grid">
<label class="check-item"><input class="download-method" type="checkbox" value="ani-cli"> <span>ani-cli</span></label>
<label class="check-item"><input class="download-method" type="checkbox" value="anipy-cli"> <span>anipy-cli</span></label>
<label class="check-item"><input class="download-method" type="checkbox" value="animdl"> <span>animdl</span></label>
</div>
<div class="field-hint">Jobs try checked methods in this order: ani-cli, anipy-cli, animdl. With one method checked, fallback retry is disabled.</div>
</section> </section>
<section class="settings settings-main"> <section class="settings settings-main">
@@ -612,34 +601,6 @@ CONFIG_HTML = r"""<!doctype html>
<div class="muted" id="jellyfinSyncStatus">No recent Jellyfin handoff job.</div> <div class="muted" id="jellyfinSyncStatus">No recent Jellyfin handoff job.</div>
</section> </section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Watchlist filesystem sync</h2>
<p class="muted">Manually reconcile downloaded episode flags with the files currently present under the download library.</p>
</div>
<button id="reconcileWatchlistDownloadsBtn" type="button">Sync now</button>
</div>
<div class="field-hint">This scans the configured download library, removes downloaded episode flags for files that no longer exist, and adds flags for episodes already present on disk.</div>
<div class="muted" id="watchlistReconcileStatus">No watchlist filesystem sync run yet.</div>
</section>
<section class="settings settings-main">
<div class="toolbar">
<div>
<h2>Watchlist backup</h2>
<p class="muted">Export or restore a complete watchlist database snapshot, including removed-show records.</p>
</div>
<div class="row">
<button id="exportWatchlistBtn" type="button">Export</button>
<button id="importWatchlistBtn" type="button">Import</button>
</div>
</div>
<input id="importWatchlistFile" type="file" accept="application/json,.json" hidden>
<div class="field-hint">Import replaces the current watchlist with the selected backup file.</div>
<div class="muted" id="watchlistBackupStatus">No watchlist backup action run yet.</div>
</section>
<section class="settings settings-main"> <section class="settings settings-main">
<div class="toolbar"> <div class="toolbar">
<div> <div>
@@ -712,8 +673,7 @@ CONFIG_HTML = r"""<!doctype html>
mode: "sub", mode: "sub",
quality: "best", quality: "best",
download_dir: "", download_dir: "",
anipy_cli_fallback_enabled: false, download_backup_enabled: false,
download_methods: ["ani-cli"],
watchlist_auto_refresh_enabled: false, watchlist_auto_refresh_enabled: false,
watchlist_auto_refresh_minutes: 60, watchlist_auto_refresh_minutes: 60,
watchlist_refresh_delay_seconds: 5, watchlist_refresh_delay_seconds: 5,
@@ -734,9 +694,7 @@ CONFIG_HTML = r"""<!doctype html>
home_path: "" home_path: ""
}, },
jellyfinSyncRunning: false, jellyfinSyncRunning: false,
jellyfinSyncJobId: null, jellyfinSyncJobId: null
watchlistReconcileRunning: false,
watchlistImportRunning: false
}; };
const $ = (id) => document.getElementById(id); const $ = (id) => document.getElementById(id);
@@ -773,39 +731,16 @@ CONFIG_HTML = r"""<!doctype html>
el.textContent = job.message || "Jellyfin handoff idle."; el.textContent = job.message || "Jellyfin handoff idle.";
} }
function setWatchlistReconcileButton(running) {
$("reconcileWatchlistDownloadsBtn").disabled = !!running;
$("reconcileWatchlistDownloadsBtn").textContent = running ? "Syncing..." : "Sync now";
}
function setWatchlistReconcileStatus(text) {
$("watchlistReconcileStatus").textContent = text || "No watchlist filesystem sync run yet.";
}
function setWatchlistBackupStatus(text) {
$("watchlistBackupStatus").textContent = text || "No watchlist backup action run yet.";
}
function setWatchlistImportButton(running) {
$("importWatchlistBtn").disabled = !!running;
$("importWatchlistBtn").textContent = running ? "Importing..." : "Import";
}
function selectedWebhookEvents() { function selectedWebhookEvents() {
return [...document.querySelectorAll(".webhook-event:checked")].map((input) => input.value); return [...document.querySelectorAll(".webhook-event:checked")].map((input) => input.value);
} }
function selectedDownloadMethods() {
const methods = [...document.querySelectorAll(".download-method:checked")].map((input) => input.value);
return methods.length ? methods : ["ani-cli"];
}
function formConfigPayload() { function formConfigPayload() {
return { return {
download_dir: $("downloadDir").value, download_dir: $("downloadDir").value,
mode: $("configMode").value, mode: $("configMode").value,
quality: $("configQuality").value, quality: $("configQuality").value,
download_methods: selectedDownloadMethods(), download_backup_enabled: $("downloadBackupEnabled").value === "true",
watchlist_auto_refresh_enabled: $("watchlistAutoRefreshEnabled").value === "true", watchlist_auto_refresh_enabled: $("watchlistAutoRefreshEnabled").value === "true",
watchlist_auto_refresh_minutes: $("watchlistAutoRefreshMinutes").value, watchlist_auto_refresh_minutes: $("watchlistAutoRefreshMinutes").value,
watchlist_refresh_delay_seconds: $("watchlistRefreshDelaySeconds").value, watchlist_refresh_delay_seconds: $("watchlistRefreshDelaySeconds").value,
@@ -825,10 +760,7 @@ CONFIG_HTML = r"""<!doctype html>
$("downloadDir").value = data.download_dir; $("downloadDir").value = data.download_dir;
$("configMode").value = data.mode; $("configMode").value = data.mode;
$("configQuality").value = data.quality; $("configQuality").value = data.quality;
const methods = new Set(data.download_methods || ["ani-cli"]); $("downloadBackupEnabled").value = String(Boolean(data.download_backup_enabled));
for (const input of document.querySelectorAll(".download-method")) {
input.checked = methods.has(input.value);
}
$("watchlistAutoRefreshEnabled").value = String(Boolean(data.watchlist_auto_refresh_enabled)); $("watchlistAutoRefreshEnabled").value = String(Boolean(data.watchlist_auto_refresh_enabled));
$("watchlistAutoRefreshMinutes").value = data.watchlist_auto_refresh_minutes; $("watchlistAutoRefreshMinutes").value = data.watchlist_auto_refresh_minutes;
$("watchlistRefreshDelaySeconds").value = data.watchlist_refresh_delay_seconds; $("watchlistRefreshDelaySeconds").value = data.watchlist_refresh_delay_seconds;
@@ -910,57 +842,6 @@ CONFIG_HTML = r"""<!doctype html>
} }
} }
async function reconcileWatchlistDownloads() {
state.watchlistReconcileRunning = true;
setWatchlistReconcileButton(true);
setWatchlistReconcileStatus("Scanning the download library...");
try {
const data = await api("/api/config/watchlist/reconcile-downloads", {
method: "POST",
body: JSON.stringify(formConfigPayload())
});
const changed = Number(data.changed || 0);
const total = Number(data.total || 0);
setWatchlistReconcileStatus(`${changed} updated out of ${total} watchlist entries.`);
setNotice(data.message || "Watchlist filesystem sync completed.");
} catch (error) {
setWatchlistReconcileStatus("Watchlist filesystem sync failed.");
setNotice(error.message);
} finally {
state.watchlistReconcileRunning = false;
setWatchlistReconcileButton(false);
}
}
function exportWatchlistBackup() {
setWatchlistBackupStatus("Preparing watchlist backup download...");
window.location.href = "/api/config/watchlist/export";
}
async function importWatchlistBackup(file) {
if (!file) return;
state.watchlistImportRunning = true;
setWatchlistImportButton(true);
setWatchlistBackupStatus("Reading backup file...");
try {
const payload = JSON.parse(await file.text());
setWatchlistBackupStatus("Importing backup...");
const data = await api("/api/config/watchlist/import", {
method: "POST",
body: JSON.stringify(payload)
});
setWatchlistBackupStatus(`${data.imported || 0} watchlist entries imported.`);
setNotice(data.message || "Watchlist backup imported.");
} catch (error) {
setWatchlistBackupStatus("Watchlist backup import failed.");
setNotice(error.message);
} finally {
$("importWatchlistFile").value = "";
state.watchlistImportRunning = false;
setWatchlistImportButton(false);
}
}
function closePathBrowser() { function closePathBrowser() {
state.pathBrowser = { state.pathBrowser = {
targetId: "", targetId: "",
@@ -1089,8 +970,7 @@ CONFIG_HTML = r"""<!doctype html>
const data = await api("/api/version"); const data = await api("/api/version");
$("versionLine").textContent = `${data.name} ${data.version}`; $("versionLine").textContent = `${data.name} ${data.version}`;
$("aniCliVersionLine").textContent = `ani-cli ${data.ani_cli_version || "Unavailable"}`; $("aniCliVersionLine").textContent = `ani-cli ${data.ani_cli_version || "Unavailable"}`;
$("anipyCliVersionLine").textContent = `anipy-cli ${data.anipy_cli_version || "Unavailable"}`; $("anikotoCliVersionLine").textContent = `anikoto-cli ${data.anikoto_cli_version || "Unavailable"}`;
$("animdlVersionLine").textContent = `animdl ${data.animdl_version || "Unavailable"}`;
} }
async function openChangelog() { async function openChangelog() {
@@ -1107,10 +987,6 @@ CONFIG_HTML = r"""<!doctype html>
$("saveConfigBtn").addEventListener("click", saveConfig); $("saveConfigBtn").addEventListener("click", saveConfig);
$("testWebhookBtn").addEventListener("click", testWebhook); $("testWebhookBtn").addEventListener("click", testWebhook);
$("runJellyfinSyncBtn").addEventListener("click", runJellyfinSync); $("runJellyfinSyncBtn").addEventListener("click", runJellyfinSync);
$("reconcileWatchlistDownloadsBtn").addEventListener("click", reconcileWatchlistDownloads);
$("exportWatchlistBtn").addEventListener("click", exportWatchlistBackup);
$("importWatchlistBtn").addEventListener("click", () => $("importWatchlistFile").click());
$("importWatchlistFile").addEventListener("change", (event) => importWatchlistBackup((event.target.files || [])[0]));
$("openChangelogBtn").addEventListener("click", openChangelog); $("openChangelogBtn").addEventListener("click", openChangelog);
$("closeChangelogBtn").addEventListener("click", closeChangelog); $("closeChangelogBtn").addEventListener("click", closeChangelog);
$("pathBrowserCloseBtn").addEventListener("click", closePathBrowser); $("pathBrowserCloseBtn").addEventListener("click", closePathBrowser);
+3 -1
View File
@@ -6,6 +6,9 @@ services:
args: args:
ANI_CLI: ${ANI_CLI:-https://github.com/pystardust/ani-cli.git} ANI_CLI: ${ANI_CLI:-https://github.com/pystardust/ani-cli.git}
ANI_CLI_BRANCH: ${ANI_CLI_BRANCH:-master} ANI_CLI_BRANCH: ${ANI_CLI_BRANCH:-master}
ANIKOTO_CLI: ${ANIKOTO_CLI:-https://github.com/Slimyslushy/anikoto-cli.git}
ANIKOTO_CLI_BRANCH: ${ANIKOTO_CLI_BRANCH:-main}
ANI_CLI_WEB: ${ANI_CLI_WEB:-https://gitea.coreplay.eu/Dymas/ani-cli-web.git}
image: ani-cli-web:latest image: ani-cli-web:latest
container_name: ani-cli-web container_name: ani-cli-web
restart: unless-stopped restart: unless-stopped
@@ -20,7 +23,6 @@ services:
ANI_CLI_WEB_ALLOW_REMOTE: ${ANI_CLI_WEB_ALLOW_REMOTE:-false} ANI_CLI_WEB_ALLOW_REMOTE: ${ANI_CLI_WEB_ALLOW_REMOTE:-false}
ANI_CLI_WEB_AUTH_USERNAME: ${ANI_CLI_WEB_AUTH_USERNAME:-} ANI_CLI_WEB_AUTH_USERNAME: ${ANI_CLI_WEB_AUTH_USERNAME:-}
ANI_CLI_WEB_AUTH_PASSWORD: ${ANI_CLI_WEB_AUTH_PASSWORD:-} ANI_CLI_WEB_AUTH_PASSWORD: ${ANI_CLI_WEB_AUTH_PASSWORD:-}
ANI_CLI_WEB_JOB_STDOUT: ${ANI_CLI_WEB_JOB_STDOUT:-true}
ANI_CLI_DOWNLOAD_DIR: /downloads ANI_CLI_DOWNLOAD_DIR: /downloads
volumes: volumes:
- ./downloads:/downloads - ./downloads:/downloads
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.3 KiB

+11 -158
View File
@@ -20,23 +20,19 @@ from pathlib import Path
from urllib.parse import parse_qs, unquote, urlencode, urlparse, urlunparse from urllib.parse import parse_qs, unquote, urlencode, urlparse, urlunparse
from app_support import ( from app_support import (
ANIMDL, ANIKOTO_CLI,
ANI_CLI, ANI_CLI,
ANIPY_CLI,
APP_NAME, APP_NAME,
CHANGELOG_FILE, CHANGELOG_FILE,
CLIENT_DISCONNECT_ERRORS, CLIENT_DISCONNECT_ERRORS,
MAX_JSON_BODY_BYTES, MAX_JSON_BODY_BYTES,
MODE_CHOICES, MODE_CHOICES,
PROJECT_ROOT,
VERSION, VERSION,
client_address_is_local, client_address_is_local,
configured_remote_path_roots, configured_remote_path_roots,
debug_enabled, debug_enabled,
debug_log, debug_log,
load_project_text_file, load_project_text_file,
cli_executable_exists_any,
cli_executable_exists,
normalize_config, normalize_config,
path_is_within_roots, path_is_within_roots,
remote_access_allowed, remote_access_allowed,
@@ -49,8 +45,6 @@ from app_support import (
validate_discord_webhook_url, validate_discord_webhook_url,
) )
FAVICON_PATH = PROJECT_ROOT / "favicon.png"
@dataclass(frozen=True) @dataclass(frozen=True)
class HandlerContext: class HandlerContext:
add_to_watchlist: object add_to_watchlist: object
@@ -58,7 +52,6 @@ class HandlerContext:
download_watchlist_item: object download_watchlist_item: object
ensure_runtime: object ensure_runtime: object
episode_list: object episode_list: object
export_watchlist_backup: object
get_config_snapshot: object get_config_snapshot: object
get_jellyfin_sync_status: object get_jellyfin_sync_status: object
get_watchlist_homepage_summary: object get_watchlist_homepage_summary: object
@@ -66,9 +59,7 @@ class HandlerContext:
get_watchlist_refresh_status: object get_watchlist_refresh_status: object
http_error: object http_error: object
index_html: str index_html: str
import_watchlist_backup: object
queue_html: str queue_html: str
reconcile_watchlist_downloaded_files: object
remove_from_watchlist: object remove_from_watchlist: object
run_jellyfin_sync: object run_jellyfin_sync: object
runtime_state: object runtime_state: object
@@ -95,27 +86,10 @@ def build_handler_class(context):
def dependency_status(): def dependency_status():
checks = ["curl", "sed", "grep", "openssl", "fzf", "aria2c", "ffmpeg", "yt-dlp"] checks = ["curl", "sed", "grep", "openssl", "fzf", "aria2c", "ffmpeg", "yt-dlp", "jq", "mpv"]
result = {name: bool(shutil.which(name)) for name in checks} result = {name: bool(shutil.which(name)) for name in checks}
result["curl-impersonate"] = cli_executable_exists_any( result["ani-cli"] = bool(shutil.which(ANI_CLI) or (Path(ANI_CLI).exists() and os.access(ANI_CLI, os.X_OK)))
"curl_firefox135", result["anikoto-cli"] = bool(shutil.which(ANIKOTO_CLI) or (Path(ANIKOTO_CLI).exists() and os.access(ANIKOTO_CLI, os.X_OK)))
"curl_chrome136",
"curl_chrome116",
"curl_ff117",
)
result["ani-cli"] = cli_executable_exists(ANI_CLI)
result["anipy-cli"] = cli_executable_exists_any(
ANIPY_CLI,
"anipy-cli",
"/usr/local/bin/anipy-cli",
"/usr/bin/anipy-cli",
)
result["animdl"] = cli_executable_exists_any(
ANIMDL,
"animdl",
"/usr/local/bin/animdl",
"/usr/bin/animdl",
)
return result return result
@@ -178,66 +152,6 @@ def browse_filesystem(path_value="", mode="dir", allowed_roots=None):
} }
def queue_list_payload(runtime, page=1, per_page=10):
page = max(1, int(page or 1))
per_page = min(50, max(1, int(per_page or 10)))
download_queue = runtime["download_queue"]
jellyfin_jobs = runtime.get("watchlist_jellyfin_sync")
download_payload = download_queue.list(page=1, per_page=50)
jobs = list(download_payload.get("jobs") or [])
for download_page in range(2, int(download_payload.get("pages") or 1) + 1):
jobs.extend(download_queue.list(page=download_page, per_page=50).get("jobs") or [])
if jellyfin_jobs is not None:
jobs.extend(jellyfin_jobs.list_queue_jobs(limit=200))
jobs.sort(key=lambda job: str(job.get("created_at") or ""), reverse=True)
total = len(jobs)
pages = max(1, (total + per_page - 1) // per_page)
offset = (page - 1) * per_page
return {
"jobs": jobs[offset : offset + per_page],
"page": page,
"per_page": per_page,
"total": total,
"pages": pages,
}
def queue_get_job(runtime, job_id):
try:
return runtime["download_queue"].get(job_id)
except KeyError:
jellyfin_jobs = runtime.get("watchlist_jellyfin_sync")
if jellyfin_jobs is None:
raise
return jellyfin_jobs.get_queue_job(job_id)
def queue_remove_job(runtime, job_id):
try:
return runtime["download_queue"].remove(job_id)
except KeyError:
jellyfin_jobs = runtime.get("watchlist_jellyfin_sync")
if jellyfin_jobs is None:
raise
return jellyfin_jobs.remove_queue_job(job_id)
def queue_clear_finished(runtime):
result = runtime["download_queue"].clear_finished()
jellyfin_jobs = runtime.get("watchlist_jellyfin_sync")
if jellyfin_jobs is not None:
result["count"] = int(result.get("count") or 0) + int(jellyfin_jobs.clear_finished_queue_jobs().get("count") or 0)
return result
def queue_clear_failed(runtime):
result = runtime["download_queue"].clear_failed()
jellyfin_jobs = runtime.get("watchlist_jellyfin_sync")
if jellyfin_jobs is not None:
result["count"] = int(result.get("count") or 0) + int(jellyfin_jobs.clear_failed_queue_jobs().get("count") or 0)
return result
@lru_cache(maxsize=1) @lru_cache(maxsize=1)
def installed_ani_cli_version(): def installed_ani_cli_version():
try: try:
@@ -245,11 +159,6 @@ def installed_ani_cli_version():
[ANI_CLI, "--version"], [ANI_CLI, "--version"],
check=True, check=True,
capture_output=True, capture_output=True,
env={
**os.environ.copy(),
"ANI_CLI_PLAYER": "download",
"TERM": os.environ.get("TERM", "xterm-256color"),
},
text=True, text=True,
timeout=6, timeout=6,
) )
@@ -260,26 +169,10 @@ def installed_ani_cli_version():
@lru_cache(maxsize=1) @lru_cache(maxsize=1)
def installed_anipy_cli_version(): def installed_anikoto_cli_version():
try: try:
completed = subprocess.run( completed = subprocess.run(
[ANIPY_CLI, "--version"], [ANIKOTO_CLI, "--version"],
check=True,
capture_output=True,
text=True,
timeout=6,
)
except (OSError, subprocess.SubprocessError):
return "Unavailable"
version = str(completed.stdout or completed.stderr or "").strip()
return version or "Unavailable"
@lru_cache(maxsize=1)
def installed_animdl_version():
try:
completed = subprocess.run(
[ANIMDL, "--version"],
check=True, check=True,
capture_output=True, capture_output=True,
text=True, text=True,
@@ -488,7 +381,6 @@ class Handler(BaseHTTPRequestHandler):
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{APP_NAME} sign in</title> <title>{APP_NAME} sign in</title>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png">
<style> <style>
:root {{ :root {{
color-scheme: dark; color-scheme: dark;
@@ -652,11 +544,6 @@ class Handler(BaseHTTPRequestHandler):
self.ensure_client_access() self.ensure_client_access()
if parsed.path == "/": if parsed.path == "/":
self.html(Handler._context(self).index_html) self.html(Handler._context(self).index_html)
elif parsed.path in {"/favicon.png", "/favicon.ico"}:
if not FAVICON_PATH.exists():
self.error(HTTPStatus.NOT_FOUND, "Favicon not found")
return
self.file(FAVICON_PATH)
elif parsed.path == "/queue": elif parsed.path == "/queue":
self.html(Handler._context(self).queue_html) self.html(Handler._context(self).queue_html)
elif parsed.path == "/config": elif parsed.path == "/config":
@@ -671,8 +558,7 @@ class Handler(BaseHTTPRequestHandler):
"name": APP_NAME, "name": APP_NAME,
"version": VERSION, "version": VERSION,
"ani_cli_version": installed_ani_cli_version(), "ani_cli_version": installed_ani_cli_version(),
"anipy_cli_version": installed_anipy_cli_version(), "anikoto_cli_version": installed_anikoto_cli_version(),
"animdl_version": installed_animdl_version(),
} }
) )
elif parsed.path == "/api/changelog": elif parsed.path == "/api/changelog":
@@ -723,7 +609,7 @@ class Handler(BaseHTTPRequestHandler):
params = parse_qs(parsed.query) params = parse_qs(parsed.query)
page = (params.get("page") or ["1"])[0] page = (params.get("page") or ["1"])[0]
per_page = (params.get("per_page") or ["10"])[0] per_page = (params.get("per_page") or ["10"])[0]
self.json(queue_list_payload(runtime, page=page, per_page=per_page)) self.json(runtime["download_queue"].list(page=page, per_page=per_page))
elif parsed.path.startswith("/api/queue/"): elif parsed.path.startswith("/api/queue/"):
runtime = Handler._runtime(self) runtime = Handler._runtime(self)
parts = parsed.path.strip("/").split("/") parts = parsed.path.strip("/").split("/")
@@ -731,7 +617,7 @@ class Handler(BaseHTTPRequestHandler):
self.error(HTTPStatus.NOT_FOUND, "Not found") self.error(HTTPStatus.NOT_FOUND, "Not found")
return return
_, _, job_id = parts _, _, job_id = parts
self.json(queue_get_job(runtime, job_id)) self.json(runtime["download_queue"].get(job_id))
elif parsed.path.startswith("/api/watchlist/thumb/"): elif parsed.path.startswith("/api/watchlist/thumb/"):
runtime = Handler._runtime(self) runtime = Handler._runtime(self)
show_id = unquote(parsed.path[len("/api/watchlist/thumb/") :]).strip() show_id = unquote(parsed.path[len("/api/watchlist/thumb/") :]).strip()
@@ -760,12 +646,6 @@ class Handler(BaseHTTPRequestHandler):
elif parsed.path == "/api/config/jellyfin/sync-status": elif parsed.path == "/api/config/jellyfin/sync-status":
Handler._runtime(self) Handler._runtime(self)
self.json(Handler._context(self).get_jellyfin_sync_status()) self.json(Handler._context(self).get_jellyfin_sync_status())
elif parsed.path == "/api/config/watchlist/export":
Handler._runtime(self)
self.json_attachment(
Handler._context(self).export_watchlist_backup(),
"ani-cli-web-watchlist-backup.json",
)
else: else:
self.error(HTTPStatus.NOT_FOUND, "Not found") self.error(HTTPStatus.NOT_FOUND, "Not found")
except Exception as exc: except Exception as exc:
@@ -807,17 +687,6 @@ class Handler(BaseHTTPRequestHandler):
if str(merged.get(key) or "").strip(): if str(merged.get(key) or "").strip():
Handler.validate_remote_path(self, merged[key], label) Handler.validate_remote_path(self, merged[key], label)
self.json(Handler._context(self).start_jellyfin_sync(merged), HTTPStatus.ACCEPTED) self.json(Handler._context(self).start_jellyfin_sync(merged), HTTPStatus.ACCEPTED)
elif parsed.path == "/api/config/watchlist/reconcile-downloads":
payload = Handler.require_json_object(self, self.body_json())
current = Handler._context(self).get_config_snapshot()
merged = dict(current)
merged.update(payload)
if str(merged.get("download_dir") or "").strip():
Handler.validate_remote_path(self, merged["download_dir"], "download directory")
self.json(Handler._context(self).reconcile_watchlist_downloaded_files(merged))
elif parsed.path == "/api/config/watchlist/import":
payload = Handler.require_json_object(self, self.body_json())
self.json(Handler._context(self).import_watchlist_backup(payload))
elif parsed.path == "/api/config/webhook/test": elif parsed.path == "/api/config/webhook/test":
payload = Handler.require_json_object(self, self.body_json()) payload = Handler.require_json_object(self, self.body_json())
validate_discord_webhook_url(payload.get("discord_webhook_url")) validate_discord_webhook_url(payload.get("discord_webhook_url"))
@@ -833,10 +702,7 @@ class Handler(BaseHTTPRequestHandler):
self.json(runtime["download_queue"].retry_all_failed()) self.json(runtime["download_queue"].retry_all_failed())
elif parsed.path == "/api/queue/clear-finished": elif parsed.path == "/api/queue/clear-finished":
runtime = Handler._runtime(self) runtime = Handler._runtime(self)
self.json(queue_clear_finished(runtime)) self.json(runtime["download_queue"].clear_finished())
elif parsed.path == "/api/queue/clear-failed":
runtime = Handler._runtime(self)
self.json(queue_clear_failed(runtime))
elif parsed.path.startswith("/api/queue/"): elif parsed.path.startswith("/api/queue/"):
runtime = Handler._runtime(self) runtime = Handler._runtime(self)
parts = parsed.path.strip("/").split("/") parts = parsed.path.strip("/").split("/")
@@ -847,7 +713,7 @@ class Handler(BaseHTTPRequestHandler):
actions = { actions = {
"retry": runtime["download_queue"].retry, "retry": runtime["download_queue"].retry,
"cancel": runtime["download_queue"].cancel, "cancel": runtime["download_queue"].cancel,
"remove": lambda job_id: queue_remove_job(runtime, job_id), "remove": runtime["download_queue"].remove,
} }
if action not in actions: if action not in actions:
self.error(HTTPStatus.NOT_FOUND, "Not found") self.error(HTTPStatus.NOT_FOUND, "Not found")
@@ -1036,19 +902,6 @@ class Handler(BaseHTTPRequestHandler):
data = json.dumps(payload).encode("utf-8") data = json.dumps(payload).encode("utf-8")
self.write_response_bytes(data, status, {"Content-Type": "application/json"}) self.write_response_bytes(data, status, {"Content-Type": "application/json"})
def json_attachment(self, payload, filename, status=HTTPStatus.OK):
data = json.dumps(payload, indent=2, sort_keys=True).encode("utf-8")
safe_filename = str(filename or "watchlist-backup.json").replace('"', "")
self.write_response_bytes(
data,
status,
{
"Content-Type": "application/json",
"Content-Disposition": f'attachment; filename="{safe_filename}"',
"Cache-Control": "no-store",
},
)
def write_response_bytes(self, payload, status, headers): def write_response_bytes(self, payload, status, headers):
try: try:
self.send_response(status) self.send_response(status)
+110 -317
View File
@@ -13,23 +13,18 @@ import time
import re import re
from app_support import ( from app_support import (
ANIMDL, ANIKOTO_CLI,
ANIPY_CLI,
ANI_CLI,
DOWNLOAD_METHOD_CHOICES,
MAX_LOG_LINES, MAX_LOG_LINES,
PROJECT_ROOT, PROJECT_ROOT,
QUEUE_PATH, QUEUE_PATH,
STATE_DB_PATH, STATE_DB_PATH,
WORKER_SHUTDOWN_TIMEOUT_SECONDS, WORKER_SHUTDOWN_TIMEOUT_SECONDS,
build_job, build_job,
cli_executable_exists,
command_for_job, command_for_job,
debug_log, debug_log,
finalize_library_files, finalize_library_files,
job_output_dir, job_output_dir,
job_staging_dir, job_staging_dir,
job_stdout_enabled,
load_json, load_json,
normalize_config, normalize_config,
now_iso, now_iso,
@@ -41,11 +36,6 @@ from app_support import (
LOG_PERSIST_INTERVAL_SECONDS = 0.75 LOG_PERSIST_INTERVAL_SECONDS = 0.75
LOG_PERSIST_LINE_BATCH = 8 LOG_PERSIST_LINE_BATCH = 8
DOWNLOAD_BACKEND_COMMANDS = {
"ani-cli": ANI_CLI,
"anipy-cli": ANIPY_CLI,
"animdl": ANIMDL,
}
JOB_STRUCTURED_COLUMNS = ( JOB_STRUCTURED_COLUMNS = (
"show_id", "show_id",
"title", "title",
@@ -656,74 +646,6 @@ class JellyfinSyncJobs:
def _copy_job(self, job): def _copy_job(self, job):
return json.loads(json.dumps(job)) return json.loads(json.dumps(job))
def _queue_job_payload(self, job):
config = job.get("config") or {}
target_paths = [
str(config.get(key) or "").strip()
for key in ("jellyfin_tv_dir", "jellyfin_movie_dir")
if str(config.get(key) or "").strip()
]
log_lines = [str(job.get("message") or "")]
if job.get("current_file"):
log_lines.append(f"Moving: {job['current_file']}")
for item in job.get("items") or []:
title = str(item.get("title") or item.get("show_id") or "Unknown title")
outcome = "moved" if item.get("moved") else str(item.get("reason") or "skipped")
target = str(item.get("target") or "").strip()
suffix = f" -> {target}" if target else ""
log_lines.append(f"{title}: {outcome}{suffix}")
payload = self._copy_job(job)
payload.update(
{
"job_type": "jellyfin_handoff",
"title": "Jellyfin handoff",
"anime_name": "Jellyfin handoff",
"mode": "copy",
"quality": "library",
"episodes": f"{int(job.get('completed') or 0)}/{int(job.get('total') or 0)} entries",
"download_dir": str(config.get("download_dir") or ""),
"target_dir": ", ".join(target_paths),
"exit_code": 1 if job.get("status") == "failed" else None,
"log": [line for line in log_lines if line],
}
)
return payload
def list_queue_jobs(self, limit=50):
limit = min(200, max(1, int(limit or 50)))
with self.lock, self._connect() as conn:
rows = conn.execute(
"SELECT payload FROM jellyfin_sync_jobs ORDER BY created_at DESC LIMIT ?",
(limit,),
).fetchall()
return [self._queue_job_payload(self._job_from_row(row)) for row in rows]
def get_queue_job(self, job_id):
with self.lock, self._connect() as conn:
row = conn.execute("SELECT payload FROM jellyfin_sync_jobs WHERE id = ?", (job_id,)).fetchone()
if row:
return self._queue_job_payload(self._job_from_row(row))
raise KeyError("Job not found")
def remove_queue_job(self, job_id):
with self.lock:
job = self.get_queue_job(job_id)
if job["status"] in {"pending", "running"}:
raise ValueError("Running Jellyfin handoff jobs cannot be removed")
with self._connect() as conn:
conn.execute("DELETE FROM jellyfin_sync_jobs WHERE id = ?", (job_id,))
return {"ok": True}
def clear_finished_queue_jobs(self):
with self.lock, self._connect() as conn:
cursor = conn.execute("DELETE FROM jellyfin_sync_jobs WHERE status IN ('done', 'interrupted')")
return {"ok": True, "count": cursor.rowcount}
def clear_failed_queue_jobs(self):
with self.lock, self._connect() as conn:
cursor = conn.execute("DELETE FROM jellyfin_sync_jobs WHERE status = 'failed'")
return {"ok": True, "count": cursor.rowcount}
def _find_locked(self, job_id): def _find_locked(self, job_id):
for job in self.jobs: for job in self.jobs:
if job["id"] == job_id: if job["id"] == job_id:
@@ -1213,66 +1135,14 @@ class DownloadQueue:
with self.lock: with self.lock:
return self._find(job_id) return self._find(job_id)
def _job_identity_fields(self, job):
return (
str(job.get("show_id") or "").strip(),
str(job.get("title") or "").strip(),
str(job.get("anime_name") or "").strip(),
str(job.get("media_type") or "").strip(),
str(job.get("season") or "").strip(),
str(job.get("episode_offset") or "").strip(),
str(job.get("query") or "").strip(),
job.get("result_index"),
str(job.get("mode") or "").strip().lower(),
str(job.get("quality") or "").strip().lower(),
str(job.get("episodes") or "").strip(),
str(job.get("download_dir") or "").strip(),
)
def _find_reusable_failed_job_locked(self, job):
expected = self._job_identity_fields(job)
with self._connect() as conn:
rows = conn.execute(
"""
SELECT *
FROM jobs
WHERE status = 'failed'
ORDER BY created_at DESC
"""
).fetchall()
for row in rows:
candidate = self._job_from_row(row)
if self._job_identity_fields(candidate) == expected:
return candidate
return None
def _reset_retryable_job_locked(self, job):
job["status"] = "pending"
job["exit_code"] = None
job["pid"] = None
job["started_at"] = None
job["finished_at"] = None
job["updated_at"] = now_iso()
job["cancel_requested"] = False
job["log"] = []
self._save_job_locked(job)
return job
def _prepare_job(self, job):
if self.job_prepare_fn is None:
return job
prepared = self.job_prepare_fn(dict(job))
return prepared if prepared is not None else job
def add(self, payload): def add(self, payload):
job = build_job(payload, self.config_getter()) job = build_job(payload, self.config_getter())
job = self._prepare_job(job) if self.job_prepare_fn is not None:
prepared = self.job_prepare_fn(dict(job))
if prepared is not None:
job = prepared
with self.lock: with self.lock:
reusable = self._find_reusable_failed_job_locked(job) self._save_job_locked(job)
if reusable is not None:
job = self._reset_retryable_job_locked(reusable)
else:
self._save_job_locked(job)
self.wakeup.set() self.wakeup.set()
return job return job
@@ -1350,21 +1220,32 @@ class DownloadQueue:
raise ValueError("Running jobs cannot be retried") raise ValueError("Running jobs cannot be retried")
if job["status"] not in {"failed", "canceled"}: if job["status"] not in {"failed", "canceled"}:
raise ValueError("Only failed or canceled jobs can be retried") raise ValueError("Only failed or canceled jobs can be retried")
job = self._prepare_job(job) job["status"] = "pending"
with self.lock: job["exit_code"] = None
job = self._reset_retryable_job_locked(job) job["pid"] = None
job["started_at"] = None
job["finished_at"] = None
job["updated_at"] = now_iso()
job["cancel_requested"] = False
job["log"] = []
self._save_job_locked(job)
self.wakeup.set() self.wakeup.set()
return job return job
def retry_all_failed(self): def retry_all_failed(self):
count = 0 count = 0
with self.lock: with self.lock, self._connect() as conn:
with self._connect() as conn: rows = conn.execute("SELECT * FROM jobs WHERE status = 'failed'").fetchall()
rows = conn.execute("SELECT * FROM jobs WHERE status = 'failed'").fetchall() for row in rows:
for row in rows: job = self._job_from_row(row)
job = self._prepare_job(self._job_from_row(row)) job["status"] = "pending"
with self.lock: job["exit_code"] = None
self._reset_retryable_job_locked(job) job["pid"] = None
job["started_at"] = None
job["finished_at"] = None
job["updated_at"] = now_iso()
job["log"] = []
self._upsert_job_conn(conn, job)
count += 1 count += 1
if count: if count:
self.wakeup.set() self.wakeup.set()
@@ -1409,11 +1290,6 @@ class DownloadQueue:
cursor = conn.execute("DELETE FROM jobs WHERE status IN ('done', 'canceled')") cursor = conn.execute("DELETE FROM jobs WHERE status IN ('done', 'canceled')")
return {"ok": True, "count": cursor.rowcount} return {"ok": True, "count": cursor.rowcount}
def clear_failed(self):
with self.lock, self._connect() as conn:
cursor = conn.execute("DELETE FROM jobs WHERE status = 'failed'")
return {"ok": True, "count": cursor.rowcount}
def _find(self, job_id): def _find(self, job_id):
with self._connect() as conn: with self._connect() as conn:
row = conn.execute("SELECT * FROM jobs WHERE id = ?", (job_id,)).fetchone() row = conn.execute("SELECT * FROM jobs WHERE id = ?", (job_id,)).fetchone()
@@ -1435,7 +1311,6 @@ class DownloadQueue:
text = strip_control(line).strip() text = strip_control(line).strip()
if not text: if not text:
return return
self._stdout_log(job, text)
with self.lock: with self.lock:
job.setdefault("log", []).append(text) job.setdefault("log", []).append(text)
job["log"] = job["log"][-MAX_LOG_LINES:] job["log"] = job["log"][-MAX_LOG_LINES:]
@@ -1449,16 +1324,6 @@ class DownloadQueue:
if should_flush: if should_flush:
self._save_job_locked(job) self._save_job_locked(job)
def _stdout_log(self, job, line):
if not job_stdout_enabled():
return
text = strip_control(str(line)).strip()
if not text:
return
job_id = str((job or {}).get("id") or "-")
backend = str((job or {}).get("download_backend") or "download")
print(f"[download:{job_id}:{backend}] {text}", flush=True)
def _cleanup_staging_dir(self, job): def _cleanup_staging_dir(self, job):
staging_dir = job_staging_dir(job) staging_dir = job_staging_dir(job)
if not staging_dir.exists(): if not staging_dir.exists():
@@ -1471,6 +1336,16 @@ class DownloadQueue:
debug_log("queue.cleanup.staging_failed", job_id=job.get("id"), staging_dir=staging_dir, error=exc) debug_log("queue.cleanup.staging_failed", job_id=job.get("id"), staging_dir=staging_dir, error=exc)
return False return False
def _command_available(self, command_name):
return bool(shutil.which(command_name) or (os.path.exists(command_name) and os.access(command_name, os.X_OK)))
def _download_attempts(self):
config = normalize_config(self.config_getter() or {})
attempts = [("ani-cli", command_for_job)]
if config.get("download_backup_enabled"):
attempts.append(("anikoto-cli", lambda job: command_for_job(job, downloader="anikoto-cli")))
return attempts
def _fail_job_startup(self, job, exc): def _fail_job_startup(self, job, exc):
message = f"Could not start download: {exc}" message = f"Could not start download: {exc}"
with self.lock: with self.lock:
@@ -1509,49 +1384,6 @@ class DownloadQueue:
self._save_job_locked(job) self._save_job_locked(job)
self._cleanup_staging_dir(job) self._cleanup_staging_dir(job)
def _command_attempts(self, job, 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_PLAYER": "download",
"ANI_CLI_MODE": job["mode"],
"ANI_CLI_QUALITY": job["quality"],
"TERM": os.environ.get("TERM", "xterm-256color"),
},
}
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 and selected_methods == ["ani-cli"]:
attempts.append(attempt_for("ani-cli"))
return attempts, selected_methods, unavailable_methods
def _run(self): def _run(self):
while not self.stop_event.is_set(): while not self.stop_event.is_set():
job = self._next_pending() job = self._next_pending()
@@ -1564,37 +1396,19 @@ class DownloadQueue:
def _run_job(self, job): def _run_job(self, job):
staging_dir = job_staging_dir(job) staging_dir = job_staging_dir(job)
target_dir = job_output_dir(job) target_dir = job_output_dir(job)
env = os.environ.copy()
env.update(
{
"ANI_CLI_DOWNLOAD_DIR": str(staging_dir),
"ANI_CLI_MODE": job["mode"],
"ANI_CLI_QUALITY": job["quality"],
"TERM": env.get("TERM", "xterm-256color"),
}
)
staging_dir.mkdir(parents=True, exist_ok=True) staging_dir.mkdir(parents=True, exist_ok=True)
target_dir.mkdir(parents=True, exist_ok=True) target_dir.mkdir(parents=True, exist_ok=True)
attempts, selected_methods, unavailable_methods = self._command_attempts(job, staging_dir) attempts = self._download_attempts()
if not attempts: primary_command = attempts[0][1](job)
with self.lock:
job["status"] = "failed"
job["started_at"] = now_iso()
job["finished_at"] = job["started_at"]
job["updated_at"] = job["finished_at"]
job["exit_code"] = 1
job["pid"] = None
job["staging_dir"] = str(staging_dir)
job["target_dir"] = str(target_dir)
job["download_backend"] = selected_methods[0] if selected_methods else None
job["fallback_used"] = False
job["log"] = [
f"Staging in: {job['staging_dir']}",
f"Final folder: {job['target_dir']}",
f"Download methods: {', '.join(selected_methods)}",
(
"No selected download methods are installed or executable: "
f"{', '.join(unavailable_methods or selected_methods)}."
),
]
self.current_process = None
self.current_job_id = None
self.current_job = None
self._save_job_locked(job)
self._cleanup_staging_dir(job)
return
primary_command = attempts[0]["command"]
with self.lock: with self.lock:
job["status"] = "running" job["status"] = "running"
@@ -1603,52 +1417,48 @@ class DownloadQueue:
job["command"] = printable_command(primary_command) job["command"] = printable_command(primary_command)
job["staging_dir"] = str(staging_dir) job["staging_dir"] = str(staging_dir)
job["target_dir"] = str(target_dir) job["target_dir"] = str(target_dir)
job["download_backend"] = attempts[0]["backend"]
job["fallback_used"] = False
job["log"] = [ job["log"] = [
f"Starting: {job['command']}", f"Starting: {job['command']}",
f"Staging in: {job['staging_dir']}", f"Staging in: {job['staging_dir']}",
f"Final folder: {job['target_dir']}", f"Final folder: {job['target_dir']}",
f"Download methods: {', '.join(selected_methods)}",
] ]
if len(attempts) > 1: if len(attempts) > 1:
job["log"].append("Fallback retry is enabled for the selected download methods.") job["log"].append("Backup downloads are enabled. Failed ani-cli jobs will retry with anikoto-cli.")
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["cancel_requested"] = False
job["_dirty_log_lines"] = 0 job["_dirty_log_lines"] = 0
self._save_job_locked(job) self._save_job_locked(job)
try: try:
exit_code = 1 last_exit_code = 1
successful_backend = None last_finalize_error = None
finalize_error = None
moved_files = [] moved_files = []
watchlist_sync_error = None watchlist_sync_error = None
synced_watchlist_item = None synced_watchlist_item = None
cleanup_staging = False canceled = False
for index, attempt in enumerate(attempts): download_succeeded = False
backend = attempt["backend"]
command = attempt["command"] for attempt_index, (downloader_name, command_builder) in enumerate(attempts):
should_fallback = index + 1 < len(attempts) command = command_builder(job)
if attempt_index > 0:
self._cleanup_staging_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
self._append_log(job, f"Retrying with backup downloader: {downloader_name}.")
self._append_log(job, "anikoto-cli does not support ani-cli quality flags, so the saved quality preference is ignored for the backup attempt.")
if downloader_name == "anikoto-cli" and not self._command_available(ANIKOTO_CLI):
self._append_log(job, "Backup downloader anikoto-cli is not installed or not executable.")
continue
with self.lock: with self.lock:
job["download_backend"] = backend
job["command"] = printable_command(command) job["command"] = printable_command(command)
if index > 0:
job["fallback_used"] = True
job["pid"] = None
job["updated_at"] = now_iso() job["updated_at"] = now_iso()
if index > 0:
job.setdefault("log", []).append(f"Retrying with {backend}: {job['command']}")
self._save_job_locked(job) self._save_job_locked(job)
self._append_log(job, f"Launching {downloader_name}: {job['command']}")
try: try:
process = subprocess.Popen( process = subprocess.Popen(
command, command,
cwd=str(PROJECT_ROOT), cwd=str(PROJECT_ROOT),
env=attempt["env"], env=env,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
text=True, text=True,
@@ -1656,14 +1466,10 @@ class DownloadQueue:
start_new_session=True, start_new_session=True,
) )
except Exception as exc: except Exception as exc:
if should_fallback: last_exit_code = 1
self._append_log(job, f"{backend} could not start: {exc}") last_finalize_error = None
self._append_log(job, f"Retrying with {attempts[index + 1]['backend']} fallback.") self._append_log(job, f"Could not start {downloader_name}: {exc}")
self._cleanup_staging_dir(job) continue
staging_dir.mkdir(parents=True, exist_ok=True)
continue
self._fail_job_startup(job, exc)
return
with self.lock: with self.lock:
self.current_process = process self.current_process = process
self.current_job_id = job["id"] self.current_job_id = job["id"]
@@ -1681,65 +1487,53 @@ class DownloadQueue:
self.current_job_id = None self.current_job_id = None
self.current_job = None self.current_job = None
self._save_job_locked(job) self._save_job_locked(job)
canceled = bool(job.get("cancel_requested"))
if job.get("cancel_requested"): if canceled:
last_exit_code = exit_code
break break
if exit_code != 0:
if exit_code != 0 and should_fallback: last_exit_code = exit_code
self._append_log(job, f"{backend} failed with exit code {exit_code}.") last_finalize_error = None
self._append_log(job, f"Retrying with {attempts[index + 1]['backend']} fallback.") self._append_log(job, f"{downloader_name} failed with exit code {exit_code}.")
self._cleanup_staging_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
continue continue
if exit_code == 0: try:
try: moved_files = finalize_library_files(job)
moved_files = finalize_library_files(job) except Exception as exc:
except Exception as exc: last_exit_code = 1
finalize_error = exc last_finalize_error = exc
no_files_downloaded = "No downloaded files were found in the staging folder" in str(exc) message = str(exc).strip()
if no_files_downloaded and should_fallback: if "No downloaded files were found in the staging folder" in message:
self._append_log( self._append_log(
job, job,
f"{backend} exited without downloading any files. Retrying with {attempts[index + 1]['backend']} fallback.", f"{downloader_name} exited without downloading any files. The selected result or episodes may not be downloadable.",
) )
self._cleanup_staging_dir(job)
staging_dir.mkdir(parents=True, exist_ok=True)
finalize_error = None
continue
else: else:
successful_backend = backend self._append_log(job, f"Library rename failed after {downloader_name}: {exc}")
if self.watchlist_sync_fn is not None: continue
try:
synced_watchlist_item = self.watchlist_sync_fn(job) download_succeeded = True
except Exception as exc: last_exit_code = 0
watchlist_sync_error = exc last_finalize_error = None
break if self.watchlist_sync_fn is not None:
try:
synced_watchlist_item = self.watchlist_sync_fn(job)
except Exception as exc:
watchlist_sync_error = exc
break break
with self.lock: with self.lock:
canceled = job.get("cancel_requested") job["exit_code"] = last_exit_code
job["exit_code"] = 1 if finalize_error else exit_code
job["pid"] = None job["pid"] = None
job["finished_at"] = now_iso() job["finished_at"] = now_iso()
job["updated_at"] = job["finished_at"] job["updated_at"] = job["finished_at"]
cleanup_staging = False
if canceled: if canceled:
job["status"] = "canceled" job["status"] = "canceled"
job.setdefault("log", []).append("Canceled.") job.setdefault("log", []).append("Canceled.")
elif finalize_error:
job["status"] = "failed"
message = str(finalize_error).strip()
if "No downloaded files were found in the staging folder" in message:
job.setdefault("log", []).append(
f"{job.get('download_backend') or 'Downloader'} exited without downloading any files. The selected result or episodes may not be downloadable."
)
else:
job.setdefault("log", []).append(f"Library rename failed: {finalize_error}")
cleanup_staging = True cleanup_staging = True
elif exit_code == 0: elif download_succeeded:
job["status"] = "done" job["status"] = "done"
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: for path in moved_files:
job.setdefault("log", []).append(f"Saved: {path}") job.setdefault("log", []).append(f"Saved: {path}")
job.setdefault("log", []).append("Download completed.") job.setdefault("log", []).append("Download completed.")
@@ -1752,11 +1546,10 @@ class DownloadQueue:
job.setdefault("log", []).append("Watchlist download state synced.") job.setdefault("log", []).append("Watchlist download state synced.")
else: else:
job["status"] = "failed" job["status"] = "failed"
job.setdefault("log", []).append( if last_finalize_error and "No downloaded files were found in the staging folder" not in str(last_finalize_error):
f"{job.get('download_backend') or 'Download'} failed with exit code {exit_code}." job.setdefault("log", []).append(f"Download failed after all downloaders were tried: {last_finalize_error}")
) else:
cleanup_staging = True job.setdefault("log", []).append("Download failed after all configured downloaders were tried.")
if canceled:
cleanup_staging = True cleanup_staging = True
self.current_process = None self.current_process = None
self.current_job_id = None self.current_job_id = None
@@ -1764,7 +1557,7 @@ class DownloadQueue:
self._save_job_locked(job) self._save_job_locked(job)
if cleanup_staging: if cleanup_staging:
self._cleanup_staging_dir(job) self._cleanup_staging_dir(job)
elif exit_code == 0 and not finalize_error and not canceled: elif download_succeeded:
download_title = ( download_title = (
str((synced_watchlist_item or {}).get("title") or "").strip() str((synced_watchlist_item or {}).get("title") or "").strip()
or str(job.get("title") or "").strip() or str(job.get("title") or "").strip()
@@ -1788,4 +1581,4 @@ class DownloadQueue:
except Exception as exc: except Exception as exc:
debug_log("discord_webhook.download_completed_failed", title=download_title, error=exc) debug_log("discord_webhook.download_completed_failed", title=download_title, error=exc)
except Exception as exc: except Exception as exc:
self._fail_job_runtime(job, exc, process=process) self._fail_job_runtime(job, exc)
+2 -18
View File
@@ -11,7 +11,6 @@ QUEUE_HTML = r"""<!doctype html>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>ani-cli web queue</title> <title>ani-cli web queue</title>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png">
<style> <style>
:root { :root {
color-scheme: dark; color-scheme: dark;
@@ -318,7 +317,6 @@ QUEUE_HTML = r"""<!doctype html>
<p class="muted">Monitor active downloads, inspect recent output, and clean up finished jobs without leaving the queue view.</p> <p class="muted">Monitor active downloads, inspect recent output, and clean up finished jobs without leaving the queue view.</p>
<div class="row"> <div class="row">
<button class="ghost" id="retryFailedBtn" type="button">Retry failed</button> <button class="ghost" id="retryFailedBtn" type="button">Retry failed</button>
<button class="ghost" id="clearFailedBtn" type="button">Clear failed</button>
<button class="ghost" id="removeFinishedBtn" type="button">Remove finished</button> <button class="ghost" id="removeFinishedBtn" type="button">Remove finished</button>
</div> </div>
</section> </section>
@@ -390,13 +388,6 @@ QUEUE_HTML = r"""<!doctype html>
} }
function buildJobSummary(job) { function buildJobSummary(job) {
if (job.job_type === "jellyfin_handoff") {
const progress = `${job.completed || 0}/${job.total || 0} entries`;
const moved = `${job.moved || 0} moved`;
const files = `${job.moved_files || 0} files`;
const target = job.target_dir || "Jellyfin libraries";
return `${progress} · ${moved} · ${files} · ${target}`;
}
const libraryName = job.anime_name || job.title; const libraryName = job.anime_name || job.title;
const season = String(job.season || "1").padStart(2, "0"); const season = String(job.season || "1").padStart(2, "0");
const seasonFolder = `Season ${season}`; const seasonFolder = `Season ${season}`;
@@ -410,12 +401,6 @@ QUEUE_HTML = r"""<!doctype html>
function renderJobActions(job, actions) { function renderJobActions(job, actions) {
actions.innerHTML = ""; actions.innerHTML = "";
if (job.job_type === "jellyfin_handoff") {
if (job.status !== "running" && job.status !== "pending") {
actions.append(actionButton("Remove", () => queueAction(job.id, "remove")));
}
return;
}
if (job.status === "running" || job.status === "pending") { if (job.status === "running" || job.status === "pending") {
actions.append(actionButton("Cancel", () => queueAction(job.id, "cancel"), "danger")); actions.append(actionButton("Cancel", () => queueAction(job.id, "cancel"), "danger"));
return; return;
@@ -429,14 +414,14 @@ QUEUE_HTML = r"""<!doctype html>
function updateJobCard(item, job) { function updateJobCard(item, job) {
item.dataset.jobId = job.id; item.dataset.jobId = job.id;
item.dataset.jobStatus = job.status; item.dataset.jobStatus = job.status;
item.querySelector(".job-title").textContent = job.title || "Untitled job"; item.querySelector(".job-title").textContent = job.title;
item.querySelector(".job-head .muted").textContent = buildJobSummary(job); item.querySelector(".job-head .muted").textContent = buildJobSummary(job);
const status = item.querySelector(".status"); const status = item.querySelector(".status");
status.textContent = job.status; status.textContent = job.status;
status.className = "status"; status.className = "status";
status.classList.add(job.status); status.classList.add(job.status);
item.querySelector("pre").textContent = buildJobLog(job) || "Waiting..."; item.querySelector("pre").textContent = buildJobLog(job) || "Waiting...";
item.querySelector(".toolbar .muted").textContent = job.exit_code === null || job.exit_code === undefined ? "" : `exit ${job.exit_code}`; item.querySelector(".toolbar .muted").textContent = job.exit_code === null ? "" : `exit ${job.exit_code}`;
renderJobActions(job, item.querySelector(".row")); renderJobActions(job, item.querySelector(".row"));
state.jobCards[job.id] = item; state.jobCards[job.id] = item;
} }
@@ -563,7 +548,6 @@ QUEUE_HTML = r"""<!doctype html>
} }
$("removeFinishedBtn").addEventListener("click", () => queueBulkAction("/api/queue/clear-finished", "Removed finished and canceled jobs")); $("removeFinishedBtn").addEventListener("click", () => queueBulkAction("/api/queue/clear-finished", "Removed finished and canceled jobs"));
$("clearFailedBtn").addEventListener("click", () => queueBulkAction("/api/queue/clear-failed", "Removed failed jobs"));
$("retryFailedBtn").addEventListener("click", () => queueBulkAction("/api/queue/retry-failed", "Retried failed jobs")); $("retryFailedBtn").addEventListener("click", () => queueBulkAction("/api/queue/retry-failed", "Retried failed jobs"));
(async function init() { (async function init() {
-2
View File
@@ -11,7 +11,6 @@ INDEX_HTML = r"""<!doctype html>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>ani-cli web</title> <title>ani-cli web</title>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png">
<style> <style>
:root { :root {
color-scheme: dark; color-scheme: dark;
@@ -688,7 +687,6 @@ INDEX_HTML = r"""<!doctype html>
const payload = { const payload = {
show_id: state.selected.id, show_id: state.selected.id,
query: state.selected.title, query: state.selected.title,
result_index: state.selected.index,
title: state.selected.title, title: state.selected.title,
anime_name: $("animeNameInput").value || state.selected.title, anime_name: $("animeNameInput").value || state.selected.title,
media_type: $("trackMediaType").value, media_type: $("trackMediaType").value,
+59 -859
View File
File diff suppressed because it is too large Load Diff
-1
View File
@@ -11,7 +11,6 @@ WATCHLIST_HTML = r"""<!doctype html>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>ani-cli web - watchlist</title> <title>ani-cli web - watchlist</title>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon.png">
<style> <style>
:root { :root {
color-scheme: dark; color-scheme: dark;