Compare commits
31
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
536949c18d | ||
|
|
337afac91e | ||
|
|
c0c77590b1 | ||
|
|
441b908937 | ||
|
|
949d3cb6f4 | ||
|
|
3ce8d62e70 | ||
|
|
38737ab84e | ||
|
|
4437519430 | ||
|
|
3b29e173c2 | ||
|
|
9706f5d2de | ||
|
|
206dc3baf1 | ||
|
|
f5a3badb56 | ||
|
|
3e4b4f24f7 | ||
|
|
86f5903e74 | ||
|
|
bd4ee78811 | ||
|
|
b7b39cdc92 | ||
|
|
09532e5216 | ||
|
|
e1786e7cf2 | ||
|
|
409a8d8b0c | ||
|
|
e83162a2ac | ||
|
|
5cd9a0c4c2 | ||
|
|
a803aa046a | ||
|
|
73f77a13c3 | ||
|
|
dd76fcf5d4 | ||
|
|
1cb28095a7 | ||
|
|
e1760e3316 | ||
|
|
272e06daf6 | ||
|
|
4db985b90d | ||
|
|
8073d0aa9d | ||
|
|
81e0995f7d | ||
|
|
608b0058e3 |
No files matched your search
@@ -807,6 +807,8 @@ per-attempt page and no per-swap ledger anywhere else.
|
|||||||
| `frank-tts` — cloned-voice speech, Dankclaw | `serverAppConfigs-docs/Dankclaw/Frank TTS.md` |
|
| `frank-tts` — cloned-voice speech, Dankclaw | `serverAppConfigs-docs/Dankclaw/Frank TTS.md` |
|
||||||
| Dankclaw kernel posture (`iommu=pt` since 2026-10-18) | `serverAppConfigs-docs/LLM/dankclaw-iommu-translation-off.md` |
|
| Dankclaw kernel posture (`iommu=pt` since 2026-10-18) | `serverAppConfigs-docs/LLM/dankclaw-iommu-translation-off.md` |
|
||||||
| Deployment flow | `serverAppConfigs-docs/Architecture/Deployment Flow.md` |
|
| Deployment flow | `serverAppConfigs-docs/Architecture/Deployment Flow.md` |
|
||||||
|
| Movie-library relayout (Jellyfin format, Dankserver) | `serverAppConfigs-docs/Dankserver/Media Relayout.md` |
|
||||||
|
| SSD-backed metadata view (Jellyfin writes, Plex reads, Dankserver) | `serverAppConfigs-docs/Dankserver/Media Metadata View.md` |
|
||||||
|
|
||||||
There is no page for a retired endpoint and no change diary. A payload swap
|
There is no page for a retired endpoint and no change diary. A payload swap
|
||||||
appends one row to the live endpoint's **Eras** table; when the endpoint itself
|
appends one row to the live endpoint's **Eras** table; when the endpoint itself
|
||||||
@@ -848,5 +850,7 @@ history.
|
|||||||
| Writing a doc | **One owner per fact** — edit the endpoint/service page; everywhere else links. Never restate a number. |
|
| Writing a doc | **One owner per fact** — edit the endpoint/service page; everywhere else links. Never restate a number. |
|
||||||
| Benchmarking (only when ordered) | **Stop and mask the service first**, bench, unmask, restart, verify health. |
|
| Benchmarking (only when ordered) | **Stop and mask the service first**, bench, unmask, restart, verify health. |
|
||||||
| Deleting a file | **Move it to trash** — `.AGENTTRASH/` (in-repo) or `~/.AGENTTRASH/YYYYMMDD-topic`. Never `rm`. |
|
| Deleting a file | **Move it to trash** — `.AGENTTRASH/` (in-repo) or `~/.AGENTTRASH/YYYYMMDD-topic`. Never `rm`. |
|
||||||
|
| Relayout the movie library | **Census → plan → read the exceptions → `scripts/media-apply.py`** — renames only, journal before every move; never hand-rename, never copy. |
|
||||||
|
| Making media metadata visible to both media servers | **Bind `/var/lib/media-meta/view`** — the overlay `scripts/media-view.sh` installs (array below, NVMe above). Never write the array; never delete through the view. |
|
||||||
| Tests exist | **Run them** — verify tests pass after every change. Do not commit failing tests. |
|
| Tests exist | **Run them** — verify tests pass after every change. Do not commit failing tests. |
|
||||||
| Real secrets | **Never commit** — host `.env` files; `.env.example` holds placeholders only. |
|
| Real secrets | **Never commit** — host `.env` files; `.env.example` holds placeholders only. |
|
||||||
+6
-28
@@ -94,34 +94,12 @@ services:
|
|||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|
||||||
# ═══════════════════════════════════════════════
|
# Jellyfin left this file when it became a Quadlet: the container is owned by
|
||||||
# Jellyfin — media server (movies, TV, music)
|
# dankserver/jellyfin/quadlet/jellyfin.container, its data roots are under
|
||||||
# ═══════════════════════════════════════════════
|
# /home/dank/Containers/jellyfin, and its library arrives through the SSD
|
||||||
jellyfin:
|
# overlay at /var/lib/media-meta/view (scripts/media-view.sh). The block that
|
||||||
image: jellyfin/jellyfin:latest
|
# was here named /opt/jellyfin/* and a read-only array bind — a second owner for
|
||||||
container_name: jellyfin
|
# the container name `jellyfin`, wrong on every path. One owner per container.
|
||||||
restart: unless-stopped
|
|
||||||
ports:
|
|
||||||
- "8096:8096/tcp"
|
|
||||||
- "8920:8920/tcp"
|
|
||||||
- "7359:7359/udp"
|
|
||||||
- "1900:1900/udp"
|
|
||||||
environment:
|
|
||||||
- JELLYFIN_PublishedServerUrl=http://192.168.0.235:8096
|
|
||||||
- DOTNET_SYSTEM_IO_DISABLEFILELOCKING=true
|
|
||||||
volumes:
|
|
||||||
- /opt/jellyfin/config:/config
|
|
||||||
- /opt/jellyfin/cache:/cache
|
|
||||||
- /mnt/entropy:/data:ro
|
|
||||||
devices:
|
|
||||||
- /dev/dri:/dev/dri
|
|
||||||
group_add:
|
|
||||||
- "992"
|
|
||||||
healthcheck:
|
|
||||||
test: ["CMD", "curl", "-f", "http://localhost:8096/health"]
|
|
||||||
interval: 30s
|
|
||||||
timeout: 10s
|
|
||||||
retries: 3
|
|
||||||
|
|
||||||
# ═══════════════════════════════════════════════
|
# ═══════════════════════════════════════════════
|
||||||
# Calibre-Web — e-book web frontend
|
# Calibre-Web — e-book web frontend
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# /etc/systemd/system/var-lib-media\x2dmeta-view.mount
|
||||||
|
# (repo copy: dankserver/host/systemd/media-meta-view.mount — systemd names a
|
||||||
|
# mount unit after its ESCAPED path, so the host file carries the \x2d spelling)
|
||||||
|
#
|
||||||
|
# Installed by `scripts/media-view.sh install` — the repo copy is the source of
|
||||||
|
# truth (fleet law: never edit host state by hand).
|
||||||
|
#
|
||||||
|
# An OverlayFS merge that shows the media library as one tree while splitting
|
||||||
|
# where its bytes live: lowerdir is the RAID6 array (the source of truth, never
|
||||||
|
# written), upperdir is a directory on the NVMe that receives every write. The
|
||||||
|
# two media servers bind the result at /var/lib/media-meta/view, so Jellyfin's
|
||||||
|
# sidecars and artwork are SSD bytes — which is what makes them fast to browse
|
||||||
|
# and what keeps Plex's NFO agent off the spinners — while the media itself stays
|
||||||
|
# on the array untouched.
|
||||||
|
#
|
||||||
|
# RequiresMountsFor is load-bearing: without the array mounted, lowerdir is an
|
||||||
|
# empty directory and the view would present an empty library as truth.
|
||||||
|
|
||||||
|
[Unit]
|
||||||
|
Description=SSD-backed overlay view of the media library (array + NVMe metadata layer)
|
||||||
|
RequiresMountsFor=/mnt/entropy
|
||||||
|
|
||||||
|
[Mount]
|
||||||
|
What=overlay
|
||||||
|
Where=/var/lib/media-meta/view
|
||||||
|
Type=overlay
|
||||||
|
Options=lowerdir=/mnt/entropy,upperdir=/var/lib/media-meta/upper,workdir=/var/lib/media-meta/work
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=local-fs.target
|
||||||
@@ -4,8 +4,9 @@
|
|||||||
# Unit name: jellyfin.service (podman 5.x drops the container- prefix)
|
# Unit name: jellyfin.service (podman 5.x drops the container- prefix)
|
||||||
#
|
#
|
||||||
# Data roots migrated from /opt/jellyfin to /home/dank/Containers/jellyfin at
|
# Data roots migrated from /opt/jellyfin to /home/dank/Containers/jellyfin at
|
||||||
# cutover. All of /mnt/entropy is mounted read-only at /data: nothing automated
|
# cutover. The library is seen through /var/lib/media-meta/view, the SSD-backed
|
||||||
# anywhere writes to the entropy volume. Every
|
# overlay view (scripts/media-view.sh): the array stays read-only and untouched,
|
||||||
|
# while the sidecars this server writes land on the NVMe. Every
|
||||||
# creation-time environment variable of the compose-era container is kept as
|
# creation-time environment variable of the compose-era container is kept as
|
||||||
# an Environment= line (zero drift); none of them are secrets, so this unit
|
# an Environment= line (zero drift); none of them are secrets, so this unit
|
||||||
# needs no EnvironmentFile.
|
# needs no EnvironmentFile.
|
||||||
@@ -41,7 +42,12 @@ PublishPort=7359:7359/udp
|
|||||||
PublishPort=1900:1900/udp
|
PublishPort=1900:1900/udp
|
||||||
Volume=/home/dank/Containers/jellyfin/config:/config
|
Volume=/home/dank/Containers/jellyfin/config:/config
|
||||||
Volume=/home/dank/Containers/jellyfin/cache:/cache
|
Volume=/home/dank/Containers/jellyfin/cache:/cache
|
||||||
Volume=/mnt/entropy:/data:ro
|
# The view, writable: Jellyfin is the metadata writer (movie.nfo / tvshow.nfo /
|
||||||
|
# episode.nfo + artwork) and those bytes belong on the SSD. The condition is the
|
||||||
|
# fail-closed guard — starting against a plain, unmounted view directory would let
|
||||||
|
# a scan conclude the library is empty.
|
||||||
|
Volume=/var/lib/media-meta/view:/data
|
||||||
|
UnitConfigItem=ConditionPathIsMountPoint=/var/lib/media-meta/view
|
||||||
AddDevice=/dev/dri
|
AddDevice=/dev/dri
|
||||||
GroupAdd=keep-groups
|
GroupAdd=keep-groups
|
||||||
HealthCmd=curl -f http://localhost:8096/health
|
HealthCmd=curl -f http://localhost:8096/health
|
||||||
|
|||||||
@@ -11,7 +11,12 @@ Environment=PUID=0
|
|||||||
Environment=PGID=0
|
Environment=PGID=0
|
||||||
Environment=TZ=America/Los_Angeles
|
Environment=TZ=America/Los_Angeles
|
||||||
Volume=/var/lib/plexmediaserver:/config
|
Volume=/var/lib/plexmediaserver:/config
|
||||||
Volume=/mnt/entropy:/mnt/entropy:ro
|
# The container path stays /mnt/entropy on purpose: every path Plex recorded in
|
||||||
|
# its own database is written against it, so swapping the HOST source of this bind
|
||||||
|
# to the overlay view re-points Plex at the sidecars without touching its DB.
|
||||||
|
# Read-only: Plex consumes the NFO files Jellyfin writes and writes no metadata.
|
||||||
|
Volume=/var/lib/media-meta/view:/mnt/entropy:ro
|
||||||
|
UnitConfigItem=ConditionPathIsMountPoint=/var/lib/media-meta/view
|
||||||
PublishPort=32400:32400
|
PublishPort=32400:32400
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
|
|||||||
+16
-1
@@ -155,13 +155,14 @@ DANKCLAW_COMPOSE_SERVICES = set()
|
|||||||
# Quadlet unit now — the last compose services were the hermes trio, whose units
|
# Quadlet unit now — the last compose services were the hermes trio, whose units
|
||||||
# have owned their containers since 2026-09-12 and whose compose blocks were
|
# have owned their containers since 2026-09-12 and whose compose blocks were
|
||||||
# dead weight that could only create a duplicate container.
|
# dead weight that could only create a duplicate container.
|
||||||
|
# jellyfin left the Dankserver file on 2026-10-30 for the same reason: its Quadlet
|
||||||
|
# has owned the container since the cutover, so a compose edit must not restart it.
|
||||||
|
|
||||||
DANKSERVER_COMPOSE_SERVICES = {
|
DANKSERVER_COMPOSE_SERVICES = {
|
||||||
"mariadb",
|
"mariadb",
|
||||||
"redis",
|
"redis",
|
||||||
"romm",
|
"romm",
|
||||||
"copyparty",
|
"copyparty",
|
||||||
"jellyfin",
|
|
||||||
"calibre-web",
|
"calibre-web",
|
||||||
}
|
}
|
||||||
COMPOSE_NAME_MAP = {"mariadb": "romm-mariadb", "redis": "romm-redis"}
|
COMPOSE_NAME_MAP = {"mariadb": "romm-mariadb", "redis": "romm-redis"}
|
||||||
@@ -421,6 +422,11 @@ def services_for_path(path: str, target: str) -> set[str]:
|
|||||||
"tautulli": {"tautulli"},
|
"tautulli": {"tautulli"},
|
||||||
}
|
}
|
||||||
_add(services, *quadlet_services.get(parts[1], set()))
|
_add(services, *quadlet_services.get(parts[1], set()))
|
||||||
|
if len(parts) >= 2 and parts[1] == "host":
|
||||||
|
# dankserver/host/systemd/media-meta-view.mount is the overlay view the
|
||||||
|
# media servers bind (scripts/media-view.sh installs it on the host),
|
||||||
|
# so editing it restarts jellyfin and plex and nothing else.
|
||||||
|
_add(services, "jellyfin", "plex")
|
||||||
if len(parts) >= 2 and parts[1] == "observability":
|
if len(parts) >= 2 and parts[1] == "observability":
|
||||||
# Dankserver's node-exporter became a Quadlet unit on 2026-09-16
|
# Dankserver's node-exporter became a Quadlet unit on 2026-09-16
|
||||||
# (serverAppConfigs-docs/Infrastructure/Observability.md): its
|
# (serverAppConfigs-docs/Infrastructure/Observability.md): its
|
||||||
@@ -466,6 +472,15 @@ def services_for_path(path: str, target: str) -> set[str]:
|
|||||||
# that service (target_services then keeps it only on the host being
|
# that service (target_services then keeps it only on the host being
|
||||||
# planned). Editing the collector restarts the running exporter.
|
# planned). Editing the collector restarts the running exporter.
|
||||||
_add(services, "gpu-exporter")
|
_add(services, "gpu-exporter")
|
||||||
|
if name == "media-view.sh":
|
||||||
|
# The installer is what creates, mounts and reaps the view both media
|
||||||
|
# servers read, so a change to it is a restart of that pair.
|
||||||
|
_add(services, "jellyfin", "plex")
|
||||||
|
if name == "jellyfin-providers.sh":
|
||||||
|
# The deployer runs this before jellyfin restarts, so a change to
|
||||||
|
# the installer is a change to the provider state Jellyfin loads at
|
||||||
|
# start-up: it selects jellyfin and nothing else.
|
||||||
|
_add(services, "jellyfin")
|
||||||
if name in {"frank-zealot.env", "frank-zealot.service", "serve-frank-zealot.sh"}:
|
if name in {"frank-zealot.env", "frank-zealot.service", "serve-frank-zealot.sh"}:
|
||||||
# llama.cpp frank-zealot (2026-09-16) keeps its unit, env and serve
|
# llama.cpp frank-zealot (2026-09-16) keeps its unit, env and serve
|
||||||
# files here; each one selects frank-zealot only. The ik_llama.cpp
|
# files here; each one selects frank-zealot only. The ik_llama.cpp
|
||||||
|
|||||||
@@ -1202,7 +1202,42 @@ deploy_romm_stack() {
|
|||||||
systemctl --user restart "$service.service"
|
systemctl --user restart "$service.service"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ensure_media_view() {
|
||||||
|
# Both media servers read their library through the SSD-backed overlay view
|
||||||
|
# (scripts/media-view.sh, dankserver/host/systemd/media-meta-view.mount): the
|
||||||
|
# array below stays read-only and every sidecar Jellyfin writes lands on the
|
||||||
|
# NVMe.
|
||||||
|
# Raise it BEFORE either container starts — a start against a bare view
|
||||||
|
# directory would let a scan clear the library database. The units also carry
|
||||||
|
# ConditionPathIsMountPoint, so a missing view skips the start rather than
|
||||||
|
# damaging anything. A relayout must be followed by `media-view.sh reap`
|
||||||
|
# (see the Media Relayout page), which this is not.
|
||||||
|
run_privileged bash scripts/media-view.sh install
|
||||||
|
bash scripts/media-view.sh verify
|
||||||
|
}
|
||||||
|
|
||||||
|
ensure_provider_keys() {
|
||||||
|
# Jellyfin's metadata-provider keys live in the container-owned plugin
|
||||||
|
# configuration under the data root, never in git; scripts/jellyfin-providers.sh
|
||||||
|
# is what places them, reading the per-service host env file (the fleet
|
||||||
|
# convention: grafana, caddy, pihole, copyparty, romm, seerr) that holds
|
||||||
|
# TMDB_API_KEY= and TVDB_API_KEY=, mode 600.
|
||||||
|
# Missing keys mean a keyless provider — a degraded library, not a broken
|
||||||
|
# deploy — so this warns rather than failing the restart. But when the keys
|
||||||
|
# ARE placed it must happen before the restart: the plugin and its keys are
|
||||||
|
# read at start-up, so a restart first would boot the old state.
|
||||||
|
local keys=/home/dank/Containers/jellyfin/provider-keys
|
||||||
|
if [ -f "$keys" ]; then
|
||||||
|
bash scripts/jellyfin-providers.sh install
|
||||||
|
else
|
||||||
|
echo "WARN: $keys is absent - provider keys NOT placed"
|
||||||
|
echo "WARN: create it (TMDB_API_KEY=, TVDB_API_KEY=, chmod 600) and re-deploy jellyfin"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
deploy_jellyfin() {
|
deploy_jellyfin() {
|
||||||
|
ensure_media_view
|
||||||
|
ensure_provider_keys
|
||||||
cp "dankserver/jellyfin/quadlet/jellyfin.container" "$quadlet_unit_dir/"
|
cp "dankserver/jellyfin/quadlet/jellyfin.container" "$quadlet_unit_dir/"
|
||||||
systemctl --user daemon-reload
|
systemctl --user daemon-reload
|
||||||
if ! systemctl --user is-active -q jellyfin.service; then
|
if ! systemctl --user is-active -q jellyfin.service; then
|
||||||
@@ -1290,6 +1325,7 @@ deploy_copyparty() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
deploy_plex() {
|
deploy_plex() {
|
||||||
|
ensure_media_view
|
||||||
cp "dankserver/plex/quadlet/plex.container" "$quadlet_unit_dir/"
|
cp "dankserver/plex/quadlet/plex.container" "$quadlet_unit_dir/"
|
||||||
systemctl --user daemon-reload
|
systemctl --user daemon-reload
|
||||||
if ! systemctl --user is-active -q plex.service; then
|
if ! systemctl --user is-active -q plex.service; then
|
||||||
|
|||||||
Executable
+391
@@ -0,0 +1,391 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# Metadata-provider keys for Jellyfin on Dankserver, placed from the service's
|
||||||
|
# host env file with the plugin artifact pinned by content hash.
|
||||||
|
#
|
||||||
|
# Jellyfin 12 ships TMDb, OMDb, StudioImages and MusicBrainz as CORE plugins:
|
||||||
|
# they have no DLL, only a configuration file under the data root, and that
|
||||||
|
# file is created by the running container. So the fleet's provider state lives
|
||||||
|
# in two places the repo does not track —
|
||||||
|
#
|
||||||
|
# plugins/configurations/Jellyfin.Plugin.Tmdb.xml <TmdbApiKey /> (empty = keyless)
|
||||||
|
# plugins/configurations/Jellyfin.Plugin.Tvdb.xml <ApiKey /> (needs the plugin)
|
||||||
|
#
|
||||||
|
# and both are owned by the container user. This tool is the only thing that
|
||||||
|
# writes them: it is declarative, idempotent, and it never prints a key, so it
|
||||||
|
# is safe to run while someone watches.
|
||||||
|
#
|
||||||
|
# Keys never enter git and never enter argv (a value in argv is world-readable
|
||||||
|
# through /proc/<pid>/cmdline). They live in the per-service host env file that
|
||||||
|
# is the fleet's convention for container credentials (grafana, caddy, pihole,
|
||||||
|
# copyparty, romm, seerr), mode 600:
|
||||||
|
#
|
||||||
|
# /home/dank/Containers/jellyfin/provider-keys
|
||||||
|
# TMDB_API_KEY=<key>
|
||||||
|
# TVDB_API_KEY=<key>
|
||||||
|
# TVDB_PIN=<pin> # optional: only a subscriber-shaped TVDB key
|
||||||
|
# # needs one; a licensed key does not
|
||||||
|
#
|
||||||
|
# TVDB itself is not core in 12 (the metadata provider was cut in 10.9), so it
|
||||||
|
# is installed from the official catalog, pinned three ways: sha256 of the
|
||||||
|
# artifact, the plugin's own guid, and its version. A matching sha proves only
|
||||||
|
# that the bytes are the bytes fetched — the guid check is what says it is the
|
||||||
|
# official plugin, the version check is what says it is the one this file was
|
||||||
|
# reviewed against.
|
||||||
|
#
|
||||||
|
# Subcommands
|
||||||
|
# plan print the resolved shape and every pending change, touch nothing
|
||||||
|
# install fetch+verify the plugin, then write the keys (backing up first)
|
||||||
|
# verify prove the plugin and both keys are actually in place
|
||||||
|
#
|
||||||
|
# Nothing here restarts Jellyfin: the deployer owns that
|
||||||
|
# (`scripts/deploy.sh dankserver --service jellyfin`), and the plugin is read at
|
||||||
|
# start-up, so a key written while the server runs is not live until then.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
# Jellyfin's data root on Dankserver (the rootless quadlet's config volume).
|
||||||
|
JF_ROOT="${JF_ROOT:-/home/dank/Containers/jellyfin/config}"
|
||||||
|
PROV_KEYS="${PROV_KEYS:-/home/dank/Containers/jellyfin/provider-keys}"
|
||||||
|
TRASH_DIR="${TRASH_DIR:-$HOME/.AGENTTRASH/$(date +%Y%m%d)-jellyfin-providers}"
|
||||||
|
|
||||||
|
PLUGINS="$JF_ROOT/plugins"
|
||||||
|
CONF="$PLUGINS/configurations"
|
||||||
|
TMDB_XML="$CONF/Jellyfin.Plugin.Tmdb.xml"
|
||||||
|
TVDB_XML="$CONF/Jellyfin.Plugin.Tvdb.xml"
|
||||||
|
|
||||||
|
# repo.jellyfin.org catalog entry for TheTVDB (owner: jellyfin), version 24.0.0.0,
|
||||||
|
# targetAbi 12.0.0.0 — published 2026-09-09, verified against Jellyfin 12.4.0.
|
||||||
|
TVDB_PLUGIN_URL="${TVDB_PLUGIN_URL:-https://repo.jellyfin.org/files/plugin/thetvdb/thetvdb_24.0.0.0.zip}"
|
||||||
|
TVDB_PLUGIN_SHA256="${TVDB_PLUGIN_SHA256:-6302fba3857bdde8d186dd099fac09ca43247041f531a528a2c788add70a5319}"
|
||||||
|
TVDB_PLUGIN_GUID="${TVDB_PLUGIN_GUID:-a677c0da-fac5-4cde-941a-7134223f14c8}"
|
||||||
|
TVDB_PLUGIN_VERSION="${TVDB_PLUGIN_VERSION:-24.0.0.0}"
|
||||||
|
PLUGIN_DIR="thetvdb_${TVDB_PLUGIN_VERSION}"
|
||||||
|
TVDB_DLL="Jellyfin.Plugin.Tvdb.dll"
|
||||||
|
|
||||||
|
die() {
|
||||||
|
printf 'jellyfin-providers: %s\n' "$*" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
printf 'usage: %s plan|install|verify\n' "$0" >&2
|
||||||
|
exit 2
|
||||||
|
}
|
||||||
|
|
||||||
|
# check_keys — refuse anything that is not a readable private file. An open
|
||||||
|
# mode is an error, not a warning: a provider key readable by every user on the
|
||||||
|
# box is the failure this check exists to catch.
|
||||||
|
check_keys() {
|
||||||
|
[ -f "$PROV_KEYS" ] ||
|
||||||
|
die "$PROV_KEYS not found (create it with TMDB_API_KEY= and TVDB_API_KEY=, then chmod 600)"
|
||||||
|
local mode
|
||||||
|
mode="$(stat -c '%a' "$PROV_KEYS")"
|
||||||
|
case "$mode" in
|
||||||
|
600 | 400) ;;
|
||||||
|
*) die "$PROV_KEYS is mode $mode; provider keys must be 600" ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# keys_value <VAR> — the value of one variable in the env file, or nothing.
|
||||||
|
keys_value() {
|
||||||
|
awk -v k="$1" -F= '$1 == k { sub(/^[^=]*=/, ""); print; exit }' "$PROV_KEYS"
|
||||||
|
}
|
||||||
|
|
||||||
|
# keys_len <VAR> — the length, and a refusal naming the variable when absent.
|
||||||
|
# The value itself is never printed.
|
||||||
|
keys_len() {
|
||||||
|
local v
|
||||||
|
v="$(keys_value "$1")"
|
||||||
|
[ -n "$v" ] || die "$PROV_KEYS has no $1= line"
|
||||||
|
printf '%s' "${#v}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# xml_value <file> <element> — the element's text, empty when it is absent or
|
||||||
|
# self-closing.
|
||||||
|
xml_value() {
|
||||||
|
python3 - "$1" "$2" <<'PYEOF'
|
||||||
|
import re, sys
|
||||||
|
src = open(sys.argv[1], encoding="utf-8").read()
|
||||||
|
m = re.search(r"<%s(?:\s*/>|>(.*?)</%s>)" % (sys.argv[2], sys.argv[2]), src, re.S)
|
||||||
|
print(m.group(1).strip() if (m and m.group(1)) else "")
|
||||||
|
PYEOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# xml_secret <file> <element> <var> — set <element> to that variable from the
|
||||||
|
# env file. The python process reads the env file itself, so the value never
|
||||||
|
# appears in argv.
|
||||||
|
xml_secret() {
|
||||||
|
python3 - "$1" "$2" "$3" "$PROV_KEYS" <<'PYEOF'
|
||||||
|
import os, re, sys, tempfile
|
||||||
|
from xml.sax.saxutils import escape
|
||||||
|
|
||||||
|
xml_path, elem, var, keys_path = sys.argv[1:5]
|
||||||
|
val = ""
|
||||||
|
for line in open(keys_path, encoding="utf-8"):
|
||||||
|
key, sep, rest = line.partition("=")
|
||||||
|
if sep and key.strip() == var:
|
||||||
|
val = rest.strip()
|
||||||
|
break
|
||||||
|
if not val:
|
||||||
|
raise SystemExit(f"{keys_path} has no {var}= line")
|
||||||
|
|
||||||
|
src = open(xml_path, encoding="utf-8").read()
|
||||||
|
pat = re.compile(r"([ \t]*)<%s(?:\s*/>|>.*?</%s>)" % (elem, elem), re.S)
|
||||||
|
text = escape(val)
|
||||||
|
if pat.search(src):
|
||||||
|
out = pat.sub(lambda m: f"{m.group(1)}<{elem}>{text}</{elem}>", src, count=1)
|
||||||
|
else:
|
||||||
|
out = src.replace("</Configuration>", f" <{elem}>{text}</{elem}>\n</Configuration>", 1)
|
||||||
|
if out == src:
|
||||||
|
raise SystemExit(f"{xml_path}: no place to write <{elem}>")
|
||||||
|
|
||||||
|
d, base = os.path.split(xml_path)
|
||||||
|
fd, tmp = tempfile.mkstemp(prefix=base + ".", dir=d)
|
||||||
|
with os.fdopen(fd, "w", encoding="utf-8") as fh:
|
||||||
|
fh.write(out)
|
||||||
|
os.chmod(tmp, os.stat(xml_path).st_mode & 0o777)
|
||||||
|
os.replace(tmp, xml_path)
|
||||||
|
PYEOF
|
||||||
|
}
|
||||||
|
|
||||||
|
# plugin_meta <zip> — "guid version" out of the artifact's own meta.json.
|
||||||
|
plugin_meta() {
|
||||||
|
python3 - "$1" <<'PYEOF'
|
||||||
|
import json, sys, zipfile
|
||||||
|
meta = json.loads(zipfile.ZipFile(sys.argv[1]).read("meta.json").decode("utf-8"))
|
||||||
|
print(meta.get("guid", ""), meta.get("version", ""))
|
||||||
|
PYEOF
|
||||||
|
}
|
||||||
|
|
||||||
|
check_root() {
|
||||||
|
# Never fabricate a data root: creating plugins/ beside a typo'd path would
|
||||||
|
# install a plugin Jellyfin never loads and hide the operator's mistake.
|
||||||
|
[ -d "$CONF" ] || die "no Jellyfin configuration directory at $CONF (is JF_ROOT right?)"
|
||||||
|
}
|
||||||
|
|
||||||
|
check_keys_report() {
|
||||||
|
check_keys
|
||||||
|
# A refusal inside a command substitution used as a printf argument would
|
||||||
|
# be swallowed, so the guard runs as its own statement.
|
||||||
|
keys_len TMDB_API_KEY >/dev/null
|
||||||
|
keys_len TVDB_API_KEY >/dev/null
|
||||||
|
printf '= keys read from %s (mode %s)\n' "$PROV_KEYS" "$(stat -c '%a' "$PROV_KEYS")"
|
||||||
|
}
|
||||||
|
|
||||||
|
# The plugin's state, as one line in the shared vocabulary.
|
||||||
|
plugin_state() {
|
||||||
|
if [ -d "$PLUGINS/$PLUGIN_DIR" ]; then
|
||||||
|
printf '= %s already installed\n' "$PLUGIN_DIR"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
local other
|
||||||
|
other="$(find "$PLUGINS" -maxdepth 1 -type d -name 'thetvdb_*' ! -name "$PLUGIN_DIR" -printf '%f\n' 2>/dev/null | sort | tr '\n' ' ')"
|
||||||
|
if [ -n "$other" ]; then
|
||||||
|
printf '+ install %s (a different version is present: %s)\n' "$PLUGIN_DIR" "$other"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
printf '+ install %s from %s\n' "$PLUGIN_DIR" "${TVDB_PLUGIN_URL#*://}"
|
||||||
|
}
|
||||||
|
|
||||||
|
tmdb_state() {
|
||||||
|
if [ ! -f "$TMDB_XML" ]; then
|
||||||
|
printf '! %s is missing — save the TMDb plugin settings once in the UI, then re-run\n' "$TMDB_XML" >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
local now want
|
||||||
|
now="$(xml_value "$TMDB_XML" TmdbApiKey)"
|
||||||
|
want="$(keys_value TMDB_API_KEY)"
|
||||||
|
if [ -n "$now" ] && [ "$now" = "$want" ]; then
|
||||||
|
printf '= TmdbApiKey already set (%s chars)\n' "${#now}"
|
||||||
|
elif [ -n "$now" ]; then
|
||||||
|
printf '+ replace the TmdbApiKey in Jellyfin.Plugin.Tmdb.xml\n'
|
||||||
|
else
|
||||||
|
printf '+ set TmdbApiKey in Jellyfin.Plugin.Tmdb.xml\n'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
tvdb_state() {
|
||||||
|
local now
|
||||||
|
if [ -f "$TVDB_XML" ]; then
|
||||||
|
now="$(xml_value "$TVDB_XML" ApiKey)"
|
||||||
|
if [ -n "$now" ]; then
|
||||||
|
printf '= Jellyfin.Plugin.Tvdb.xml already has an ApiKey (kept)\n'
|
||||||
|
else
|
||||||
|
printf '+ fill the empty ApiKey in Jellyfin.Plugin.Tvdb.xml\n'
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
printf '+ create Jellyfin.Plugin.Tvdb.xml with ApiKey\n'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
write_plugin() {
|
||||||
|
local tmp guid version other
|
||||||
|
other="$(find "$PLUGINS" -maxdepth 1 -type d -name 'thetvdb_*' ! -name "$PLUGIN_DIR" -printf '%f\n' 2>/dev/null | sort | tr '\n' ' ')"
|
||||||
|
[ -z "$other" ] ||
|
||||||
|
die "another TheTVDB version is already installed ($other); move it to the trash first — two copies of one plugin means two loads"
|
||||||
|
|
||||||
|
tmp="$(mktemp "${TMPDIR:-/tmp}/thetvdb.XXXXXX.zip")"
|
||||||
|
# The pins are checked before anything is unpacked, so a bad artifact never
|
||||||
|
# reaches the plugins directory.
|
||||||
|
curl -fsSL --max-time 300 -o "$tmp" "$TVDB_PLUGIN_URL" ||
|
||||||
|
{ rm -f "$tmp"; die "could not fetch $TVDB_PLUGIN_URL"; }
|
||||||
|
local got
|
||||||
|
got="$(sha256sum "$tmp" | cut -d' ' -f1)"
|
||||||
|
if [ "$got" != "$TVDB_PLUGIN_SHA256" ]; then
|
||||||
|
rm -f "$tmp"
|
||||||
|
die "artifact sha256 is $got, pinned $TVDB_PLUGIN_SHA256 — refusing to install"
|
||||||
|
fi
|
||||||
|
read -r guid version <<<"$(plugin_meta "$tmp")"
|
||||||
|
if [ "$guid" != "$TVDB_PLUGIN_GUID" ]; then
|
||||||
|
rm -f "$tmp"
|
||||||
|
die "artifact guid is $guid, expected the official $TVDB_PLUGIN_GUID — refusing to install"
|
||||||
|
fi
|
||||||
|
if [ "$version" != "$TVDB_PLUGIN_VERSION" ]; then
|
||||||
|
rm -f "$tmp"
|
||||||
|
die "artifact version is $version, pinned $TVDB_PLUGIN_VERSION — refusing to install"
|
||||||
|
fi
|
||||||
|
mkdir -p "$PLUGINS/$PLUGIN_DIR"
|
||||||
|
python3 -m zipfile -e "$tmp" "$PLUGINS/$PLUGIN_DIR/"
|
||||||
|
rm -f "$tmp"
|
||||||
|
[ -f "$PLUGINS/$PLUGIN_DIR/$TVDB_DLL" ] ||
|
||||||
|
die "$PLUGIN_DIR has no $TVDB_DLL — the artifact does not match this plugin"
|
||||||
|
printf '+ installed %s\n' "$PLUGIN_DIR"
|
||||||
|
}
|
||||||
|
|
||||||
|
backup_to_trash() {
|
||||||
|
local f="$1"
|
||||||
|
mkdir -p "$TRASH_DIR"
|
||||||
|
local dest="$TRASH_DIR/$(basename "$f").$(date +%H%M%S)"
|
||||||
|
cp -p "$f" "$dest"
|
||||||
|
printf '+ previous copy kept at %s\n' "$dest"
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd_write_keys() {
|
||||||
|
local now want
|
||||||
|
KEYS_CHANGED=0
|
||||||
|
# TMDb: the running container owns this file and every setting in it. Only
|
||||||
|
# the key element is touched, so country, language and adult settings
|
||||||
|
# survive the edit.
|
||||||
|
[ -f "$TMDB_XML" ] ||
|
||||||
|
die "$TMDB_XML is missing — save the TMDb plugin settings once in the UI, then re-run"
|
||||||
|
now="$(xml_value "$TMDB_XML" TmdbApiKey)"
|
||||||
|
keys_len TMDB_API_KEY >/dev/null
|
||||||
|
want="$(keys_value TMDB_API_KEY)"
|
||||||
|
if [ "$now" = "$want" ]; then
|
||||||
|
printf '= TmdbApiKey already set (%s chars)\n' "${#now}"
|
||||||
|
else
|
||||||
|
backup_to_trash "$TMDB_XML"
|
||||||
|
xml_secret "$TMDB_XML" TmdbApiKey TMDB_API_KEY
|
||||||
|
printf '+ set TmdbApiKey (%s chars)\n' "${#want}"
|
||||||
|
KEYS_CHANGED=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# TVDB: never overwrite a config the operator saved through the UI.
|
||||||
|
if [ -f "$TVDB_XML" ]; then
|
||||||
|
now="$(xml_value "$TVDB_XML" ApiKey)"
|
||||||
|
if [ -n "$now" ]; then
|
||||||
|
printf '= Jellyfin.Plugin.Tvdb.xml already has an ApiKey (kept)\n'
|
||||||
|
else
|
||||||
|
keys_len TVDB_API_KEY >/dev/null
|
||||||
|
backup_to_trash "$TVDB_XML"
|
||||||
|
xml_secret "$TVDB_XML" ApiKey TVDB_API_KEY
|
||||||
|
if [ -n "$(keys_value TVDB_PIN)" ] && [ -z "$(xml_value "$TVDB_XML" Pin)" ]; then
|
||||||
|
xml_secret "$TVDB_XML" Pin TVDB_PIN
|
||||||
|
fi
|
||||||
|
printf '+ filled the ApiKey in Jellyfin.Plugin.Tvdb.xml\n'
|
||||||
|
KEYS_CHANGED=1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
keys_len TVDB_API_KEY >/dev/null
|
||||||
|
cat >"$TVDB_XML" <<XML
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||||
|
<ApiKey />
|
||||||
|
</Configuration>
|
||||||
|
XML
|
||||||
|
xml_secret "$TVDB_XML" ApiKey TVDB_API_KEY
|
||||||
|
if [ -n "$(keys_value TVDB_PIN)" ]; then
|
||||||
|
xml_secret "$TVDB_XML" Pin TVDB_PIN
|
||||||
|
fi
|
||||||
|
printf '+ created Jellyfin.Plugin.Tvdb.xml with ApiKey\n'
|
||||||
|
KEYS_CHANGED=1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd_plan() {
|
||||||
|
check_root
|
||||||
|
check_keys_report
|
||||||
|
printf 'root %s\n' "$JF_ROOT"
|
||||||
|
plugin_state
|
||||||
|
tmdb_state
|
||||||
|
tvdb_state
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd_install() {
|
||||||
|
check_root
|
||||||
|
check_keys_report
|
||||||
|
KEYS_CHANGED=0
|
||||||
|
if [ ! -d "$PLUGINS/$PLUGIN_DIR" ]; then
|
||||||
|
write_plugin
|
||||||
|
KEYS_CHANGED=1
|
||||||
|
else
|
||||||
|
printf '= %s already installed\n' "$PLUGIN_DIR"
|
||||||
|
fi
|
||||||
|
cmd_write_keys
|
||||||
|
if [ "$KEYS_CHANGED" -eq 1 ]; then
|
||||||
|
# Nothing is restarted here on purpose: this host may be playing media
|
||||||
|
# right now, and both the plugin and its keys are read at start-up.
|
||||||
|
printf 'next: scripts/deploy.sh dankserver --service jellyfin (the plugin and keys load at start-up)\n'
|
||||||
|
else
|
||||||
|
printf '= no change\n'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd_verify() {
|
||||||
|
check_root
|
||||||
|
local bad=0
|
||||||
|
if [ ! -d "$PLUGINS/$PLUGIN_DIR" ]; then
|
||||||
|
printf 'MISSING plugin directory %s (thetvdb not installed)\n' "$PLUGIN_DIR" >&2
|
||||||
|
bad=1
|
||||||
|
else
|
||||||
|
local dlls abi
|
||||||
|
dlls="$(find "$PLUGINS/$PLUGIN_DIR" -maxdepth 1 -name '*.dll' | wc -l)"
|
||||||
|
[ "$dlls" -ge 1 ] || {
|
||||||
|
printf 'MISSING %s inside %s\n' "$TVDB_DLL" "$PLUGIN_DIR" >&2
|
||||||
|
bad=1
|
||||||
|
}
|
||||||
|
abi="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1])).get("targetAbi","?"))' \
|
||||||
|
"$PLUGINS/$PLUGIN_DIR/meta.json" 2>/dev/null || echo '?')"
|
||||||
|
printf 'plugin %s: %s dll(s), targetAbi %s\n' "$PLUGIN_DIR" "$dlls" "$abi"
|
||||||
|
fi
|
||||||
|
local pair elem n
|
||||||
|
for pair in "$TMDB_XML|TmdbApiKey" "$TVDB_XML|ApiKey"; do
|
||||||
|
elem="${pair##*|}"
|
||||||
|
if [ ! -f "${pair%%|*}" ]; then
|
||||||
|
printf 'MISSING %s\n' "${pair%%|*}" >&2
|
||||||
|
bad=1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if ! python3 -c 'import sys, xml.dom.minidom; xml.dom.minidom.parse(sys.argv[1])' "${pair%%|*}"; then
|
||||||
|
printf 'UNPARSEABLE %s\n' "${pair%%|*}" >&2
|
||||||
|
bad=1
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
n="$(xml_value "${pair%%|*}" "$elem")"
|
||||||
|
if [ -z "$n" ]; then
|
||||||
|
printf 'MISSING key: %s has no %s value\n' "$(basename "${pair%%|*}")" "$elem" >&2
|
||||||
|
bad=1
|
||||||
|
else
|
||||||
|
printf '%s %s chars\n' "$elem" "${#n}"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
[ "$bad" -eq 0 ] || return 1
|
||||||
|
printf 'providers configured\n'
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd="${1:-}"
|
||||||
|
case "$cmd" in
|
||||||
|
plan) cmd_plan ;;
|
||||||
|
install) cmd_install ;;
|
||||||
|
verify) cmd_verify ;;
|
||||||
|
*) usage ;;
|
||||||
|
esac
|
||||||
Executable
+434
@@ -0,0 +1,434 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""media-apply.py — the only thing that moves movie-library files.
|
||||||
|
|
||||||
|
Reads the plan written by scripts/media-plan.py (rows: `action src dst kind`,
|
||||||
|
plus `mkdir` rows) and performs it with rename(2) — bytes are never copied, so
|
||||||
|
an interrupted run has destroyed nothing. Every run is resumable and every run
|
||||||
|
is undoable: the journal is a write-ahead log, one JSON line per intent, fsynced
|
||||||
|
BEFORE the rename it authorises. That ordering is the whole design: a crash
|
||||||
|
leaves either an intent whose move happened or an intent whose move never ran,
|
||||||
|
and both are recognised on the next run. A file that moved with no journal line
|
||||||
|
cannot have been moved by this tool, and saying so is better than guessing.
|
||||||
|
|
||||||
|
Four subcommands:
|
||||||
|
|
||||||
|
apply --plan PLAN [--root ROOT] [--journal J] [--apply]
|
||||||
|
no --apply => report only and write NOTHING, not even folders
|
||||||
|
rollback --journal J [--root ROOT] [--apply]
|
||||||
|
back to the paths the census saw; folders this run created are
|
||||||
|
removed only when empty, never anything else
|
||||||
|
verify --plan PLAN --journal J [--root ROOT]
|
||||||
|
every planned file is at its target with the journalled byte count
|
||||||
|
sweep --journal J [--root ROOT] [--apply]
|
||||||
|
removes the folders THIS RUN emptied — the journal has to show a
|
||||||
|
file that moved out of one, so a folder someone made by hand can
|
||||||
|
never be removed by this tool, and rollback puts them back
|
||||||
|
|
||||||
|
Refusals (exit 2, before the first rename, with nothing touched): a target
|
||||||
|
already occupied by a foreign file, a source that has gone, a duplicate target,
|
||||||
|
a path outside --root, a journal that was already rolled back, a move that would
|
||||||
|
cross a filesystem (rename cannot do it and a silent copy at 24 TB would not).
|
||||||
|
|
||||||
|
--force relaxes only the guards that are opinion (journal already rolled back,
|
||||||
|
paths outside --root); it never allows overwriting a file.
|
||||||
|
|
||||||
|
The journal defaults to `media-relayout.jsonl` BESIDE the library root, never
|
||||||
|
inside it: a log inside the library is a file Jellyfin will index.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
DEFAULT_ROOT = os.environ.get("MEDIA_ROOT", "/mnt/entropy/Movies")
|
||||||
|
|
||||||
|
|
||||||
|
class Refuse(Exception):
|
||||||
|
"""A gate the operator has to see; every refusal leaves the library alone."""
|
||||||
|
|
||||||
|
|
||||||
|
def journal_default(root):
|
||||||
|
return os.path.join(os.path.dirname(os.path.abspath(root)), "media-relayout.jsonl")
|
||||||
|
|
||||||
|
|
||||||
|
class Journal:
|
||||||
|
"""Append-only JSONL of intents. Writes are fsynced before the tool acts."""
|
||||||
|
|
||||||
|
def __init__(self, path, writing):
|
||||||
|
self.path, self.writing = path, writing
|
||||||
|
self.records = []
|
||||||
|
if os.path.exists(path):
|
||||||
|
for line in open(path):
|
||||||
|
line = line.strip()
|
||||||
|
if line:
|
||||||
|
self.records.append(json.loads(line))
|
||||||
|
self._fh = None
|
||||||
|
|
||||||
|
def already(self, dst, src):
|
||||||
|
"""An intent for this exact (src, dst) was written by an earlier run."""
|
||||||
|
return any(r.get("op") == "move" and r.get("dst") == dst
|
||||||
|
and r.get("src") == src for r in self.records)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def rolled_back(self):
|
||||||
|
return any(r.get("op") == "rollback" for r in self.records)
|
||||||
|
|
||||||
|
def record(self, **fields):
|
||||||
|
self.records.append(fields)
|
||||||
|
if not self.writing:
|
||||||
|
return
|
||||||
|
if self._fh is None:
|
||||||
|
self._fh = open(self.path, "a")
|
||||||
|
self._fh.write(json.dumps(fields) + "\n")
|
||||||
|
self._fh.flush()
|
||||||
|
os.fsync(self._fh.fileno())
|
||||||
|
|
||||||
|
def close(self):
|
||||||
|
if self._fh:
|
||||||
|
self._fh.close()
|
||||||
|
|
||||||
|
|
||||||
|
def read_plan(path, root, force):
|
||||||
|
"""Plan rows: (mkdirs, keeps, moves), header dropped, all paths under root."""
|
||||||
|
mkdirs, keeps, moves = [], [], []
|
||||||
|
with open(path) as fh:
|
||||||
|
header = fh.readline()
|
||||||
|
if not header.startswith("action\t"):
|
||||||
|
raise Refuse(f"{path} is not a plan (first column is "
|
||||||
|
f"{header.split(chr(9))[0]!r}, expected 'action') —"
|
||||||
|
" regenerate it with scripts/media-plan.py")
|
||||||
|
for line in fh:
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if f[0] == "mkdir":
|
||||||
|
mkdirs.append(f[2])
|
||||||
|
elif f[0] == "keep":
|
||||||
|
keeps.append(f[1])
|
||||||
|
elif f[0] == "move":
|
||||||
|
moves.append((f[1], f[2], f[3] if len(f) > 3 else ""))
|
||||||
|
if not force:
|
||||||
|
for p in mkdirs + keeps + [p for m in moves for p in m[:2]]:
|
||||||
|
if p and not (p == root or p.startswith(root + os.sep)):
|
||||||
|
raise Refuse(f"{p} is outside --root {root}"
|
||||||
|
" (a plan built for another library?)")
|
||||||
|
return mkdirs, keeps, moves
|
||||||
|
|
||||||
|
|
||||||
|
# A relayout moves bytes on the raw array; the sidecars the media servers wrote
|
||||||
|
# live in the SSD overlay and stay at the old coordinates. Naming the follow-up
|
||||||
|
# here is the point of use for that fact — see the Media Metadata View page.
|
||||||
|
METADATA_HINT = ("note: sidecars written through the metadata view did not follow"
|
||||||
|
" these moves — next: scripts/media-view.sh reap")
|
||||||
|
|
||||||
|
|
||||||
|
def do_apply(args, root, journal, mkdirs, keeps, moves):
|
||||||
|
"""Preflight everything, then folders, then renames. Never a partial start."""
|
||||||
|
problems = []
|
||||||
|
for p in keeps:
|
||||||
|
if not os.path.exists(p):
|
||||||
|
problems.append(f"missing: keep row source is gone: {p}")
|
||||||
|
seen = {}
|
||||||
|
for src, dst, _kind in moves:
|
||||||
|
if src == dst:
|
||||||
|
problems.append(f"no-op: move row whose target is its source: {src}")
|
||||||
|
if dst in seen:
|
||||||
|
problems.append(f"double-claim: {dst} claimed by {seen[dst]} and {src}")
|
||||||
|
seen[dst] = src
|
||||||
|
if journal.already(dst, src) and os.path.exists(dst) and not os.path.exists(src):
|
||||||
|
continue # this intent already ran
|
||||||
|
if not os.path.exists(src):
|
||||||
|
if os.path.exists(dst):
|
||||||
|
problems.append(f"missing: {src} is gone and {dst} carries no journal"
|
||||||
|
" line — it moved outside this tool, refusing to guess")
|
||||||
|
else:
|
||||||
|
problems.append(f"missing: {src} is gone and {dst} is not there either")
|
||||||
|
continue
|
||||||
|
if os.path.exists(dst):
|
||||||
|
problems.append(f"occupied: target already holds another file: {dst}")
|
||||||
|
if problems:
|
||||||
|
print(f"refusing: {len(problems)} problem(s), nothing moved")
|
||||||
|
for p in sorted(problems)[:20]:
|
||||||
|
print(" " + p)
|
||||||
|
if len(problems) > 20:
|
||||||
|
print(f" ... and {len(problems) - 20} more")
|
||||||
|
return 2
|
||||||
|
|
||||||
|
if not args.apply:
|
||||||
|
print(f"dry run: {len(moves)} moves, {len(mkdirs)} folders, {len(keeps)} files stay"
|
||||||
|
" — nothing written (pass --apply to run)")
|
||||||
|
for src, dst, _k in moves[:5]:
|
||||||
|
print(f" would move {src} -> {dst}")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
made = {r.get("dst") for r in journal.records if r.get("op") == "mkdir"}
|
||||||
|
dirs_new = 0
|
||||||
|
for d in sorted(mkdirs, key=lambda q: q.count(os.sep)):
|
||||||
|
if d in made or os.path.isdir(d):
|
||||||
|
continue
|
||||||
|
journal.record(op="mkdir", dst=d)
|
||||||
|
os.makedirs(d, exist_ok=True)
|
||||||
|
dirs_new += 1
|
||||||
|
|
||||||
|
# same-filesystem gate: rename cannot cross a device, and a silent copy of a
|
||||||
|
# library this size is not an option anyone would choose.
|
||||||
|
cross = []
|
||||||
|
for src, dst, _k in moves:
|
||||||
|
if journal.already(dst, src) and os.path.exists(dst) and not os.path.exists(src):
|
||||||
|
continue
|
||||||
|
parent = os.path.dirname(dst)
|
||||||
|
if os.path.exists(parent) and os.stat(parent).st_dev != os.stat(src).st_dev:
|
||||||
|
cross.append(dst)
|
||||||
|
if cross:
|
||||||
|
print(f"refusing: {len(cross)} moves would cross a filesystem"
|
||||||
|
f" (first: {cross[0]}) — this tool only renames")
|
||||||
|
return 2
|
||||||
|
|
||||||
|
moved = done = 0
|
||||||
|
for src, dst, _kind in moves:
|
||||||
|
if journal.already(dst, src):
|
||||||
|
if os.path.exists(dst) and not os.path.exists(src):
|
||||||
|
done += 1
|
||||||
|
continue
|
||||||
|
if os.path.exists(src) and not os.path.exists(dst):
|
||||||
|
os.rename(src, dst) # the intent ran, the move did not
|
||||||
|
moved += 1
|
||||||
|
continue
|
||||||
|
st = os.stat(src)
|
||||||
|
journal.record(op="move", src=src, dst=dst, bytes=st.st_size, mode=st.st_mode,
|
||||||
|
mtime=st.st_mtime, ino=st.st_ino, dev=st.st_dev)
|
||||||
|
os.rename(src, dst)
|
||||||
|
moved += 1
|
||||||
|
print(f"applied: {moved} moved, {done} already done in an earlier run,"
|
||||||
|
f" {dirs_new} folders created, {len(keeps)} files stayed put")
|
||||||
|
print(f"journal: {journal.path} — undo with: media-apply.py rollback"
|
||||||
|
f" --journal {journal.path} --root {root} --apply")
|
||||||
|
print(METADATA_HINT)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def do_verify(root, journal, moves, keeps):
|
||||||
|
"""Every planned file sits at its target carrying the journalled bytes."""
|
||||||
|
journalled = {(r["src"], r["dst"]): r for r in journal.records
|
||||||
|
if r.get("op") == "move"}
|
||||||
|
todo = moves or [(r["src"], r["dst"], "") for r in journal.records
|
||||||
|
if r.get("op") == "move"]
|
||||||
|
bad, missing, bytes_total = [], [], 0
|
||||||
|
for src, dst, _kind in todo:
|
||||||
|
rec = journalled.get((src, dst))
|
||||||
|
if not os.path.exists(dst):
|
||||||
|
missing.append(dst)
|
||||||
|
continue
|
||||||
|
size = os.path.getsize(dst)
|
||||||
|
bytes_total += size
|
||||||
|
if rec and rec.get("bytes") not in (None, size):
|
||||||
|
bad.append(f"{dst}: {size} bytes on disk, {rec['bytes']} at move time")
|
||||||
|
if os.path.exists(src) and src != dst:
|
||||||
|
bad.append(f"{src}: still in place as well as at {dst}")
|
||||||
|
missing += [p for p in keeps if not os.path.exists(p)]
|
||||||
|
if bad or missing:
|
||||||
|
print(f"verify FAILED: {len(missing)} absent, {len(bad)} wrong")
|
||||||
|
for line in (missing + bad)[:20]:
|
||||||
|
print(" " + line)
|
||||||
|
return 2
|
||||||
|
empty = [r["dst"] for r in journal.records
|
||||||
|
if r.get("op") == "mkdir" and os.path.isdir(r["dst"])
|
||||||
|
and not os.listdir(r["dst"])]
|
||||||
|
print(f"verified: {len(todo)} files at their targets"
|
||||||
|
f" ({bytes_total / 2**40:.2f} TiB), {len(keeps)} kept")
|
||||||
|
if empty:
|
||||||
|
print(f"note: {len(empty)} folders are empty (left in place — see the trash law)")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def sweep_targets(root, journal):
|
||||||
|
"""The folders this run emptied, deepest first — never one it did not touch.
|
||||||
|
|
||||||
|
Eligibility is evidence, not appearance. A folder is a target only when the
|
||||||
|
journal shows a file moved out of it or out of something beneath it, it holds
|
||||||
|
nothing but empty directories now, it is not a folder this run created, and no
|
||||||
|
journalled destination lives under it. A symlink anywhere inside disqualifies
|
||||||
|
the whole subtree: this tool never follows a link, and deleting the folder
|
||||||
|
that holds one would delete someone's mount point.
|
||||||
|
|
||||||
|
Proof carries downward: once a folder qualifies, every empty directory inside
|
||||||
|
it qualifies too. A release's junk (``Screens/``, ``Sample/``) is never itself
|
||||||
|
a move source, so without that rule it holds its emptied parent on disk
|
||||||
|
forever — the parent can never be removed while the child stands.
|
||||||
|
"""
|
||||||
|
moves = [r for r in journal.records if r.get("op") == "move"]
|
||||||
|
made = {r["dst"] for r in journal.records if r.get("op") == "mkdir"}
|
||||||
|
# An rmdir INTENT that failed left its folder standing, and the next sweep
|
||||||
|
# must be able to finish it: only a folder that is really gone is out of
|
||||||
|
# scope. Counting every intent as done made 146 live survivors permanent.
|
||||||
|
gone = {r["path"] for r in journal.records if r.get("op") == "rmdir"
|
||||||
|
and not os.path.exists(r["path"])}
|
||||||
|
dsts = [r["dst"] for r in moves]
|
||||||
|
marked = set()
|
||||||
|
for r in moves:
|
||||||
|
d = os.path.dirname(r["src"])
|
||||||
|
while d.startswith(root + os.sep):
|
||||||
|
marked.add(d)
|
||||||
|
d = os.path.dirname(d)
|
||||||
|
|
||||||
|
def removable(d):
|
||||||
|
if d in made or d in gone or os.path.islink(d):
|
||||||
|
return False
|
||||||
|
if any(p == d or p.startswith(d + os.sep) for p in dsts):
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
entries = os.listdir(d)
|
||||||
|
except OSError:
|
||||||
|
return False
|
||||||
|
for e in entries:
|
||||||
|
c = os.path.join(d, e)
|
||||||
|
if os.path.islink(c) or not os.path.isdir(c) or not removable(c):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
roots = [d for d in marked if removable(d)]
|
||||||
|
targets = set(roots)
|
||||||
|
for d in roots:
|
||||||
|
for dirpath, dirnames, _ in os.walk(d):
|
||||||
|
for c in dirnames:
|
||||||
|
p = os.path.join(dirpath, c)
|
||||||
|
if removable(p):
|
||||||
|
targets.add(p)
|
||||||
|
return sorted(targets, key=lambda q: (-q.count(os.sep), q))
|
||||||
|
|
||||||
|
|
||||||
|
def do_sweep(args, root, journal):
|
||||||
|
"""Remove the empty husks an apply left standing at the library root."""
|
||||||
|
moves = [r for r in journal.records if r.get("op") == "move"]
|
||||||
|
if not moves:
|
||||||
|
raise Refuse("the journal holds no moves — a sweep follows an apply, it"
|
||||||
|
" does not guess at emptiness")
|
||||||
|
outside = sorted({p for r in moves for p in (r["src"], r["dst"])
|
||||||
|
if not (p == root or p.startswith(root + os.sep))})
|
||||||
|
if outside and not args.force:
|
||||||
|
raise Refuse(f"{len(outside)} journalled paths are outside --root {root}"
|
||||||
|
f" (first: {outside[0]}) — this journal belongs to another"
|
||||||
|
" library")
|
||||||
|
targets = sweep_targets(root, journal)
|
||||||
|
if not targets:
|
||||||
|
print("swept: 0 folders — nothing this run emptied is still standing empty")
|
||||||
|
return 0
|
||||||
|
if not args.apply:
|
||||||
|
print(f"dry run: {len(targets)} folders this run emptied would be removed"
|
||||||
|
" (pass --apply to run)")
|
||||||
|
for d in targets[:5]:
|
||||||
|
print(f" would remove {d}")
|
||||||
|
return 0
|
||||||
|
removed = 0
|
||||||
|
for d in targets:
|
||||||
|
journal.record(op="rmdir", path=d)
|
||||||
|
try:
|
||||||
|
os.rmdir(d)
|
||||||
|
except OSError as e:
|
||||||
|
print(f"left in place: {d} ({e})")
|
||||||
|
continue
|
||||||
|
removed += 1
|
||||||
|
print(f"swept: {removed} folders removed, {len(targets) - removed} left in place")
|
||||||
|
print(f"journal: {journal.path} — rollback recreates them with their files")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def do_rollback(args, root, journal):
|
||||||
|
"""Undo every journalled move, newest first, after a full preflight."""
|
||||||
|
moves = [r for r in journal.records if r.get("op") == "move"]
|
||||||
|
made = [r["dst"] for r in journal.records if r.get("op") == "mkdir"]
|
||||||
|
swept = [r["path"] for r in journal.records if r.get("op") == "rmdir"]
|
||||||
|
problems = []
|
||||||
|
for r in reversed(moves):
|
||||||
|
src, dst = r["src"], r["dst"]
|
||||||
|
if not args.force and not (src.startswith(root + os.sep)
|
||||||
|
or dst.startswith(root + os.sep)):
|
||||||
|
problems.append(f"outside --root: {dst}")
|
||||||
|
continue
|
||||||
|
if os.path.exists(dst) and os.path.exists(src):
|
||||||
|
problems.append(f"occupied: both paths exist, refusing to choose:"
|
||||||
|
f" {src} | {dst}")
|
||||||
|
elif os.path.exists(dst):
|
||||||
|
pass
|
||||||
|
elif os.path.exists(src):
|
||||||
|
continue # already undone
|
||||||
|
else:
|
||||||
|
problems.append(f"missing: {dst} is gone and {src} never came back")
|
||||||
|
if problems:
|
||||||
|
print(f"refusing: {len(problems)} problem(s), nothing moved back")
|
||||||
|
for p in problems[:20]:
|
||||||
|
print(" " + p)
|
||||||
|
return 2
|
||||||
|
if not args.apply:
|
||||||
|
print(f"dry run: would move {len(moves)} files back to their census paths"
|
||||||
|
f" and recreate {len(set(swept))} swept folders (pass --apply)")
|
||||||
|
return 0
|
||||||
|
undone = 0
|
||||||
|
recreated = 0
|
||||||
|
for d in sorted(set(swept), key=lambda q: q.count(os.sep)):
|
||||||
|
if not os.path.isdir(d):
|
||||||
|
os.makedirs(d, exist_ok=True)
|
||||||
|
recreated += 1
|
||||||
|
for r in reversed(moves):
|
||||||
|
if os.path.exists(r["dst"]) and not os.path.exists(r["src"]):
|
||||||
|
os.makedirs(os.path.dirname(r["src"]), exist_ok=True)
|
||||||
|
os.rename(r["dst"], r["src"])
|
||||||
|
undone += 1
|
||||||
|
dirs_gone = 0
|
||||||
|
for d in sorted(set(made), key=lambda q: -q.count(os.sep)):
|
||||||
|
if d == root or not os.path.isdir(d) or os.listdir(d):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
os.rmdir(d)
|
||||||
|
dirs_gone += 1
|
||||||
|
except OSError as e:
|
||||||
|
print(f"left in place: {d} ({e})")
|
||||||
|
journal.record(op="rollback", undone=undone, dirs_removed=dirs_gone,
|
||||||
|
t=datetime.datetime.now(datetime.timezone.utc).isoformat())
|
||||||
|
print(f"rolled back: {undone} files back where the census found them,"
|
||||||
|
f" {dirs_gone} empty folders removed, {recreated} swept folders recreated")
|
||||||
|
print(METADATA_HINT)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ap = argparse.ArgumentParser(description="apply or undo a media relayout plan")
|
||||||
|
ap.add_argument("cmd", choices=["apply", "rollback", "verify", "sweep"])
|
||||||
|
ap.add_argument("--plan")
|
||||||
|
ap.add_argument("--root", default=DEFAULT_ROOT)
|
||||||
|
ap.add_argument("--journal")
|
||||||
|
ap.add_argument("--apply", action="store_true", help="actually change the library")
|
||||||
|
ap.add_argument("--force", action="store_true", help="relax the opinion guards")
|
||||||
|
args = ap.parse_args()
|
||||||
|
|
||||||
|
root = os.path.abspath(args.root)
|
||||||
|
if not os.path.isdir(root):
|
||||||
|
raise Refuse(f"no library at {root}")
|
||||||
|
journal_path = args.journal or journal_default(root)
|
||||||
|
journal = Journal(journal_path, args.apply)
|
||||||
|
try:
|
||||||
|
if args.cmd in ("rollback", "verify", "sweep") and not os.path.exists(journal_path):
|
||||||
|
raise Refuse(f"no journal at {journal_path} — nothing to {args.cmd}")
|
||||||
|
if args.cmd in ("rollback", "sweep") and not args.force and journal.rolled_back:
|
||||||
|
raise Refuse("this journal has already been rolled back"
|
||||||
|
" (--force to replay it)")
|
||||||
|
if args.cmd in ("apply", "verify") and not args.plan:
|
||||||
|
raise Refuse(f"{args.cmd} needs --plan")
|
||||||
|
mkdirs, keeps, moves = (read_plan(args.plan, root, args.force)
|
||||||
|
if args.plan else ([], [], []))
|
||||||
|
if args.cmd == "apply":
|
||||||
|
return do_apply(args, root, journal, mkdirs, keeps, moves)
|
||||||
|
if args.cmd == "verify":
|
||||||
|
return do_verify(root, journal, moves, keeps)
|
||||||
|
if args.cmd == "sweep":
|
||||||
|
return do_sweep(args, root, journal)
|
||||||
|
return do_rollback(args, root, journal)
|
||||||
|
finally:
|
||||||
|
journal.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
sys.exit(main())
|
||||||
|
except Refuse as r:
|
||||||
|
print(f"refusing: {r}")
|
||||||
|
sys.exit(2)
|
||||||
Executable
+304
@@ -0,0 +1,304 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
r"""Read-only census of a media library — the inputs a relayout plan is built from.
|
||||||
|
|
||||||
|
media-census.py enumerate [--root DIR] [--out TSV]
|
||||||
|
media-census.py probe [--enum TSV] [--out TSV] [--jobs N]
|
||||||
|
media-census.py plex [--root DIR] [--db PATH] [--out TSV]
|
||||||
|
media-census.py jellyfin [--root DIR] [--db PATH] [--container PREFIX] [--out TSV]
|
||||||
|
|
||||||
|
Run it on the machine that mounts the library. Nothing here writes inside the
|
||||||
|
library and nothing is deleted; the only outputs are the TSVs below, and the
|
||||||
|
scripts/media-plan.py and scripts/media-shows.py read them instead of touching media.
|
||||||
|
|
||||||
|
enum bytes <TAB> ext <TAB> path every file under the root
|
||||||
|
probed bytes <TAB> path <TAB> dur <TAB> vcodec <TAB> WxH <TAB> fps <TAB> vbit
|
||||||
|
<TAB> pix <TAB> audio <TAB> subs <TAB> spare <TAB> format
|
||||||
|
one row per video-container file; `audio` is "codec:lang+codec:lang",
|
||||||
|
`subs` the subtitle-stream count, `spare` the audio streams carrying
|
||||||
|
neither a default nor a forced tag
|
||||||
|
plex file <TAB> leaf_title <TAB> … (see PLEX_COLS)
|
||||||
|
the titles Plex already matched — the authority over any filename guess
|
||||||
|
|
||||||
|
jellyfin file <TAB> series <TAB> year <TAB> season <TAB> episode <TAB> extra
|
||||||
|
<TAB> tmdb (see JF_COLS)
|
||||||
|
one row per episode Jellyfin has matched and numbered, its path mapped
|
||||||
|
from the container's view back to the host; `year` and `tmdb` are the
|
||||||
|
SERIES' values, `extra` is Jellyfin's own extra type when the row is
|
||||||
|
bonus material rather than an episode
|
||||||
|
|
||||||
|
`probe` is the slow step (every container is opened for its header, ~10 min for
|
||||||
|
4,800 files on a spindle array at 6 workers) so it is `nice`d: a census beside a
|
||||||
|
live service must not starve it. `enumerate` is a stat pass, so re-censusing
|
||||||
|
before a plan is cheap — and a plan is only ever as good as these files.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import csv
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sqlite3
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
|
# Containers ffprobe can read. .iso/.img are deliberately NOT here: a disc image is
|
||||||
|
# a mounted volume, not a stream, and the planner treats it as a movie file instead.
|
||||||
|
PROBE_EXT = {".mkv", ".mp4", ".avi", ".m4v", ".mov", ".ts", ".m2ts", ".mpg",
|
||||||
|
".mpeg", ".webm", ".wmv", ".divx", ".ogm", ".bdmv", ".vob"}
|
||||||
|
# Trash and thumbnail caches are not library content, and this repo's trash policy
|
||||||
|
# means .AGENTTRASH can sit inside a movie folder at any time.
|
||||||
|
SKIP_DIR = re.compile(r"(^|/)(\.AGENTTRASH|\.thumbnails|\.Trash-\d+|@eaDir|lost\+found)(/|$)")
|
||||||
|
PLEX_DB = ("/var/lib/plexmediaserver/Library/Application Support/Plex Media Server"
|
||||||
|
"/Plug-in Support/Databases/com.plexapp.plugins.library.db")
|
||||||
|
PLEX_COLS = ["file", "leaf_title", "leaf_orig", "leaf_edition", "leaf_year",
|
||||||
|
"parent_title", "parent_orig", "parent_year", "parent_edition",
|
||||||
|
"container", "vcodec", "acodec", "width", "height"]
|
||||||
|
PLEX_SQL = """
|
||||||
|
select p.file, coalesce(mi.title,''), coalesce(mi.original_title,''),
|
||||||
|
coalesce(mi.edition_title,''), coalesce(mi.year,0),
|
||||||
|
coalesce(par.title,''), coalesce(par.original_title,''),
|
||||||
|
coalesce(par.year,0), coalesce(par.edition_title,''),
|
||||||
|
m.container, m.video_codec, m.audio_codec, m.width, m.height
|
||||||
|
from media_parts p
|
||||||
|
join media_items m on m.id = p.media_item_id
|
||||||
|
join metadata_items mi on mi.id = m.metadata_item_id
|
||||||
|
left join metadata_items par on par.id = mi.parent_id
|
||||||
|
where p.file like ? and p.deleted_at is null and mi.deleted_at is null
|
||||||
|
"""
|
||||||
|
JF_DB = "/home/dank/Containers/jellyfin/config/data/jellyfin.db"
|
||||||
|
JF_COLS = ["file", "series", "year", "season", "episode", "extra", "tmdb"]
|
||||||
|
# Jellyfin stores the entity's .NET class as its type; the TV tree is these three.
|
||||||
|
JF_TV = ("MediaBrowser.Controller.Entities.TV.Episode",
|
||||||
|
"MediaBrowser.Controller.Entities.TV.Season",
|
||||||
|
"MediaBrowser.Controller.Entities.TV.Series")
|
||||||
|
JF_SQL = ("""select Id, Path, IndexNumber, ParentIndexNumber, ExtraType, IsVirtualItem
|
||||||
|
from BaseItems where Type = ? and Path like ?""")
|
||||||
|
|
||||||
|
|
||||||
|
def enumerate_lib(root, out):
|
||||||
|
"""One row per real file under root: size, extension, path."""
|
||||||
|
rows = []
|
||||||
|
for dirpath, dirnames, filenames in os.walk(root):
|
||||||
|
dirnames[:] = [d for d in dirnames
|
||||||
|
if not SKIP_DIR.search(os.path.join(dirpath, d))]
|
||||||
|
for fn in filenames:
|
||||||
|
p = os.path.join(dirpath, fn)
|
||||||
|
if SKIP_DIR.search(p):
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
st = os.lstat(p)
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
if not os.path.isfile(p) or os.path.islink(p):
|
||||||
|
continue # links and sockets are not library content
|
||||||
|
rows.append((st.st_size, os.path.splitext(fn)[1].lower(), p))
|
||||||
|
with open(out, "w") as fh:
|
||||||
|
for size, ext, p in rows:
|
||||||
|
fh.write(f"{size}\t{ext}\t{p}\n")
|
||||||
|
vid = [r for r in rows if r[1] in PROBE_EXT]
|
||||||
|
print(f"files={len(rows)} bytes={sum(r[0] for r in rows)} "
|
||||||
|
f"probes={len(vid)} probe_bytes={sum(r[0] for r in vid)} -> {out}")
|
||||||
|
|
||||||
|
|
||||||
|
def probe_one(path):
|
||||||
|
"""ffprobe header read -> the probed TSV row (never the whole file)."""
|
||||||
|
try:
|
||||||
|
os.nice(19) # a census must never outrank the media server
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
size = os.stat(path).st_size
|
||||||
|
except OSError:
|
||||||
|
return None
|
||||||
|
cmd = ["ffprobe", "-v", "error", "-print_format", "json",
|
||||||
|
"-show_format", "-show_streams", path]
|
||||||
|
try:
|
||||||
|
done = subprocess.run(cmd, capture_output=True, text=True, timeout=180)
|
||||||
|
data = json.loads(done.stdout or "{}")
|
||||||
|
except (subprocess.TimeoutExpired, subprocess.SubprocessError,
|
||||||
|
json.JSONDecodeError, OSError):
|
||||||
|
data = {}
|
||||||
|
st = data.get("streams", [])
|
||||||
|
fmt = data.get("format", {})
|
||||||
|
v = next((s for s in st if s.get("codec_type") == "video"), None)
|
||||||
|
aud = [s for s in st if s.get("codec_type") == "audio"]
|
||||||
|
subs = [s for s in st if s.get("codec_type") == "subtitle"]
|
||||||
|
fps = "-"
|
||||||
|
if v and v.get("r_frame_rate"):
|
||||||
|
try:
|
||||||
|
n, d = (int(x) for x in v["r_frame_rate"].split("/", 1))
|
||||||
|
fps = f"{round(n / d * 100) / 100:g}" if d else "0"
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def num(x):
|
||||||
|
try:
|
||||||
|
return round(float(x))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return [size, path, num(fmt.get("duration")),
|
||||||
|
(v or {}).get("codec_name", "-"),
|
||||||
|
f"{v['width']}x{v['height']}" if v else "-",
|
||||||
|
fps, num(fmt.get("bit_rate")), (v or {}).get("pix_fmt", "-"),
|
||||||
|
"+".join(f"{a.get('codec_name', '?')}:{a.get('language', '?')}" for a in aud),
|
||||||
|
len(subs),
|
||||||
|
sum(1 for a in aud if not re.search(
|
||||||
|
r"default|forced", json.dumps(a.get("tags", {})), re.I)),
|
||||||
|
fmt.get("format_name", "-") or "-"]
|
||||||
|
|
||||||
|
|
||||||
|
def probe(enum, out, jobs):
|
||||||
|
"""Probe every video-container row of the enum TSV, in parallel."""
|
||||||
|
paths = []
|
||||||
|
with open(enum) as fh:
|
||||||
|
for line in fh:
|
||||||
|
f = line.rstrip("\n").split("\t", 2)
|
||||||
|
if len(f) == 3 and f[1] in PROBE_EXT:
|
||||||
|
paths.append(f[2])
|
||||||
|
print(f"probing {len(paths)} files with {jobs} workers", flush=True)
|
||||||
|
done = unreadable = 0
|
||||||
|
with open(out, "w") as fh, ThreadPoolExecutor(max_workers=jobs) as pool:
|
||||||
|
for row in pool.map(probe_one, paths):
|
||||||
|
if row is None:
|
||||||
|
continue
|
||||||
|
if row[3] == "-":
|
||||||
|
unreadable += 1 # header unreadable: the row still gets written
|
||||||
|
done += 1
|
||||||
|
fh.write("\t".join(str(x) for x in row) + "\n")
|
||||||
|
if done % 250 == 0:
|
||||||
|
print(f" {done}/{len(paths)}", flush=True)
|
||||||
|
print(f"probed={done} unreadable={unreadable} -> {out}")
|
||||||
|
|
||||||
|
|
||||||
|
def plex(root, db, out):
|
||||||
|
"""Plex's matched titles for files under root, from its DB, read-only."""
|
||||||
|
uri = f"file:{db}?mode=ro&immutable=1"
|
||||||
|
try:
|
||||||
|
con = sqlite3.connect(uri, uri=True)
|
||||||
|
except sqlite3.Error as e:
|
||||||
|
sys.exit(f"cannot open the Plex database read-only ({db}): {e}\n"
|
||||||
|
"Plex runs rootless here: run this as the container's owner, never "
|
||||||
|
"with sudo — rootless storage is per-user, so root sees nothing.")
|
||||||
|
with open(out, "w", newline="") as fh:
|
||||||
|
w = csv.writer(fh, delimiter="\t", lineterminator="\n")
|
||||||
|
w.writerow(PLEX_COLS)
|
||||||
|
n = 0
|
||||||
|
for r in con.execute(PLEX_SQL, (root + "/%",)):
|
||||||
|
w.writerow([str(x).replace("\t", " ").replace("\n", " ") for x in r])
|
||||||
|
n += 1
|
||||||
|
print(f"plex-matched={n} -> {out}")
|
||||||
|
|
||||||
|
|
||||||
|
def jellyfin(root, db, container, out):
|
||||||
|
"""The episodes Jellyfin has already numbered, for the library at root.
|
||||||
|
|
||||||
|
Jellyfin stores each path as the CONTAINER sees it (its quadlet binds the
|
||||||
|
share at /data), so `container` is the in-container prefix that stands for
|
||||||
|
root and every exported path is mapped back to the host before it is written
|
||||||
|
— the planner reads host paths, because it plans moves on the array.
|
||||||
|
|
||||||
|
The numbering is what matters, not the name: a release group's file name says
|
||||||
|
nothing about which episode a file is, and Jellyfin has already answered that
|
||||||
|
question against a provider. Season/episode come from the episode row; the
|
||||||
|
series name, its year and its TMDb id come from the Series row the episode
|
||||||
|
climbs to, because those are the only place Jellyfin keeps them.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
con = sqlite3.connect(f"file:{db}?mode=ro", uri=True)
|
||||||
|
except sqlite3.Error as e:
|
||||||
|
sys.exit(f"cannot open the Jellyfin database read-only ({db}): {e}\n"
|
||||||
|
"It is a WAL database: copy it with its -wal and -shm beside it and"
|
||||||
|
" point --db at the copy. Never sudo it — the data dir belongs to"
|
||||||
|
" the container's owner, and rootless storage is per-user.")
|
||||||
|
have = {r[1] for r in con.execute("pragma table_info(BaseItems)")}
|
||||||
|
need = {"Id", "ParentId", "Type", "Path", "IndexNumber", "ParentIndexNumber",
|
||||||
|
"ExtraType", "IsVirtualItem", "ProductionYear", "Data"}
|
||||||
|
if not need <= have:
|
||||||
|
sys.exit(f"{db} is not a Jellyfin library database (BaseItems has no"
|
||||||
|
f" {','.join(sorted(need - have))})")
|
||||||
|
node = {}
|
||||||
|
for i, pid, typ, name, year, data in con.execute(
|
||||||
|
"select Id, ParentId, Type, Name, ProductionYear, Data from BaseItems"
|
||||||
|
" where Type in (?,?,?)", JF_TV):
|
||||||
|
node[i] = (pid, typ, name, year, data)
|
||||||
|
|
||||||
|
def series_of(item_id):
|
||||||
|
"""Climb episode -> season -> series; empty when the chain is broken."""
|
||||||
|
for _ in range(4):
|
||||||
|
if item_id not in node:
|
||||||
|
return "", "", ""
|
||||||
|
pid, typ, name, year, data = node[item_id]
|
||||||
|
if typ.endswith(".Series"):
|
||||||
|
try:
|
||||||
|
tmdb = (json.loads(data or "{}")
|
||||||
|
.get("ProviderIds") or {}).get("Tmdb", "")
|
||||||
|
except ValueError:
|
||||||
|
tmdb = ""
|
||||||
|
return name or "", year or "", tmdb
|
||||||
|
item_id = pid
|
||||||
|
return "", "", ""
|
||||||
|
|
||||||
|
# The fleet's quadlets bind the share at /data, so the container's view of
|
||||||
|
# this library is /data/<the library's own folder name> unless told otherwise.
|
||||||
|
if not container:
|
||||||
|
container = "/data/" + os.path.basename(os.path.normpath(root))
|
||||||
|
prefix = container.rstrip("/") + "/"
|
||||||
|
written = skipped = 0
|
||||||
|
with open(out, "w", newline="") as fh:
|
||||||
|
w = csv.writer(fh, delimiter="\t", lineterminator="\n")
|
||||||
|
w.writerow(JF_COLS)
|
||||||
|
for iid, path, idx, pidx, extra, virt in con.execute(JF_SQL,
|
||||||
|
(JF_TV[0], prefix + "%")):
|
||||||
|
if virt or not path or not path.startswith(prefix):
|
||||||
|
skipped += 1 # a virtual item is a variant, not a file
|
||||||
|
continue
|
||||||
|
sname, syear, tmdb = series_of(iid)
|
||||||
|
w.writerow([root + "/" + path[len(prefix):],
|
||||||
|
sname.replace("\t", " "), syear,
|
||||||
|
"" if pidx is None else pidx,
|
||||||
|
"" if idx is None else idx,
|
||||||
|
(extra or "").replace("\t", " "), tmdb])
|
||||||
|
written += 1
|
||||||
|
print(f"episodes={written} virtual-or-blank={skipped} -> {out}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||||
|
sub = ap.add_subparsers(dest="cmd", required=True)
|
||||||
|
root = {"default": os.environ.get("MEDIA_ROOT", "/mnt/entropy/Movies"),
|
||||||
|
"help": "library root"}
|
||||||
|
e = sub.add_parser("enumerate", help="stat every file under the root")
|
||||||
|
e.add_argument("--root", **root)
|
||||||
|
e.add_argument("--out", default="/tmp/media-enum.tsv")
|
||||||
|
p = sub.add_parser("probe", help="ffprobe every video container")
|
||||||
|
p.add_argument("--enum", default="/tmp/media-enum.tsv")
|
||||||
|
p.add_argument("--out", default="/tmp/media-probed.tsv")
|
||||||
|
p.add_argument("--jobs", type=int, default=6)
|
||||||
|
x = sub.add_parser("plex", help="export Plex's matched titles for the root")
|
||||||
|
x.add_argument("--root", **root)
|
||||||
|
x.add_argument("--db", default=PLEX_DB)
|
||||||
|
x.add_argument("--out", default="/tmp/plex-movies.tsv")
|
||||||
|
j = sub.add_parser("jellyfin",
|
||||||
|
help="export the episodes Jellyfin has numbered for the root")
|
||||||
|
j.add_argument("--root", default=os.environ.get("MEDIA_ROOT",
|
||||||
|
"/mnt/entropy/TVShows"),
|
||||||
|
help="TV library root on the host")
|
||||||
|
j.add_argument("--db", default=JF_DB)
|
||||||
|
j.add_argument("--container", default=None,
|
||||||
|
help="the in-container prefix that stands for --root"
|
||||||
|
" (default /data/<root's folder name>)")
|
||||||
|
j.add_argument("--out", default="/tmp/jellyfin-tv.tsv")
|
||||||
|
a = ap.parse_args()
|
||||||
|
if a.cmd == "enumerate":
|
||||||
|
enumerate_lib(a.root, a.out)
|
||||||
|
elif a.cmd == "probe":
|
||||||
|
probe(a.enum, a.out, a.jobs)
|
||||||
|
elif a.cmd == "plex":
|
||||||
|
plex(a.root, a.db, a.out)
|
||||||
|
else:
|
||||||
|
jellyfin(a.root, a.db, a.container, a.out)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Executable
+881
@@ -0,0 +1,881 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
r"""Plan a Jellyfin-format relayout of a movie library. DRY RUN ONLY: nothing moves.
|
||||||
|
|
||||||
|
media-plan.py [--root DIR] [--enum TSV] [--probed TSV] [--plex TSV]
|
||||||
|
[--out TSV] [--exceptions TXT]
|
||||||
|
|
||||||
|
Inputs are the TSVs scripts/media-census.py exports, plus the optional Plex
|
||||||
|
metadata-cache export whose clean titles outrank the filename parser; the title
|
||||||
|
authority for everything Plex never matched is scripts/media-titles.py beside this
|
||||||
|
file. No media file is opened, so planning the whole library costs seconds.
|
||||||
|
|
||||||
|
PLAN is a TSV the executor (scripts/media-apply.py) replays verbatim —
|
||||||
|
`action<TAB>src<TAB>dst<TAB>kind` with action in mkdir | move | keep, ordered so
|
||||||
|
parents exist before children. EXCEPTIONS is the human review list: every file the
|
||||||
|
plan could not name a film for, every collision it refused, the READMEs the
|
||||||
|
executor must write into the piles it created.
|
||||||
|
|
||||||
|
Target contract (jellyfin.org/docs/general/server/media/movies):
|
||||||
|
* one folder per movie, named Title (year)
|
||||||
|
* the video inside is named exactly like the folder
|
||||||
|
* extra copies of one film are versions: "<base> - <label>.ext"; the base must
|
||||||
|
match the folder name character-for-character or Jellyfin splits them apart
|
||||||
|
* extras live in canonical subfolders (trailers, featurettes, deleted scenes,
|
||||||
|
interviews, samples, shorts, clips, other, extras, backdrops, theme-music)
|
||||||
|
* sidecars <base>.<lang>.<flag>.<ext>; images <base>-<poster|backdrop|...>
|
||||||
|
* <>:"/\|?* break the matcher, so they never survive here
|
||||||
|
* VIDEO_TS/BDMV folders support neither multiple versions nor parts
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import collections
|
||||||
|
import csv
|
||||||
|
import importlib.util
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
# Inputs are the census TSVs scripts/media-census.py writes; the engine beside
|
||||||
|
# this file is the title authority. Nothing here opens a media file.
|
||||||
|
def parse_args():
|
||||||
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||||
|
ap.add_argument("--root", default=os.environ.get("MEDIA_ROOT", "/mnt/entropy/Movies"),
|
||||||
|
help="library root the plan moves files inside")
|
||||||
|
ap.add_argument("--enum", default="/tmp/media-enum.tsv", help="file census TSV")
|
||||||
|
ap.add_argument("--probed", default="/tmp/media-probed.tsv", help="ffprobe census TSV")
|
||||||
|
ap.add_argument("--plex", default="/tmp/plex-movies.tsv",
|
||||||
|
help="Plex metadata export (clean titles); absent = parser only")
|
||||||
|
ap.add_argument("--out", default="/tmp/jf-plan.tsv")
|
||||||
|
ap.add_argument("--exceptions", default="/tmp/jf-exceptions.txt")
|
||||||
|
return ap.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
ARGS = parse_args()
|
||||||
|
ROOT = ARGS.root
|
||||||
|
ENUM, PROBE, PLEX = ARGS.enum, ARGS.probed, ARGS.plex
|
||||||
|
PLAN, EXCEPTS = ARGS.out, ARGS.exceptions
|
||||||
|
ENGINE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "media-titles.py")
|
||||||
|
|
||||||
|
spec = importlib.util.spec_from_file_location("eng", ENGINE)
|
||||||
|
eng = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(eng)
|
||||||
|
|
||||||
|
VIDEO_EXT = {".mkv", ".mp4", ".avi", ".m4v", ".mov", ".ts", ".m2ts", ".mpg",
|
||||||
|
".mpeg", ".webm", ".wmv", ".divx", ".ogm", ".flv", ".vob", ".iso",
|
||||||
|
".img", ".mpv", ".3gp", ".rm", ".rmvb", ".m2v", ".mpv"}
|
||||||
|
DISC_DIRS = {"BDMV", "VIDEO_TS", "CERTIFICATE", "AACS", "MLPI", "CLIPINF"}
|
||||||
|
SUB_EXT = {".srt", ".ass", ".ssa", ".sub", ".vtt"}
|
||||||
|
IMG_EXT = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif", ".tbn"}
|
||||||
|
AUDIO_DIR_RE = re.compile(r"soundtrack|score|\bost\b|music|song|album", re.I)
|
||||||
|
SUB_KEEP_RE = re.compile(r"^(?!.*\d)(?!.*[._])(?!.*\[)(?!.*\()([A-Za-z][A-Za-z -]{1,24})$")
|
||||||
|
MUSIC_TOP_RE = re.compile(r"(?i)soundtrack|score|\bost\b|music|album|song")
|
||||||
|
SUB_DIR_OK = re.compile(r"(?i)^$|subs?|subtitles?|srt|custom ?subs?(\[.*\])?"
|
||||||
|
r"|(additional|extra) languages|languages|resources")
|
||||||
|
AUDIO_DIR_OK = re.compile(r"(?i)^$|audio(s)?|extra audio|additional audio"
|
||||||
|
r"|(additional|extra) languages|commentar(y|ies)"
|
||||||
|
r"|audio commentary|audio tracks?")
|
||||||
|
|
||||||
|
|
||||||
|
AUDIO_EXT = {".mp3", ".flac", ".m4a", ".aac", ".dts", ".wav", ".ac3", ".mka",
|
||||||
|
".opus", ".ogg", ".wma"}
|
||||||
|
NFO_EXT = {".nfo"}
|
||||||
|
IMG_PRIMARY = {"poster", "folder", "cover", "default", "movie", "show"}
|
||||||
|
IMG_OTHER = {"backdrop": "backdrop", "fanart": "backdrop", "background": "backdrop",
|
||||||
|
"banner": "banner", "logo": "logo", "clearlogo": "logo",
|
||||||
|
"thumb": "thumb", "landscape": "thumb", "art": "backdrop",
|
||||||
|
"clearart": "backdrop", "jacket": "poster", "cdart": "poster",
|
||||||
|
"discart": "poster", "disc": "poster"}
|
||||||
|
RES_RE = re.compile(r"\b(2160|4320|4k|1080|720|576|480|360|240)(i|p)\b", re.I)
|
||||||
|
# The characters Jellyfin's matcher cannot carry in a file or folder name. "/" is
|
||||||
|
# one of them and it is the dangerous one: Plex titles KEEP it ("V/H/S/2", and this
|
||||||
|
# library holds six of those films), so leaving it in a name turns ONE movie folder
|
||||||
|
# into a nested tree — Movies/V/H/S/2 (2013)/… — and Jellyfin then sees four
|
||||||
|
# folders, none of them a movie.
|
||||||
|
BAD_CHARS = re.compile(r'[<>:"/|?*]')
|
||||||
|
PART_RE = re.compile(r"(?:^|[\s._-])(cd|dvd|part|pt|disc|disk|disco)"
|
||||||
|
r"[\s._-]?([1-9abcd])(?:$|[\s._-])", re.I)
|
||||||
|
LANGS = {"en", "eng", "it", "ita", "ja", "jpn", "de", "deu", "fr", "fra", "es",
|
||||||
|
"spa", "pt", "por", "ru", "rus", "zh", "chi", "ko", "kor", "sv", "no",
|
||||||
|
"da", "nl", "pl", "cs", "hu", "fi", "tr", "el", "he", "ar", "uk", "ro",
|
||||||
|
"bg", "hr", "sr", "sk", "et", "lv", "lt", "id", "th", "vi", "fa", "la"}
|
||||||
|
FLAGS = {"default", "forced", "foreign", "sdh", "cc"}
|
||||||
|
NOISE = {"sub", "subs", "subtitle", "subtitles", "srt", "ass", "ext", "external",
|
||||||
|
"the", "a", "an", "of", "and", "in", "on", "to", "for", "with", "ita",
|
||||||
|
"eng", "english", "italian", "dual", "audio", "forced", "default"}
|
||||||
|
# Canonical title where the library only ever carries a localised name.
|
||||||
|
OVERRIDES = {"28 giorni dopo": "28 Days Later",
|
||||||
|
"28 settimane dopo": "28 Weeks Later"}
|
||||||
|
|
||||||
|
rows = []
|
||||||
|
for line in open(ENUM):
|
||||||
|
size, ext, path = line.rstrip("\n").split("\t")
|
||||||
|
rows.append({"bytes": int(size), "ext": ext.lower(), "path": path})
|
||||||
|
ext_by = {r["path"]: r["ext"] for r in rows}
|
||||||
|
size_by = {r["path"]: r["bytes"] for r in rows}
|
||||||
|
|
||||||
|
|
||||||
|
def rel(p):
|
||||||
|
return p[len(ROOT) + 1:] if p.startswith(ROOT + "/") else p
|
||||||
|
|
||||||
|
|
||||||
|
def parts_of(p):
|
||||||
|
return [q for q in rel(p).split("/") if q]
|
||||||
|
|
||||||
|
|
||||||
|
def is_qb(p):
|
||||||
|
return ".!q" in p.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def disc_root(p, fold=True):
|
||||||
|
"""Absolute root of the disc tree containing p (the MOVIE folder that holds
|
||||||
|
BDMV/ or VIDEO_TS/), else None. `fold` also folds bookkeeping dirs that sit
|
||||||
|
beside the tree (CERTIFICATE/, AACS/) into the same movie folder."""
|
||||||
|
ps = parts_of(p)
|
||||||
|
kinds = DISC_DIRS if fold else {"BDMV", "VIDEO_TS"}
|
||||||
|
for i, q in enumerate(ps):
|
||||||
|
if q.upper() in kinds:
|
||||||
|
return ROOT + "/" + "/".join(ps[:i])
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def row_name(r):
|
||||||
|
"""The name a disc-tree entry should be identified by: the engine stores the
|
||||||
|
disc's own folder for it, because "BDMV/index.bdmv" is not a title."""
|
||||||
|
return r.get("name_src") or r["path"]
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize(name):
|
||||||
|
name = BAD_CHARS.sub("-", name).replace("\\", "-")
|
||||||
|
name = re.sub(r"\s+", " ", name).strip().strip(". ").strip()
|
||||||
|
return (name[:180] or "untitled")
|
||||||
|
|
||||||
|
|
||||||
|
def ok_year(y):
|
||||||
|
"""True for a year a real film could carry. Plex stores 0 for a file it indexed
|
||||||
|
but never MATCHED, and "0" is a digit string: trusting it named 67 movie
|
||||||
|
folders "Title (0)" — Jellyfin items with no identity, no year and no poster."""
|
||||||
|
return bool(re.fullmatch(r"(18|19|20)\d\d", y or "")) and 1870 <= int(y) <= 2035
|
||||||
|
|
||||||
|
|
||||||
|
WORD_SPLIT = re.compile(r"[^0-9a-z]+")
|
||||||
|
|
||||||
|
|
||||||
|
def words_of(text):
|
||||||
|
"""The matching words of a name — or of a whole path, which is how callers
|
||||||
|
pass a file together with the folders it sits in.
|
||||||
|
|
||||||
|
Split on everything that is not a letter or a digit: eng.norm does not touch
|
||||||
|
"/", so feeding it "…/Psycho (1960)/Extras" left glued tokens like
|
||||||
|
"(1960)/extras" and "(1954)" that no other name could ever agree with, and a
|
||||||
|
film could not be found by the folder that holds it — which is how one box
|
||||||
|
set's featurettes ended up in another film's folder. Both sides of every
|
||||||
|
comparison below go through here. A bare number counts only when it could be
|
||||||
|
a release year, so release junk ("1080", "2160", "2", "7") never poses as
|
||||||
|
agreement between two names.
|
||||||
|
"""
|
||||||
|
out = set()
|
||||||
|
for w in WORD_SPLIT.split(eng.norm(text)):
|
||||||
|
if not w or w in eng.STOP:
|
||||||
|
continue
|
||||||
|
if w.isdigit() and not 1870 <= int(w) <= 2100:
|
||||||
|
continue
|
||||||
|
out.add(w)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ Plex titles
|
||||||
|
# Plex has matched a large part of the library's media files to real titles
|
||||||
|
# ("Alien", not "Alien.Directors.Cut.1979.1080p.BRrip.x265…"). Where the cache
|
||||||
|
# holds a MATCH, its title wins; the filename parser carries everything else
|
||||||
|
# (extras, disc trees, TV, and whatever Plex never matched).
|
||||||
|
def plex_titles():
|
||||||
|
if not os.path.exists(PLEX):
|
||||||
|
return {}
|
||||||
|
out = {}
|
||||||
|
with open(PLEX, newline="") as fh:
|
||||||
|
for r in csv.DictReader(fh, delimiter="\t"):
|
||||||
|
t, y = r["leaf_title"].strip(), r["leaf_year"].strip()
|
||||||
|
# Only a match is an authority, and a match carries a year: an
|
||||||
|
# unmatched row echoes the filename, which the parser reads better
|
||||||
|
# than a copied release name. The junk filter stays for the matches
|
||||||
|
# whose title still carries the release.
|
||||||
|
if t and ok_year(y) and not re.search(
|
||||||
|
r"2160p|1080p|x26[45]|hevc|bluray|web-?dl", t.lower()):
|
||||||
|
out[r["file"]] = (t, y)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
PLEX_T = plex_titles()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------- classification
|
||||||
|
erows = eng.load(PROBE)
|
||||||
|
for r in erows:
|
||||||
|
for k, v in OVERRIDES.items():
|
||||||
|
if k in r["title"]:
|
||||||
|
r["title"] = v
|
||||||
|
r["key"] = eng.key_of(r["title"])
|
||||||
|
r["base"] = eng.base_words(r["title"], r["key"])
|
||||||
|
marks = {r["path"] for r in erows if eng.is_watermark(r["path"], r["bytes"])}
|
||||||
|
extras = {r["path"] for r in erows
|
||||||
|
if r["path"] not in marks
|
||||||
|
and eng.is_extra(r["path"], r["title"], r["dur"], ROOT,
|
||||||
|
matched=r["path"] in PLEX_T)}
|
||||||
|
eps = collections.defaultdict(list)
|
||||||
|
for r in erows:
|
||||||
|
if r["path"] in marks | extras:
|
||||||
|
continue
|
||||||
|
ep = eng.ep_of(r["path"])
|
||||||
|
if ep:
|
||||||
|
eps[ep[0]].append(r) # keyed by the SERIES, never the episode title
|
||||||
|
tv_keys = {k for k, v in eps.items() if len(v) >= 5}
|
||||||
|
tv_set = {r["path"] for k, v in eps.items() if k in tv_keys for r in v}
|
||||||
|
# A folder INSIDE a movie's folder is bonus territory even when its own name is
|
||||||
|
# not bonus vocabulary: "Saboteur (1942)/Exrtas/Saboteur - A Closer Look" is a
|
||||||
|
# misspelled Extras folder, so the engine's folder rule never fired and the
|
||||||
|
# featurette became a movie folder of its own — whose words then out-scored the
|
||||||
|
# real Saboteur and drew that box set's other extras into it. The rule needs no
|
||||||
|
# metadata: the folder holding the longest film owns the shorter video filed
|
||||||
|
# under it, provided that shorter file's own name speaks the film's name — a
|
||||||
|
# whole other film filed there stays a film, exactly as the engine's folder rule
|
||||||
|
# exempts it. A different year in the file's own name IS that other film
|
||||||
|
# speaking, so it vetoes the rule.
|
||||||
|
def longest_of(rows):
|
||||||
|
"""The longest video in each folder: that folder's feature."""
|
||||||
|
out = {}
|
||||||
|
for r in rows:
|
||||||
|
if r["dur"] > 0:
|
||||||
|
d = os.path.dirname(r["path"])
|
||||||
|
if d not in out or r["dur"] > out[d]["dur"]:
|
||||||
|
out[d] = r
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def not_disc(r):
|
||||||
|
"""A disc tree's innards belong to the tree, never to the folder's film."""
|
||||||
|
return not any(c in DISC_DIRS for c in r["path"].split("/"))
|
||||||
|
|
||||||
|
|
||||||
|
FEATURE_OF = longest_of([r for r in erows if r["path"] not in marks and not_disc(r)])
|
||||||
|
|
||||||
|
|
||||||
|
def under_a_feature(path, dur):
|
||||||
|
"""True when a folder above this file holds a longer film that this file's
|
||||||
|
own name names."""
|
||||||
|
d = os.path.dirname(os.path.dirname(path))
|
||||||
|
while len(d) > len(ROOT):
|
||||||
|
f = FEATURE_OF.get(d)
|
||||||
|
if f:
|
||||||
|
if f["dur"] < dur:
|
||||||
|
return False
|
||||||
|
theirs = (words_of(os.path.basename(f["path"]))
|
||||||
|
| words_of(os.path.basename(d)))
|
||||||
|
if not words_of(os.path.basename(path)) & theirs:
|
||||||
|
return False
|
||||||
|
y1 = re.search(r"(?:18|19|20)\d\d", os.path.basename(path))
|
||||||
|
y2 = re.search(r"(?:18|19|20)\d\d", os.path.basename(f["path"]))
|
||||||
|
return not (y1 and y2 and y1.group() != y2.group())
|
||||||
|
d = os.path.dirname(d)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
extras |= {r["path"] for r in erows
|
||||||
|
if r["dur"] > 0 and r["path"] not in marks | extras | tv_set
|
||||||
|
and not_disc(r) and under_a_feature(r["path"], r["dur"])}
|
||||||
|
film_rows = [r for r in erows if r["path"] not in marks | extras | tv_set]
|
||||||
|
|
||||||
|
buckets = collections.defaultdict(list)
|
||||||
|
for r in film_rows:
|
||||||
|
buckets[eng.key_of(r["title"])].append(r)
|
||||||
|
|
||||||
|
|
||||||
|
def res_label(r):
|
||||||
|
m = RES_RE.search(r["res"] or "")
|
||||||
|
if m:
|
||||||
|
n, kind = m.group(1).lower(), m.group(2).lower()
|
||||||
|
return "2160p" if n in ("2160", "4k") else f"{n}{kind}"
|
||||||
|
try:
|
||||||
|
w = int((r["res"] or "0x0").split("x")[0])
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
w = 0
|
||||||
|
return "2160p" if w >= 3000 else ("1080p" if w >= 1400 else "SD")
|
||||||
|
|
||||||
|
|
||||||
|
def version_label(r):
|
||||||
|
bits = [res_label(r)]
|
||||||
|
vc = (r["vcodec"] or "").lower()
|
||||||
|
if vc in ("av1", "libaom", "libsvtav1"):
|
||||||
|
bits.append("AV1")
|
||||||
|
elif vc in ("vc1", "wmv3"):
|
||||||
|
bits.append("VC1")
|
||||||
|
if r["bytes"] > 40 * 2**30:
|
||||||
|
bits.append("REMUX")
|
||||||
|
if "imax" in r["path"].lower():
|
||||||
|
bits.append("IMAX")
|
||||||
|
return sanitize(" ".join(bits))
|
||||||
|
|
||||||
|
|
||||||
|
def group_name(items):
|
||||||
|
titles = collections.Counter(r["title"] for r in items)
|
||||||
|
ascii_t = [t for t in titles if t.isascii()]
|
||||||
|
title = (collections.Counter(ascii_t) if ascii_t else titles).most_common(1)[0][0]
|
||||||
|
years = [r["year"] for r in items if r["year"] != "?"] or \
|
||||||
|
[r["soft"] for r in items if r["soft"]]
|
||||||
|
year = collections.Counter(years).most_common(1)[0][0] if years else None
|
||||||
|
return sanitize(f"{title} ({year})" if year else title)
|
||||||
|
|
||||||
|
|
||||||
|
groups = []
|
||||||
|
for key, items in buckets.items():
|
||||||
|
for c in eng.dur_clusters(items):
|
||||||
|
g = {"rows": c, "base": group_name(c)}
|
||||||
|
known = [PLEX_T[r["path"]] for r in c if r["path"] in PLEX_T]
|
||||||
|
if known:
|
||||||
|
# one film, one name: a Plex match for any copy names every copy
|
||||||
|
t, yr = collections.Counter(known).most_common(1)[0][0]
|
||||||
|
g["plex"] = t
|
||||||
|
g["base"] = sanitize(f"{t} ({yr})" if ok_year(yr) else t)
|
||||||
|
groups.append(g)
|
||||||
|
clash = {b for b, n in collections.Counter(g["base"] for g in groups).items() if n > 1}
|
||||||
|
for g in groups:
|
||||||
|
if g["base"] in clash:
|
||||||
|
# two genuinely different films share title+year: separate folders, and
|
||||||
|
# say so in the name, because Jellyfin can only match one item per folder
|
||||||
|
g["base"] = sanitize(g["base"] + " - " + version_label(g["rows"][0]))
|
||||||
|
g["flag"] = "same title and year as another film"
|
||||||
|
|
||||||
|
moves, mkdirs, problems, readmes = [], set(), [], []
|
||||||
|
# --------------------------------------------------------------- unnamed groups
|
||||||
|
# A movie folder has to READ as a title. The engine's runtime veto says any file
|
||||||
|
# over 45 minutes that is not named like an extra is a film whatever it is called
|
||||||
|
# — true for Jellyfin, but a 118-minute documentary named "Extra 1" then produced
|
||||||
|
# a movie folder named "1", and folders named "1", "2", "3" are Jellyfin items
|
||||||
|
# with no identity whose numeric keys went on to capture every subtitle named
|
||||||
|
# "3_Eng.srt" in the library. A year proves a dated feature ("300 (2006)" is a
|
||||||
|
# real film), so the gate is: a year, or a word that is not release noise. With
|
||||||
|
# neither, the rows are bonus material until a human names them.
|
||||||
|
def named(g):
|
||||||
|
if re.search(r"\(\d{4}\)$", g["base"]):
|
||||||
|
return True
|
||||||
|
return any(w.isalpha() and len(w) >= 2 for w in eng.name_words(g["base"]))
|
||||||
|
|
||||||
|
|
||||||
|
unnamed = [g for g in groups if not named(g)]
|
||||||
|
groups = [g for g in groups if named(g)]
|
||||||
|
for g in unnamed:
|
||||||
|
for r in g["rows"]:
|
||||||
|
extras.add(r["path"])
|
||||||
|
problems.append(
|
||||||
|
f"UNNAMED film {rel(r['path'])} parses to {g['base']!r} with no year: "
|
||||||
|
f"bonus pile, not a folder — give the file a real name and re-plan")
|
||||||
|
|
||||||
|
|
||||||
|
group_of = {}
|
||||||
|
for g in groups:
|
||||||
|
g["dir"] = f"{ROOT}/{g['base']}"
|
||||||
|
for r in g["rows"]:
|
||||||
|
group_of[r["path"]] = g
|
||||||
|
dr = disc_root(r["path"])
|
||||||
|
if dr:
|
||||||
|
group_of[dr] = g
|
||||||
|
by_path = {r["path"]: r for r in erows}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------- bookkeeping
|
||||||
|
claimed = {}
|
||||||
|
existing = set(rows_map := {r["path"] for r in rows})
|
||||||
|
|
||||||
|
|
||||||
|
at_target, booked_src = set(), set()
|
||||||
|
|
||||||
|
|
||||||
|
def book(src, dst, kind):
|
||||||
|
if src == dst:
|
||||||
|
at_target.add(src)
|
||||||
|
booked_src.add(src)
|
||||||
|
return
|
||||||
|
if os.environ.get("JF_TRACE") and (src in booked_src or dst.lower() in claimed):
|
||||||
|
why = "rebook" if src in booked_src else "collision"
|
||||||
|
problems.append(f"TRACE {why} kind={kind} src={rel(src)} dst={rel(dst)}")
|
||||||
|
if src in booked_src: # one source may only ever move once
|
||||||
|
problems.append(f"RE-BOOK {rel(src)} -> {rel(dst)} (already planned)")
|
||||||
|
return
|
||||||
|
if dst.lower() in claimed:
|
||||||
|
problems.append(f"COLLISION {rel(dst)}\n from {rel(src)}\n and {rel(claimed[dst.lower()])}")
|
||||||
|
return
|
||||||
|
claimed[dst.lower()] = src
|
||||||
|
booked_src.add(src)
|
||||||
|
comp = dst.split("/")
|
||||||
|
for i in range(1, len(comp)):
|
||||||
|
mkdirs.add("/".join(comp[:i]))
|
||||||
|
moves.append((src, dst, kind))
|
||||||
|
|
||||||
|
|
||||||
|
def uniq_dir(d, stem, ext, hint=""):
|
||||||
|
"""A destination that no other source already took."""
|
||||||
|
cand = f"{d}/{stem}{ext}"
|
||||||
|
if cand.lower() not in claimed:
|
||||||
|
return cand
|
||||||
|
n = 2
|
||||||
|
while True:
|
||||||
|
cand = f"{d}/{stem} {n}{ext}"
|
||||||
|
if cand.lower() not in claimed:
|
||||||
|
return cand
|
||||||
|
n += 1
|
||||||
|
|
||||||
|
|
||||||
|
def media_dir(p):
|
||||||
|
dr = disc_root(p)
|
||||||
|
return os.path.dirname(dr) if dr else os.path.dirname(p)
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ films
|
||||||
|
# film / disc moves: the largest copy carries the folder's own name, the rest
|
||||||
|
# become Jellyfin versions; a disc tree moves as a whole into the movie folder
|
||||||
|
|
||||||
|
|
||||||
|
def vext(p):
|
||||||
|
return ext_by.get(p) or os.path.splitext(p)[1].lower()
|
||||||
|
disc_children = collections.defaultdict(list)
|
||||||
|
for q in rows:
|
||||||
|
dr = disc_root(q["path"])
|
||||||
|
if dr:
|
||||||
|
disc_children[dr].append(q["path"])
|
||||||
|
for g in groups:
|
||||||
|
primary_taken = False
|
||||||
|
disc_kinds = set()
|
||||||
|
for r in sorted(g["rows"], key=lambda r: -r["bytes"]):
|
||||||
|
dr = disc_root(r["path"])
|
||||||
|
if dr:
|
||||||
|
inner = os.path.basename(dr).upper()
|
||||||
|
if inner in disc_kinds:
|
||||||
|
problems.append(
|
||||||
|
f"DISC-VERSION {g['base']}: extra disc tree {rel(dr)} cannot "
|
||||||
|
f"share the folder — discs have no multiple versions")
|
||||||
|
continue
|
||||||
|
disc_kinds.add(inner)
|
||||||
|
for src in disc_children[dr]:
|
||||||
|
tail = os.path.relpath(src, dr)
|
||||||
|
fold = disc_root(src, fold=False)
|
||||||
|
if fold != dr and os.path.dirname(src) != dr:
|
||||||
|
tail = os.path.basename(dr) + "/" + tail
|
||||||
|
book(src, f"{g['dir']}/{tail}", "disc-file")
|
||||||
|
continue
|
||||||
|
label = "" if not primary_taken else f" - {version_label(r)}"
|
||||||
|
dst = f"{g['dir']}/{g['base']}{label}{vext(r['path'])}"
|
||||||
|
n = 2
|
||||||
|
while dst.lower() in claimed:
|
||||||
|
dst = f"{g['dir']}/{g['base']}{label} {n}{vext(r['path'])}"
|
||||||
|
n += 1
|
||||||
|
r["dst"] = dst
|
||||||
|
primary_taken = True
|
||||||
|
book(r["path"], dst, "film")
|
||||||
|
g["primary"] = primary_taken
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------ extras
|
||||||
|
EXTRA_KIND = [("trailers", r"trailer|teaser|preview|promo|tv spot|advert"),
|
||||||
|
("deleted scenes", r"deleted|alt scene|gag reel|blooper|outtake"),
|
||||||
|
("interviews", r"interview|cast and crew|session piece|commentary"),
|
||||||
|
("featurettes", r"featurette|behind the scenes|making of|documentary"
|
||||||
|
r"|heritage|archival|video archive|music video"),
|
||||||
|
("samples", r"sample"),
|
||||||
|
("shorts", r"\bshort\b|short film"),
|
||||||
|
("clips", r"\bclip\b|scene selection|montage|epilogue|isolated score"),
|
||||||
|
("other", r"menu|logo|gallery|still|trivia|storyboard|screen test"
|
||||||
|
r"|audition|test footage|watermark|workprint|poster"
|
||||||
|
r"|picture in picture|singalong|karaoke|original score")]
|
||||||
|
THEME_RE = re.compile(r"theme|opening song|ending song|soundtrack|score"
|
||||||
|
r"|\bost\b|music|song", re.I)
|
||||||
|
BACKDROP_RE = re.compile(r"backdrop|intro", re.I)
|
||||||
|
|
||||||
|
|
||||||
|
def extras_kind(path):
|
||||||
|
low = path.lower()
|
||||||
|
if THEME_RE.search(low):
|
||||||
|
return "theme-music"
|
||||||
|
if BACKDROP_RE.search(low):
|
||||||
|
return "backdrops"
|
||||||
|
for name, rx in EXTRA_KIND:
|
||||||
|
if re.search(rx, low):
|
||||||
|
return name
|
||||||
|
return "extras"
|
||||||
|
|
||||||
|
|
||||||
|
def attachable_title(path):
|
||||||
|
t, _y = eng.title_of(path)
|
||||||
|
return eng.key_of(t) if t and not eng.is_watermark(path, size_by.get(path, 0)) else None
|
||||||
|
|
||||||
|
|
||||||
|
for g in groups:
|
||||||
|
g["tops"] = {parts_of(r["path"])[0] for r in g["rows"]}
|
||||||
|
top_groups = collections.defaultdict(list)
|
||||||
|
for g in groups:
|
||||||
|
for t in g["tops"]:
|
||||||
|
top_groups[t].append(g)
|
||||||
|
group_by_key = collections.defaultdict(list)
|
||||||
|
for g in groups:
|
||||||
|
for r in g["rows"]:
|
||||||
|
group_by_key[eng.key_of(r["title"])].append(g)
|
||||||
|
group_by_dir = {}
|
||||||
|
for g in groups:
|
||||||
|
for r in g["rows"]:
|
||||||
|
group_by_dir.setdefault(media_dir(r["path"]), g)
|
||||||
|
dr = disc_root(r["path"])
|
||||||
|
if dr:
|
||||||
|
group_by_dir.setdefault(os.path.dirname(dr), g)
|
||||||
|
|
||||||
|
|
||||||
|
def group_words(g):
|
||||||
|
"""The words that identify a film group: its titles and the names of the
|
||||||
|
folders its files live in."""
|
||||||
|
if "words" not in g:
|
||||||
|
w = set()
|
||||||
|
if g.get("plex"):
|
||||||
|
w |= words_of(g["plex"])
|
||||||
|
for r in g["rows"]:
|
||||||
|
w |= words_of(r["title"])
|
||||||
|
w |= words_of(os.path.basename(media_dir(r["path"])))
|
||||||
|
g["words"] = w
|
||||||
|
return g["words"]
|
||||||
|
|
||||||
|
|
||||||
|
def score_words(text, groups):
|
||||||
|
"""Ranks film groups by how many of their words a path carries."""
|
||||||
|
words = words_of(text)
|
||||||
|
out = [(len(words & group_words(g)), g) for g in groups]
|
||||||
|
out.sort(key=lambda x: -x[0])
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def group_for_orphan(path):
|
||||||
|
"""Which movie folder an extra/sidecar belongs to. A collection folder holding
|
||||||
|
several films must be resolved by WORDS, never by position: taking the first
|
||||||
|
film of the folder attached every loose subtitle in a box set to whichever
|
||||||
|
movie happened to sort first."""
|
||||||
|
stem = os.path.splitext(os.path.basename(path))[0]
|
||||||
|
key = attachable_title(path)
|
||||||
|
if key and group_by_key.get(key):
|
||||||
|
cands = group_by_key[key]
|
||||||
|
top = parts_of(path)[0] if len(parts_of(path)) > 1 else ""
|
||||||
|
same_top = [g for g in cands if any(parts_of(r["path"])[0] == top
|
||||||
|
for r in g["rows"] if len(parts_of(r["path"])) > 1)]
|
||||||
|
pool = same_top or cands
|
||||||
|
return pool[0] if len(pool) == 1 else score_words(stem, pool)[0][1]
|
||||||
|
gtop = parts_of(path)[0] if len(parts_of(path)) > 1 else ""
|
||||||
|
pool = top_groups.get(gtop, [])
|
||||||
|
if len(pool) > 1: # a loose collection folder (box set, bonus
|
||||||
|
n, g = score_words(stem + " " + os.path.dirname(path), pool)[0]
|
||||||
|
return g if n >= 2 else None # album) is resolved by words, not position
|
||||||
|
if len(pool) == 1:
|
||||||
|
return pool[0]
|
||||||
|
d = os.path.dirname(path)
|
||||||
|
while d and d != ROOT:
|
||||||
|
if d in group_by_dir:
|
||||||
|
return group_by_dir[d]
|
||||||
|
d = os.path.dirname(d)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
tv_tops = {parts_of(p)[0] for p in tv_set}
|
||||||
|
def film_folder_for(path):
|
||||||
|
"""A movie folder for a lone feature that has no group: derived from its own
|
||||||
|
title when a year proves it, else None (goes to the review pile)."""
|
||||||
|
t, y = eng.title_of(path)
|
||||||
|
if not t or y == "?":
|
||||||
|
return None
|
||||||
|
d = uniq_dir(ROOT, sanitize(f"{t} ({y})"), "", "")
|
||||||
|
mkdirs.add(d)
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
for p in sorted(extras):
|
||||||
|
if disc_root(p):
|
||||||
|
continue
|
||||||
|
if len(parts_of(p)) > 1 and parts_of(p)[0] in tv_tops:
|
||||||
|
continue # a special inside a TV set: staged with it
|
||||||
|
g = group_for_orphan(p)
|
||||||
|
ext = ext_by.get(p, os.path.splitext(p)[1].lower())
|
||||||
|
stem = os.path.splitext(os.path.basename(p))[0]
|
||||||
|
if g is None:
|
||||||
|
solo = film_folder_for(p)
|
||||||
|
if solo:
|
||||||
|
dst = f"{solo}/{sanitize(stem)}{ext}"
|
||||||
|
problems.append(f"REVIEW lone feature {rel(p)}: gets its own folder "
|
||||||
|
f"{os.path.basename(solo)}")
|
||||||
|
else:
|
||||||
|
dst = f"{ROOT}/_unsorted bonus/{extras_kind(p)}/{sanitize(stem)}{ext}"
|
||||||
|
problems.append(f"NO-FILM extra {rel(p)}")
|
||||||
|
else:
|
||||||
|
dst = f"{g['dir']}/{extras_kind(p)}/{sanitize(stem)}{ext}"
|
||||||
|
book(p, uniq_dir(os.path.dirname(dst), os.path.basename(dst)[:-(len(ext) or 1)],
|
||||||
|
ext, os.path.dirname(p)), "extra")
|
||||||
|
|
||||||
|
# Disc trees the engine never saw (a MakeMKV scratch folder, an AACS remnant):
|
||||||
|
# they belong to the film of the folder they sit in, kept as a named subtree.
|
||||||
|
for q in rows:
|
||||||
|
pth = q["path"]
|
||||||
|
dr = disc_root(pth)
|
||||||
|
if not dr or pth in by_path or pth in booked_src:
|
||||||
|
continue
|
||||||
|
g = group_by_dir.get(dr) or (top_groups[parts_of(pth)[0]][0]
|
||||||
|
if len(top_groups.get(parts_of(pth)[0], [])) == 1 else None)
|
||||||
|
if g is None:
|
||||||
|
continue
|
||||||
|
inner = os.path.basename(dr)
|
||||||
|
problems.append(f"REVIEW disc remnant {rel(dr)} moves inside {g['base']} "
|
||||||
|
f"(a second master: compare before keeping both)")
|
||||||
|
for src in disc_children[dr]:
|
||||||
|
if src not in booked_src:
|
||||||
|
book(src, f"{g['dir']}/{inner}/{os.path.relpath(src, dr)}", "disc-file")
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------- sidecars
|
||||||
|
media_by_dir = collections.defaultdict(list)
|
||||||
|
for r in erows:
|
||||||
|
if r["path"] in marks:
|
||||||
|
continue
|
||||||
|
dst = r.get("dst")
|
||||||
|
if dst:
|
||||||
|
media_by_dir[media_dir(r["path"])].append(r)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def sidecar_target(path, e):
|
||||||
|
d = os.path.dirname(path)
|
||||||
|
stem = os.path.splitext(os.path.basename(path))[0]
|
||||||
|
if d == ROOT:
|
||||||
|
# A file loose in the library ROOT names no film. media_by_dir[ROOT] is the
|
||||||
|
# set of root-level MOVIE files, and scoring a stray name against it
|
||||||
|
# attached every root-level leftover to whichever root-level film was
|
||||||
|
# largest — the plan sent ".DS_Store" into "Titanic (1997)/other/" that
|
||||||
|
# way. A root-level stray is nobody's sidecar: it goes to the review pile.
|
||||||
|
return None, None
|
||||||
|
cands = media_by_dir.get(d, [])
|
||||||
|
if not cands:
|
||||||
|
nd = os.path.dirname(d) # a subs/ or Extras/ folder beside the media
|
||||||
|
while nd and nd != ROOT and not media_by_dir.get(nd):
|
||||||
|
nd = os.path.dirname(nd)
|
||||||
|
if media_by_dir.get(nd):
|
||||||
|
if nd != d: # a NAMED subfolder (Subs/, Extras/, the
|
||||||
|
g = group_for_orphan(path) # box set's own root) carries evidence
|
||||||
|
dirs = {os.path.dirname(r.get("dst", "")) for r in media_by_dir[nd]}
|
||||||
|
if g is None or g["dir"] not in dirs:
|
||||||
|
# ...but only a STRONG word match outranks the folder the file
|
||||||
|
# sits in. A weak one is an accident: "3_Eng.srt" scored 1
|
||||||
|
# against a junk group named "3" and escaped its own movie's
|
||||||
|
# Subs/ folder, while "03 xXx 3 Return Of Xander Cage" scores 4
|
||||||
|
# and genuinely belongs to the other film of the set.
|
||||||
|
n = score_words(stem, [g])[0][0] if g else 0
|
||||||
|
if n >= 2:
|
||||||
|
return (g, None)
|
||||||
|
cands = media_by_dir[nd]
|
||||||
|
if not cands:
|
||||||
|
g = group_for_orphan(path)
|
||||||
|
return (g, None) if g else (None, None)
|
||||||
|
if len(cands) > 1:
|
||||||
|
w = [t for t in eng.norm(stem).split() if t not in NOISE]
|
||||||
|
cands = sorted(cands, key=lambda r: (
|
||||||
|
-len([t for t in eng.norm(os.path.basename(r["path"])).split() if t in w]),
|
||||||
|
-r["bytes"]))
|
||||||
|
return (None, cands[0])
|
||||||
|
|
||||||
|
|
||||||
|
for q in rows:
|
||||||
|
p, e = q["path"], q["ext"]
|
||||||
|
if p in by_path or p in extras or p in marks or is_qb(p) or disc_root(p):
|
||||||
|
continue
|
||||||
|
if os.path.basename(p).startswith("._"):
|
||||||
|
# macOS AppleDouble resource forks: a few hundred bytes carrying a name
|
||||||
|
# COPIED FROM ANOTHER FILM. Word-matching them to "their" film scattered
|
||||||
|
# dead files through four movie folders (and they are video extensions in
|
||||||
|
# the AppleDouble case, so they also skipped every other filter). They
|
||||||
|
# claim nothing: one pile, and they stay in it — nothing is deleted here.
|
||||||
|
book(p, uniq_dir(f"{ROOT}/_macos sidecars", sanitize(os.path.basename(p))[:120],
|
||||||
|
"", os.path.dirname(p)), "junk")
|
||||||
|
problems.append(f"JUNK AppleDouble sidecar {rel(p)} (macOS resource fork)")
|
||||||
|
continue
|
||||||
|
if e == ".iso": # a DVD image: Jellyfin plays it as a movie file
|
||||||
|
g = group_for_orphan(p)
|
||||||
|
stem = os.path.splitext(os.path.basename(p))[0]
|
||||||
|
if g is not None:
|
||||||
|
book(p, uniq_dir(g["dir"], g["base"], e, stem) if g.get("primary") is not None
|
||||||
|
else f"{g['dir']}/{g['base']}{e}", "iso")
|
||||||
|
else:
|
||||||
|
solo = film_folder_for(p)
|
||||||
|
if solo:
|
||||||
|
book(p, f"{solo}/{sanitize(stem)}.iso", "iso")
|
||||||
|
problems.append(f"REVIEW disc image {rel(p)}: DVD rip, alone in its "
|
||||||
|
f"folder -> {os.path.basename(solo)}")
|
||||||
|
else:
|
||||||
|
book(p, f"{ROOT}/_unsorted files/{sanitize(os.path.basename(p))}", "orphan")
|
||||||
|
problems.append(f"NO-FILM disc image {rel(p)}")
|
||||||
|
continue
|
||||||
|
if e in VIDEO_EXT:
|
||||||
|
# A video the engine never probed (unreadable header) and not an AppleDouble
|
||||||
|
# remnant: the plan does not guess at it, so it stays where it is.
|
||||||
|
continue
|
||||||
|
stem = os.path.splitext(os.path.basename(p))[0]
|
||||||
|
g, media = sidecar_target(p, e)
|
||||||
|
if media is not None:
|
||||||
|
base = os.path.splitext(os.path.basename(media["dst"]))[0]
|
||||||
|
d = os.path.dirname(media["dst"])
|
||||||
|
elif g is not None:
|
||||||
|
base, d = g["base"], g["dir"]
|
||||||
|
else:
|
||||||
|
# Nothing names this file's film. NEVER scatter it: an archive set or an
|
||||||
|
# album tree must move as the unit it is, so the whole loose folder goes
|
||||||
|
# to the review pile and a film folder's own leftovers stay with it.
|
||||||
|
ps = parts_of(p)
|
||||||
|
if len(ps) < 2:
|
||||||
|
dst = f"{ROOT}/_unsorted files/{sanitize(stem)}{e}"
|
||||||
|
elif len(top_groups.get(ps[0], [])):
|
||||||
|
dst = f"{top_groups[ps[0]][0]['dir']}/other/{'/'.join(sanitize(c) for c in ps[1:])}"
|
||||||
|
else:
|
||||||
|
dst = f"{ROOT}/_unsorted files/{'/'.join(sanitize(c) for c in ps)}"
|
||||||
|
book(dst=dst, src=p, kind="orphan")
|
||||||
|
problems.append(f"NO-OWNER sidecar {rel(p)}")
|
||||||
|
continue
|
||||||
|
if e in IMG_EXT:
|
||||||
|
low = eng.norm(stem)
|
||||||
|
word = next((w for w in IMG_PRIMARY if re.search(rf"\b{w}\b", low)), None)
|
||||||
|
kind = "poster" if word else next(
|
||||||
|
(IMG_OTHER[w] for w in IMG_OTHER if re.search(rf"\b{w}\b", low)), None)
|
||||||
|
if re.search(r"\btheme\b", low):
|
||||||
|
dst = f"{d}/theme{e}"
|
||||||
|
else:
|
||||||
|
dst = f"{d}/{base}-{kind or 'art'}{e}" if kind else f"{d}/{sanitize(stem)}{e}"
|
||||||
|
book(p, uniq_dir(os.path.dirname(dst), os.path.basename(dst)[:-(len(e) or 1)],
|
||||||
|
e, os.path.dirname(p)), "image")
|
||||||
|
elif e in NFO_EXT:
|
||||||
|
book(p, uniq_dir(d, base, ".nfo", os.path.dirname(p)), "nfo")
|
||||||
|
elif e in SUB_EXT | AUDIO_EXT:
|
||||||
|
toks = [t for t in re.split(r"[._\-\s]+", stem) if t]
|
||||||
|
langs = [t.lower() for t in toks if t.lower() in LANGS]
|
||||||
|
flags = [t.lower() for t in toks if t.lower() in FLAGS]
|
||||||
|
keep = [sanitize(t) for t in toks
|
||||||
|
if t.lower() not in langs + flags + list(NOISE)
|
||||||
|
and not re.fullmatch(r"\d{3,4}p?|cd\d+|\d{4}|\d+ch|\d+.\d", t.lower())]
|
||||||
|
bits = [base] + ([sanitize(" ".join(keep[:4]))] if keep else []) + langs + flags
|
||||||
|
if e in SUB_EXT:
|
||||||
|
# subtitles: <movie>/srt[/language]
|
||||||
|
d = f"{d}/srt"
|
||||||
|
pd = os.path.basename(os.path.dirname(p))
|
||||||
|
if SUB_KEEP_RE.match(pd) and not SUB_DIR_OK.match(pd):
|
||||||
|
d = f"{d}/{sanitize(pd)}" # a real language folder survives
|
||||||
|
elif e in AUDIO_EXT and AUDIO_DIR_RE.search(os.path.dirname(p)):
|
||||||
|
d = f"{d}/theme-music" # bonus audio: one canonical folder
|
||||||
|
pd = os.path.basename(os.path.dirname(p))
|
||||||
|
if not MUSIC_TOP_RE.search(pd): # an album folder keeps its name
|
||||||
|
d = f"{d}/{sanitize(pd)}"
|
||||||
|
else:
|
||||||
|
pd = os.path.basename(os.path.dirname(p))
|
||||||
|
if not AUDIO_DIR_OK.match(pd):
|
||||||
|
d = f"{d}/{sanitize(pd)}"
|
||||||
|
stem2 = ".".join(bits) or sanitize(os.path.basename(p))
|
||||||
|
book(p, uniq_dir(d, stem2, e, os.path.basename(p)), "track")
|
||||||
|
else:
|
||||||
|
book(p, uniq_dir(f"{d}/other", sanitize(os.path.basename(p))[:120], e,
|
||||||
|
os.path.dirname(p)), "misc")
|
||||||
|
|
||||||
|
readmes.append(f"README in {ROOT}: this library was relaid out into Jellyfin's "
|
||||||
|
"one-folder-per-movie layout; `_release-group watermarks/`, "
|
||||||
|
"`_unsorted files/` and `_unsorted bonus/` hold files no film claims.")
|
||||||
|
for key in tv_keys:
|
||||||
|
readmes.append(f"README in {ROOT}/{sanitize('_TV needs a TV library - ' + key)}: "
|
||||||
|
"these are TV episodes found in the MOVIES library; move the folder "
|
||||||
|
"to a TV library source, else Jellyfin groups them wrongly as one "
|
||||||
|
"movie.")
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------- TV
|
||||||
|
for key in tv_keys:
|
||||||
|
items = eps[key]
|
||||||
|
year = collections.Counter(r["year"] for r in items if r["year"] != "?")
|
||||||
|
yr = year.most_common(1)[0][0] if year else None
|
||||||
|
base = f"_TV needs a TV library - {sanitize(key)}" + (f" ({yr})" if yr else "")
|
||||||
|
tops = {parts_of(r["path"])[0] for r in items}
|
||||||
|
members = sorted([r["path"] for r in items]
|
||||||
|
+ [q for q in extras | marks if len(parts_of(q)) > 1
|
||||||
|
and parts_of(q)[0] in tops],
|
||||||
|
key=lambda q: q)
|
||||||
|
for src in members:
|
||||||
|
rel_parts = rel(src).split("/")
|
||||||
|
tail = "/".join(sanitize(c) for c in rel_parts[:-1] + [rel_parts[-1]])
|
||||||
|
book(src, f"{ROOT}/{base}/{tail}", "tv")
|
||||||
|
for p in sorted(marks):
|
||||||
|
stem, ext = os.path.splitext(os.path.basename(p))
|
||||||
|
book(p, uniq_dir(f"{ROOT}/_release-group watermarks", sanitize(stem)[:120], ext,
|
||||||
|
os.path.dirname(p)), "watermark")
|
||||||
|
for q in rows:
|
||||||
|
if is_qb(q["path"]):
|
||||||
|
problems.append(f"SKIPPED qBittorrent partial {rel(q['path'])}")
|
||||||
|
book(q["path"], q["path"], "qb-partial") # stays: resume data
|
||||||
|
|
||||||
|
# --------------------------------------------------------------- debug hook
|
||||||
|
if os.environ.get("JF_ISO"):
|
||||||
|
x = ROOT + "/High Anxiety (Mel Brooks) [1977] nordic subs DVDrip/High Anxiety [1977].iso"
|
||||||
|
print("attachable", attachable_title(x), "| dir", os.path.dirname(x) in group_by_dir,
|
||||||
|
"| g", group_for_orphan(x) and group_for_orphan(x)["base"])
|
||||||
|
raise SystemExit
|
||||||
|
if os.environ.get("JF_DEBUG"):
|
||||||
|
import json
|
||||||
|
unp = sorted({r["path"] for r in rows} - {m[0] for m in moves} - at_target)
|
||||||
|
print("at_target", len(at_target))
|
||||||
|
print("kinds-unplanned", collections.Counter(os.path.splitext(u)[1] for u in unp).most_common(8))
|
||||||
|
for up in unp[:20]:
|
||||||
|
d = os.path.dirname(up)
|
||||||
|
print("UNP", up[:90], "| mediadir?", d in media_by_dir, "| by_path", up in by_path,
|
||||||
|
"| extras", up in extras, "| discroot", disc_root(up), "| ext", os.path.splitext(up)[1])
|
||||||
|
|
||||||
|
# -------------------------------------------------------------- validation
|
||||||
|
srcs = [m[0] for m in moves]
|
||||||
|
dup_src = {s for s, n in collections.Counter(srcs).items() if n > 1}
|
||||||
|
for s in dup_src:
|
||||||
|
problems.append(f"DOUBLE-CLAIM source {rel(s)}")
|
||||||
|
all_src = set(srcs)
|
||||||
|
untouched = {r["path"] for r in rows} - all_src
|
||||||
|
bad_names, into_alive, lost = [], [], []
|
||||||
|
for src, dst, kind in moves:
|
||||||
|
for comp in dst[len(ROOT) + 1:].split("/"):
|
||||||
|
if (BAD_CHARS.search(comp) or comp != comp.strip(". ") or not comp
|
||||||
|
or len(comp) > 200):
|
||||||
|
bad_names.append(f"{kind}: {comp!r} in {rel(dst)}")
|
||||||
|
if dst in untouched: # something staying put is there
|
||||||
|
into_alive.append(f"{rel(src)} -> {rel(dst)} (occupied by a file that stays)")
|
||||||
|
if dst.lower() in {s.lower() for s in untouched}:
|
||||||
|
into_alive.append(f"{rel(src)} -> {rel(dst)} (case-collides with a survivor)")
|
||||||
|
if dst.startswith(src + "/") and src in all_src:
|
||||||
|
lost.append(f"{rel(src)} moves into its own subtree")
|
||||||
|
# every accounted-for source must exist exactly once in the plan
|
||||||
|
covered = {r["path"] for r in rows}
|
||||||
|
not_planned = sorted(covered - all_src - at_target)
|
||||||
|
|
||||||
|
with open(PLAN, "w") as out:
|
||||||
|
out.write("action\tsrc\tdst\tkind\n")
|
||||||
|
for d in sorted(mkdirs, key=lambda q: (q.count("/"), q)):
|
||||||
|
if d.startswith(ROOT + "/") and d != ROOT:
|
||||||
|
out.write(f"mkdir\t\t{d}\tdir\n")
|
||||||
|
for p in sorted(booked_src - {m[0] for m in moves}):
|
||||||
|
out.write(f"keep\t{p}\t{p}\n")
|
||||||
|
for src, dst, kind in sorted(moves, key=lambda m: m[0]):
|
||||||
|
out.write(f"move\t{src}\t{dst}\t{kind}\n")
|
||||||
|
|
||||||
|
kinds = collections.Counter(k for _s, _d, k in moves)
|
||||||
|
films_n = sum(1 for _s, _d, k in moves if k == "film")
|
||||||
|
folders = len({os.path.dirname(d) for _s, d, k in moves if k in ("film", "disc-file")})
|
||||||
|
with open(EXCEPTS, "w") as out:
|
||||||
|
out.write(f"groups={len(groups)} moves={len(moves)} folders={folders}\n")
|
||||||
|
out.write(f"kinds={dict(kinds)}\n")
|
||||||
|
out.write(f"at-target-already={len(at_target)} unplanned={len(not_planned)}\n")
|
||||||
|
out.write("\n=== WHAT THE EXECUTOR MUST ALSO WRITE ===\n")
|
||||||
|
out.write("\n".join(sorted(set(readmes))) + "\n")
|
||||||
|
out.write("\n=== PROBLEMS ===\n")
|
||||||
|
out.write("\n".join(sorted(set(problems))) + "\n")
|
||||||
|
out.write("\n=== BAD NAMES ===\n" + "\n".join(sorted(set(bad_names))) + "\n")
|
||||||
|
out.write("\n=== TARGETS OCCUPIED BY SURVIVORS ===\n"
|
||||||
|
+ "\n".join(sorted(set(into_alive))) + "\n")
|
||||||
|
out.write("\n=== LEFTOVER SOURCES (first 60) ===\n"
|
||||||
|
+ "\n".join(rel(p) for p in not_planned[:60]) + "\n")
|
||||||
|
print(f"groups={len(groups)} folders={folders} moves={len(moves)} "
|
||||||
|
f"mkdirs={len(mkdirs)} film-files={films_n}")
|
||||||
|
print(f"kinds={dict(kinds)}")
|
||||||
|
print(f"problems={len(set(problems))} bad-names={len(set(bad_names))} "
|
||||||
|
f"occupied={len(set(into_alive))} double-claim={len(dup_src)}")
|
||||||
|
print(f"left-unmoved={len(not_planned)}")
|
||||||
|
print(f"tv-keys={len(tv_keys)} extras={len(extras)} marks={len(marks)} "
|
||||||
|
f"disc-trees={len(disc_children)}")
|
||||||
Executable
+705
@@ -0,0 +1,705 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
r"""Plan a Jellyfin-format relayout of a TV library. DRY RUN ONLY: nothing moves.
|
||||||
|
|
||||||
|
media-shows.py [--root DIR] [--enum TSV] [--titles TSV] [--probed TSV]
|
||||||
|
[--tmdb-cache JSON] [--tmdb] [--movies DIR] [--park DIR]
|
||||||
|
[--out TSV] [--exceptions TXT]
|
||||||
|
|
||||||
|
Inputs are the TSVs scripts/media-census.py exports: `enumerate` lists what
|
||||||
|
exists, `jellyfin` says which episode each file IS, and `probe` is optional
|
||||||
|
duration evidence for the movie rule. The TMDb cache is a plain JSON file so a
|
||||||
|
plan stays reproducible: answers are read from it, and a miss is looked up only
|
||||||
|
with --tmdb and TMDB_API_KEY in the environment (the key never enters argv, and
|
||||||
|
never the cache).
|
||||||
|
|
||||||
|
Target contract (jellyfin.org/docs/general/server/media/shows):
|
||||||
|
|
||||||
|
<root>/<Series> (<year>)/Season <NN>/<Series> SxxEyy.ext
|
||||||
|
one file holding two episodes SxxE01-E02 specials Season 00
|
||||||
|
a documented part of an episode SxxE03 Part 1 sidecars <base>.<lang>.srt
|
||||||
|
bonus material <series>/trailers|featurettes|… — never a season
|
||||||
|
|
||||||
|
PLAN is the TSV scripts/media-apply.py replays: `action src dst kind`, every
|
||||||
|
mkdir row before any move, and `keep` for a file already where the contract puts
|
||||||
|
it. EXCEPTIONS is the review list: everything parked, and why.
|
||||||
|
|
||||||
|
The authority chain, strongest first, for one video file:
|
||||||
|
|
||||||
|
1. a provider row — Jellyfin matched this exact path and numbered it.
|
||||||
|
2. the file's own SxxEyy / 1x04 marker, the series taken from a sibling's
|
||||||
|
provider row, else from the nearest ancestor folder that has one, else from
|
||||||
|
the release folder's own words.
|
||||||
|
3. nothing. An unnumbered file is PARKED under --park with its relative path
|
||||||
|
kept: an invented `Season 01` steals an episode number from the file that
|
||||||
|
really is S01E01, and a wrong number is worse than no number.
|
||||||
|
|
||||||
|
Guards, and why each is here:
|
||||||
|
|
||||||
|
* One series, one folder, one year: the year ANY provider row of a series
|
||||||
|
states is shared by all of them, so a box set split across five release
|
||||||
|
folders lands inside ONE series folder instead of five.
|
||||||
|
* Two files claiming one episode: the bigger keeps the standard name, the other
|
||||||
|
parks with the reason. Nothing is overwritten and nothing is deleted — which
|
||||||
|
encode survives is Frank's call, and a parked file is one rename from home.
|
||||||
|
* A file with no episode evidence that runs long and that the MOVIE provider
|
||||||
|
names leaves the TV library for --movies under the movie naming contract.
|
||||||
|
Long and unnamed parks; short and unnumbered is bonus material.
|
||||||
|
* Series art and the show nfo follow the SERIES, a season image follows its
|
||||||
|
SEASON, a subtitle or episode nfo follows the episode's new base — an episode
|
||||||
|
moved without its subtitles is a half-move.
|
||||||
|
* Hidden files, macOS forks, qBittorrent resume parts and the park pile itself
|
||||||
|
are never moved. Skipping the park is what makes re-planning stable.
|
||||||
|
|
||||||
|
The plan crosses three trees (the library, its park, the movie library), so the
|
||||||
|
executor runs with --root at the share that holds them, on its own journal —
|
||||||
|
one journal is one undo:
|
||||||
|
|
||||||
|
python3 scripts/media-shows.py --root /mnt/entropy/TVShows \
|
||||||
|
--enum /tmp/tv-enum.tsv --titles /tmp/jellyfin-tv.tsv \
|
||||||
|
--out /mnt/entropy/media-tv-plan.tsv
|
||||||
|
python3 scripts/media-apply.py apply --plan /mnt/entropy/media-tv-plan.tsv \
|
||||||
|
--root /mnt/entropy --journal /mnt/entropy/media-tv-relayout.jsonl
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import collections
|
||||||
|
import importlib.util
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
ARGS = argparse.ArgumentParser(description=__doc__.splitlines()[0],
|
||||||
|
formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
|
ARGS.add_argument("--root", default=os.environ.get("TV_ROOT", "/mnt/entropy/TVShows"),
|
||||||
|
help="the TV library the plan reorganises")
|
||||||
|
ARGS.add_argument("--enum", default="/tmp/tv-enum.tsv",
|
||||||
|
help="file census (media-census.py enumerate)")
|
||||||
|
ARGS.add_argument("--titles", default="/tmp/jellyfin-tv.tsv",
|
||||||
|
help="the provider's numbering (media-census.py jellyfin)")
|
||||||
|
ARGS.add_argument("--probed", default="",
|
||||||
|
help="ffprobe census for the movie rule; absent = no durations")
|
||||||
|
ARGS.add_argument("--tmdb-cache", default="/tmp/tmdb-cache.json",
|
||||||
|
help="TMDb answers as an input file, so a plan is reproducible")
|
||||||
|
ARGS.add_argument("--tmdb", action="store_true",
|
||||||
|
help="look up cache misses (needs TMDB_API_KEY)")
|
||||||
|
ARGS.add_argument("--movies", default="/mnt/entropy/Movies",
|
||||||
|
help="movie library a mis-filed film moves into")
|
||||||
|
ARGS.add_argument("--park", default="",
|
||||||
|
help="review pile (default: <root>/_unsorted)")
|
||||||
|
ARGS.add_argument("--out", default="/tmp/tv-plan.tsv")
|
||||||
|
ARGS.add_argument("--exceptions", default="/tmp/tv-exceptions.txt")
|
||||||
|
ARGS = ARGS.parse_args()
|
||||||
|
|
||||||
|
ROOT = os.path.abspath(ARGS.root)
|
||||||
|
PARK = os.path.abspath(ARGS.park or os.path.join(ROOT, "_unsorted"))
|
||||||
|
MOVIES = os.path.abspath(ARGS.movies)
|
||||||
|
ENGINE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "media-titles.py")
|
||||||
|
_spec = importlib.util.spec_from_file_location("eng", ENGINE)
|
||||||
|
eng = importlib.util.module_from_spec(_spec)
|
||||||
|
_spec.loader.exec_module(eng)
|
||||||
|
|
||||||
|
try:
|
||||||
|
CACHE = json.load(open(ARGS.tmdb_cache))
|
||||||
|
except (OSError, ValueError):
|
||||||
|
CACHE = {}
|
||||||
|
API_KEY = os.environ.get("TMDB_API_KEY", "")
|
||||||
|
|
||||||
|
VIDEO_EXT = {".mkv", ".mp4", ".avi", ".m4v", ".mov", ".ts", ".m2ts", ".mpg",
|
||||||
|
".mpeg", ".webm", ".wmv", ".divx", ".ogm", ".flv", ".vob", ".rm",
|
||||||
|
".rmvb", ".iso", ".img"}
|
||||||
|
SUB_EXT = {".srt", ".ass", ".ssa", ".sub", ".vtt"}
|
||||||
|
IMG_EXT = {".jpg", ".jpeg", ".png", ".webp", ".bmp", ".gif", ".tbn"}
|
||||||
|
NFO_EXT = {".nfo", ".xml"}
|
||||||
|
# The container's own word, for a sidecar named "episode.mkv.srt": that component
|
||||||
|
# is not a language flag, and both matchers read the plain form better.
|
||||||
|
CONTAINER = {e[1:] for e in VIDEO_EXT}
|
||||||
|
# Files that are never anybody's business: hidden, forks, resume state, thumbnails.
|
||||||
|
SKIP_NAME = re.compile(r"^(\.|#|dl_state|@eaDir|Thumbs\.db|desktop\.ini)|\.!q$")
|
||||||
|
# Show-level metadata keeps its own name at the series root; season-level at the
|
||||||
|
# season root; folder.jpg stays with the folder it was written for.
|
||||||
|
SERIES_META = re.compile(r"^(tvshow|series|show)(\.nfo|\.xml)?$"
|
||||||
|
r"|^(poster|tvposter|banner|clearlogo|clearart|logo|art"
|
||||||
|
r"|landscape|fanart|showbackdrop|backdrop)\d*\."
|
||||||
|
r"(jpg|jpeg|png|webp|bmp)$", re.I)
|
||||||
|
SEASON_META = re.compile(r"^season\.nfo$", re.I)
|
||||||
|
FOLDER_IMAGE = re.compile(r"^folder\.(jpg|jpeg|png|webp|bmp)$", re.I)
|
||||||
|
SEASON_DIR_RE = re.compile(r"(?i)^\s*(?:season[\s._-]*(\d{1,3})|s(\d{1,2}))\s*$")
|
||||||
|
BAD_CHARS = re.compile(r'[<>:"/|?*]')
|
||||||
|
# SxxEyy with an optional second episode, plus the 1x04 form. The second number
|
||||||
|
# is guarded so a resolution never reads as an episode: "S01E01-1080p" is ONE
|
||||||
|
# episode, and the longest-match backtracking of \d{1,3} is what the (?![\dpP])
|
||||||
|
# after it closes off.
|
||||||
|
EP_RANGE = re.compile(r"(?i)(?<![a-z0-9])s(?:eason[\s._-]*)?(\d{1,2})[\s._-]*"
|
||||||
|
r"e(?:p(?:isode)[\s._-]*)?(\d{1,3})"
|
||||||
|
r"(?:[\s._-]*(?:-|to[\s._-]*|ep)[\s._-]*(?:s\d{1,2}[\s._-]*)?e?"
|
||||||
|
r"(\d{1,3})(?![\dpP]))?")
|
||||||
|
NX_RE = re.compile(r"(?i)(?<![a-z0-9])(\d{1,2})x(\d{2,3})(?![a-z0-9])")
|
||||||
|
PART_RE = re.compile(r"(?i)(?:^|[\s._-])(?:part|pt|cd|dvd|disc|disk)[\s._-]?"
|
||||||
|
r"([1-9]|i{1,3})(?:$|[\s._-])")
|
||||||
|
# Jellyfin reads these subfolder names as bonus material. The planner emits only
|
||||||
|
# these spellings, so 40k extras end up in nine folders instead of nine hundred.
|
||||||
|
EXTRA_DIRS = [
|
||||||
|
("trailers", r"trailer|teaser|promo|advert|commercial|tv[\s._-]spot|preview"),
|
||||||
|
("deleted scenes", r"deleted"),
|
||||||
|
("behind the scenes", r"behind[\s._-]the[\s._-]scenes|making[\s._-]of|bts"
|
||||||
|
r"|b[\s._-]roll|gag[\s._-]reel|blooper|outtake|archival"
|
||||||
|
r"|video[\s._-]archive|scene[\s._-]selection|menu|gallery"
|
||||||
|
r"|stills?|visual[\s._-]effects|workprint"),
|
||||||
|
("interviews", r"interview|cast[\s._-]and[\s._-]crew|audition|commentary"),
|
||||||
|
("scene cuts", r"scene[\s._-]cuts?|scenecuts"),
|
||||||
|
("featurettes", r"featurettes?"),
|
||||||
|
("shorts", r"shorts?"),
|
||||||
|
("clips", r"clips?"),
|
||||||
|
]
|
||||||
|
# Jellyfin's own ExtraType values, folded onto the same folder names.
|
||||||
|
EXTRA_KIND = {"trailer": "trailers", "tmbd": "trailers", "intro": "other",
|
||||||
|
"movie": "other", "scene": "scene cuts", "clip": "clips",
|
||||||
|
"interview": "interviews", "behindthescenes": "behind the scenes",
|
||||||
|
"deleted": "deleted scenes", "other": "other", "epilogue": "other",
|
||||||
|
"opening": "other", "featurette": "featurettes", "short": "shorts"}
|
||||||
|
SHORT_MAX = 45 * 60 # over this, a file with no episode evidence is film-shaped
|
||||||
|
|
||||||
|
|
||||||
|
def rel(p):
|
||||||
|
return p[len(ROOT) + 1:] if p.startswith(ROOT + "/") else p
|
||||||
|
|
||||||
|
|
||||||
|
def clean(name):
|
||||||
|
"""A name the filesystem and both matchers can read."""
|
||||||
|
name = BAD_CHARS.sub("-", name).replace("\\", "-").strip(" .")
|
||||||
|
return name[:180] or "untitled"
|
||||||
|
|
||||||
|
|
||||||
|
def title_of(text):
|
||||||
|
"""The engine's parser, applied to a name that has dots but no extension:
|
||||||
|
splitext would read "The.Examples" as stem "The", so a sentinel extension is
|
||||||
|
added and the parser sees an ordinary media name."""
|
||||||
|
return eng.title_of(text + ".mkv")
|
||||||
|
|
||||||
|
|
||||||
|
def markers(name):
|
||||||
|
"""(season, [episodes], position) the episode markers a name states; None when
|
||||||
|
it states none. The position is where the series' own words stop."""
|
||||||
|
stem = os.path.splitext(name)[0]
|
||||||
|
m = EP_RANGE.search(stem)
|
||||||
|
if m:
|
||||||
|
eps = [int(m.group(2))] + ([int(m.group(3))] if m.group(3) else [])
|
||||||
|
return int(m.group(1)), eps, m.start()
|
||||||
|
m = NX_RE.search(stem)
|
||||||
|
if m:
|
||||||
|
return int(m.group(1)), [int(m.group(2))], m.start()
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
LEAD_IDX_RE = re.compile(r"^\s*(\d{1,3})\s*[-\u2013.\s]\s*[A-Za-z]")
|
||||||
|
PACK_IDX_RE = re.compile(r"^\s*\d+[a-z]?[.)]\s+")
|
||||||
|
|
||||||
|
|
||||||
|
def num(x):
|
||||||
|
try:
|
||||||
|
return int(x)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
def folder_season(path):
|
||||||
|
"""The season the FOLDER states (`Season 3`, `S3`). A human wrote it, and where
|
||||||
|
a scraped row disagrees this is the one that survives a re-plan."""
|
||||||
|
m = SEASON_DIR_RE.match(os.path.basename(os.path.dirname(path)))
|
||||||
|
return int(next(g for g in m.groups() if g)) if m else None
|
||||||
|
|
||||||
|
|
||||||
|
def leading_index(name):
|
||||||
|
"""The number a file leads with inside a season folder — `01 Kyle MacLachlan,
|
||||||
|
Sinead OC.avi` is episode 1. Both matchers read it that way."""
|
||||||
|
m = LEAD_IDX_RE.match(os.path.splitext(name)[0])
|
||||||
|
return int(m.group(1)) if m else None
|
||||||
|
|
||||||
|
|
||||||
|
def tmdb(kind, query):
|
||||||
|
"""A provider answer for a query: the cache first, TMDb once, then never again."""
|
||||||
|
ck = f"{kind}:{query}"
|
||||||
|
if ck in CACHE:
|
||||||
|
hit = CACHE[ck]
|
||||||
|
# A cache an operator edited by hand can carry the year as a number; the
|
||||||
|
# comparison below is text, so the wire form is imposed on every hit.
|
||||||
|
return None if hit is None else {**hit, "year": str(hit.get("year") or "")}
|
||||||
|
if not (ARGS.tmdb and API_KEY):
|
||||||
|
return None
|
||||||
|
path = "movie" if kind == "movie" else "tv"
|
||||||
|
url = ("https://api.themoviedb.org/3/search/" + path + "?query="
|
||||||
|
+ urllib.parse.quote(query) + "&include_adult=false")
|
||||||
|
req = urllib.request.Request(url, headers={
|
||||||
|
# The key rides a header, never the URL: a token in argv is world-readable.
|
||||||
|
"Authorization": "Bearer " + API_KEY,
|
||||||
|
"Accept": "application/json"})
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req, timeout=20) as r:
|
||||||
|
found = json.load(r).get("results") or []
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
print(f"tmdb {kind} lookup failed for {query!r}: {e}", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
if not found:
|
||||||
|
CACHE[ck] = None
|
||||||
|
return None
|
||||||
|
top = found[0]
|
||||||
|
CACHE[ck] = {"name": (top.get("title") or top.get("name") or "").strip(),
|
||||||
|
"year": str(top.get("year") or "") or (top.get("first_air_date") or "")[:4],
|
||||||
|
"id": top.get("id")}
|
||||||
|
return CACHE[ck]
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------- the inputs
|
||||||
|
rows = []
|
||||||
|
for line in open(ARGS.enum):
|
||||||
|
size, ext, path = line.rstrip("\n").split("\t")
|
||||||
|
p = os.path.abspath(path)
|
||||||
|
base = os.path.basename(p)
|
||||||
|
if not p.startswith(ROOT + "/") or SKIP_NAME.search(base):
|
||||||
|
continue
|
||||||
|
if p.startswith(PARK + "/"):
|
||||||
|
continue # the park is already reviewed; re-parking is churn
|
||||||
|
rows.append({"bytes": int(size), "ext": ext.lower(), "path": p, "base": base})
|
||||||
|
held = {r["path"]: r for r in rows}
|
||||||
|
|
||||||
|
titles = {}
|
||||||
|
with open(ARGS.titles) as fh:
|
||||||
|
cols = fh.readline().rstrip("\n").split("\t")
|
||||||
|
for line in fh:
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
f += [""] * (len(cols) - len(f))
|
||||||
|
row = dict(zip(cols, f))
|
||||||
|
p = os.path.abspath(row.get("file", ""))
|
||||||
|
if p in held:
|
||||||
|
titles[p] = row
|
||||||
|
|
||||||
|
dur = {}
|
||||||
|
if ARGS.probed and os.path.exists(ARGS.probed):
|
||||||
|
for line in open(ARGS.probed):
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if len(f) > 2:
|
||||||
|
try:
|
||||||
|
dur[os.path.abspath(f[1])] = float(f[2] or 0)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
video = [r for r in rows if r["ext"] in VIDEO_EXT]
|
||||||
|
other = [r for r in rows if r["ext"] not in VIDEO_EXT]
|
||||||
|
|
||||||
|
# What a folder already knows: one provider row names the series for every sibling
|
||||||
|
# that has no row of its own — that is how an anime pack gets its show.
|
||||||
|
by_dir = collections.defaultdict(list)
|
||||||
|
for r in video:
|
||||||
|
by_dir[os.path.dirname(r["path"])].append(r)
|
||||||
|
dir_series = {}
|
||||||
|
for d, rs in by_dir.items():
|
||||||
|
names = collections.Counter(titles[r["path"]]["series"] for r in rs
|
||||||
|
if r["path"] in titles and titles[r["path"]]["series"])
|
||||||
|
if names:
|
||||||
|
dir_series[d] = names.most_common(1)[0][0]
|
||||||
|
|
||||||
|
|
||||||
|
# The registry: every series a provider names, judged before any folder is. Folder
|
||||||
|
# words may resolve a file to one of these names; they may never invent one. That is
|
||||||
|
# the whole difference between "NCOP 01.mkv" inside the Gurren Lagann pack, which the
|
||||||
|
# pack names, and a new root folder called "01 NCOP".
|
||||||
|
registry = {}
|
||||||
|
for _p, _t in titles.items():
|
||||||
|
if _t["series"]:
|
||||||
|
registry.setdefault(eng.norm(_t["series"]), _t["series"])
|
||||||
|
|
||||||
|
|
||||||
|
def known_in(text):
|
||||||
|
"""The registered series named inside this text: the longest registered name
|
||||||
|
whose words appear in it as a run. "(CBB) Tengen Toppa Gurren Lagann - The
|
||||||
|
Complete Series (BD 1080p)" names "Tengen Toppa Gurren Lagann"."""
|
||||||
|
hay = " " + eng.norm(text) + " "
|
||||||
|
best = ""
|
||||||
|
for k, name in registry.items():
|
||||||
|
if len(k) >= 5 and f" {k} " in hay and len(k) > len(best):
|
||||||
|
best = name
|
||||||
|
return best
|
||||||
|
|
||||||
|
|
||||||
|
# Rows also name their own folders' ancestors, so an already-filed library stays
|
||||||
|
# where it is when it is re-planned.
|
||||||
|
for d, name in sorted(dir_series.items()):
|
||||||
|
a = os.path.dirname(d)
|
||||||
|
while a and a != ROOT and len(a) > len(ROOT):
|
||||||
|
if a not in dir_series:
|
||||||
|
got = known_in(os.path.basename(a))
|
||||||
|
if got:
|
||||||
|
dir_series[a] = got
|
||||||
|
a = os.path.dirname(a)
|
||||||
|
|
||||||
|
|
||||||
|
def series_of_dir(d):
|
||||||
|
"""The series an ancestor folder belongs to, nearest first: the folder holding
|
||||||
|
provider rows, else a folder whose own words name a registered show."""
|
||||||
|
while d and d != ROOT and len(d) > len(ROOT):
|
||||||
|
if d in dir_series:
|
||||||
|
return dir_series[d]
|
||||||
|
got = known_in(os.path.basename(d))
|
||||||
|
if got:
|
||||||
|
return got
|
||||||
|
d = os.path.dirname(d)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def extras_kind(path, extra_type):
|
||||||
|
"""The canonical bonus subfolder, from Jellyfin's own extra type first."""
|
||||||
|
if extra_type:
|
||||||
|
return EXTRA_KIND.get(eng.norm(extra_type).replace(" ", ""), "other")
|
||||||
|
for d in rel(path).split("/")[:-1]:
|
||||||
|
if eng.folder_is_bonus(d):
|
||||||
|
for name, rx in EXTRA_DIRS:
|
||||||
|
if re.search(rx, d, re.I):
|
||||||
|
return name
|
||||||
|
return "extras"
|
||||||
|
stem = os.path.splitext(os.path.basename(path))[0]
|
||||||
|
for name, rx in EXTRA_DIRS:
|
||||||
|
if re.search(rx, stem, re.I):
|
||||||
|
return name
|
||||||
|
return "extras"
|
||||||
|
|
||||||
|
|
||||||
|
# ----------------------------------------------------------- classify every file
|
||||||
|
series = {} # norm(name) -> {name, year, tmdb}
|
||||||
|
claimants = [] # (row, name, season, [episodes])
|
||||||
|
extras = [] # (row, name, bonus folder)
|
||||||
|
films = [] # film-shaped, no episode evidence
|
||||||
|
unknown = [] # (row, reason)
|
||||||
|
|
||||||
|
|
||||||
|
def series_name(name):
|
||||||
|
"""The provider's name as a folder: the pack index a scene release leads with
|
||||||
|
("4a. Seed DESTINY") falls away, and the doubled spaces it leaves fold."""
|
||||||
|
return clean(re.sub(r"\s{2,}", " ", PACK_IDX_RE.sub("", name)).strip(" ."))
|
||||||
|
|
||||||
|
|
||||||
|
def note(name, year="", tmdb_id=""):
|
||||||
|
"""Register a series; the first name wins, the first year and id stick."""
|
||||||
|
k = eng.norm(name)
|
||||||
|
s = series.setdefault(k, {"name": series_name(name), "year": "", "tmdb": ""})
|
||||||
|
if year and not s["year"]:
|
||||||
|
s["year"] = str(year)
|
||||||
|
if tmdb_id and not s["tmdb"]:
|
||||||
|
s["tmdb"] = str(tmdb_id)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def as_extra(r, owner, extra_type):
|
||||||
|
"""Bonus material needs a series to file under; with none it is parked."""
|
||||||
|
if not owner:
|
||||||
|
unknown.append((r, "bonus material with no series to own it"))
|
||||||
|
return
|
||||||
|
note(owner)
|
||||||
|
extras.append((r, owner, extras_kind(r["path"], extra_type)))
|
||||||
|
|
||||||
|
for r in video:
|
||||||
|
p, t = r["path"], titles.get(r["path"])
|
||||||
|
marked = markers(r["base"])
|
||||||
|
fs = folder_season(p)
|
||||||
|
if t and t["extra"]:
|
||||||
|
as_extra(r, t["series"] or series_of_dir(os.path.dirname(p)), t["extra"])
|
||||||
|
continue
|
||||||
|
row = bool(t and t["series"] and (t["season"] or t["episode"]))
|
||||||
|
if marked or row:
|
||||||
|
# The provider row names the SHOW: its registered name, its year. The number
|
||||||
|
# that orders the file comes from the file's own name first, then the folder
|
||||||
|
# it sits in, then the row — scraped pack rows number episodes badly, where
|
||||||
|
# every file under SNL 16-20/Season 16..19 carries season 16 episode 20.
|
||||||
|
owner = (t["series"] if t and t["series"]
|
||||||
|
else series_of_dir(os.path.dirname(p))
|
||||||
|
or title_of(r["base"][:marked[2]])[0])
|
||||||
|
if not eng.norm(owner).replace(" ", ""):
|
||||||
|
unknown.append((r, "an episode marker with no series name to hang it on"))
|
||||||
|
continue
|
||||||
|
if marked:
|
||||||
|
season, eps = marked[0], marked[1]
|
||||||
|
else:
|
||||||
|
season = fs if fs is not None else num(t["season"])
|
||||||
|
ep = leading_index(r["base"]) or num(t["episode"])
|
||||||
|
if not 0 < season <= 200 or ep <= 0:
|
||||||
|
unknown.append((r, f"a provider row of season {season} episode {ep}"))
|
||||||
|
continue
|
||||||
|
eps = [ep]
|
||||||
|
claimants.append((r, owner, season, eps))
|
||||||
|
note(owner, t["year"] if t else "", t["tmdb"] if t else "")
|
||||||
|
continue
|
||||||
|
owner = series_of_dir(os.path.dirname(p))
|
||||||
|
if fs is not None and owner and leading_index(r["base"]):
|
||||||
|
claimants.append((r, owner, fs, [leading_index(r["base"])]))
|
||||||
|
note(owner)
|
||||||
|
continue
|
||||||
|
if eng.is_extra(p, title_of(r["base"])[0], dur.get(p, 0.0), ROOT):
|
||||||
|
as_extra(r, owner, "")
|
||||||
|
continue
|
||||||
|
if dur.get(p, 0.0) >= SHORT_MAX or (p not in dur and title_of(r["base"])[1] != "?"):
|
||||||
|
films.append(r)
|
||||||
|
continue
|
||||||
|
unknown.append((r, "no episode marker, no provider row, nothing long enough to be a film"))
|
||||||
|
|
||||||
|
# A provider that never states a year still has one: the search is exact, so a
|
||||||
|
# miss parks nothing — the folder simply carries no year, which Jellyfin accepts.
|
||||||
|
for k, s in series.items():
|
||||||
|
if not s["year"]:
|
||||||
|
got = tmdb("tv", s["name"])
|
||||||
|
if got and eng.norm(got["name"]) == k:
|
||||||
|
s["year"] = got["year"]
|
||||||
|
|
||||||
|
if ARGS.tmdb:
|
||||||
|
with open(ARGS.tmdb_cache, "w") as fh:
|
||||||
|
json.dump(CACHE, fh, indent=1, sort_keys=True)
|
||||||
|
|
||||||
|
|
||||||
|
def series_dir(name):
|
||||||
|
s = series.get(eng.norm(name))
|
||||||
|
if not s:
|
||||||
|
s = {"name": clean(name), "year": ""}
|
||||||
|
label = f"{s['name']} ({s['year']})" if s["year"] else s["name"]
|
||||||
|
return f"{ROOT}/{clean(label)}"
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------- the plan
|
||||||
|
moves, keeps, parked = [], [], []
|
||||||
|
reasons = collections.defaultdict(list)
|
||||||
|
used = {} # dst -> the file that claimed it
|
||||||
|
|
||||||
|
|
||||||
|
def book(r, dst_dir, name, kind):
|
||||||
|
"""Claim one destination for one file, or park the file when it is taken."""
|
||||||
|
dst = f"{dst_dir}/{name}"
|
||||||
|
if dst == r["path"]:
|
||||||
|
keeps.append((r["path"], dst, kind))
|
||||||
|
return
|
||||||
|
if dst in used:
|
||||||
|
park(r, "something else in the plan already files there")
|
||||||
|
return
|
||||||
|
if dst in held:
|
||||||
|
park(r, f"{rel(dst)} already occupies that name")
|
||||||
|
return
|
||||||
|
used[dst] = r["path"]
|
||||||
|
moves.append((r["path"], dst, kind))
|
||||||
|
|
||||||
|
|
||||||
|
def park(r, reason):
|
||||||
|
parked.append((r, reason))
|
||||||
|
reasons[reason].append(r)
|
||||||
|
dst = f"{PARK}/{rel(r['path'])}"
|
||||||
|
if dst in used or dst in held:
|
||||||
|
dst = f"{PARK}/{rel(r['path'])}.{len(reasons)}"
|
||||||
|
used[dst] = r["path"]
|
||||||
|
moves.append((r["path"], dst, "park"))
|
||||||
|
|
||||||
|
|
||||||
|
# Episodes first: two files claiming one number is a decision Frank makes, not one
|
||||||
|
# the plan sneaks — the bigger copy takes the standard name, the other waits.
|
||||||
|
by_ep = collections.defaultdict(list)
|
||||||
|
for r, name, season, eps in claimants:
|
||||||
|
for e in eps:
|
||||||
|
by_ep[(eng.norm(name), season, e)].append(r)
|
||||||
|
winners, losers = set(), {}
|
||||||
|
for (key, season, e), rs in by_ep.items():
|
||||||
|
rs.sort(key=lambda r: (-r["bytes"], r["path"]))
|
||||||
|
winners.add(rs[0]["path"])
|
||||||
|
for r in rs[1:]:
|
||||||
|
losers.setdefault(r["path"],
|
||||||
|
f"duplicate of {series[key]['name']} S{season:02d}E{e:02d}"
|
||||||
|
" (the bigger copy keeps the standard name)")
|
||||||
|
for r, name, season, eps in claimants:
|
||||||
|
if r["path"] in losers:
|
||||||
|
park(r, losers[r["path"]])
|
||||||
|
continue
|
||||||
|
s = series[eng.norm(name)]
|
||||||
|
label = f" S{season:02d}E" + "-E".join(f"{e:02d}" for e in eps)
|
||||||
|
part = PART_RE.search(os.path.splitext(r["base"])[0])
|
||||||
|
tail = f" Part {part.group(1).upper()}" if part and len(eps) == 1 else ""
|
||||||
|
book(r, f"{series_dir(name)}/Season {season:02d}", s["name"] + label + tail + r["ext"],
|
||||||
|
"tv")
|
||||||
|
|
||||||
|
for r, name, kind in extras:
|
||||||
|
book(r, f"{series_dir(name)}/{kind}",
|
||||||
|
clean(os.path.splitext(r["base"])[0]) + r["ext"], "extra")
|
||||||
|
|
||||||
|
# A film inside the TV library leaves for the movie library only when the movie
|
||||||
|
# provider answers with a title that matches the name and, when the name states a
|
||||||
|
# year, that year too.
|
||||||
|
for r in films:
|
||||||
|
parsed, year = title_of(r["base"])
|
||||||
|
got = tmdb("movie", parsed)
|
||||||
|
words = eng.norm(got["name"]).split() if got else []
|
||||||
|
if not got or not eng.mentions(parsed.split(), words) \
|
||||||
|
or (year != "?" and got["year"] and got["year"] != year):
|
||||||
|
park(r, "long file with no episode evidence, and no movie provider names it")
|
||||||
|
continue
|
||||||
|
label = f"{got['name']} ({got['year']})" if got["year"] else got["name"]
|
||||||
|
book(r, f"{MOVIES}/{clean(label)}", clean(label) + r["ext"], "movie")
|
||||||
|
|
||||||
|
# ------------------------------------------------- sidecars and folder metadata
|
||||||
|
moved = {} # src -> dst, for every booked file
|
||||||
|
for src, dst, _kind in moves:
|
||||||
|
moved[src] = dst
|
||||||
|
for src, dst, _kind in keeps:
|
||||||
|
moved[src] = dst
|
||||||
|
dst_dirs = collections.defaultdict(collections.Counter)
|
||||||
|
video_dst = collections.defaultdict(list)
|
||||||
|
for src, dst in moved.items():
|
||||||
|
if src in {r["path"] for r in video}:
|
||||||
|
video_dst[os.path.dirname(src)].append((src, dst))
|
||||||
|
dst_dirs[os.path.dirname(src)][os.path.dirname(dst)] += 1
|
||||||
|
|
||||||
|
|
||||||
|
def series_name_at(d):
|
||||||
|
"""The series the files of a folder belong to — a registered name, or nothing.
|
||||||
|
Folder words alone never name a show."""
|
||||||
|
names = [titles[src]["series"] for src, _ in video_dst.get(d, [])
|
||||||
|
if src in titles and titles[src]["series"]]
|
||||||
|
if names:
|
||||||
|
return collections.Counter(names).most_common(1)[0][0]
|
||||||
|
return series_of_dir(d)
|
||||||
|
|
||||||
|
|
||||||
|
def season_at(d):
|
||||||
|
"""The season number a folder carries: its own name, else its files' rows."""
|
||||||
|
m = SEASON_DIR_RE.match(os.path.basename(d))
|
||||||
|
if m:
|
||||||
|
return int(m.group(1) or m.group(2))
|
||||||
|
nums = [int(titles[src]["season"]) for src, _ in video_dst.get(d, [])
|
||||||
|
if src in titles and titles[src]["season"]]
|
||||||
|
return collections.Counter(nums).most_common(1)[0][0] if nums else None
|
||||||
|
|
||||||
|
|
||||||
|
def folder_dst(d):
|
||||||
|
"""Where a folder's own images go: the one place its files landed, or the
|
||||||
|
series root when they scattered across several seasons."""
|
||||||
|
c = dst_dirs.get(d)
|
||||||
|
if not c:
|
||||||
|
return None
|
||||||
|
if len(c) == 1:
|
||||||
|
return c.most_common(1)[0][0]
|
||||||
|
owner = series_name_at(d)
|
||||||
|
return series_dir(owner) if owner else None
|
||||||
|
|
||||||
|
|
||||||
|
def anchor_of(path, stem):
|
||||||
|
"""The video file this sidecar names: the longest file name it extends, in the
|
||||||
|
NEAREST folder that holds one. A scene pack buries its subtitles two levels
|
||||||
|
down (`Pack/Subs/<release-name>/<release>.jpn.srt`), so the search climbs to
|
||||||
|
the library root instead of stopping at the parent."""
|
||||||
|
d = os.path.dirname(path)
|
||||||
|
while len(d) >= len(ROOT) and d.startswith(ROOT):
|
||||||
|
best = None
|
||||||
|
for src, dst in video_dst.get(d, []):
|
||||||
|
base = os.path.basename(src)
|
||||||
|
for prefix in (os.path.splitext(base)[0], base):
|
||||||
|
if stem == prefix or stem.startswith(prefix + "."):
|
||||||
|
if best is None or len(prefix) > best[2]:
|
||||||
|
best = (src, dst, len(prefix))
|
||||||
|
if best:
|
||||||
|
return best[0], best[1]
|
||||||
|
up = os.path.dirname(d)
|
||||||
|
if up == d:
|
||||||
|
break
|
||||||
|
d = up
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def sidecar_name(r, src, dst):
|
||||||
|
"""The sidecar's own tail — language, flag, the media extension an nfo must
|
||||||
|
carry — hung on the video file's new base."""
|
||||||
|
stem = os.path.splitext(os.path.basename(src))[0]
|
||||||
|
tail = os.path.splitext(r["base"])[0][len(stem):]
|
||||||
|
# "Show.S01E01.mkv.srt" carries the container's name, not a flag: Jellyfin and
|
||||||
|
# Plex both read "Show S01E01.srt", so the container word is dropped here.
|
||||||
|
parts = tail.split(".")
|
||||||
|
if len(parts) > 1 and parts[1].lower() in CONTAINER:
|
||||||
|
tail = "".join("." + p for p in parts[2:])
|
||||||
|
if r["ext"] in NFO_EXT:
|
||||||
|
tail = os.path.splitext(os.path.basename(src))[1] + tail
|
||||||
|
return clean(os.path.splitext(os.path.basename(dst))[0] + tail + r["ext"])
|
||||||
|
|
||||||
|
|
||||||
|
for r in other:
|
||||||
|
p, base = r["path"], r["base"]
|
||||||
|
stem = os.path.splitext(base)[0]
|
||||||
|
d = os.path.dirname(p)
|
||||||
|
if r["ext"] in SUB_EXT or r["ext"] in NFO_EXT or r["ext"] in IMG_EXT:
|
||||||
|
a = anchor_of(p, stem)
|
||||||
|
if a:
|
||||||
|
book(r, os.path.dirname(a[1]), sidecar_name(r, a[0], a[1]),
|
||||||
|
"subtitle" if r["ext"] in SUB_EXT else
|
||||||
|
("nfo" if r["ext"] in NFO_EXT else "image"))
|
||||||
|
continue
|
||||||
|
if SERIES_META.match(base):
|
||||||
|
owner = series_name_at(d)
|
||||||
|
if owner:
|
||||||
|
book(r, series_dir(owner), base, "meta")
|
||||||
|
continue
|
||||||
|
elif SEASON_META.match(base):
|
||||||
|
owner, season = series_name_at(d), season_at(d)
|
||||||
|
if owner and season is not None:
|
||||||
|
book(r, f"{series_dir(owner)}/Season {season:02d}", base, "meta")
|
||||||
|
continue
|
||||||
|
elif r["ext"] in IMG_EXT or r["ext"] in NFO_EXT:
|
||||||
|
dest = folder_dst(d)
|
||||||
|
if dest:
|
||||||
|
book(r, dest, base, "meta")
|
||||||
|
continue
|
||||||
|
unknown.append((r, "not media, or media with nothing beside it to belong to"))
|
||||||
|
|
||||||
|
for r, reason in unknown:
|
||||||
|
park(r, reason)
|
||||||
|
|
||||||
|
|
||||||
|
# --------------------------------------------------------------------- output
|
||||||
|
def write_plan():
|
||||||
|
"""mkdir rows for every destination folder and its whole chain, then the moves.
|
||||||
|
Parents first: the executor creates directories in the order they are listed."""
|
||||||
|
chain = set()
|
||||||
|
for _s, dst, _k in moves:
|
||||||
|
d = os.path.dirname(dst)
|
||||||
|
while d and d not in (ROOT, MOVIES, PARK, os.path.dirname(d)):
|
||||||
|
chain.add(d)
|
||||||
|
d = os.path.dirname(d)
|
||||||
|
dirs = sorted(chain, key=lambda p: (p.count("/"), p))
|
||||||
|
with open(ARGS.out, "w") as fh:
|
||||||
|
fh.write("action\tsrc\tdst\tkind\n")
|
||||||
|
for d in dirs:
|
||||||
|
fh.write(f"mkdir\t\t{d}\tdir\n")
|
||||||
|
for src, dst, kind in sorted(moves, key=lambda m: (m[1], m[0])):
|
||||||
|
fh.write(f"move\t{src}\t{dst}\t{kind}\n")
|
||||||
|
for src, dst, kind in sorted(keeps, key=lambda m: (m[1], m[0])):
|
||||||
|
fh.write(f"keep\t{src}\t{dst}\t{kind}\n")
|
||||||
|
sized = sum(held[s]["bytes"] for s, _d, _k in moves if s in held)
|
||||||
|
print(f"plan: {len(moves) - len(parked)} moves, {len(parked)} parked"
|
||||||
|
f" ({sized / 2**40:.2f} TiB touched), {len(keeps)} already placed,"
|
||||||
|
f" {len(dirs)} folders -> {ARGS.out}")
|
||||||
|
|
||||||
|
|
||||||
|
def write_exceptions():
|
||||||
|
no_year = sorted(s["name"] for s in series.values() if not s["year"])
|
||||||
|
with open(ARGS.exceptions, "w") as fh:
|
||||||
|
fh.write(f"TV relayout exceptions for {ROOT}\n\n")
|
||||||
|
fh.write(f"Everything below is parked under {PARK}, keeping the relative path it\n")
|
||||||
|
fh.write("has today. Nothing is deleted and nothing is overwritten: a parked file\n")
|
||||||
|
fh.write("is one rename from wherever the plan is pointed next.\n\n")
|
||||||
|
for reason in sorted(reasons, key=lambda r: -len(reasons[r])):
|
||||||
|
rs = reasons[reason]
|
||||||
|
fh.write(f"{len(rs):>6} files {reason}\n")
|
||||||
|
for r in sorted(rs, key=lambda r: rel(r["path"]))[:400]:
|
||||||
|
fh.write(f" {rel(r['path'])}\n")
|
||||||
|
if len(rs) > 400:
|
||||||
|
fh.write(f" … and {len(rs) - 400} more\n")
|
||||||
|
fh.write("\n")
|
||||||
|
if no_year:
|
||||||
|
fh.write(f"{len(no_year):>6} series carry no year from any provider, so their\n")
|
||||||
|
fh.write(" folders are named without one; Jellyfin still matches them.\n")
|
||||||
|
fh.write(" " + ", ".join(no_year[:300]) + "\n")
|
||||||
|
print(f"exceptions -> {ARGS.exceptions}")
|
||||||
|
|
||||||
|
|
||||||
|
write_plan()
|
||||||
|
write_exceptions()
|
||||||
Executable
+579
@@ -0,0 +1,579 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
r"""The fleet's release-filename title parser: the grouping authority the relayout
|
||||||
|
plan uses for every file the media server never matched.
|
||||||
|
|
||||||
|
report: media-titles.py report [--probed TSV] [--out TSV]
|
||||||
|
the similar-copies census (DUP / REVIEW / EP-DUP groups) over the
|
||||||
|
ffprobe census scripts/media-census.py writes.
|
||||||
|
parse: media-titles.py parse NAME [NAME…]
|
||||||
|
what title_of() makes of a name — the way to check a naming accident
|
||||||
|
before it merges two films into one folder.
|
||||||
|
|
||||||
|
scripts/media-plan.py imports this file; over there the paths are arguments.
|
||||||
|
|
||||||
|
Method — three independent signals, so a naming accident cannot merge two films:
|
||||||
|
* franchise KEY : first two significant title tokens. Stable across parts of a
|
||||||
|
collection ("resident evil", "jump street") but NOT film-specific — it only
|
||||||
|
selects candidates.
|
||||||
|
* DURATION : the identity check. Same master encodes differ by at most a
|
||||||
|
couple of seconds; different films / cuts do not collide.
|
||||||
|
* SIZE SPREAD + YEAR: decide the tier. Two files same size AND same duration
|
||||||
|
are byte-twins (the hash pass confirms); same duration but different sizes are
|
||||||
|
two different releases of one film (deletable redundancy).
|
||||||
|
|
||||||
|
TV episodes (SxxEyy in the name) are grouped separately: their uniform runtime
|
||||||
|
would otherwise collapse a whole series into one "duplicate" group.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import collections, os, re, sys, unicodedata
|
||||||
|
|
||||||
|
# Library root: the folder directly under this one names the film, and every
|
||||||
|
# folder below it is structure (Extras/, Featurettes/, Disc 1/, BDMV/). Tests pass
|
||||||
|
# their own; nothing here reads media, only the census TSVs about it.
|
||||||
|
ROOT = os.environ.get("MEDIA_ROOT", "/mnt/entropy/Movies")
|
||||||
|
|
||||||
|
# Release-tag vocabulary: never part of a title, so the title stops here.
|
||||||
|
JUNK = {"5.1", "7.1", "2.0", "1.0", "6.1", "4.0", "extras"}
|
||||||
|
JUNK_RE = re.compile(
|
||||||
|
r"^("
|
||||||
|
r"\d{3,5}p|\d{1,2}k|uhd|fhd|imax|x ?26[45]|hevc|h ?26[45]|avc|av1|x ?av1|vp[89]"
|
||||||
|
r"|divx|xvid|mpeg-?2?|\d{1,2}-?bit|hdr ?10\+?|hdr|dolby ?vision|dvh?|sdr|hybrid"
|
||||||
|
r"|re-?mux|bd-?re-?mux|blu-?ray|bdrip|brrip|bdmux|bd|web-?(rip|dl)?|hdtv|hdrip"
|
||||||
|
r"|dvd-?(rip|r|scr)?|hdcam|telesync|screener|ppv|re-?encode|extras?"
|
||||||
|
r"|ddp? ?\d(\.\d)?|dd\d(\.\d)?|dts-?hd(\.ma)?|dts|truehd|atmos|e-?ac-?3?|ac-?3"
|
||||||
|
r"|aac\d?(\.\d)?|lpcm|flac|opus|mp3|pcm|\d\.\d(\.\d)?|5 ?1|7 ?1|2 ?0|1 ?0|6 ?1|4 ?0"
|
||||||
|
r"|subs?-?(it|en|eng|ita)?|subbed|dual(-?audio)?|multi|dub(bed)?"
|
||||||
|
r"|ita|eng|en|it|de|fr|es|pl|jp|jap|lat|esp|ger|fre|rus|zho|hin|tam|tel"
|
||||||
|
r"|repack|proper|internal|limited|unrated|extended|director'?s|cut|remaster(ed)?|uncut"
|
||||||
|
r"|special|edition|version|crc32|sha1|sfv|par2|nfo|nzb|mux|complete|unpack"
|
||||||
|
r"|certified|scene|p2p|part\d*|cd\d+|disc\d+|disk\d+|vob|idx|srt|ass|ssa|sup"
|
||||||
|
r"|m3u|cue|log|txt|mkv|mp4|avi|imdb\d[\d.]*|yts(\.\w+)?|rarbg|collection"
|
||||||
|
r")$", re.I)
|
||||||
|
RES_RE = re.compile(r"^\d{3,5}p$|^\d{1,2}k$|^\d+x\d+$", re.I)
|
||||||
|
YEAR_RE = re.compile(r"^(19|20)\d{2}$")
|
||||||
|
SEP_RE = re.compile(r"[._\s\-\[\](){}+!~,;:&@#/*'\"`´|]+")
|
||||||
|
IDX_RE = re.compile(r"^\d{1,3}[a-z]?$")
|
||||||
|
WORD_RE = re.compile(r"[A-Za-z0-9']+$")
|
||||||
|
EP_RE = re.compile(r"(?i)(?<![a-z0-9])(s(?:eason[\s._-]*)?\d{1,2}[\s._-]*e(?:p(?:isode)?[\s._-]*)?"
|
||||||
|
r"\d{1,3}|s\d{1,2}\s?e\d{1,3}|\d{1,2}x\d{2,3}|part[\s._-]*\d{1,2})")
|
||||||
|
STOP = {"a", "an", "the", "il", "la", "le", "i", "un", "une", "el", "los", "las", "de",
|
||||||
|
"del", "du", "di", "e", "ed", "y", "and", "et", "for", "of", "to", "in", "on",
|
||||||
|
"vs", "avec", "con"}
|
||||||
|
|
||||||
|
|
||||||
|
def norm(s):
|
||||||
|
s = unicodedata.normalize("NFKD", s)
|
||||||
|
return "".join(c for c in s if not unicodedata.combining(c)).lower().strip()
|
||||||
|
|
||||||
|
|
||||||
|
def fold_sep(s):
|
||||||
|
"""norm(), plus: every character that is left over is not a letter, digit or
|
||||||
|
apostrophe, so it is a SEPARATOR whatever codepoint it hides behind — not part
|
||||||
|
of the word. Release groups file titles with lookalikes for punctuation the
|
||||||
|
filesystem forbids: "V ∕ H ∕ S ∕ 2" carries U+2215 DIVISION SLASH, "S꞉ Viral"
|
||||||
|
U+A789, and en dashes hide in "Star Wars – A New Hope". NFKD does NOT fold
|
||||||
|
those (no decomposition mapping), and the scrubber inside title_of DELETES
|
||||||
|
what it does not recognise, so five different V/H/S films squashed into the one
|
||||||
|
title "vhs2" and would have shared one folder. Deleting is the wrong operation:
|
||||||
|
they separate. Accents are gone by then — norm() removed the combining marks —
|
||||||
|
so "Amélie" stays "amelie" and never splits into "ame lie"."""
|
||||||
|
return " ".join("".join(c if c.isalnum() or c == "'" else " "
|
||||||
|
for c in norm(s)).split())
|
||||||
|
|
||||||
|
def title_of(path):
|
||||||
|
"""(title, year) from the basename.
|
||||||
|
|
||||||
|
Two passes. (1) Tokenize; every 4-digit year becomes a sentinel in place.
|
||||||
|
(2) Find the boundary: the first release-noise token (resolution / codec /
|
||||||
|
source / audio / language / group vocabulary) ends the title, so trailing
|
||||||
|
tags can never leak in. (3) Collect up to that boundary: leading index
|
||||||
|
numbers ("03."), leading year sentinels ("1940 - Fantasia") and leading noise
|
||||||
|
are skipped while nothing is collected; once a real word appears a sentinel
|
||||||
|
CLOSES the title. A title sitting BEFORE the year sentinel ("300.2006.…" or
|
||||||
|
"12. Monkeys. 1995") is a numeric title and is prepended to what follows.
|
||||||
|
"""
|
||||||
|
# fold_sep BEFORE tokenizing, so a lookalike separator separates (see its
|
||||||
|
# docstring): the tokens below are then plain alnum/apostrophe words.
|
||||||
|
name = fold_sep(os.path.splitext(os.path.basename(path))[0])
|
||||||
|
raw = [x for x in SEP_RE.split(name) if x]
|
||||||
|
years = [t for t in raw if YEAR_RE.match(t)]
|
||||||
|
two_year = len(years) > 1 # "2001…1968": one of them is the title
|
||||||
|
toks, year = [], None
|
||||||
|
for t in raw:
|
||||||
|
if YEAR_RE.match(t):
|
||||||
|
year = t # last year wins
|
||||||
|
toks.append("\x00")
|
||||||
|
else:
|
||||||
|
toks.append(t)
|
||||||
|
stop, seen_word = len(toks), False
|
||||||
|
for i, t in enumerate(toks):
|
||||||
|
if t == "\x00":
|
||||||
|
continue
|
||||||
|
c = norm(re.sub(r"[^A-Za-z0-9']", "", t))
|
||||||
|
if (not c or RES_RE.match(c) or JUNK_RE.match(c) or c in JUNK
|
||||||
|
or not WORD_RE.match(c)) and not (IDX_RE.match(t) and not seen_word):
|
||||||
|
stop = i
|
||||||
|
break
|
||||||
|
if c.isalpha():
|
||||||
|
seen_word = True
|
||||||
|
# A leading number is a track index ("01. Bye Bye Baby", "05. Casablanca") unless
|
||||||
|
# it is three digits or more ("007", "000", "1408") or the name carries no year
|
||||||
|
# at all ("12 Monkeys", "831"). A 1-2 digit prefix beside a year is an index;
|
||||||
|
# the rare real title that looks like one ("0 Things I Hate About You (1999)")
|
||||||
|
# loses its zero to the parser — Plex's matched title overrides it anyway.
|
||||||
|
lead = re.split(r"[\s._-]", name, maxsplit=1)[0]
|
||||||
|
track_idx = bool(re.fullmatch(r"\d{1,2}[a-z]?", lead)) and bool(year)
|
||||||
|
keep, num_title = [], None
|
||||||
|
for t in toks[:stop]:
|
||||||
|
if t == "\x00":
|
||||||
|
if keep:
|
||||||
|
break # year closes the title
|
||||||
|
if two_year and t is not None and years[0] != year:
|
||||||
|
# two DIFFERENT years ("2001…1968"): the leading one is the title;
|
||||||
|
# "1940 - Fantasia (1940)" repeats one year, so it is not a title
|
||||||
|
num_title = num_title or years[0]
|
||||||
|
continue # leading year: not the title
|
||||||
|
if track_idx and IDX_RE.match(t) and not keep and t == lead:
|
||||||
|
continue # "03." / "1c." ordering
|
||||||
|
c = norm(re.sub(r"[^A-Za-z0-9']", "", t))
|
||||||
|
if not keep and not c.isalpha():
|
||||||
|
num_title = num_title or (c or None)
|
||||||
|
continue # numeric title candidate
|
||||||
|
keep.append(c)
|
||||||
|
if len(keep) >= 8:
|
||||||
|
break
|
||||||
|
keep = [k for k in keep if not re.fullmatch(r"(disc|disk|cd|dvd|part)\d*", k)]
|
||||||
|
title = " ".join(keep)
|
||||||
|
if not keep:
|
||||||
|
# nothing collected: a pure-numeric leading token is the title even when it
|
||||||
|
# sits where the release year sits ("1917.2019.2160p…"), and "300.2006…" too
|
||||||
|
for t in raw:
|
||||||
|
c = norm(re.sub(r"[^A-Za-z0-9']", "", t))
|
||||||
|
if re.fullmatch(r"(?:[12]\d{3}|\d{1,3})", c) and (c != year or two_year):
|
||||||
|
return c, year or "?" # never "2160p" / "1080": they are not pure digits
|
||||||
|
if (num_title and title and not keep[0].isdigit()
|
||||||
|
and (two_year or not track_idx)):
|
||||||
|
# a leading number that only echoes the release year ("1917.2019.2160p…") is
|
||||||
|
# not a title; one that stands alone ("300.2006…", "1900") is
|
||||||
|
title = f"{num_title} {title}"
|
||||||
|
title = title or num_title or norm(name).replace("\x00", " ").strip()[:40]
|
||||||
|
return title, year or "?"
|
||||||
|
|
||||||
|
|
||||||
|
# Bonus-material vocabulary: names that say nothing about the film, so an extras
|
||||||
|
# file otherwise pairs with an extras file of another movie. "Short", "Spot" and
|
||||||
|
# "Clip" are deliberately absent — they are film titles (Short Circuit, Spot,
|
||||||
|
# Clips) — and the running-time guard in is_extra() carries the rest.
|
||||||
|
SEP_CLS = r"[\s._-]+"
|
||||||
|
|
||||||
|
|
||||||
|
def _bonus_rx(word):
|
||||||
|
return r"(?<![a-z0-9])" + re.sub(r"[ \-]+", lambda _: SEP_CLS, word.lower()) + r"(?![a-z0-9])"
|
||||||
|
|
||||||
|
|
||||||
|
EXTRA_WORDS = [
|
||||||
|
"trailer", "teaser", "featurette", "featurettes", "promo", "promotional",
|
||||||
|
"tv spot", "advert", "commercial", "behind the scenes", "making of",
|
||||||
|
"deleted scene", "deleted scenes", "deleted footage", "gag reel", "blooper",
|
||||||
|
"blooper reel", "bloopers", "outtake", "outtakes", "scene selection", "menu",
|
||||||
|
"menus", "gallery", "still gallery", "stills", "poster", "posters",
|
||||||
|
"montage", "preview", "previews", "epilogue", "visual effects",
|
||||||
|
"music video", "singalong", "karaoke", "trivia", "storyboard", "workprint",
|
||||||
|
"test footage", "screen test", "audition", "cast and crew", "isolated score",
|
||||||
|
"picture in picture", "original score", "session piece", "sample",
|
||||||
|
"watermark",
|
||||||
|
]
|
||||||
|
# Words that describe bonus material which can run LONG (a feature-length
|
||||||
|
# commentary-track cut, a documentary about the film). Trusted only below
|
||||||
|
# LONG_BONUS_MAX, so a 40-minute "commentary" release stays a film.
|
||||||
|
EXTRA_LONG_WORDS = ["interview", "interviews", "commentary", "video archive",
|
||||||
|
"archival", "documentary"]
|
||||||
|
EXTRA_NAME_RE = re.compile(
|
||||||
|
"|".join([_bonus_rx(w) for w in EXTRA_WORDS] + [r"scene[\s._-]?\d{1,3}"]), re.I)
|
||||||
|
EXTRA_LONG_NAME_RE = re.compile(
|
||||||
|
"|".join(_bonus_rx(w) for w in EXTRA_LONG_WORDS), re.I)
|
||||||
|
# Bonus-folder vocabulary. fullmatch = the folder IS an extras subtree; a bare
|
||||||
|
# search = it only mentions extras ("... + Extras" box sets hold feature films).
|
||||||
|
EXTRA_FOLDER_RE = re.compile(
|
||||||
|
r"(?:extras?|bonus(?:[\s._-]material)?|specials?|special[\s._-]featurettes?|"
|
||||||
|
r"featurettes?|deleted(?:[\s._-](?:scenes?|footage))?|trailers?|"
|
||||||
|
r"making[\s._-]of|behind[\s._-]the[\s._-]scenes|shorts?|interviews?|"
|
||||||
|
r"archiv(?:al|es)|video[\s._-]archive)", re.I)
|
||||||
|
VIDEO_SUFFIX = (".mkv", ".mp4", ".avi", ".m4v", ".mov", ".m2ts", ".mpg", ".webm")
|
||||||
|
|
||||||
|
# Anything longer than this is a feature film whatever it is called: a 118-minute
|
||||||
|
# file named "The Menu" or "Trailer" is a movie, not bonus material.
|
||||||
|
SHORT_MAX = 45 * 60
|
||||||
|
|
||||||
|
# Long-shaped bonus vocabulary is only believed below this: a 40-minute
|
||||||
|
# commentary cut is an alternate version of the film, not a featurette.
|
||||||
|
LONG_BONUS_MAX = 25 * 60
|
||||||
|
|
||||||
|
|
||||||
|
def name_words(x):
|
||||||
|
"""Bare words of a file or folder name: no release metadata, no year."""
|
||||||
|
x = os.path.splitext(x)[0] if x.lower().endswith(VIDEO_SUFFIX) else x
|
||||||
|
return [w for w in SEP_RE.split(fold_sep(x))
|
||||||
|
if w and not YEAR_RE.match(w) and not RES_RE.match(w)
|
||||||
|
and not JUNK_RE.match(w) and w not in JUNK]
|
||||||
|
|
||||||
|
|
||||||
|
def mentions(short, long_):
|
||||||
|
"""True when one word list appears in the other, anywhere: a release names
|
||||||
|
the film at the front ("Alien (1979) - Trailer") or in the middle
|
||||||
|
("Movies/Alien (1979)/Extra - Logo.mkv"), so a start-of-string rule fails."""
|
||||||
|
a, b = sorted([short, long_], key=len)
|
||||||
|
if not a:
|
||||||
|
return False
|
||||||
|
return any(b[i:i + len(a)] == a for i in range(len(b) - len(a) + 1))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def folder_is_bonus(folder):
|
||||||
|
"""True when a folder name carries bonus vocabulary as WHOLE words: the
|
||||||
|
vocabulary is matched against a padded word string, so "Extras" decides and
|
||||||
|
a word that merely CONTAINS one ("Extraordinary", "The Extras") does not."""
|
||||||
|
return bool(EXTRA_FOLDER_RE.search(" " + " ".join(SEP_RE.split(norm(folder))) + " "))
|
||||||
|
|
||||||
|
|
||||||
|
def is_extra(path, title="", dur=0.0, root=ROOT, matched=False):
|
||||||
|
"""True for bonus material, which must never be counted as a film: a file
|
||||||
|
called "Trailer", "Menu", "Sample" or "Gallery" carries no film identity, so
|
||||||
|
extras otherwise pair with each other across the whole library.
|
||||||
|
|
||||||
|
Evidence, strongest first:
|
||||||
|
* RUNTIME VETOES EVERYTHING. The film "The Menu", the film "Trailer", the
|
||||||
|
franchise "Trailer Park Boys" and "ET — The Extra-Terrestrial" all carry
|
||||||
|
bonus vocabulary and run 100+ minutes: anything over SHORT_MAX is a film —
|
||||||
|
unless it sits in an explicit bonus folder and metadata never matched it.
|
||||||
|
Bonus words that describe long-form material (commentary, documentary,
|
||||||
|
interview) are believed only
|
||||||
|
under LONG_BONUS_MAX, so a feature-length commentary cut stays a film.
|
||||||
|
* The file's OWN name carries bonus vocabulary -> bonus.
|
||||||
|
* ANY folder of the path below the film's own root folder carries bonus
|
||||||
|
vocabulary ("Alien (1979)/Extras/", ".../Featurettes/Deleted Scenes/",
|
||||||
|
"Angel's Egg (1985)/04b. Angel's Egg Extras/") -> bonus. The ROOT folder
|
||||||
|
is exempt because it names the film, and a film may be titled with a
|
||||||
|
bonus word; and a file whose own name IS that folder's film name is the
|
||||||
|
feature, not the bonus ("Ghost in the Shell Movies/Extras/
|
||||||
|
Ghost in the Shell - CM3.mp4" is a bonus, but a whole other film filed
|
||||||
|
there stays a film).
|
||||||
|
Bonus vocabulary is matched as whole words only, so the film
|
||||||
|
"Angel's Egg - Extra - Logo" inside "04b. Angel's Egg Extras" is reached by
|
||||||
|
the folder rule, and "…Extras" as a fragment of a word is never evidence.
|
||||||
|
"""
|
||||||
|
parts = [q for q in path.split("/") if q]
|
||||||
|
if not parts:
|
||||||
|
return False
|
||||||
|
name = (os.path.splitext(parts[-1])[0]
|
||||||
|
if parts[-1].lower().endswith(VIDEO_SUFFIX) else "")
|
||||||
|
# A bonus folder anywhere BELOW the film's own folder decides for its
|
||||||
|
# contents; the film's own folder is the film's name, whatever words it
|
||||||
|
# contains, so it is exempt. It is the first component AFTER ROOT's own
|
||||||
|
# components — those are the mount path, not folders of the library.
|
||||||
|
head = len([q for q in root.split("/") if q]) + 1
|
||||||
|
bonus_folder = False
|
||||||
|
for folder in parts[head:-1]:
|
||||||
|
if folder_is_bonus(folder):
|
||||||
|
# The folder's words are often the film's own name ("Angel's Egg
|
||||||
|
# Extras/"), and then they are not bonus evidence. Only WORDS can name
|
||||||
|
# a film: "extras" is release junk that name_words drops, so a folder
|
||||||
|
# like "01. Extras" reduces to its index — and an index matching an
|
||||||
|
# index ("NCOP 01") is a coincidence, not a title.
|
||||||
|
nw = [w for w in name_words(name) if not w.isdigit()]
|
||||||
|
fw = [w for w in name_words(folder) if not w.isdigit()]
|
||||||
|
bonus_folder = not fw or not mentions(nw, fw)
|
||||||
|
break
|
||||||
|
if dur and dur > SHORT_MAX:
|
||||||
|
# Runtime vetoes vocabulary, with ONE exception: an explicit bonus folder
|
||||||
|
# and no metadata match. A 90-minute "Feature Length Rough Cut" sitting in
|
||||||
|
# a movie's Featurettes/ is a disc extra, and nothing in the library claims
|
||||||
|
# it as a film. Long-form bonus words still describe a film, so a
|
||||||
|
# feature-length commentary cut or documentary keeps its runtime veto.
|
||||||
|
if bonus_folder and not matched and not EXTRA_LONG_NAME_RE.search(name):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
if EXTRA_NAME_RE.search(name):
|
||||||
|
return True
|
||||||
|
if dur and dur <= LONG_BONUS_MAX and EXTRA_LONG_NAME_RE.search(name):
|
||||||
|
return True
|
||||||
|
return bonus_folder
|
||||||
|
|
||||||
|
# Release-group watermark clips: a group drops a tiny ad ("ETRG.mp4",
|
||||||
|
# "RARBG.com.mp4") into many different films, so its name carries no film
|
||||||
|
# identity at all and it would otherwise pair with itself across the library.
|
||||||
|
WATERMARK_MAX = 20 * 2**20
|
||||||
|
WATERMARK_RE = re.compile(r"(?i)^(rarbg(\.com)?|etrg|tgx|fbt|cpbg|yify|yts|"
|
||||||
|
r"rarbg\.com|playnomore|imprs|gauntlet|dfx|macmote|"
|
||||||
|
r"psi-axiz|acm|tlf|sa8|ebp|gtv|w4g|don|aAF|enjoy|hd4u)$")
|
||||||
|
|
||||||
|
|
||||||
|
def is_watermark(path, size):
|
||||||
|
"""True for a release group's own watermark video, not a film or an extra."""
|
||||||
|
return (size < WATERMARK_MAX
|
||||||
|
and bool(WATERMARK_RE.match(os.path.splitext(os.path.basename(path))[0])))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def key_of(title):
|
||||||
|
"""Franchise key: first two alphabetic tokens (numeric-only title = itself).
|
||||||
|
Deliberately NOT film-specific — it only selects candidates for the duration
|
||||||
|
check, so a two-token collision (two films starting "star trek") cannot by
|
||||||
|
itself create a duplicate group."""
|
||||||
|
toks = [t for t in title.split() if t]
|
||||||
|
words = [t for t in toks if not t.isdigit()]
|
||||||
|
if not words:
|
||||||
|
return " ".join(toks)
|
||||||
|
sig = [t for t in words if t not in STOP] or words
|
||||||
|
return " ".join(sig[:2])
|
||||||
|
|
||||||
|
|
||||||
|
def ep_of(path):
|
||||||
|
"""(series, episode-token) when the basename carries SxxEyy / 1x04 / Part N.
|
||||||
|
|
||||||
|
The series is the name BEFORE the marker. The box-set convention writes
|
||||||
|
"Monk - S05E03 - Mr. Monk and the Big Game", and the episode title that
|
||||||
|
follows the marker is not the show's name: keying on the whole title gave
|
||||||
|
every episode of one series its own group, so a 124-episode box set never
|
||||||
|
reached the size that calls something a series and each episode was filed
|
||||||
|
as its own film. A file named only by its marker has nothing before it, so
|
||||||
|
its whole title stands in."""
|
||||||
|
name = os.path.splitext(os.path.basename(path))[0]
|
||||||
|
m = EP_RE.search(name)
|
||||||
|
if not m:
|
||||||
|
return None
|
||||||
|
head = name[:m.start()].strip(" ._\u2013-")
|
||||||
|
series = title_of(head)[0] if head else None
|
||||||
|
return series or title_of(path)[0], norm(m.group(1)).replace(" ", "")
|
||||||
|
|
||||||
|
|
||||||
|
def base_words(title, key):
|
||||||
|
"""Title tokens beyond the franchise key — the FILM identity part.
|
||||||
|
"evil dead rise" under key "evil dead" -> {rise}; "28 days later" -> {}."""
|
||||||
|
kt = set(key.split())
|
||||||
|
return frozenset(t for t in title.split() if t not in kt and t not in STOP
|
||||||
|
and not t.isdigit() and not YEAR_RE.match(t))
|
||||||
|
|
||||||
|
|
||||||
|
def folder_year(path):
|
||||||
|
"""Soft year evidence: a 19xx/20xx token in any parent directory name
|
||||||
|
(collection folders are written both "(2002)" and "[2009]"; a range such as
|
||||||
|
"(1998-2000)" deliberately yields nothing)."""
|
||||||
|
for d in reversed(os.path.dirname(path).split("/")):
|
||||||
|
ys = [y for y in re.split(r"[^0-9]+", d) if YEAR_RE.match(y)]
|
||||||
|
if len(ys) == 1:
|
||||||
|
return ys[0]
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def same_film(a, b):
|
||||||
|
"""Two files are copies of ONE film only when the film-identity tokens agree
|
||||||
|
(one side may carry none) AND no year evidence contradicts them. The key says
|
||||||
|
the franchise matches; this says the actual film matches — it is what keeps
|
||||||
|
'Evil Dead' (2013) apart from 'Evil Dead Rise' (2023) and the four Austin
|
||||||
|
Powers apart when their runtimes happen to agree."""
|
||||||
|
ba, bb = a["base"], b["base"]
|
||||||
|
if ba and bb and ba != bb:
|
||||||
|
return False
|
||||||
|
ya = a["year"] if a["year"] != "?" else a["soft"]
|
||||||
|
yb = b["year"] if b["year"] != "?" else b["soft"]
|
||||||
|
if ya and yb and ya != yb:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def dur_close(a, b):
|
||||||
|
if a <= 0 or b <= 0:
|
||||||
|
return True # unknown duration: let titles decide
|
||||||
|
return abs(a - b) <= max(2.0, 0.003 * max(a, b))
|
||||||
|
|
||||||
|
|
||||||
|
# Blu-ray folder trees: ffprobe only sees the clip it is pointed at, so each tree
|
||||||
|
# is folded into ONE entry, described by its longest main-feature clip (measured
|
||||||
|
# with ffprobe on the largest BDMV/STREAM file) and sized by the whole tree.
|
||||||
|
BDMV_MAIN = {
|
||||||
|
"Avengers.Age.of.Ultron.2015.2160p.BluRay.HEVC.TrueHD.7.1.Atmos-TERMiNAL":
|
||||||
|
dict(dur=8478.2, vcodec="hevc", res="3840x2160", pix="yuv420p10le", fps=23.976),
|
||||||
|
"Full.Metal.Jacket.1987.2160p.BluRay.HEVC.DTS-HD.MA.5.1-EATDIK":
|
||||||
|
dict(dur=6993.3, vcodec="hevc", res="3840x2160", pix="yuv420p10le", fps=23.976),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def load(probed):
|
||||||
|
"""Probe rows -> film entries. AppleDouble (._*) sidecars are dropped; each
|
||||||
|
BDMV tree collapses to one entry carrying the tree's total size."""
|
||||||
|
trees = {}
|
||||||
|
rows = []
|
||||||
|
for line in open(probed):
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if len(f) < 12:
|
||||||
|
continue
|
||||||
|
b, p, dur, vc, res, fps, vbit, pix, aud, subs, _d, fmt = f[:12]
|
||||||
|
base = os.path.basename(p)
|
||||||
|
if base.startswith("._") or base == ".DS_Store":
|
||||||
|
continue # macOS sidecar, not media
|
||||||
|
m = re.search(r"/(BDMV|VIDEO_TS)/", p)
|
||||||
|
root = p[:m.start()] if m else None
|
||||||
|
if root or re.search(r"/(CERTIFICATE|AACS|MLPI|CLIPINF|PLAYLIST|BACKUP|REGISTRL)/", p):
|
||||||
|
if not root:
|
||||||
|
continue # disc bookkeeping: folds into its tree, never a film
|
||||||
|
if root:
|
||||||
|
t = trees.setdefault(root, dict(bytes=0, audio=set(), subs=0))
|
||||||
|
t["bytes"] += int(b)
|
||||||
|
t["audio"].update(a for a in aud.split("+") if a)
|
||||||
|
t["subs"] += int(subs or 0)
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
dur = float(dur)
|
||||||
|
except ValueError:
|
||||||
|
dur = 0.0
|
||||||
|
rows.append(dict(bytes=int(b), path=p, dur=dur, vcodec=vc, res=res, fps=fps,
|
||||||
|
vbit=int(float(vbit or 0)), pix=pix, audio=aud, subs=subs, fmt=fmt))
|
||||||
|
for root, t in trees.items():
|
||||||
|
folder = os.path.basename(root)
|
||||||
|
m = BDMV_MAIN.get(folder, {})
|
||||||
|
rows.append(dict(bytes=t["bytes"], path=root + "/BDMV/index.bdmv",
|
||||||
|
name_src=folder, dur=m.get("dur", 0.0),
|
||||||
|
vcodec=m.get("vcodec", "hevc"), res=m.get("res", "-"),
|
||||||
|
fps=m.get("fps", 0), vbit=0, pix=m.get("pix", "-"),
|
||||||
|
audio="+".join(sorted(t["audio"])), subs=str(t["subs"]),
|
||||||
|
fmt="BDMV tree"))
|
||||||
|
for r in rows:
|
||||||
|
r["title"], r["year"] = title_of(r.get("name_src") or r["path"])
|
||||||
|
r["key"] = key_of(r["title"])
|
||||||
|
r["base"] = base_words(r["title"], r["key"])
|
||||||
|
r["soft"] = folder_year(r["path"])
|
||||||
|
r["ep"] = ep_of(r["path"])
|
||||||
|
return rows
|
||||||
|
|
||||||
|
|
||||||
|
def dur_clusters(items):
|
||||||
|
"""Group files that are the SAME FILM and the SAME MASTER duration. Candidate
|
||||||
|
buckets come from the franchise key; same_film then rejects the different
|
||||||
|
installments whose runtimes happen to agree."""
|
||||||
|
clusters = []
|
||||||
|
for r in sorted(items, key=lambda r: -r["dur"]):
|
||||||
|
for c in clusters:
|
||||||
|
if dur_close(c[0]["dur"], r["dur"]) and same_film(c[0], r):
|
||||||
|
c.append(r)
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
clusters.append([r])
|
||||||
|
return clusters
|
||||||
|
|
||||||
|
|
||||||
|
def tier(groups):
|
||||||
|
"""Split same-film/same-duration clusters: DUP = different releases of one
|
||||||
|
film (real, deletable redundancy); REVIEW = identical size AND duration
|
||||||
|
(byte-twins — the hash pass is the authority) or clashing year evidence."""
|
||||||
|
dup, review = [], []
|
||||||
|
for g in groups:
|
||||||
|
g.sort(key=lambda r: -r["bytes"])
|
||||||
|
years = {(r["year"] if r["year"] != "?" else r["soft"]) for r in g}
|
||||||
|
years.discard(None)
|
||||||
|
spread = (g[0]["bytes"] - g[-1]["bytes"]) / g[0]["bytes"] if g[0]["bytes"] else 0
|
||||||
|
if len(years) > 1 or spread < 0.005:
|
||||||
|
review.append(g)
|
||||||
|
else:
|
||||||
|
dup.append(g)
|
||||||
|
return dup, review
|
||||||
|
|
||||||
|
|
||||||
|
def redun(g):
|
||||||
|
return sum(r["bytes"] for r in sorted(g, key=lambda r: -r["bytes"])[1:])
|
||||||
|
|
||||||
|
|
||||||
|
def line(r):
|
||||||
|
return (f"{r['bytes']/2**30:8.2f} GiB {r['res']:>9} {r['vcodec']:<7} "
|
||||||
|
f"{r['pix']:<11} {r['dur']/60:5.0f}m {r['audio'][:34]:<34}")
|
||||||
|
|
||||||
|
|
||||||
|
def dump(tag, groups, out, note=""):
|
||||||
|
for gi, g in enumerate(groups, 1):
|
||||||
|
g.sort(key=lambda r: -r["bytes"])
|
||||||
|
print(f"\n### {tag}-{gi}. {g[0]['title']} ({g[0]['year']}) — {len(g)} files, "
|
||||||
|
f"{redun(g)/2**30:.1f} GiB redundant {note}".rstrip())
|
||||||
|
for rank, r in enumerate(g):
|
||||||
|
print(f" {line(r)} [{r['year']}] "
|
||||||
|
f"{r['path'].replace(ROOT + '/', '')}")
|
||||||
|
out.write(f"{tag}\t{gi}\t{rank}\t{r['bytes']}\t{r['res']}\t{r['vcodec']}\t"
|
||||||
|
f"{r['pix']}\t{int(r['dur'])}\t{r['audio']}\t{r['title']}|{r['year']}\t"
|
||||||
|
f"{r['path']}\n")
|
||||||
|
|
||||||
|
|
||||||
|
def report(probed, out_path):
|
||||||
|
rows = load(probed)
|
||||||
|
for r in rows:
|
||||||
|
r["mark"] = is_watermark(r["path"], r["bytes"])
|
||||||
|
r["extra"] = not r["mark"] and is_extra(r["path"], r["title"], r["dur"])
|
||||||
|
marks = [r for r in rows if r["mark"]]
|
||||||
|
movies = [r for r in rows if not r["ep"] and not r["extra"] and not r["mark"]]
|
||||||
|
episodes = [r for r in rows if r["ep"] and not r["extra"] and not r["mark"]]
|
||||||
|
extras = [r for r in rows if r["extra"]]
|
||||||
|
print(f"census: {len(rows)} video entries = {len(movies)} films + {len(episodes)} "
|
||||||
|
f"TV episodes + {len(extras)} bonus/trailers "
|
||||||
|
f"({sum(r['bytes'] for r in extras)/2**30:.1f} GiB) "
|
||||||
|
f"+ {len(marks)} release-group watermarks")
|
||||||
|
|
||||||
|
out = open(out_path, "w")
|
||||||
|
|
||||||
|
buckets = collections.defaultdict(list)
|
||||||
|
for r in movies:
|
||||||
|
buckets[r["key"]].append(r)
|
||||||
|
cand = {k: v for k, v in buckets.items() if len(v) > 1}
|
||||||
|
clusters = [c for v in cand.values() for c in dur_clusters(v) if len(c) > 1]
|
||||||
|
dup, review = tier(clusters)
|
||||||
|
dup.sort(key=lambda g: -redun(g))
|
||||||
|
review.sort(key=lambda g: -redun(g))
|
||||||
|
dump("DUP", dup, out)
|
||||||
|
dump("REVIEW", review, out, "— same key+duration, sizes near-identical OR years clash: RECHECK")
|
||||||
|
|
||||||
|
|
||||||
|
ebuf = collections.defaultdict(list)
|
||||||
|
for r in episodes:
|
||||||
|
ebuf[(r["ep"][0], r["ep"][1])].append(r)
|
||||||
|
edup, ereview = tier([c for v in ebuf.values() if len(v) > 1
|
||||||
|
for c in dur_clusters(v) if len(c) > 1])
|
||||||
|
edup.sort(key=lambda g: -redun(g))
|
||||||
|
dump("EP-DUP", edup, out)
|
||||||
|
|
||||||
|
shows = collections.Counter()
|
||||||
|
for r in episodes:
|
||||||
|
shows[r["ep"][0]] += 1
|
||||||
|
multi = {s: n for s, n in shows.items() if n > 4}
|
||||||
|
print("\n=== TV-series content inside Movies/ (>4 episodes) ===")
|
||||||
|
for s, n in sorted(multi.items(), key=lambda kv: -kv[1]):
|
||||||
|
print(f" {n:4d} episodes {s}")
|
||||||
|
out.close()
|
||||||
|
|
||||||
|
print(f"\nDUP groups={len(dup)} files={sum(len(g) for g in dup)} "
|
||||||
|
f"redundant={sum(redun(g) for g in dup)/2**40:.2f} TiB", file=sys.stderr)
|
||||||
|
print(f"REVIEW groups={len(review)} "
|
||||||
|
f"redundant={sum(redun(g) for g in review)/2**40:.2f} TiB", file=sys.stderr)
|
||||||
|
print(f"EP-DUP groups={len(edup)} "
|
||||||
|
f"ep-redundant={sum(redun(g) for g in edup)/2**30:.1f} GiB", file=sys.stderr)
|
||||||
|
|
||||||
|
|
||||||
|
def parse(names):
|
||||||
|
for n in names:
|
||||||
|
t, y = title_of(n)
|
||||||
|
print(f"{t!r} ({y})\tkey={key_of(t)!r}\t{n[:70]}")
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||||
|
sub = ap.add_subparsers(dest="cmd", required=True)
|
||||||
|
rep = sub.add_parser("report", help="similar-copy census over a probe TSV")
|
||||||
|
rep.add_argument("--probed", default="/tmp/media-probed.tsv")
|
||||||
|
rep.add_argument("--out", default="/tmp/media-similar.tsv")
|
||||||
|
par = sub.add_parser("parse", help="show the parsed title/year of a name")
|
||||||
|
par.add_argument("names", nargs="+")
|
||||||
|
a = ap.parse_args()
|
||||||
|
if a.cmd == "report":
|
||||||
|
report(a.probed, a.out)
|
||||||
|
else:
|
||||||
|
parse(a.names)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Executable
+281
@@ -0,0 +1,281 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""media-tv.py — move TV episodes out of the movie library into a TV library.
|
||||||
|
|
||||||
|
`scripts/media-plan.py` refuses to file TV: it parks episode files in a
|
||||||
|
`_TV needs a TV library - <series>` pile for a human. This is the other half of
|
||||||
|
that refusal. It walks the movie library, groups the files whose own name
|
||||||
|
carries an episode marker by SERIES, and writes a plan that places each series
|
||||||
|
in the shape both Jellyfin and Plex read without help:
|
||||||
|
|
||||||
|
<library>/<Series> (<year>)/Season <NN>/<the file's own name>.<ext>
|
||||||
|
|
||||||
|
It writes a plan and nothing else. Every rename belongs to
|
||||||
|
`scripts/media-apply.py` — the write-ahead journal, the refusal to overwrite,
|
||||||
|
the all-or-nothing preflight, the rollback. Apply it with `--root` set to the
|
||||||
|
share that holds BOTH libraries (`/mnt/entropy`, not `/mnt/entropy/Movies`) so
|
||||||
|
the executor's own path guard still has teeth, and give it its own journal —
|
||||||
|
one journal is one undo, and a TV move must not be welded to a movie relayout:
|
||||||
|
|
||||||
|
python3 scripts/media-tv.py --root /mnt/entropy/Movies \
|
||||||
|
--library /mnt/entropy/TVShows --out /mnt/entropy/media-tv-plan.tsv
|
||||||
|
python3 scripts/media-apply.py apply --plan /mnt/entropy/media-tv-plan.tsv \
|
||||||
|
--root /mnt/entropy --journal /mnt/entropy/media-relayout-tv.jsonl
|
||||||
|
python3 scripts/media-apply.py verify --plan /mnt/entropy/media-tv-plan.tsv \
|
||||||
|
--root /mnt/entropy --journal /mnt/entropy/media-relayout-tv.jsonl
|
||||||
|
python3 scripts/media-apply.py sweep --root /mnt/entropy \
|
||||||
|
--journal /mnt/entropy/media-relayout-tv.jsonl --apply
|
||||||
|
|
||||||
|
Guards, and why each one is here:
|
||||||
|
|
||||||
|
* The same five-file threshold as the planner. An episode-shaped name on one
|
||||||
|
file is not a series — `The Godfather Part 2` is a film — so a smaller group
|
||||||
|
is reported and left exactly where it is.
|
||||||
|
* An episode whose name carries no season number is reported and left, never
|
||||||
|
filed into an invented `Season 01`.
|
||||||
|
* A sidecar that shares an episode's own name (`.srt`, `.nfo`, …) follows the
|
||||||
|
episode; an episode without its subtitles is a half-move.
|
||||||
|
* A group that would claim one destination twice is not resolved here. The
|
||||||
|
executor refuses the whole plan before its first rename, which is the right
|
||||||
|
outcome: nobody but Frank decides which copy of an episode is the keeper.
|
||||||
|
* An episode the destination library already holds at the same byte count is
|
||||||
|
left where it is. The TV library has held a complete Monk box set for years
|
||||||
|
while the movie library carried a second copy of all 124 episodes; a mover
|
||||||
|
that did not look where it was filing would have put both inside one library.
|
||||||
|
* A symlink is never followed and never moved.
|
||||||
|
"""
|
||||||
|
import argparse
|
||||||
|
import collections
|
||||||
|
import importlib.util
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def parse_args():
|
||||||
|
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||||
|
ap.add_argument("--root", default=os.environ.get("MEDIA_ROOT", "/mnt/entropy/Movies"),
|
||||||
|
help="the movie library to empty of TV")
|
||||||
|
ap.add_argument("--library", default="/mnt/entropy/TVShows",
|
||||||
|
help="the TV library to fill (a different share or folder)")
|
||||||
|
ap.add_argument("--out", default=None,
|
||||||
|
help="plan TSV for scripts/media-apply.py"
|
||||||
|
" (default: media-tv-plan.tsv beside --root)")
|
||||||
|
ap.add_argument("--min", type=int, default=5,
|
||||||
|
help="episode files that make something a series")
|
||||||
|
a = ap.parse_args()
|
||||||
|
if not a.out:
|
||||||
|
a.out = os.path.join(os.path.dirname(os.path.abspath(a.root)),
|
||||||
|
"media-tv-plan.tsv")
|
||||||
|
return a
|
||||||
|
|
||||||
|
|
||||||
|
ARGS = parse_args()
|
||||||
|
ENGINE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "media-titles.py")
|
||||||
|
spec = importlib.util.spec_from_file_location("eng", ENGINE)
|
||||||
|
eng = importlib.util.module_from_spec(spec)
|
||||||
|
spec.loader.exec_module(eng)
|
||||||
|
|
||||||
|
BAD_CHARS = re.compile(r'[<>:"/|?*]')
|
||||||
|
# The season and episode, from the token media-titles.ep_of() hands back: s05e03, 1x04.
|
||||||
|
SEASON_RE = re.compile(r"^(?:s(\d{1,2})e(\d{1,4})|(\d{1,2})x(\d{1,4}))$")
|
||||||
|
# Sidecars that follow an episode; macOS forks and resume data do not.
|
||||||
|
SKIP_SIDECAR = re.compile(r"^(\._|\.DS_|dl_state)")
|
||||||
|
# A box set that stamps the show's year into every file name; the folder's year
|
||||||
|
# must not be added on top of it.
|
||||||
|
YEAR_TAIL = re.compile(r"[(\[]\s*(18|19|20)\d\d\s*[)\]]\s*$")
|
||||||
|
SMALL = {"a", "an", "the", "and", "of", "for", "in", "on", "to", "with", "at",
|
||||||
|
"by", "de", "la", "vs"}
|
||||||
|
|
||||||
|
|
||||||
|
def clean(name):
|
||||||
|
"""A name the filesystem and both matchers can read (same law as the plan)."""
|
||||||
|
name = BAD_CHARS.sub("-", name).replace("\\", "-")
|
||||||
|
name = re.sub(r"\s+", " ", name).strip().strip(". ").strip()
|
||||||
|
return name[:180] or "untitled"
|
||||||
|
|
||||||
|
|
||||||
|
def episode_name(path):
|
||||||
|
"""The file's own name, kept: the publisher's spelling beats a re-derivation.
|
||||||
|
|
||||||
|
A dot-separated release name (`monk.s05e03.mkv`) becomes words; a name that
|
||||||
|
already carries spaces is left exactly as it was written."""
|
||||||
|
stem, ext = os.path.splitext(os.path.basename(path))
|
||||||
|
if " " not in stem and stem.count(".") >= 2:
|
||||||
|
stem = stem.replace(".", " ")
|
||||||
|
return clean(stem) + ext.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def head_of(path):
|
||||||
|
"""The publisher's series name: the text BEFORE the episode marker.
|
||||||
|
|
||||||
|
`Monk - S05E03 - Mr. Monk and the Big Game` is `Monk`; the episode title
|
||||||
|
after the marker is not the show's name, and case is kept because it is the
|
||||||
|
library's own spelling, not a normalised key."""
|
||||||
|
name = os.path.splitext(os.path.basename(path))[0]
|
||||||
|
m = eng.EP_RE.search(name)
|
||||||
|
head = name[:m.start()] if m else name
|
||||||
|
return re.sub(r"\s+", " ", re.sub(r"[._]+", " ", head)).strip(" -\u2013").strip()
|
||||||
|
|
||||||
|
|
||||||
|
def title_series(head):
|
||||||
|
"""The series folder in the case a TV library is written in.
|
||||||
|
|
||||||
|
Only a name with no capital at all is retitled: a box set that was
|
||||||
|
lower-cased on the way in would otherwise file `monk (2002)` among
|
||||||
|
`Dexter's Laboratory (1996)`. A name the publisher wrote with case is left
|
||||||
|
exactly as it was written."""
|
||||||
|
if any(c.isupper() for c in head):
|
||||||
|
return head # the publisher wrote it with case; keep it
|
||||||
|
words = head.split()
|
||||||
|
return " ".join(w if i and w.lower() in SMALL else w.capitalize()
|
||||||
|
for i, w in enumerate(words))
|
||||||
|
|
||||||
|
|
||||||
|
def season_episode(token):
|
||||||
|
"""(season, episode) an episode token states, or None when it states none."""
|
||||||
|
m = SEASON_RE.match(token)
|
||||||
|
if not m:
|
||||||
|
return None
|
||||||
|
return int(m.group(1) or m.group(3)), int(m.group(2) or m.group(4))
|
||||||
|
|
||||||
|
|
||||||
|
def sidecars(path):
|
||||||
|
"""The files beside PATH that carry its exact stem — its subtitles, its nfo."""
|
||||||
|
d, stem = os.path.split(path)
|
||||||
|
stem = os.path.splitext(stem)[0]
|
||||||
|
out = []
|
||||||
|
try:
|
||||||
|
entries = sorted(os.listdir(d))
|
||||||
|
except OSError:
|
||||||
|
return out
|
||||||
|
for e in entries:
|
||||||
|
p = os.path.join(d, e)
|
||||||
|
if p == path or os.path.islink(p) or not os.path.isfile(p):
|
||||||
|
continue
|
||||||
|
s, _ext = os.path.splitext(e)
|
||||||
|
if s == stem and not SKIP_SIDECAR.match(e):
|
||||||
|
out.append(p)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def episodes_under(root):
|
||||||
|
"""(path, series, token) for every episode-shaped video file under root."""
|
||||||
|
for dirpath, dirnames, filenames in os.walk(root):
|
||||||
|
dirnames[:] = sorted(d for d in dirnames
|
||||||
|
if not d.startswith(".") and d != "@eaDir")
|
||||||
|
for f in filenames:
|
||||||
|
p = os.path.join(dirpath, f)
|
||||||
|
if os.path.islink(p) or not f.lower().endswith(eng.VIDEO_SUFFIX):
|
||||||
|
continue
|
||||||
|
e = eng.ep_of(p)
|
||||||
|
if e:
|
||||||
|
yield p, e[0], e[1]
|
||||||
|
|
||||||
|
|
||||||
|
def walk(root, library):
|
||||||
|
"""Episode files under ROOT keyed by series, ignoring the destination."""
|
||||||
|
groups = collections.defaultdict(list)
|
||||||
|
lib = os.path.abspath(library)
|
||||||
|
for p, series, token in episodes_under(root):
|
||||||
|
if os.path.abspath(p).startswith(lib + os.sep):
|
||||||
|
continue
|
||||||
|
groups[series].append((p, token))
|
||||||
|
return groups
|
||||||
|
|
||||||
|
|
||||||
|
def holdings(library):
|
||||||
|
"""What the destination already carries: (series, season, episode) -> sizes.
|
||||||
|
|
||||||
|
The one expensive read here, and the one that matters. The movie library
|
||||||
|
holds a complete Monk box set that the TV library has held for years: 124
|
||||||
|
episodes, the same episode list, identical byte counts. Moving it would
|
||||||
|
have filed a second copy of a whole show into the library that already had
|
||||||
|
it. Byte count is the test on purpose — two encodes of one episode are two
|
||||||
|
files and both are wanted; the same bytes are the same file."""
|
||||||
|
out = collections.defaultdict(set)
|
||||||
|
for p, series, token in episodes_under(library):
|
||||||
|
se = season_episode(token)
|
||||||
|
if se is None:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
out[(series, *se)].add(os.path.getsize(p))
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
root, library = os.path.abspath(ARGS.root), os.path.abspath(ARGS.library)
|
||||||
|
groups = walk(root, library)
|
||||||
|
held_here = holdings(library)
|
||||||
|
mkdirs = {library}
|
||||||
|
rows, lines, skipped = set(), [], []
|
||||||
|
for series, items in sorted(groups.items()):
|
||||||
|
if len(items) < ARGS.min:
|
||||||
|
skipped.append(f"skipped: {head_of(items[0][0]) or series} — {len(items)} "
|
||||||
|
f"episode-shaped file{'s' if len(items) != 1 else ''},"
|
||||||
|
f" below {ARGS.min}: not a series")
|
||||||
|
continue
|
||||||
|
years = collections.Counter(y for y in (eng.folder_year(p) for p, _t in items)
|
||||||
|
if y)
|
||||||
|
year = years.most_common(1)[0][0] if years else None
|
||||||
|
heads = collections.Counter(head_of(p) for p, _t in items)
|
||||||
|
head = heads.most_common(1)[0][0]
|
||||||
|
if year and YEAR_TAIL.search(head):
|
||||||
|
year = None
|
||||||
|
name = clean(f"{title_series(head)}{f' ({year})' if year else ''}")
|
||||||
|
series_dir = os.path.join(library, name)
|
||||||
|
moved = held = 0
|
||||||
|
for path, token in items:
|
||||||
|
se = season_episode(token)
|
||||||
|
if se is None:
|
||||||
|
skipped.append(f"skipped: {path} — no season number in its name,"
|
||||||
|
" nothing here invents one")
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
size = os.path.getsize(path)
|
||||||
|
except OSError:
|
||||||
|
continue
|
||||||
|
if size in held_here.get((series, *se), ()):
|
||||||
|
held += 1
|
||||||
|
continue
|
||||||
|
dst_dir = os.path.join(series_dir, f"Season {se[0]:02d}")
|
||||||
|
mkdirs.update([series_dir, dst_dir])
|
||||||
|
base = episode_name(path)
|
||||||
|
rows.add((path, os.path.join(dst_dir, base)))
|
||||||
|
moved += 1
|
||||||
|
for side in sidecars(path):
|
||||||
|
rows.add((side, os.path.join(
|
||||||
|
dst_dir, clean(os.path.splitext(base)[0]
|
||||||
|
+ os.path.splitext(side)[1].lower()))))
|
||||||
|
if moved:
|
||||||
|
lines.append(f"{name}: {moved} episodes"
|
||||||
|
f"{'' if year else ' (no year in any folder name)'}"
|
||||||
|
f" -> {series_dir}")
|
||||||
|
if held:
|
||||||
|
lines.append(f"{name}: {held}"
|
||||||
|
f" {'episode' if held == 1 else 'episodes'} already in"
|
||||||
|
f" {library} at the same byte count — left there")
|
||||||
|
|
||||||
|
# An empty plan creates nothing, not even the library folder: a run that
|
||||||
|
# found no series must not be able to leave a stray directory behind.
|
||||||
|
dirs = sorted(mkdirs, key=lambda p: (p.count(os.sep), p)) if rows else []
|
||||||
|
with open(ARGS.out, "w") as out:
|
||||||
|
out.write("action\tsrc\tdst\tkind\n")
|
||||||
|
for d in dirs:
|
||||||
|
out.write(f"mkdir\t\t{d}\tdir\n")
|
||||||
|
for src, dst in sorted(rows):
|
||||||
|
out.write(f"move\t{src}\t{dst}\ttv\n")
|
||||||
|
|
||||||
|
for line in lines + skipped:
|
||||||
|
print(line)
|
||||||
|
print(f"plan: {len(rows)} moves, {len(dirs)} folders -> {ARGS.out}")
|
||||||
|
if rows:
|
||||||
|
print(f"next: python3 scripts/media-apply.py apply --plan {ARGS.out}"
|
||||||
|
f" --root {os.path.dirname(root)}"
|
||||||
|
f" --journal {os.path.join(os.path.dirname(root),'media-relayout-tv.jsonl')}"
|
||||||
|
" (add --apply to run it)")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
Executable
+241
@@ -0,0 +1,241 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# The SSD-backed metadata view that lets Jellyfin and Plex share one metadata set.
|
||||||
|
#
|
||||||
|
# Both servers read a Kodi-format sidecar (movie.nfo / tvshow.nfo / episode.nfo)
|
||||||
|
# ONLY when it sits next to the media file. The media lives on the RAID6 array at
|
||||||
|
# /mnt/entropy — five spinning disks, deliberately mounted read-only for every
|
||||||
|
# container, and slow to seek for a thousand tiny XML reads. So neither media
|
||||||
|
# server binds the array any more: they bind an OverlayFS merge of it with a
|
||||||
|
# writable layer on the NVMe.
|
||||||
|
#
|
||||||
|
# lowerdir /mnt/entropy RAID6 (ROTA=1) — source of truth, never written
|
||||||
|
# upperdir /var/lib/media-meta/upper NVMe — every sidecar and image byte
|
||||||
|
# workdir /var/lib/media-meta/work NVMe — kernel bookkeeping
|
||||||
|
# Where /var/lib/media-meta/view the merged tree jellyfin and plex bind
|
||||||
|
#
|
||||||
|
# Everything the view needs — layers, bookkeeping, trash, the merged door — lives
|
||||||
|
# under the one directory /var/lib/media-meta, so nothing new appears under /mnt.
|
||||||
|
#
|
||||||
|
# Consequences worth knowing:
|
||||||
|
# * A sidecar write is a few KB onto the SSD; the media bytes never move and the
|
||||||
|
# array is still never written by anything automated.
|
||||||
|
# * Deleting through the view writes a whiteout into the upper layer instead of
|
||||||
|
# deleting from the array. Nothing should delete through the view: deletions
|
||||||
|
# go to .AGENTTRASH on the RAW path, and `reap` clears the resulting ghosts.
|
||||||
|
# * inotify events on the lower layer do not cross the merge, so a file that
|
||||||
|
# arrives on the raw array is invisible to a file-system watcher in the view:
|
||||||
|
# trigger a library refresh (see the Media Relayout page).
|
||||||
|
#
|
||||||
|
# Subcommands
|
||||||
|
# plan [--upper-base D] [--lower D] print the resolved shape, touch nothing
|
||||||
|
# verify prove the live mount IS that shape
|
||||||
|
# install [--upper-base D] create the layers, install+enable+start the unit
|
||||||
|
# reap [--apply] [--upper-base D] [--lower D]
|
||||||
|
# list upper-layer entries whose media is gone
|
||||||
|
# status one line per fact a human wants
|
||||||
|
#
|
||||||
|
# install must run as root (it writes /etc/systemd/system and mounts); the others
|
||||||
|
# are read-only and safe unprivileged.
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
LOWER=/mnt/entropy
|
||||||
|
UPPER_BASE=/var/lib/media-meta
|
||||||
|
VIEW=$UPPER_BASE/view
|
||||||
|
# systemd names a mount unit after its ESCAPED path ('/' -> '-', a literal '-'
|
||||||
|
# -> '\x2d'), so the unit on the host is var-lib-media\x2dmeta-view.mount. The
|
||||||
|
# repo copy keeps a readable name; install writes it under the escaped one.
|
||||||
|
UNIT='var-lib-media\x2dmeta-view.mount'
|
||||||
|
UNIT_DIR=/etc/systemd/system
|
||||||
|
# The rootless containers write as this host uid (the deploy user). The upper
|
||||||
|
# layer has to accept those writes, so it is owned by them.
|
||||||
|
MEDIA_UID="${MEDIA_VIEW_UID:-1000}"
|
||||||
|
MEDIA_GID="${MEDIA_VIEW_GID:-1000}"
|
||||||
|
|
||||||
|
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||||
|
UNIT_SRC="$REPO_ROOT/dankserver/host/systemd/media-meta-view.mount"
|
||||||
|
|
||||||
|
die() {
|
||||||
|
printf 'media-view: %s\n' "$*" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
printf 'usage: %s plan|verify|install|reap|status [options]\n' "$0" >&2
|
||||||
|
exit 2
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd="${1:-}"
|
||||||
|
[ -n "$cmd" ] || usage
|
||||||
|
shift || true
|
||||||
|
|
||||||
|
upper_base="$UPPER_BASE"
|
||||||
|
lower="$LOWER"
|
||||||
|
apply=0
|
||||||
|
while [ $# -gt 0 ]; do
|
||||||
|
case "$1" in
|
||||||
|
--upper-base) [ $# -ge 2 ] || usage; upper_base="$2"; shift 2 ;;
|
||||||
|
--lower) [ $# -ge 2 ] || usage; lower="$2"; shift 2 ;;
|
||||||
|
--apply) apply=1; shift ;;
|
||||||
|
*) usage ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
upper="$upper_base/upper"
|
||||||
|
work="$upper_base/work"
|
||||||
|
|
||||||
|
# The device backing a path, and whether the kernel calls it rotational.
|
||||||
|
rotational_of() {
|
||||||
|
local src rota
|
||||||
|
src="$(findmnt -no SOURCE --target "$1" 2>/dev/null || true)"
|
||||||
|
[ -n "$src" ] || die "cannot find a block device behind $1"
|
||||||
|
rota="$(lsblk -no ROTA "$src" 2>/dev/null | head -1 || true)"
|
||||||
|
[ -n "$rota" ] || die "cannot read the rotational flag of $src"
|
||||||
|
printf '%s %s\n' "$src" "$rota"
|
||||||
|
}
|
||||||
|
|
||||||
|
require_upper_base() {
|
||||||
|
[ -d "$upper_base" ] \
|
||||||
|
|| die "refused: upper base $upper_base does not exist (put it on the SSD first)"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_plan() {
|
||||||
|
printf 'lowerdir=%s\nupperdir=%s\nworkdir=%s\nmountpoint=%s\nunit=%s/%s\n' \
|
||||||
|
"$lower" "$upper" "$work" "$VIEW" "$UNIT_DIR" "$UNIT"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_verify() {
|
||||||
|
local fail=0 fst opts src rota
|
||||||
|
if ! mountpoint -q "$VIEW"; then
|
||||||
|
printf 'refused: %s is not a mountpoint — a plain directory here is a\n' "$VIEW" >&2
|
||||||
|
printf ' library that scans itself empty, not a metadata view.\n' >&2
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
fst="$(findmnt -no FSTYPE --mountpoint "$VIEW" 2>/dev/null || true)"
|
||||||
|
if [ "$fst" != overlay ]; then
|
||||||
|
printf 'refused: %s is mounted %s, not overlay\n' "$VIEW" "${fst:-nothing}" >&2
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
opts="$(findmnt -no OPTIONS --mountpoint "$VIEW" 2>/dev/null || true)"
|
||||||
|
local want
|
||||||
|
for want in "lowerdir=$LOWER" "upperdir=$UPPER_BASE/upper" "workdir=$UPPER_BASE/work"; do
|
||||||
|
case ",$opts," in
|
||||||
|
*",${want},"*) ;;
|
||||||
|
*) printf 'refused: %s options lack %s (got: %s)\n' "$VIEW" "$want" "$opts" >&2
|
||||||
|
fail=1 ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
read -r src rota <<<"$(rotational_of "$UPPER_BASE")"
|
||||||
|
if [ "$rota" != 0 ]; then
|
||||||
|
printf 'refused: the upper layer sits on %s which is rotational — metadata\n' "$src" >&2
|
||||||
|
printf ' belongs on the SSD, not on the array.\n' >&2
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
if [ ! -w "$UPPER_BASE/upper" ]; then
|
||||||
|
printf 'refused: %s is not writable\n' "$upper" >&2
|
||||||
|
fail=1
|
||||||
|
fi
|
||||||
|
[ "$fail" -eq 0 ] || return 1
|
||||||
|
printf 'view ok: %s <- overlay(%s,%s)\n' "$VIEW" "$LOWER" "$UPPER_BASE/upper"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_install() {
|
||||||
|
[ "$(id -u)" -eq 0 ] || die "install must run as root (writes $UNIT_DIR and mounts)"
|
||||||
|
require_upper_base
|
||||||
|
mountpoint -q "$LOWER" \
|
||||||
|
|| die "refused: $LOWER is not mounted; overlaying an empty directory would present an empty library"
|
||||||
|
local src rota
|
||||||
|
read -r src rota <<<"$(rotational_of "$upper_base")"
|
||||||
|
[ "$rota" = 0 ] || die "refused: $upper_base is on $src, a rotational device — the metadata layer must be on the SSD"
|
||||||
|
[ -f "$UNIT_SRC" ] || die "refused: $UNIT_SRC is missing from $REPO_ROOT"
|
||||||
|
# systemd will not create a mount point; a missing directory fails the start.
|
||||||
|
install -d -m 0755 -o "$MEDIA_UID" -g "$MEDIA_GID" "$upper" "$work" "$VIEW"
|
||||||
|
install -d -m 0700 "$upper_base/.AGENTTRASH"
|
||||||
|
install -m 0644 "$UNIT_SRC" "$UNIT_DIR/$UNIT"
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable "$UNIT" >/dev/null 2>&1 || true
|
||||||
|
systemctl start "$UNIT"
|
||||||
|
do_verify
|
||||||
|
}
|
||||||
|
|
||||||
|
# An upper-layer DIRECTORY with no counterpart below is metadata for a media
|
||||||
|
# folder that has been moved or renamed on the RAW path — a ghost the media
|
||||||
|
# servers would still browse. Files are never candidates on their own: a sidecar
|
||||||
|
# (movie.nfo, an .srt) exists ONLY above, by design, and is exactly what the view
|
||||||
|
# is for. A copy-up whose lower file was deleted by hand is the one case this
|
||||||
|
# leaves in place; the raw path is where deletions belong, so it stays visible.
|
||||||
|
do_reap() {
|
||||||
|
[ -d "$upper" ] || die "refused: $upper does not exist (no upper layer at $upper_base)"
|
||||||
|
stamp="$upper_base/.AGENTTRASH/$(date -u +%Y%m%d-%H%M%S)"
|
||||||
|
count=0
|
||||||
|
ghosts=0
|
||||||
|
reap_walk ""
|
||||||
|
if [ "$count" -eq 0 ] && [ "$ghosts" -eq 0 ]; then
|
||||||
|
printf 'reap: no ghosts under %s\n' "$upper"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
if [ "$ghosts" -gt 0 ]; then
|
||||||
|
printf 'reap: %d whiteout%s above — someone deleted THROUGH the view.\n' \
|
||||||
|
"$ghosts" "$([ "$ghosts" -eq 1 ] || printf s)" >&2
|
||||||
|
printf ' Leaving them: removing a whiteout un-hides the file it hides.\n' >&2
|
||||||
|
printf ' Deletions belong in .AGENTTRASH on the raw path.\n' >&2
|
||||||
|
fi
|
||||||
|
if [ "$count" -eq 0 ]; then return 0; fi
|
||||||
|
if [ "$apply" -eq 1 ]; then
|
||||||
|
printf 'reap: moved %d entr%s to %s\n' "$count" \
|
||||||
|
"$([ "$count" -eq 1 ] && printf y || printf ies)" "$stamp"
|
||||||
|
else
|
||||||
|
printf 'reap: %d ghost entr%s above; re-run with --apply to move them to %s\n' \
|
||||||
|
"$count" "$([ "$count" -eq 1 ] && printf y || printf ies)" "$stamp"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# Descend only where the lower layer still has the directory: a directory that is
|
||||||
|
# missing below is itself the ghost, and its whole subtree goes with it.
|
||||||
|
reap_walk() {
|
||||||
|
local rel="$1" entries abs r name
|
||||||
|
entries="$(find "$upper${rel:+/$rel}" -mindepth 1 -maxdepth 1 -print 2>/dev/null || true)"
|
||||||
|
[ -n "$entries" ] || return 0
|
||||||
|
while IFS= read -r abs; do
|
||||||
|
r="${abs#"$upper"/}"
|
||||||
|
name="${abs##*/}"
|
||||||
|
if [ -c "$abs" ] || printf '%s' "$name" | grep -q '^\.wh\.'; then
|
||||||
|
ghosts=$((ghosts + 1))
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
if [ -e "$lower/$r" ]; then
|
||||||
|
[ -d "$abs" ] && reap_walk "$r"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
[ -d "$abs" ] || continue
|
||||||
|
count=$((count + 1))
|
||||||
|
printf '%s\n' "$r"
|
||||||
|
if [ "$apply" -eq 1 ]; then
|
||||||
|
mkdir -p "$stamp/$(dirname "$r")"
|
||||||
|
mv "$abs" "$stamp/$r"
|
||||||
|
fi
|
||||||
|
done <<<"$entries"
|
||||||
|
}
|
||||||
|
|
||||||
|
do_status() {
|
||||||
|
if mountpoint -q "$VIEW"; then
|
||||||
|
printf 'view: %s (%s)\n' "$VIEW" "$(findmnt -no OPTIONS --mountpoint "$VIEW")"
|
||||||
|
else
|
||||||
|
printf 'view: %s NOT MOUNTED\n' "$VIEW"
|
||||||
|
fi
|
||||||
|
if [ -d "$upper" ]; then
|
||||||
|
printf 'upper: %s %s files %s\n' "$upper" \
|
||||||
|
"$(find "$upper" -type f 2>/dev/null | wc -l)" "$(du -sh "$upper" 2>/dev/null | cut -f1)"
|
||||||
|
else
|
||||||
|
printf 'upper: %s missing\n' "$upper"
|
||||||
|
fi
|
||||||
|
systemctl is-active "$UNIT" 2>/dev/null | sed 's/^/unit: /' || printf 'unit: %s unknown\n' "$UNIT"
|
||||||
|
}
|
||||||
|
|
||||||
|
case "$cmd" in
|
||||||
|
plan) do_plan ;;
|
||||||
|
verify) do_verify ;;
|
||||||
|
install) do_install ;;
|
||||||
|
reap) do_reap ;;
|
||||||
|
status) do_status ;;
|
||||||
|
*) usage ;;
|
||||||
|
esac
|
||||||
@@ -94,6 +94,16 @@ for path in "${CHANGED[@]}"; do
|
|||||||
dankserver/.env.example|dankclaw/.env.example)
|
dankserver/.env.example|dankclaw/.env.example)
|
||||||
add_test tests/env_consistency.bats
|
add_test tests/env_consistency.bats
|
||||||
;;
|
;;
|
||||||
|
# The SSD-backed metadata view: the installer, the host mount unit, and the
|
||||||
|
# two media units that bind the view rather than the raw array.
|
||||||
|
scripts/media-view.sh|dankserver/host/*)
|
||||||
|
add_test tests/media_overlay.bats
|
||||||
|
add_test tests/dankserver_quadlet.bats
|
||||||
|
add_test tests/deployment_scoping.bats
|
||||||
|
;;
|
||||||
|
dankserver/jellyfin/quadlet/*|dankserver/plex/quadlet/*)
|
||||||
|
add_test tests/media_overlay.bats
|
||||||
|
;;
|
||||||
shared/homepage/*)
|
shared/homepage/*)
|
||||||
add_test tests/homepage_caddy.bats
|
add_test tests/homepage_caddy.bats
|
||||||
add_test tests/homepage_config.bats
|
add_test tests/homepage_config.bats
|
||||||
|
|||||||
@@ -195,8 +195,9 @@ deploy.sh, never a live env poke.
|
|||||||
check every namespace (`sudo podman ps -a`) and native units
|
check every namespace (`sudo podman ps -a`) and native units
|
||||||
(`systemctl list-units`) before declaring a service absent. Compare
|
(`systemctl list-units`) before declaring a service absent. Compare
|
||||||
compose mount sources against the real filesystem — compose files name
|
compose mount sources against the real filesystem — compose files name
|
||||||
phantom paths (e.g. `/mnt/media/*` vs the real LUKS array at
|
phantom paths (the legacy `dankserver/compose.yaml` still names `/opt/jellyfin/*`
|
||||||
`/mnt/entropy`). Fix compose through git first, deploy second.
|
for a service whose roots moved to `/home/dank/Containers/jellyfin`). Fix
|
||||||
|
compose through git first, deploy second.
|
||||||
2. **Pre-create bind-mount sources** (`sudo mkdir -p` + `chown 1000:1000`)
|
2. **Pre-create bind-mount sources** (`sudo mkdir -p` + `chown 1000:1000`)
|
||||||
or creation fails `statfs …: no such file or directory`.
|
or creation fails `statfs …: no such file or directory`.
|
||||||
3. **Staged bring-up:** infra first (`up -d mariadb redis`), poll
|
3. **Staged bring-up:** infra first (`up -d mariadb redis`), poll
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
---
|
---
|
||||||
type: reference
|
type: reference
|
||||||
title: Jellyfin
|
title: Jellyfin
|
||||||
description: "Jellyfin on Dankserver: quadlet unit, entropy-volume data layout, ports, client access, hardware-transcode status."
|
description: "Jellyfin on Dankserver: quadlet unit, overlay metadata view, provider keys, ports, access, transcode status."
|
||||||
tags: [jellyfin, dankserver, media-server]
|
tags: [jellyfin, dankserver, media-server]
|
||||||
timestamp: 2026-09-03T00:00:00Z
|
timestamp: 2026-10-30T00:00:00Z
|
||||||
---
|
---
|
||||||
|
|
||||||
# Jellyfin
|
# Jellyfin
|
||||||
@@ -26,7 +26,7 @@ public vhost `jelly.franksdank.cloud` behind the [[Anubis]] guard chain →
|
|||||||
| Image | `ghcr.io/jellyfin/jellyfin:latest` |
|
| Image | `ghcr.io/jellyfin/jellyfin:latest` |
|
||||||
| Network | `dankserver_appnet`; publishes 8096/tcp, 8920/tcp, 7359/udp, 1900/udp |
|
| Network | `dankserver_appnet`; publishes 8096/tcp, 8920/tcp, 7359/udp, 1900/udp |
|
||||||
| Data roots | `/home/dank/Containers/jellyfin/config:/config`, `/home/dank/Containers/jellyfin/cache:/cache` |
|
| Data roots | `/home/dank/Containers/jellyfin/config:/config`, `/home/dank/Containers/jellyfin/cache:/cache` |
|
||||||
| Media | `Volume=/mnt/entropy:/data:ro` — the whole [[Entropy Volume]] mounted read-only; nothing automated writes to it |
|
| Media | `Volume=/var/lib/media-meta/view:/data` — the [[Media Metadata View]] overlay, **writable**; sidecars land on the NVMe, the [[Entropy Volume]] array underneath stays read-only and untouched |
|
||||||
| GPU | none — no `AddDevice`; CPU decode only (jellyfin-ffmpeg path) |
|
| GPU | none — no `AddDevice`; CPU decode only (jellyfin-ffmpeg path) |
|
||||||
| Env quirks | `DOTNET_SYSTEM_IO_DISABLEFILELOCKING=true`, `LD_PRELOAD=libjemalloc.so.2`, `MALLOC_TRIM_THRESHOLD_=131072` kept verbatim from the compose-era container (zero-drift cutover) |
|
| Env quirks | `DOTNET_SYSTEM_IO_DISABLEFILELOCKING=true`, `LD_PRELOAD=libjemalloc.so.2`, `MALLOC_TRIM_THRESHOLD_=131072` kept verbatim from the compose-era container (zero-drift cutover) |
|
||||||
| Health | `curl -f http://localhost:8096/health` every 30 s |
|
| Health | `curl -f http://localhost:8096/health` every 30 s |
|
||||||
@@ -36,11 +36,23 @@ public vhost `jelly.franksdank.cloud` behind the [[Anubis]] guard chain →
|
|||||||
```
|
```
|
||||||
/home/dank/Containers/jellyfin/config -> /config (JELLYFIN_DATA_DIR)
|
/home/dank/Containers/jellyfin/config -> /config (JELLYFIN_DATA_DIR)
|
||||||
/home/dank/Containers/jellyfin/cache -> /cache (JELLYFIN_CACHE_DIR, XDG_CACHE_HOME)
|
/home/dank/Containers/jellyfin/cache -> /cache (JELLYFIN_CACHE_DIR, XDG_CACHE_HOME)
|
||||||
/mnt/entropy -> /data (read-only media volume)
|
/var/lib/media-meta/view -> /data (the SSD-backed overlay view)
|
||||||
```
|
```
|
||||||
|
|
||||||
Library folders point at `/data` paths; because the mount uses the same
|
Library folders point at `/data` paths, which are the view's paths: stable
|
||||||
absolute path as on the host, library paths survive container recreation.
|
across container recreation, and identical to the raw array layout below it.
|
||||||
|
|
||||||
|
`Local metadata` — save metadata to media folders — is enabled, and it works
|
||||||
|
because `/data` is the [[Media Metadata View]] rather than the read-only array:
|
||||||
|
`movie.nfo`, `tvshow.nfo`, `episode.nfo` and artwork are written once, on the
|
||||||
|
NVMe upper layer, and [[Plex]] reads the same files. Jellyfin is the fleet's
|
||||||
|
only metadata writer.
|
||||||
|
|
||||||
|
**Failure signature:** `System.IO.IOException: Read-only file system :
|
||||||
|
'/data/Movies/<film>/movie.nfo'` means the container is binding the raw array
|
||||||
|
instead of the view — the view is unmounted or the quadlet drifted. That used to
|
||||||
|
be the expected state (pre-view, every item update logged it); today it is a
|
||||||
|
fault. Run `scripts/media-view.sh status`.
|
||||||
|
|
||||||
## Ports & access
|
## Ports & access
|
||||||
|
|
||||||
@@ -105,6 +117,61 @@ where the LDAP plugin runs. Verified 2026-09-25: `alex` authenticates over
|
|||||||
`AuthenticateByName` (HTTP 200; full plugin flow logged — service bind,
|
`AuthenticateByName` (HTTP 200; full plugin flow logged — service bind,
|
||||||
search, `ldapUid`, user bind, token).
|
search, `ldapUid`, user bind, token).
|
||||||
|
|
||||||
|
## Metadata providers and their keys (2026-10-30)
|
||||||
|
|
||||||
|
TMDb, OMDb, StudioImages and MusicBrainz are **core plugins** in Jellyfin 12 —
|
||||||
|
no DLL, just a configuration file the running container owns under the data
|
||||||
|
root. TVDB is not core (its metadata provider was removed in 10.9), so it is an
|
||||||
|
official-catalog plugin.
|
||||||
|
|
||||||
|
| Provider | Shape on disk | Key lives in |
|
||||||
|
|---|---|---|
|
||||||
|
| TMDb | `config/plugins/configurations/Jellyfin.Plugin.Tmdb.xml` → `<TmdbApiKey>` | `/home/dank/Containers/jellyfin/provider-keys` → `TMDB_API_KEY=` (mode 600) |
|
||||||
|
| TVDB | `config/plugins/thetvdb_24.0.0.0/` + `…Jellyfin.Plugin.Tvdb.xml` → `<ApiKey>` | the same file → `TVDB_API_KEY=` |
|
||||||
|
| OMDb | `…Jellyfin.Plugin.Omdb.xml` | **not configured** — its key is issued by email, 1 000 req/day, and its ToS forbids storing the returned data; ratings stay Jellyfin's own lookup |
|
||||||
|
|
||||||
|
**None of it is tracked in git.** Those XML files belong to the container and
|
||||||
|
hold credentials, and `tests/jellyfin_providers.bats` fails the suite if one is
|
||||||
|
ever committed. The keys themselves sit in the per-service host env file that is
|
||||||
|
the fleet's convention for container credentials (grafana, caddy, pihole,
|
||||||
|
copyparty, romm, seerr): `provider-keys`, mode 600, beside the data root.
|
||||||
|
`scripts/jellyfin-providers.sh` is the only thing that writes the XML:
|
||||||
|
|
||||||
|
```
|
||||||
|
scripts/jellyfin-providers.sh plan # what would change; prints no key
|
||||||
|
scripts/jellyfin-providers.sh install # plugin + keys, previous copy to trash
|
||||||
|
scripts/jellyfin-providers.sh verify # plugin + key LENGTHS, never key bytes
|
||||||
|
```
|
||||||
|
|
||||||
|
- **A key never enters git, argv, or the tool's output.** The installer reads
|
||||||
|
that 600-perm file itself (a value in argv is world-readable through
|
||||||
|
`/proc/<pid>/cmdline`) and refuses it when it is group- or world-readable —
|
||||||
|
an exposed key is an error, not a warning. A key that shows up in a transcript
|
||||||
|
or `ps` output has leaked: rotate it with the provider, rewrite
|
||||||
|
`provider-keys`, re-run `install`.
|
||||||
|
- **The TVDB artifact is pinned three ways**: sha256
|
||||||
|
`6302fba3857bdde8d186dd099fac09ca43247041f531a528a2c788add70a5319`, the
|
||||||
|
official plugin guid `a677c0da-fac5-4cde-941a-7134223f14c8`, and version
|
||||||
|
`24.0.0.0` (`targetAbi 12.0.0.0`, which fits the deployed 12.4.0). A sha
|
||||||
|
match alone only proves the bytes are the bytes fetched — the guid check is
|
||||||
|
what says it is the official plugin. Rollback: move `thetvdb_24.0.0.0/` into
|
||||||
|
the trash and revert the pin; a second `thetvdb_*` directory is a refusal,
|
||||||
|
never a quiet second copy.
|
||||||
|
- **The homelab TVDB key is a licensed key** (its JWT claims say
|
||||||
|
`community_supported: false`), so it needs **no PIN** — the optional
|
||||||
|
`TVDB_PIN=` line exists only for a subscriber-shaped key.
|
||||||
|
- **TVDB is installed and keyed but is not a metadata agent**: TMDb still
|
||||||
|
identifies everything. Enabling TVDB as a provider would re-fetch show
|
||||||
|
metadata across the library, so it is a deliberate, separate change.
|
||||||
|
- **The deployer places the keys before it restarts jellyfin**
|
||||||
|
(`ensure_provider_keys` in `scripts/deploy-remote.sh`) and warns instead of
|
||||||
|
failing when `provider-keys` is absent, so a host without it still deploys.
|
||||||
|
- **Keys and plugins are read at start-up**: after a manual `install` the
|
||||||
|
change is inert until `./scripts/deploy.sh dankserver --service jellyfin`.
|
||||||
|
- TMDb's terms are the reason this stays narrow: attribution required, no
|
||||||
|
caching beyond 6 months, no derivative works. Fine for a personal homelab,
|
||||||
|
and the reason no bulk enrichment pass runs against it.
|
||||||
|
|
||||||
## Configuration workflow
|
## Configuration workflow
|
||||||
|
|
||||||
1. Create libraries pointing at `/data` folders (movies, shows, audio).
|
1. Create libraries pointing at `/data` folders (movies, shows, audio).
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
---
|
||||||
|
type: reference
|
||||||
|
title: Media Metadata View
|
||||||
|
description: "The SSD-backed overlay the two media servers read and write: shape, guards, commands, laws, rollback."
|
||||||
|
tags: [dankserver, media-server, jellyfin, plex, storage]
|
||||||
|
timestamp: 2026-10-30T00:00:00Z
|
||||||
|
---
|
||||||
|
|
||||||
|
# Media Metadata View
|
||||||
|
|
||||||
|
`/var/lib/media-meta/view` is an OverlayFS merge of the media library: the RAID6
|
||||||
|
array below, a directory on the NVMe above. It exists so [[Jellyfin]] can write
|
||||||
|
Kodi-format sidecars (`movie.nfo`, `tvshow.nfo`, `episode.nfo`, artwork) that
|
||||||
|
[[Plex]] can read. Sidecar metadata is only useful sitting next to the media
|
||||||
|
file, and the array is read-only and slow — so the media bytes stay there and the
|
||||||
|
*writes* land on SSD. One tree to both servers, one copy of the media.
|
||||||
|
|
||||||
|
The mount point is not a volume and holds nothing while unmounted. It sits inside
|
||||||
|
`/var/lib/media-meta` so that layers, kernel bookkeeping, the reap trash and the
|
||||||
|
merged door are one directory — and so the mount table grows no new `/mnt` entry
|
||||||
|
next to the array.
|
||||||
|
|
||||||
|
## Shape
|
||||||
|
|
||||||
|
| Item | Value |
|
||||||
|
|---|---|
|
||||||
|
| Lower layer | `/mnt/entropy` — the RAID6 array, source of truth, never written |
|
||||||
|
| Upper layer | `/var/lib/media-meta/upper` — NVMe; every write lands here |
|
||||||
|
| Work dir | `/var/lib/media-meta/work` — overlayfs internals, same filesystem |
|
||||||
|
| Mount point | `/var/lib/media-meta/view` — the merged tree both servers bind |
|
||||||
|
| Unit (repo) | `dankserver/host/systemd/media-meta-view.mount` — the source of truth |
|
||||||
|
| Unit (host) | `var-lib-media\x2dmeta-view.mount` — systemd names a mount unit after its escaped path (`-` becomes `\x2d`); `media-view.sh install` writes the repo file under that name |
|
||||||
|
| Installer | `scripts/media-view.sh` — `plan` / `install` / `verify` / `status` / `reap` |
|
||||||
|
| Written by | Jellyfin only (Plex binds read-only); nothing else in the fleet touches it |
|
||||||
|
|
||||||
|
Both consumers guard the view with
|
||||||
|
`UnitConfigItem=ConditionPathIsMountPoint=/var/lib/media-meta/view`, and
|
||||||
|
`scripts/deploy.sh dankserver --service jellyfin|plex` calls `media-view.sh
|
||||||
|
install` + `verify` before it restarts either container.
|
||||||
|
|
||||||
|
| Consumer | Bind | Mode | Why that path |
|
||||||
|
|---|---|---|---|
|
||||||
|
| [[Jellyfin]] | `/var/lib/media-meta/view:/data` | rw | It is the metadata writer; its sidecars become SSD bytes |
|
||||||
|
| [[Plex]] | `/var/lib/media-meta/view:/mnt/entropy` | ro | Every path in the PMS database was recorded as `/mnt/entropy/...`; only the HOST side of the bind moved, so its database still resolves |
|
||||||
|
|
||||||
|
Putting the sidecars inside Plex's reach is not the same as making Plex use them:
|
||||||
|
a library parses `.nfo` only with its *Local media information* agent enabled, and
|
||||||
|
this change touches no library setting.
|
||||||
|
|
||||||
|
## Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scripts/media-view.sh plan # print the resolved shape, touch nothing
|
||||||
|
sudo scripts/media-view.sh install # create dirs, install the unit, mount, enable
|
||||||
|
scripts/media-view.sh verify # is the view really the overlay? (deploy gate)
|
||||||
|
scripts/media-view.sh status # mount options + upper-layer size/file count
|
||||||
|
scripts/media-view.sh reap # list ghost metadata (media moved, sidecars left)
|
||||||
|
sudo scripts/media-view.sh reap --apply # move ghosts to the SSD trash, never delete
|
||||||
|
```
|
||||||
|
|
||||||
|
`--upper-base DIR` and `--lower DIR` relocate the layers for `plan` and `reap`;
|
||||||
|
the defaults are the host values above.
|
||||||
|
|
||||||
|
## Laws
|
||||||
|
|
||||||
|
- **The upper layer must sit on non-rotational storage.** `install` and `verify`
|
||||||
|
refuse via `lsblk -dno ROTA` if the upper base lives on the array: an overlay
|
||||||
|
whose upper is the spinners is the original problem with extra steps, and it
|
||||||
|
silently turns every metadata write back into a seek.
|
||||||
|
- **Never mount the view over an unmounted lower.** `RequiresMountsFor=/mnt/entropy`
|
||||||
|
plus the container `ConditionPathIsMountPoint` guards: an empty `lowerdir`
|
||||||
|
presents an *empty library* as truth, and a media scan would then clear the
|
||||||
|
database of everything. systemd does not create a mount point, so `install`
|
||||||
|
creates `view` itself; without it the mount simply fails.
|
||||||
|
- **Never delete through the view.** A delete writes an overlayfs whiteout
|
||||||
|
(`.wh.<name>`) in the upper layer, which hides the array's copy without
|
||||||
|
removing it — the file reappears the moment the view is unmounted, and the
|
||||||
|
upper layer now carries a landmine. Deletions belong on the raw path
|
||||||
|
(`/mnt/entropy`), which the relayout tool already treats as the only writer.
|
||||||
|
`reap` refuses to remove whiteouts and only warns about them.
|
||||||
|
- **A relayout must be followed by `reap`.** Renames happen on the raw path, so
|
||||||
|
sidecars the view already wrote stay at the old coordinates: a ghost folder
|
||||||
|
the servers browse and find nothing in. `reap` lists upper-layer directories
|
||||||
|
with no counterpart below and moves them (with their subtree) into a dated
|
||||||
|
`.AGENTTRASH/` under the SSD base. It is recursive and file-safe: a sidecar
|
||||||
|
that exists only above is normal, so only *directories* are candidates.
|
||||||
|
- **File-watcher events do not cross the overlay.** Plex's NFO agent and
|
||||||
|
Jellyfin's real-time monitor see inotify events for writes that land *in the
|
||||||
|
upper layer*, not for changes on the array underneath. After any relayout or
|
||||||
|
externally-written metadata, trigger a library refresh — see
|
||||||
|
[[Media Relayout]] for the refresh procedure.
|
||||||
|
|
||||||
|
## Failure signatures
|
||||||
|
|
||||||
|
| Symptom | Meaning |
|
||||||
|
|---|---|
|
||||||
|
| `jellyfin.service` / `plex.service` show `ConditionPathIsMountPoint=... was not met` and stay inactive | The view is not mounted. Do not start them anyway: `scripts/media-view.sh install`, then `verify`. |
|
||||||
|
| Jellyfin log: `Read-only file system : '/data/Movies/…/movie.nfo'` | The container is binding the raw array, not the view — the quadlet drifted back or the view is gone. `media-view.sh status`. |
|
||||||
|
| A folder appears in a media server with artwork/`.nfo` but no video | Ghost metadata from a relayout. `media-view.sh reap`. |
|
||||||
|
| `verify` prints `refused: …/view is not a mountpoint` | A plain directory is sitting where the overlay belongs. That is the fail-closed guard working; `install` on the box. |
|
||||||
|
| `systemctl status` finds no such unit | Look under the escaped name, `var-lib-media\x2dmeta-view.mount`, not the repo file name. |
|
||||||
|
|
||||||
|
## Rollback
|
||||||
|
|
||||||
|
Revert the commit that pointed the two quadlets at the view and re-deploy both
|
||||||
|
services — they bind `/mnt/entropy` read-only again, which is the pre-view shape.
|
||||||
|
The NVMe upper layer can stay: nothing reads it once the binds are back, and it
|
||||||
|
holds no state either server needs. Unmounting alone
|
||||||
|
(`systemctl stop 'var-lib-media\x2dmeta-view.mount'`) is safe while no container
|
||||||
|
binds it, and the array is never modified by any of this.
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [[Jellyfin]] — the metadata writer; its data layout section.
|
||||||
|
- [[Plex]] — the read-only consumer; in-place bind-mount history.
|
||||||
|
- [[Media Relayout]] — the tool that moves media on the raw path and the reason `reap` exists.
|
||||||
|
- [[Entropy Volume]] — the array underneath.
|
||||||
@@ -0,0 +1,342 @@
|
|||||||
|
---
|
||||||
|
type: reference
|
||||||
|
title: Media Relayout
|
||||||
|
description: "Census, plan and apply the movie and TV libraries into Jellyfin format: gates, evidence chain, refusals, rollback."
|
||||||
|
tags: [dankserver, media-server, jellyfin, plex, procedure]
|
||||||
|
timestamp: 2026-10-30T00:00:00Z
|
||||||
|
---
|
||||||
|
|
||||||
|
# Media Relayout
|
||||||
|
|
||||||
|
The movie library on [[Entropy Volume]] arrived as release folders —
|
||||||
|
`Some.Movie.2019.1080p.WiKiTaLx.x265-FOO/` with the video inside named after
|
||||||
|
the release. [[Jellyfin]] matches a movie by folder and file name, so the
|
||||||
|
release noise cost posters, extras and versions. These scripts turn that
|
||||||
|
library into Jellyfin's own shape — the movies, the TV packs, and the TV that
|
||||||
|
turns up in the movie share — and nothing else in the repo touches a media file.
|
||||||
|
|
||||||
|
| Script | Touches media? | Output |
|
||||||
|
|---|---|---|
|
||||||
|
| `scripts/media-census.py` | reads headers only | `enum`, `probed`, `plex` TSVs |
|
||||||
|
| `scripts/media-plan.py` | no — TSVs in, TSVs out | the plan + the exception report |
|
||||||
|
| `scripts/media-apply.py` | **renames, never copies**; sweeps only what a run emptied | the moves, plus a journal |
|
||||||
|
| `scripts/media-tv.py` | no — TSV out | a plan that moves TV into the TV library |
|
||||||
|
| `scripts/media-shows.py` | no — TSV out | a plan that re-files the TV library itself |
|
||||||
|
|
||||||
|
The seams are `tests/media_relayout.bats` and `tests/media_shows.bats`; the target contract is quoted from
|
||||||
|
[jellyfin.org/docs/general/server/media/movies](https://jellyfin.org/docs/general/server/media/movies)
|
||||||
|
in `media-plan.py`'s docstring.
|
||||||
|
|
||||||
|
## Procedure
|
||||||
|
|
||||||
|
Run the census on the machine that mounts the library. `probe` is the slow step
|
||||||
|
(a header read per container) and is `nice`d — it is safe beside a live service
|
||||||
|
because it never competes for the GPU and never writes inside the library.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
C=scripts/media-census.py
|
||||||
|
$C enumerate --root /mnt/entropy/Movies --out /tmp/media-enum.tsv
|
||||||
|
$C probe --enum /tmp/media-enum.tsv --out /tmp/media-probed.tsv --jobs 6
|
||||||
|
$C plex --root /mnt/entropy/Movies --out /tmp/plex-movies.tsv
|
||||||
|
```
|
||||||
|
|
||||||
|
`plex` reads the Plex metadata cache (`--db` defaults to the path the
|
||||||
|
[[Plex]] container bind-mounts at `/var/lib/plexmediaserver`) with
|
||||||
|
`mode=ro&immutable=1`: a live server is never locked or written.
|
||||||
|
|
||||||
|
Then plan — this reads only the three TSVs, so it costs seconds and can run
|
||||||
|
anywhere, including a laptop:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scripts/media-plan.py --root /mnt/entropy/Movies \
|
||||||
|
--enum /tmp/media-enum.tsv --probed /tmp/media-probed.tsv \
|
||||||
|
--plex /tmp/plex-movies.tsv --out /tmp/jf-plan.tsv --exceptions /tmp/jf-exceptions.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
**Read `/tmp/jf-exceptions.txt` before applying.** It is not a log: every line
|
||||||
|
is a file the plan could not name a film for, a pile that needs a README, or a
|
||||||
|
collision it refused. The printed gate lines are the go/no-go:
|
||||||
|
`problems`, `bad-names`, `occupied`, `double-claim`, `left-unmoved` must all be
|
||||||
|
0 and `unplanned=0` must appear in the report. Any non-zero number means a file
|
||||||
|
that would be filed wrong, and the plan is not run until it is understood.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scripts/media-apply.py apply --plan /tmp/jf-plan.tsv --root /mnt/entropy/Movies # dry, writes nothing
|
||||||
|
scripts/media-apply.py apply --plan /tmp/jf-plan.tsv --root /mnt/entropy/Movies --apply
|
||||||
|
scripts/media-apply.py verify --plan /tmp/jf-plan.tsv --journal /mnt/entropy/media-relayout.jsonl
|
||||||
|
scripts/media-apply.py sweep --journal /mnt/entropy/media-relayout.jsonl --root /mnt/entropy/Movies # dry
|
||||||
|
scripts/media-apply.py sweep --journal /mnt/entropy/media-relayout.jsonl --root /mnt/entropy/Movies --apply
|
||||||
|
scripts/media-apply.py rollback --journal /mnt/entropy/media-relayout.jsonl --root /mnt/entropy/Movies --apply
|
||||||
|
```
|
||||||
|
|
||||||
|
An apply also leaves the release folders it emptied standing where they were —
|
||||||
|
hundreds of them at the library root — which is what `sweep` removes, dry-run
|
||||||
|
first, from the same journal.
|
||||||
|
|
||||||
|
After it lands, both servers need a scan or the change is invisible.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Run this from the host shell through `podman exec`, never from a neighbour
|
||||||
|
# container: Plex's HTTPD closes without a single byte on any request that
|
||||||
|
# arrives from another container's netns (10.89.x.x reads as "empty reply", not
|
||||||
|
# as a 401). The token rides stdin, never argv, where any process on the host
|
||||||
|
# could read it out of /proc/<pid>/cmdline.
|
||||||
|
umask 077
|
||||||
|
podman exec plex sh -c "sed -n 's/.*PlexOnlineToken=\"\\([^\"]*\\)\".*/\\1/p' \
|
||||||
|
'/config/Library/Application Support/Plex Media Server/Preferences.xml'" >/tmp/px.tok
|
||||||
|
podman exec -i plex sh -c 'read -r T
|
||||||
|
for k in $(curl -s -H "X-Plex-Token: $T" -H "Accept: application/json" \
|
||||||
|
http://127.0.0.1:32400/library/sections | grep -o "\"key\":\"[0-9]*\"" | cut -d\" -f4); do
|
||||||
|
curl -s -o /dev/null -w "plex section $k: %{http_code}\n" -X POST \
|
||||||
|
-H "X-Plex-Token: $T" "http://127.0.0.1:32400/library/sections/$k/refresh"
|
||||||
|
done' </tmp/px.tok
|
||||||
|
```
|
||||||
|
|
||||||
|
[[Jellyfin]]'s scan key lives in `~/.config/secrets/jellyfin` (mode 600; the
|
||||||
|
same value is `JELLYFIN_API_KEY` in the ignored `dankserver/.env`). It rides a
|
||||||
|
mode-600 curl config, never a header argument, and it must use the
|
||||||
|
`MediaBrowser` form — Jellyfin 12.4 refuses a bare `X-Emby-Token` for any key:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
umask 077
|
||||||
|
{ printf 'header = "Authorization: MediaBrowser Token=\\"%s\\""\n' \
|
||||||
|
"$(cat ~/.config/secrets/jellyfin)"
|
||||||
|
echo 'url = "http://127.0.0.1:8096/Library/Refresh"'
|
||||||
|
echo 'request = "POST"'; } >/tmp/jf.hdr
|
||||||
|
curl -s -K /tmp/jf.hdr -o /dev/null -w "jellyfin scan: %{http_code}\n"
|
||||||
|
shred -u /tmp/jf.hdr
|
||||||
|
```
|
||||||
|
|
||||||
|
`204` is acceptance; the scan then runs on the server's own schedule.
|
||||||
|
|
||||||
|
## TV in the movie library
|
||||||
|
|
||||||
|
A box set that arrives in the movie share stays there until someone moves it:
|
||||||
|
`media-plan.py` refuses to file TV and parks each series in
|
||||||
|
`_TV needs a TV library - <series>`. `scripts/media-tv.py` is the mover. It
|
||||||
|
walks the movie library itself (no census needed), groups episode-shaped names
|
||||||
|
by series, and writes a plan in the executor's own format:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
scripts/media-tv.py --root /mnt/entropy/Movies --library /mnt/entropy/TVShows \
|
||||||
|
--out /mnt/entropy/media-tv-plan.tsv
|
||||||
|
scripts/media-apply.py apply --plan /mnt/entropy/media-tv-plan.tsv --root /mnt/entropy
|
||||||
|
scripts/media-apply.py apply --plan /mnt/entropy/media-tv-plan.tsv --root /mnt/entropy \
|
||||||
|
--journal /mnt/entropy/media-relayout-tv.jsonl --apply
|
||||||
|
scripts/media-apply.py sweep --journal /mnt/entropy/media-relayout-tv.jsonl \
|
||||||
|
--root /mnt/entropy --apply
|
||||||
|
```
|
||||||
|
|
||||||
|
The shape it writes is the one both matchers read — `Monk (2002)/Season 05/
|
||||||
|
Monk - S05E03 - Mr. Monk and the Big Game.mkv` — keeping the file's own name,
|
||||||
|
because the publisher's spelling beats a re-derivation, with the year taken
|
||||||
|
from the box-set folder. What it refuses it prints and leaves alone:
|
||||||
|
|
||||||
|
- fewer than `--min` (5) files under one series name: one file named `Part 2`
|
||||||
|
is a film, not a series;
|
||||||
|
- an episode whose name states no season number — no invented `Season 01`;
|
||||||
|
- a destination two files both claim: the executor refuses the whole plan
|
||||||
|
before its first rename, and only Frank decides which copy is the keeper.
|
||||||
|
- an episode the destination library already holds at the same byte count. The
|
||||||
|
movie share carried a second copy of the whole Monk box set — 124 episodes
|
||||||
|
both sides, the same episode list, every byte count identical, 31.5 GiB each
|
||||||
|
way, and head+tail blake2b identical on a 6-episode sample. The mover leaves
|
||||||
|
such a series in place and says so; a duplicate is deleted or kept by Frank,
|
||||||
|
never copied into one library by a tool.
|
||||||
|
|
||||||
|
A sidecar that shares an episode's own name (`.srt`, `.nfo`) moves with it.
|
||||||
|
|
||||||
|
Below the threshold the mover prints and leaves, so a lone downloaded episode of
|
||||||
|
a real show stays in the movie library on purpose — six did on 2026-10-30.
|
||||||
|
Filing one by hand is the same executor on a hand-written plan and the same
|
||||||
|
journal; the trap is the series year. Read it from the file's own name or the
|
||||||
|
folder it owns, never from the title whose bonus folder it happens to be parked
|
||||||
|
in — `The Cleveland Show S03E07` inside `Die Hard (1988)/extras/` is not a 1988
|
||||||
|
show. A name whose marker is `Part 2` is a film and stays put.
|
||||||
|
|
||||||
|
## The TV library's own relayout
|
||||||
|
|
||||||
|
`/mnt/entropy/TVShows` is scene packs rather than a library, and Jellyfin has
|
||||||
|
already matched and numbered most of it. `scripts/media-shows.py` re-files that
|
||||||
|
library into the same shape, and it never guesses: every series name and every
|
||||||
|
episode number it writes traces to an authority, and what has none is parked for
|
||||||
|
a human to read.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. The authority: Jellyfin's own matched rows (the DB opens read-only).
|
||||||
|
python3 scripts/media-census.py jellyfin --root /mnt/entropy/TVShows \
|
||||||
|
--out /tmp/tv_titles.tsv
|
||||||
|
# 2. What is on disk (stat only), and the runtimes the film arm needs.
|
||||||
|
python3 scripts/media-census.py enumerate --root /mnt/entropy/TVShows \
|
||||||
|
--out /tmp/tv_enum.tsv
|
||||||
|
python3 scripts/media-census.py probe --root /mnt/entropy/TVShows \
|
||||||
|
--enum /tmp/tv_enum.tsv --jobs 6 --out /tmp/tv_probed.tsv
|
||||||
|
# The probe is the slow step: ffprobe reads both ends of every file.
|
||||||
|
# 3. The plan. --tmdb needs TMDB_API_KEY; without it a series no provider dated
|
||||||
|
# is simply named without a year, which Jellyfin accepts.
|
||||||
|
python3 scripts/media-shows.py --root /mnt/entropy/TVShows --enum /tmp/tv_enum.tsv \
|
||||||
|
--titles /tmp/tv_titles.tsv --probed /tmp/tv_probed.tsv \
|
||||||
|
--out /tmp/tv_plan.tsv --exceptions /tmp/tv_exceptions.txt
|
||||||
|
# 4. Read the exceptions file FIRST — it is the review, not a log. Then the same
|
||||||
|
# executor, journal and rollback as the movie relayout above.
|
||||||
|
scripts/media-apply.py apply --plan /tmp/tv_plan.tsv --root /mnt/entropy \
|
||||||
|
--journal /mnt/entropy/media-relayout-tv.jsonl --apply
|
||||||
|
```
|
||||||
|
|
||||||
|
Evidence, strongest first — the order the planner believes things:
|
||||||
|
|
||||||
|
- the **episode marker the file's own name states** (`S01E02-E03`, `1x04`);
|
||||||
|
- the **`Season NN` folder it sits in and the number its name leads with inside
|
||||||
|
it**: `Season 19/01 Charles Barkley, Nirvana.avi` is S19E01;
|
||||||
|
- the **provider row**: identity (the series name and its year), and the
|
||||||
|
numbering of a file whose own name says nothing;
|
||||||
|
- a **bonus folder** anywhere above the file, or Jellyfin's `ExtraType` → the
|
||||||
|
series' canonical extra folder;
|
||||||
|
- a runtime over `SHORT_MAX` makes it film-shaped, and it leaves for `--movies`
|
||||||
|
only when a movie provider names it — otherwise it parks.
|
||||||
|
|
||||||
|
Two refusals carry the design, and a dry run on the real library found both:
|
||||||
|
|
||||||
|
- **Folders resolve a series; they never invent one.** A folder's words may
|
||||||
|
resolve a file to a series a provider has already named — `(CBB) Tengen Toppa
|
||||||
|
Gurren Lagann - The Complete Series (BD 1080p)` names the registered *Tengen
|
||||||
|
Toppa Gurren Lagann* — but a bonus subfolder is not a show. An early run made
|
||||||
|
`…/03. Extra/01. NCOP/NCOP 01.mkv` into a root series called `01 NCOP`.
|
||||||
|
- **A scraped row does not outrank the filesystem.** Jellyfin numbers
|
||||||
|
multi-season packs badly: every file under `SNL 16-20/Season 16..19` carries
|
||||||
|
season 16 episode 20, and one Gundam row says season 2004. The folder, then
|
||||||
|
the file's own leading number, win; a row that still insists on season 1989
|
||||||
|
parks its file instead of creating `Season 1989`.
|
||||||
|
|
||||||
|
The provider's name is copied verbatim, warts included: Jellyfin carries a Series
|
||||||
|
named `Black Clover 001` (92 episodes, no year) because it matched a pack off a
|
||||||
|
filename. Renaming a show is Jellyfin's work — a folder rename never touches its
|
||||||
|
provider ids — so the mover keeps the name it is handed and strips only the pack
|
||||||
|
index a release leads with (`4a. Seed DESTINY` → `Seed DESTINY`).
|
||||||
|
|
||||||
|
## Laws
|
||||||
|
|
||||||
|
- **A moved file is a 404 in [[Plex]] until its section is rescanned.** The
|
||||||
|
database keeps the release-folder path, so playback fails `Couldn't find the
|
||||||
|
file to stream` while the scanner works through the section. Never read such a
|
||||||
|
404 as a lost file: the file is where the journal says it is.
|
||||||
|
- **Plex's matched title outranks the filename**, and a Plex year of `0` means
|
||||||
|
*indexed but never matched* — trusting it named folders `Title (0)`, which is
|
||||||
|
a Jellyfin item with no year, no poster and no identity. A year that is not a
|
||||||
|
plausible year is no information.
|
||||||
|
- **`<>:"/\|?*` never survive a name.** Plex's own title for *V/H/S/2* is
|
||||||
|
`V/H/S/2`; kept verbatim it became `Movies/V/H/S/2 (2013)/` — a tree five
|
||||||
|
levels deep that neither matcher reads as a movie.
|
||||||
|
- **A folder inside a movie's folder is bonus territory even when its name is
|
||||||
|
misspelled.** `Saboteur (1942)/Exrtas/Saboteur - A Closer Look` is a typo for
|
||||||
|
`Extras`; the vocabulary rule never fired, the featurette became a movie
|
||||||
|
folder, and its words then out-scored the real film and drew that box set's
|
||||||
|
other extras into it. The rule that replaced it needs no metadata: the folder
|
||||||
|
holding the longest film owns the shorter video filed under it, unless that
|
||||||
|
file's own name carries a different year — which is another film speaking.
|
||||||
|
- **Matching words are split on everything that is not a letter or a digit.**
|
||||||
|
Splitting on separators but not on `/` left tokens like `(1960)/extras` that
|
||||||
|
no other name could ever agree with, so a film could not be found by the
|
||||||
|
folder holding it, and extras drifted into a neighbour. A bare number counts
|
||||||
|
only when it could be a release year, so `1080` and `2160` never pose as
|
||||||
|
agreement between two names.
|
||||||
|
- **Compare two media libraries with the series engine, never a path
|
||||||
|
substring.** Filtering paths for `monk` also returns *Monkey Dust*, and
|
||||||
|
letting whichever file a walk happens to reach first stand for an episode
|
||||||
|
paired two libraries into a fake duplicate — the same sloppiness pointed the
|
||||||
|
other way deletes a file that is unique. The comparison is `media-titles.ep_of`
|
||||||
|
on both sides: series, season, episode, bytes, and a hash before anything is
|
||||||
|
trashed.
|
||||||
|
- **A deletion is staged on the item's own filesystem.** The trash rule names
|
||||||
|
`~/.AGENTTRASH`, but `/home` and `/mnt/entropy` are different devices: a 31 GiB
|
||||||
|
stage there is a copy rather than a rename, it has to fit in the 187 GiB of
|
||||||
|
free space, and `rename(2)` refuses it anyway. Trash goes beside the item —
|
||||||
|
`/mnt/entropy/.AGENTTRASH/<date>-<topic>/` — so it costs one inode change and
|
||||||
|
rolls back exactly like the move that made it.
|
||||||
|
- **A Jellyfin 401 is the header, not the key.** 12.4 authenticates only
|
||||||
|
`Authorization: MediaBrowser Token="…"` and refuses a bare `X-Emby-Token` for
|
||||||
|
every key, valid or not. This page carried "no credential on this box can
|
||||||
|
trigger a scan, the two Seerr keys answer 401" for weeks because of that
|
||||||
|
header — the keys were innocent. Before believing a 401, prove the key against
|
||||||
|
the server's own table: `select count(*) from ApiKeys where AccessToken = ?`
|
||||||
|
on `jellyfin.db` opened `mode=ro&immutable=1`.
|
||||||
|
- **A scan that must see a new folder has to be recursive.** `POST
|
||||||
|
/Library/Refresh` is the incremental pass, and inotify does not cross a
|
||||||
|
rootless podman bind, so folders created on the host stay invisible to it. The
|
||||||
|
move is `POST /Items/{id}/Refresh?Recursive=true` (204) per library, the id
|
||||||
|
from `GET /Library/VirtualFolders` — its field is `ItemId`, not `Id` — and
|
||||||
|
that same record's `RefreshStatus` is the progress read.
|
||||||
|
- **A file whose name carries no words is bonus material, not a folder.**
|
||||||
|
`.Extra 1 ( 1H 27min multisub ).mkv` parses to `1`; a movie folder named `1`
|
||||||
|
is not a movie. It becomes a review line telling a human to name the file.
|
||||||
|
- **The executor renames and nothing else.** `rename(2)` cannot cross a
|
||||||
|
filesystem, so a plan that would is refused rather than silently copying tens
|
||||||
|
of terabytes.
|
||||||
|
- **The journal is a write-ahead log: the intent is fsynced before the rename.**
|
||||||
|
An interrupted run therefore leaves only two states — an intent whose move
|
||||||
|
happened, and an intent whose move never ran — and the next run finishes the
|
||||||
|
job without moving anything twice. A file that moved with no journal line did
|
||||||
|
not move through this tool, and the tool refuses to guess on its behalf.
|
||||||
|
- **The journal lives BESIDE the library root, never inside it** (default:
|
||||||
|
`media-relayout.jsonl` next to the root): a log inside the library is a file
|
||||||
|
Jellyfin indexes.
|
||||||
|
- **Refusals are all-or-nothing.** An occupied target, a vanished source or a
|
||||||
|
duplicate destination stops the run *before the first rename*, so the operator
|
||||||
|
fixes one thing and re-runs instead of untangling a half-applied plan.
|
||||||
|
`--force` relaxes only the opinion guards; it never permits overwriting.
|
||||||
|
- **A sweep deletes only what the journal proves the run emptied.** A folder is
|
||||||
|
eligible when a journalled file moved out of it or out of something under it,
|
||||||
|
it now holds nothing but empty directories, no journalled destination lives
|
||||||
|
beneath it, and it is not a folder the run created. A folder nobody moved —
|
||||||
|
someone's placeholder — cannot be selected at all, and a symlink anywhere
|
||||||
|
inside disqualifies the subtree, because the thing behind a link is not this
|
||||||
|
tool's to delete. Every removal is journalled as `rmdir` and `rollback`
|
||||||
|
recreates the folder, so the sweep is undone by the same command as the moves.
|
||||||
|
Proof carries downward: inside a folder the journal proves emptied, every
|
||||||
|
empty directory goes with it. A release's `Screens/` or `Sample/` is never a
|
||||||
|
move source itself, so 146 of them held their emptied parents on disk after
|
||||||
|
the first sweep.
|
||||||
|
A failed `rmdir` is not a removal: only a folder that is really gone leaves
|
||||||
|
scope, so the next sweep finishes what a permission problem stopped.
|
||||||
|
- **An episode-shaped name marks a SERIES, not a film — and the series is the
|
||||||
|
name BEFORE the marker.** `Monk - S05E03 - Mr. Monk and the Big Game` is series
|
||||||
|
`Monk`. Keyed on the whole title instead, every episode became a group of one
|
||||||
|
and never reached the five-episode size that calls something a series, so a
|
||||||
|
box set was filed as 124 movies in 124 folders. The threshold still earns its
|
||||||
|
keep: a lone film named `Part 2` stays a film. Episodes of a series are parked
|
||||||
|
in `_TV needs a TV library - <series>` for a human to move to a TV source —
|
||||||
|
the movie tool never files TV.
|
||||||
|
- **One journal is one undo.** The TV move runs on its own journal, and with
|
||||||
|
`--root` set to the share that holds BOTH libraries: the executor refuses any
|
||||||
|
path outside `--root`, and a journal shared with the movie relayout would
|
||||||
|
undo both runs when only one of them is wanted.
|
||||||
|
|
||||||
|
- **Sidecar metadata does not follow a rename, because it lives on another
|
||||||
|
filesystem.** The media servers write `.nfo` and artwork through the
|
||||||
|
[[Media Metadata View]] (NVMe upper layer); this tool moves bytes on the raw
|
||||||
|
array path. After any sweep or TV move, run `scripts/media-view.sh reap`, then
|
||||||
|
the same command with `--apply`: the sidecars left at the old coordinates become
|
||||||
|
ghost folders — artwork and an `.nfo` with no video — that both servers browse
|
||||||
|
and report as content.
|
||||||
|
- **Nothing deletes through the metadata view.** A delete there writes an overlayfs
|
||||||
|
whiteout in the upper layer, which hides the array's copy without removing it;
|
||||||
|
the file returns when the view is unmounted. Deletions in this pipeline happen
|
||||||
|
on the raw path only, which is what the journal can undo.
|
||||||
|
|
||||||
|
## Rollback
|
||||||
|
|
||||||
|
One command, from the journal of the run:
|
||||||
|
`media-apply.py rollback --journal …/media-relayout.jsonl --apply`. It recreates
|
||||||
|
any folder a sweep removed, restores every source path, removes only the folders
|
||||||
|
that run created *and that are now empty*, and refuses everything if any moved
|
||||||
|
file cannot be accounted for. A rolled-back journal refuses to roll back twice.
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
[[Jellyfin]], [[Plex]], [[Media Metadata View]], [[Entropy Volume]], [[Testing Policy]], [[Storage Policy]]
|
||||||
|
|
||||||
|
## Citations
|
||||||
|
|
||||||
|
- Jellyfin Movie documentation — folder/file naming, extras subfolders,
|
||||||
|
`Movie (2013) - 216p.mkv` version and `- 4K.mkv` handling:
|
||||||
|
<https://jellyfin.org/docs/general/server/media/movies>
|
||||||
@@ -4,7 +4,7 @@ description: "Plex Media Server is a **containerized media server on Dankserver*
|
|||||||
tags:
|
tags:
|
||||||
- dankserver
|
- dankserver
|
||||||
- plex
|
- plex
|
||||||
timestamp: 2026-10-21
|
timestamp: 2026-10-30
|
||||||
---
|
---
|
||||||
|
|
||||||
# Plex
|
# Plex
|
||||||
@@ -13,7 +13,8 @@ Plex Media Server is a **containerized media server on Dankserver**: a podman
|
|||||||
quadlet (`plex.service`) under the systemd **user** generator, generated from
|
quadlet (`plex.service`) under the systemd **user** generator, generated from
|
||||||
`dankserver/plex/quadlet/plex.container`. It shares the host with a second live
|
`dankserver/plex/quadlet/plex.container`. It shares the host with a second live
|
||||||
media server, [[Jellyfin]] (`jellyfin.service`, listening on 8096); both serve
|
media server, [[Jellyfin]] (`jellyfin.service`, listening on 8096); both serve
|
||||||
from the same read-only Entropy Volume mount.
|
from the same tree — the [[Media Metadata View]] overlay — so the Kodi-format
|
||||||
|
sidecars Jellyfin writes are the files Plex reads, with no copy and no sync.
|
||||||
|
|
||||||
**Host:** Dankserver — **Ports:** 32400/tcp (PMS web + API), 32400 is the
|
**Host:** Dankserver — **Ports:** 32400/tcp (PMS web + API), 32400 is the
|
||||||
only port PMS needs for client streaming; LAN clients connect directly,
|
only port PMS needs for client streaming; LAN clients connect directly,
|
||||||
@@ -26,7 +27,7 @@ public traffic can be fronted by Caddy like every other vhost.
|
|||||||
| Runtime | podman quadlet, `plex.service` (user manager) — migrated from the native `plexmediaserver.service` OS-package unit (2026-09) |
|
| Runtime | podman quadlet, `plex.service` (user manager) — migrated from the native `plexmediaserver.service` OS-package unit (2026-09) |
|
||||||
| Image | `lscr.io/linuxserver/plex:1.43.5.11029-6bf8af3f0-ls329` (pinned; verified == the registry's `latest` manifest digest at pin time: `sha256:8f655bad…e9e2334`) |
|
| Image | `lscr.io/linuxserver/plex:1.43.5.11029-6bf8af3f0-ls329` (pinned; verified == the registry's `latest` manifest digest at pin time: `sha256:8f655bad…e9e2334`) |
|
||||||
| Data root | `/var/lib/plexmediaserver` bind-mounted **in place** as `/config` (RW) — the migration moved zero bytes |
|
| Data root | `/var/lib/plexmediaserver` bind-mounted **in place** as `/config` (RW) — the migration moved zero bytes |
|
||||||
| Media | `/mnt/entropy` mounted at the same absolute path **read-only** — every library path in the PMS database stayed valid, zero re-scan |
|
| Media | `/var/lib/media-meta/view` (the [[Media Metadata View]] overlay) bound at the same absolute path `/mnt/entropy` **read-only** — every library path in the PMS database stayed valid, zero re-scan |
|
||||||
| Identity | fleet rootless model — container root maps to the `dank` user (1000:1000); the deployer chowns the data root on first cutover |
|
| Identity | fleet rootless model — container root maps to the `dank` user (1000:1000); the deployer chowns the data root on first cutover |
|
||||||
| Port | `32400/tcp` published on `0.0.0.0` — zero client reconfiguration |
|
| Port | `32400/tcp` published on `0.0.0.0` — zero client reconfiguration |
|
||||||
| Network | `dankserver_appnet`, alias `plex`; Tautulli reaches it at `http://plex:32400` |
|
| Network | `dankserver_appnet`, alias `plex`; Tautulli reaches it at `http://plex:32400` |
|
||||||
@@ -38,7 +39,8 @@ public traffic can be fronted by Caddy like every other vhost.
|
|||||||
|
|
||||||
The migration moved zero bytes: the container bind-mounts the native data
|
The migration moved zero bytes: the container bind-mounts the native data
|
||||||
root where it already lived, and the media archive re-mounts at
|
root where it already lived, and the media archive re-mounts at
|
||||||
`/mnt/entropy` inside the container, so every absolute library path in the
|
`/mnt/entropy` inside the container — only the HOST side of that bind has moved
|
||||||
|
(array, then the overlay view), so every absolute library path in the
|
||||||
PMS database keeps resolving. The one metadata change: rootless podman
|
PMS database keeps resolving. The one metadata change: rootless podman
|
||||||
cannot map the real host uid 997, so the fleet model applies — container
|
cannot map the real host uid 997, so the fleet model applies — container
|
||||||
root runs as the `dank` user, and the deployer adopts the data root with
|
root runs as the `dank` user, and the deployer adopts the data root with
|
||||||
|
|||||||
@@ -49,8 +49,10 @@ is a locator: one row, one clause, no numbers.
|
|||||||
| [[Copyparty]] | Copyparty is a lightweight file server and media streamer that runs as a single Python binary. |
|
| [[Copyparty]] | Copyparty is a lightweight file server and media streamer that runs as a single Python binary. |
|
||||||
| [[Dankserver Stack]] | Service inventory and routing overview for the media and library host. |
|
| [[Dankserver Stack]] | Service inventory and routing overview for the media and library host. |
|
||||||
| [[Entropy Volume]] | The Dankserver entropy volume is a RAID-backed LUKS filesystem mounted at |
|
| [[Entropy Volume]] | The Dankserver entropy volume is a RAID-backed LUKS filesystem mounted at |
|
||||||
| [[Jellyfin]] | Jellyfin on Dankserver: quadlet unit, entropy-volume data layout, ports, client access, hardware-transcode status. |
|
| [[Jellyfin]] | Jellyfin on Dankserver: quadlet unit, overlay metadata view, provider keys, ports, access, transcode status. |
|
||||||
| [[mariadb]] | The MariaDB backend for RomM: quadlet unit, network alias, env-file secret handling, and the backup path. |
|
| [[mariadb]] | The MariaDB backend for RomM: quadlet unit, network alias, env-file secret handling, and the backup path. |
|
||||||
|
| [[Media Metadata View]] | The SSD-backed overlay the two media servers read and write: shape, guards, commands, laws, rollback. |
|
||||||
|
| [[Media Relayout]] | Census, plan and apply the movie and TV libraries into Jellyfin format: gates, evidence chain, refusals, rollback. |
|
||||||
| [[Plex]] | Plex Media Server is a **containerized media server on Dankserver**: a podman |
|
| [[Plex]] | Plex Media Server is a **containerized media server on Dankserver**: a podman |
|
||||||
| [[redis]] | The Redis cache and task queue for RomM: quadlet unit, network alias topology, and the backup path. |
|
| [[redis]] | The Redis cache and task queue for RomM: quadlet unit, network alias topology, and the backup path. |
|
||||||
| [[RomM]] | RomM (ROM Manager) is a self-hosted ROM library manager and player. |
|
| [[RomM]] | RomM (ROM Manager) is a self-hosted ROM library manager and player. |
|
||||||
|
|||||||
@@ -133,6 +133,16 @@ REMOTE_DEPLOY="$REPO_ROOT/scripts/deploy-remote.sh"
|
|||||||
[ -z "$output" ]
|
[ -z "$output" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "a compose edit never restarts a Quadlet-owned media server" {
|
||||||
|
# dankserver/compose.yaml is an inventory; the media servers' Quadlets own
|
||||||
|
# their containers. A compose edit that restarted them would cut real viewers
|
||||||
|
# for a change that cannot affect them.
|
||||||
|
run python3 "$PLANNER" --target dankserver --paths dankserver/compose.yaml
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
! printf '%s\n' "$output" | grep -qx jellyfin
|
||||||
|
! printf '%s\n' "$output" | grep -qx plex
|
||||||
|
}
|
||||||
|
|
||||||
@test "compose planner narrows a compose edit to the changed service" {
|
@test "compose planner narrows a compose edit to the changed service" {
|
||||||
local base head
|
local base head
|
||||||
base=$(git -C "$REPO_ROOT" rev-parse HEAD)
|
base=$(git -C "$REPO_ROOT" rev-parse HEAD)
|
||||||
|
|||||||
Executable
+297
@@ -0,0 +1,297 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
# Seam: scripts/jellyfin-providers.sh — the only mechanism that puts a
|
||||||
|
# metadata-provider key into Jellyfin's plugin configuration on Dankserver.
|
||||||
|
#
|
||||||
|
# Output vocabulary the tests depend on:
|
||||||
|
# "+ <action>" something this run will change
|
||||||
|
# "= <fact>" something already true, so nothing changes
|
||||||
|
#
|
||||||
|
# The contracts, in the order the seam tests them:
|
||||||
|
# 1. plan reports every action and never prints a key;
|
||||||
|
# 2. a secret file that is not mode 600 is refused by name and nothing is
|
||||||
|
# written — a key readable by other users is an error, not a warning;
|
||||||
|
# 3. a downloaded plugin artifact is accepted only on its pinned sha256, its
|
||||||
|
# official guid and its pinned version (a swapped or downgraded artifact
|
||||||
|
# is refused before anything lands in plugins/);
|
||||||
|
# 4. the TMDb key is written into the EXISTING config with every other
|
||||||
|
# setting preserved — Jellyfin owns that file, and clobbering it would
|
||||||
|
# silently reset country, language and adult settings;
|
||||||
|
# 5. a TVDB config the operator already saved through the UI is kept;
|
||||||
|
# 6. install is idempotent: a second run changes no byte;
|
||||||
|
# 7. whatever is replaced is copied to the trash first (fleet law: never
|
||||||
|
# overwrite without a recoverable copy);
|
||||||
|
# 8. verify answers whether the endpoint is configured, key LENGTH only;
|
||||||
|
# 9. no plugin configuration XML is tracked in git, the deployer places the
|
||||||
|
# keys BEFORE it restarts Jellyfin, and the planner routes a change to this
|
||||||
|
# installer to jellyfin.
|
||||||
|
|
||||||
|
load helpers
|
||||||
|
|
||||||
|
PROV="$REPO_ROOT/scripts/jellyfin-providers.sh"
|
||||||
|
TMDB_KEY="tmdbk3a9Zq7Rtestkey"
|
||||||
|
TVDB_KEY="tvdbk8b2Wt4Ptestkey"
|
||||||
|
GUID_OFFICIAL="a677c0da-fac5-4cde-941a-7134223f14c8"
|
||||||
|
|
||||||
|
# make_plugin <file> <guid> <version> — a zip shaped like the official TheTVDB
|
||||||
|
# artifact (meta.json + its two DLLs), so extraction is exercised for real.
|
||||||
|
make_plugin() {
|
||||||
|
python3 - "$1" "$2" "$3" <<'PYEOF'
|
||||||
|
import json, sys, zipfile
|
||||||
|
path, guid, version = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||||
|
meta = {
|
||||||
|
"category": "MoviesAndShows",
|
||||||
|
"guid": guid,
|
||||||
|
"name": "TheTVDB",
|
||||||
|
"owner": "jellyfin",
|
||||||
|
"targetAbi": "12.0.0.0",
|
||||||
|
"version": version,
|
||||||
|
}
|
||||||
|
with zipfile.ZipFile(path, "w") as z:
|
||||||
|
z.writestr("meta.json", json.dumps(meta))
|
||||||
|
z.writestr("Jellyfin.Plugin.Tvdb.dll", "dll-bytes")
|
||||||
|
z.writestr("Tvdb.Sdk.dll", "dll-bytes")
|
||||||
|
PYEOF
|
||||||
|
sha256sum "$1" | cut -d' ' -f1
|
||||||
|
}
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
JF="$BATS_TEST_TMPDIR/jf"
|
||||||
|
TRASH="$BATS_TEST_TMPDIR/trash"
|
||||||
|
SEC="$BATS_TEST_TMPDIR/secrets"
|
||||||
|
mkdir -p "$JF/plugins/configurations" "$TRASH" "$SEC"
|
||||||
|
cat >"$JF/plugins/configurations/Jellyfin.Plugin.Tmdb.xml" <<XML
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||||
|
<MetadataCountryCode>US</MetadataCountryCode>
|
||||||
|
<PreferOriginalLanguage />
|
||||||
|
<Language>en</Language>
|
||||||
|
<EnableCinematicForTrailers>false</EnableCinematicForTrailers>
|
||||||
|
<IncludeAdult>false</IncludeAdult>
|
||||||
|
<MaxCastMembers>25</MaxCastMembers>
|
||||||
|
<AddGenresFromKeywords>true</AddGenresFromKeywords>
|
||||||
|
<TmdbApiKey />
|
||||||
|
</Configuration>
|
||||||
|
XML
|
||||||
|
KEYS="$SEC/provider-keys"
|
||||||
|
printf 'TMDB_API_KEY=%s\nTVDB_API_KEY=%s\n' "$TMDB_KEY" "$TVDB_KEY" >"$KEYS"
|
||||||
|
chmod 600 "$KEYS"
|
||||||
|
ZIP="$BATS_TEST_TMPDIR/thetvdb.zip"
|
||||||
|
PIN_SHA="$(make_plugin "$ZIP" "$GUID_OFFICIAL" 24.0.0.0)"
|
||||||
|
PROV_ENV=(
|
||||||
|
JF_ROOT="$JF"
|
||||||
|
PROV_KEYS="$KEYS"
|
||||||
|
TRASH_DIR="$TRASH"
|
||||||
|
TVDB_PLUGIN_URL="file://$ZIP"
|
||||||
|
TVDB_PLUGIN_SHA256="$PIN_SHA"
|
||||||
|
TVDB_PLUGIN_GUID="$GUID_OFFICIAL"
|
||||||
|
TVDB_PLUGIN_VERSION=24.0.0.0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
prov() { env "${PROV_ENV[@]}" bash "$PROV" "$@"; }
|
||||||
|
|
||||||
|
tree_fingerprint() { find "$JF" -type f | sort | xargs -r sha256sum | sha256sum; }
|
||||||
|
|
||||||
|
@test "plan names the plugin and both config files, and prints no key" {
|
||||||
|
run prov plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -q '^+ install thetvdb_24\.0\.0\.0'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -q '^+ set TmdbApiKey'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -q '^+ create Jellyfin\.Plugin\.Tvdb\.xml'
|
||||||
|
# The whole point of running this over a shoulder: a key never appears.
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF "$TMDB_KEY"
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF "$TVDB_KEY"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "plan writes nothing" {
|
||||||
|
before="$(tree_fingerprint)"
|
||||||
|
run prov plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$(tree_fingerprint)" = "$before" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an already-configured endpoint reports no pending action" {
|
||||||
|
prov install >/dev/null
|
||||||
|
run prov plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -q '^+ '
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -q '= thetvdb_24\.0\.0\.0 already installed'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -q '= TmdbApiKey already set'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -q '= Jellyfin\.Plugin\.Tvdb\.xml already has an ApiKey'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a keys file that is not mode 600 is refused and nothing is written" {
|
||||||
|
chmod 644 "$KEYS"
|
||||||
|
before="$(tree_fingerprint)"
|
||||||
|
run prov install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qF '600'
|
||||||
|
[ "$(tree_fingerprint)" = "$before" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a missing TVDB key is refused by name before anything is fetched" {
|
||||||
|
printf 'TMDB_API_KEY=%s\n' "$TMDB_KEY" >"$KEYS"
|
||||||
|
chmod 600 "$KEYS"
|
||||||
|
run prov install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qF 'TVDB_API_KEY'
|
||||||
|
[ ! -d "$JF/plugins/thetvdb_24.0.0.0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a root with no plugin directory is refused instead of fabricated" {
|
||||||
|
# Creating plugins/ inside a wrong path would install a plugin Jellyfin
|
||||||
|
# never loads, and would hide the operator's typo.
|
||||||
|
run env "${PROV_ENV[@]}" JF_ROOT="$BATS_TEST_TMPDIR/nope" bash "$PROV" install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[ ! -d "$BATS_TEST_TMPDIR/nope/plugins" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an artifact whose sha256 is not the pinned one is refused" {
|
||||||
|
run env "${PROV_ENV[@]}" TVDB_PLUGIN_SHA256="$(printf 'f%.0s' {1..64})" \
|
||||||
|
bash "$PROV" install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'sha256'
|
||||||
|
[ ! -d "$JF/plugins/thetvdb_24.0.0.0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an artifact that is not the official TVDB plugin is refused" {
|
||||||
|
# A matching sha proves only that the bytes are the bytes we fetched.
|
||||||
|
EVIL="$BATS_TEST_TMPDIR/evil.zip"
|
||||||
|
EVIL_SHA="$(make_plugin "$EVIL" "deadbeef-0000-0000-0000-000000000000" 24.0.0.0)"
|
||||||
|
run env "${PROV_ENV[@]}" TVDB_PLUGIN_URL="file://$EVIL" TVDB_PLUGIN_SHA256="$EVIL_SHA" \
|
||||||
|
bash "$PROV" install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'guid'
|
||||||
|
[ ! -d "$JF/plugins/thetvdb_24.0.0.0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an artifact that is not the pinned version is refused" {
|
||||||
|
OLD="$BATS_TEST_TMPDIR/old.zip"
|
||||||
|
OLD_SHA="$(make_plugin "$OLD" "$GUID_OFFICIAL" 22.0.0.0)"
|
||||||
|
run env "${PROV_ENV[@]}" TVDB_PLUGIN_URL="file://$OLD" TVDB_PLUGIN_SHA256="$OLD_SHA" \
|
||||||
|
bash "$PROV" install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'version'
|
||||||
|
[ ! -d "$JF/plugins/thetvdb_24.0.0.0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a different plugin version already in plugins/ is refused, not doubled" {
|
||||||
|
# Two thetvdb_* directories means Jellyfin loads one plugin twice; the
|
||||||
|
# operator removes the old one (into the trash) deliberately, not by an
|
||||||
|
# installer quietly adding a second.
|
||||||
|
prov install >/dev/null
|
||||||
|
OLD="$BATS_TEST_TMPDIR/old.zip"
|
||||||
|
OLD_SHA="$(make_plugin "$OLD" "$GUID_OFFICIAL" 22.0.0.0)"
|
||||||
|
run env "${PROV_ENV[@]}" TVDB_PLUGIN_URL="file://$OLD" TVDB_PLUGIN_SHA256="$OLD_SHA" \
|
||||||
|
TVDB_PLUGIN_VERSION=22.0.0.0 bash "$PROV" install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qF 'thetvdb_24.0.0.0'
|
||||||
|
[ ! -d "$JF/plugins/thetvdb_22.0.0.0" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install places the plugin, keys TMDb, and preserves every other setting" {
|
||||||
|
run prov install
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF "$TMDB_KEY"
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF "$TVDB_KEY"
|
||||||
|
[ -f "$JF/plugins/thetvdb_24.0.0.0/meta.json" ]
|
||||||
|
[ -f "$JF/plugins/thetvdb_24.0.0.0/Jellyfin.Plugin.Tvdb.dll" ]
|
||||||
|
local cfg="$JF/plugins/configurations/Jellyfin.Plugin.Tmdb.xml"
|
||||||
|
# Jellyfin owns this file: the settings it wrote must survive the edit.
|
||||||
|
for setting in MetadataCountryCode Language IncludeAdult MaxCastMembers AddGenresFromKeywords; do
|
||||||
|
grep -qF "<$setting>" "$cfg" || {
|
||||||
|
echo "lost $setting"
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
done
|
||||||
|
grep -qF '<IncludeAdult>false</IncludeAdult>' "$cfg"
|
||||||
|
grep -qF "<TmdbApiKey>$TMDB_KEY</TmdbApiKey>" "$cfg"
|
||||||
|
grep -qF "<ApiKey>$TVDB_KEY</ApiKey>" "$JF/plugins/configurations/Jellyfin.Plugin.Tvdb.xml"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a TVDB config the operator saved through the UI is kept" {
|
||||||
|
cat >"$JF/plugins/configurations/Jellyfin.Plugin.Tvdb.xml" <<XML
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
|
||||||
|
<ApiKey>ui-saved-key</ApiKey>
|
||||||
|
<EnableSeasonPathMatching>true</EnableSeasonPathMatching>
|
||||||
|
</Configuration>
|
||||||
|
XML
|
||||||
|
run prov install
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'kept'
|
||||||
|
local cfg="$JF/plugins/configurations/Jellyfin.Plugin.Tvdb.xml"
|
||||||
|
grep -qF '<ApiKey>ui-saved-key</ApiKey>' "$cfg"
|
||||||
|
grep -qF '<EnableSeasonPathMatching>true</EnableSeasonPathMatching>' "$cfg"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a pin in the TVDB secret lands in the plugin config" {
|
||||||
|
# A subscriber-shaped key needs the account's PIN beside it; a licensed key
|
||||||
|
# does not, so the element appears only when the secret carries one.
|
||||||
|
printf 'TMDB_API_KEY=%s\nTVDB_API_KEY=%s\nTVDB_PIN=123456\n' "$TMDB_KEY" "$TVDB_KEY" >"$KEYS"
|
||||||
|
chmod 600 "$KEYS"
|
||||||
|
prov install >/dev/null
|
||||||
|
grep -qF '<Pin>123456</Pin>' "$JF/plugins/configurations/Jellyfin.Plugin.Tvdb.xml"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install is idempotent: a second run changes no byte" {
|
||||||
|
prov install >/dev/null
|
||||||
|
before="$(tree_fingerprint)"
|
||||||
|
run prov install
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$(tree_fingerprint)" = "$before" ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'no change'
|
||||||
|
# One plugin directory only: two versions in plugins/ means two loads.
|
||||||
|
[ "$(find "$JF/plugins" -maxdepth 1 -type d -name 'thetvdb_*' | wc -l)" -eq 1 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "what install replaces is copied to the trash first" {
|
||||||
|
prov install >/dev/null
|
||||||
|
# The TMDb config existed before this tool touched it, so the pre-edit copy
|
||||||
|
# must stay recoverable.
|
||||||
|
[ -n "$(find "$TRASH" -name 'Jellyfin.Plugin.Tmdb.xml*' -print -quit)" ]
|
||||||
|
grep -qF '<TmdbApiKey />' "$(find "$TRASH" -name 'Jellyfin.Plugin.Tmdb.xml*' | head -1)"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "verify refuses an unconfigured endpoint and names what is missing" {
|
||||||
|
run prov verify
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'thetvdb'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qiF 'TmdbApiKey'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "verify passes once the plugin and both keys are in place" {
|
||||||
|
prov install >/dev/null
|
||||||
|
run prov verify
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF "$TMDB_KEY"
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF "$TVDB_KEY"
|
||||||
|
# Length, not bytes: the operator sees the key loaded without reading it.
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qE 'TmdbApiKey .*[0-9]+ chars'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "no plugin configuration XML is tracked in this repo" {
|
||||||
|
# These files hold provider keys and are owned by the running container;
|
||||||
|
# tracking them would put credentials in git and guarantee drift.
|
||||||
|
run git -C "$REPO_ROOT" ls-files
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qF 'plugins/configurations'
|
||||||
|
! printf '%s\n' "${lines[@]}" | grep -qiF 'Plugin.Tvdb.xml'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the planner routes this installer to jellyfin" {
|
||||||
|
run python3 "$REPO_ROOT/scripts/deploy-plan.py" --target dankserver \
|
||||||
|
--paths scripts/jellyfin-providers.sh
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$output" = "jellyfin" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the deployer places the keys before it restarts jellyfin" {
|
||||||
|
# The plugin and both keys are read at start-up, so a restart that runs
|
||||||
|
# ahead of the placement boots the old, keyless state.
|
||||||
|
body="$(sed -n '/^deploy_jellyfin()/,/^\}/p' "$REPO_ROOT/scripts/deploy-remote.sh")"
|
||||||
|
[ -n "$body" ]
|
||||||
|
up="$(printf '%s\n' "$body" | grep -n 'ensure_provider_keys' | head -1 | cut -d: -f1)"
|
||||||
|
restart="$(printf '%s\n' "$body" | grep -n 'systemctl --user restart jellyfin.service' | head -1 | cut -d: -f1)"
|
||||||
|
[ "${up:-999}" -lt "${restart:-0}" ]
|
||||||
|
}
|
||||||
Executable
+161
@@ -0,0 +1,161 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
# Seam: the SSD-backed metadata view (OverlayFS) that lets Jellyfin write
|
||||||
|
# Kodi-format sidecars for Plex to read, without ever writing to the spindle
|
||||||
|
# array and without letting either media server start against a missing view.
|
||||||
|
#
|
||||||
|
# The contracts, in the order the seam tests them:
|
||||||
|
# 1. the plan puts the writable layer on the SSD and the media layer below it;
|
||||||
|
# 2. bad input is refused by name instead of mounting something surprising;
|
||||||
|
# 3. verify refuses a view that is not mounted (so nothing trusts a directory);
|
||||||
|
# 4. both media units bind the VIEW and refuse to start without it;
|
||||||
|
# 5. only those two units touch the view — the LLM and library tenants keep the
|
||||||
|
# raw volume, so the blast radius is two containers;
|
||||||
|
# 6. the deployer brings the view up BEFORE it restarts either server;
|
||||||
|
# 7. the planner routes a change to these files to both media services.
|
||||||
|
|
||||||
|
load helpers
|
||||||
|
|
||||||
|
VIEW="$REPO_ROOT/scripts/media-view.sh"
|
||||||
|
JELLY="$REPO_ROOT/dankserver/jellyfin/quadlet/jellyfin.container"
|
||||||
|
PLEX="$REPO_ROOT/dankserver/plex/quadlet/plex.container"
|
||||||
|
MOUNT=/var/lib/media-meta/view
|
||||||
|
UNIT="$REPO_ROOT/dankserver/host/systemd/media-meta-view.mount"
|
||||||
|
|
||||||
|
@test "the plan names an overlay with the spindle below and the SSD above" {
|
||||||
|
run bash "$VIEW" plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
# Lower = the array; upper/work = the NVMe. A user asked for metadata to be
|
||||||
|
# SSD-resident, so a spindle upper layer would defeat the whole mechanism.
|
||||||
|
[ "${lines[0]}" = "lowerdir=/mnt/entropy" ]
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qx 'upperdir=/var/lib/media-meta/upper'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -qx 'workdir=/var/lib/media-meta/work'
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -Fqx "mountpoint=$MOUNT"
|
||||||
|
# systemd names a mount unit after its escaped path, backslash and all.
|
||||||
|
printf '%s\n' "${lines[@]}" | grep -Fqx \
|
||||||
|
'unit=/etc/systemd/system/var-lib-media\x2dmeta-view.mount'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "install refuses a caller who cannot write the unit directory or mount" {
|
||||||
|
# Half an install is worse than none: the unit would exist without the
|
||||||
|
# directories behind it. Refuse by name before anything is created.
|
||||||
|
if [ "$(id -u)" -eq 0 ]; then
|
||||||
|
skip "this seam is running as root"
|
||||||
|
fi
|
||||||
|
run bash "$VIEW" install
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[[ "$output" == *"root"* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "verify refuses a view that is not mounted" {
|
||||||
|
# Every consumer trusts the mount point to BE the library. On a host where it
|
||||||
|
# is a plain empty directory, verify must fail loudly: that is the difference
|
||||||
|
# between a scan that finds nothing and a scan that empties a library DB.
|
||||||
|
if mountpoint -q "$MOUNT"; then
|
||||||
|
skip "$MOUNT is a real mountpoint on this host"
|
||||||
|
fi
|
||||||
|
run bash "$VIEW" verify
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[[ "$output" == *"$MOUNT"* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "both media servers bind the view and refuse to start without it" {
|
||||||
|
for unit in "$JELLY" "$PLEX"; do
|
||||||
|
grep -q "^Volume=$MOUNT:" "$unit" || return 1
|
||||||
|
grep -q "^UnitConfigItem=ConditionPathIsMountPoint=$MOUNT$" "$unit" || return 1
|
||||||
|
# The raw array must no longer be a container source: that bind is what
|
||||||
|
# made sidecar sharing impossible (read-only) and metadata slow (seeks).
|
||||||
|
! grep -q '^Volume=/mnt/entropy' "$unit"
|
||||||
|
done
|
||||||
|
# Jellyfin is the writer, so it needs the view writable; Plex only reads the
|
||||||
|
# sidecars, and keeping its bind read-only costs nothing.
|
||||||
|
grep -q "^Volume=$MOUNT:/data$" "$JELLY"
|
||||||
|
grep -q "^Volume=$MOUNT:/mnt/entropy:ro$" "$PLEX"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "only the two media units reference the view" {
|
||||||
|
run grep -rl --include='*.container' "$MOUNT" "$REPO_ROOT"/dankserver \
|
||||||
|
"$REPO_ROOT"/dankclaw "$REPO_ROOT"/frankbook-pro 2>/dev/null
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$(printf '%s\n' "$output" | wc -l)" -eq 2 ]
|
||||||
|
printf '%s\n' "$output" | grep -q 'jellyfin/quadlet/jellyfin.container'
|
||||||
|
printf '%s\n' "$output" | grep -q 'plex/quadlet/plex.container'
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the mount unit is an overlay whose upper layer lives on the SSD" {
|
||||||
|
[ -f "$UNIT" ]
|
||||||
|
run grep -E '^Options=lowerdir=/mnt/entropy,upperdir=/var/lib/media-meta/upper,workdir=/var/lib/media-meta/work' "$UNIT"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
# The unit must mount exactly where the two containers bind, or the guard and
|
||||||
|
# the bind disagree and a media server browses an empty directory.
|
||||||
|
run grep -Fqx "Where=$MOUNT" "$UNIT"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
# It must wait for the array to be unlocked: mounting an overlay whose lower
|
||||||
|
# dir is an empty /mnt/entropy would present an empty library, not an error.
|
||||||
|
run grep -E '^RequiresMountsFor=/mnt/entropy$' "$UNIT"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the deployer raises the view before it restarts either media server" {
|
||||||
|
for svc in jellyfin plex; do
|
||||||
|
body="$(sed -n "/^deploy_${svc}()/,/^\}/p" "$REPO_ROOT/scripts/deploy-remote.sh")"
|
||||||
|
[ -n "$body" ]
|
||||||
|
up="$(printf '%s\n' "$body" | grep -n 'ensure_media_view' | head -1 | cut -d: -f1)"
|
||||||
|
up_line="${up:-999}"
|
||||||
|
restart="$(printf '%s\n' "$body" | grep -n "systemctl --user restart $svc.service" | head -1 | cut -d: -f1)"
|
||||||
|
restart_line="${restart:-0}"
|
||||||
|
[ "$up_line" -lt "$restart_line" ]
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the planner routes the view files to both media services" {
|
||||||
|
run python3 "$REPO_ROOT/scripts/deploy-plan.py" --target dankserver --paths \
|
||||||
|
scripts/media-view.sh dankserver/host/systemd/media-meta-view.mount
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$output" = "jellyfin
|
||||||
|
plex" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a docs-only change still plans nothing for the view" {
|
||||||
|
run python3 "$REPO_ROOT/scripts/deploy-plan.py" --target dankserver --paths \
|
||||||
|
"serverAppConfigs-docs/Dankserver/Media Metadata View.md"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ -z "$output" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "reap names the metadata whose media has moved and touches nothing" {
|
||||||
|
# A relayout moves media on the RAW path; the sidecars the view already wrote
|
||||||
|
# stay behind at the old coordinates. Left alone they become folders a media
|
||||||
|
# server browses and finds nothing in.
|
||||||
|
tmp="$(mktemp -d)"
|
||||||
|
mkdir -p "$tmp/meta/upper/Movies/Ghost (2020)" "$tmp/meta/upper/Movies/Keeper (2020)" \
|
||||||
|
"$tmp/meta/upper/TVShows/Big Little Lies/Season 01" "$tmp/meta/work" \
|
||||||
|
"$tmp/array/Movies/Keeper (2020)" "$tmp/array/TVShows"
|
||||||
|
: > "$tmp/meta/upper/Movies/Ghost (2020)/movie.nfo"
|
||||||
|
: > "$tmp/meta/upper/Movies/Keeper (2020)/movie.nfo"
|
||||||
|
: > "$tmp/meta/upper/TVShows/Big Little Lies/Season 01/show.nfo"
|
||||||
|
: > "$tmp/array/Movies/Keeper (2020)/Keeper (2020).mkv"
|
||||||
|
|
||||||
|
run bash "$VIEW" reap --upper-base "$tmp/meta" --lower "$tmp/array"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
printf '%s\n' "$output" | grep -qxF 'Movies/Ghost (2020)'
|
||||||
|
# The series folder is still below, so the ghost is the season inside it — and
|
||||||
|
# it is reported at the level that is actually missing, not deeper.
|
||||||
|
printf '%s\n' "$output" | grep -qxF 'TVShows/Big Little Lies'
|
||||||
|
! printf '%s\n' "$output" | grep -q 'Keeper'
|
||||||
|
[ -f "$tmp/meta/upper/Movies/Ghost (2020)/movie.nfo" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "reap --apply moves ghosts to the SSD trash and keeps live metadata" {
|
||||||
|
tmp="$(mktemp -d)"
|
||||||
|
mkdir -p "$tmp/meta/upper/Movies/Ghost (2020)" "$tmp/meta/upper/Movies/Keeper (2020)" \
|
||||||
|
"$tmp/array/Movies/Keeper (2020)"
|
||||||
|
: > "$tmp/meta/upper/Movies/Ghost (2020)/movie.nfo"
|
||||||
|
: > "$tmp/meta/upper/Movies/Keeper (2020)/movie.nfo"
|
||||||
|
|
||||||
|
run bash "$VIEW" reap --apply --upper-base "$tmp/meta" --lower "$tmp/array"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ ! -e "$tmp/meta/upper/Movies/Ghost (2020)" ]
|
||||||
|
[ -f "$tmp/meta/upper/Movies/Keeper (2020)/movie.nfo" ]
|
||||||
|
# Recoverable, not deleted: the item lands in a dated trash under the SSD base.
|
||||||
|
[ "$(find "$tmp/meta/.AGENTTRASH" -name movie.nfo | wc -l)" -eq 1 ]
|
||||||
|
}
|
||||||
Executable
+623
@@ -0,0 +1,623 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
# Seam: the movie-library relayout — scripts/media-census.py (the read-only
|
||||||
|
# census), scripts/media-plan.py (the plan) and scripts/media-apply.py (the only
|
||||||
|
# thing that moves files).
|
||||||
|
#
|
||||||
|
# The library is 8k files on a 100 %-full array, so the two things this seam
|
||||||
|
# exists to catch are the ones that cost a re-run of a 20-minute census: a file
|
||||||
|
# that ends up in the WRONG movie folder, and a move that destroys something.
|
||||||
|
# Everything below runs on a synthetic library in the test's own tmpdir: no
|
||||||
|
# ffprobe, no network, no server. The probe step is the one part a fixture
|
||||||
|
# cannot run (it opens real containers), so the tests feed the planner the
|
||||||
|
# probed TSV in the column order scripts/media-census.py exports — which pins
|
||||||
|
# that contract as a side effect.
|
||||||
|
#
|
||||||
|
# The plan's own gate lines (unplanned, left-unmoved, double-claim, occupied)
|
||||||
|
# are asserted as output, because they are how the operator learns the plan is
|
||||||
|
# trustworthy before pointing the executor at 24 TB.
|
||||||
|
|
||||||
|
load helpers
|
||||||
|
|
||||||
|
PLAN="$REPO_ROOT/scripts/media-plan.py"
|
||||||
|
CENSUS="$REPO_ROOT/scripts/media-census.py"
|
||||||
|
APPLY="$REPO_ROOT/scripts/media-apply.py"
|
||||||
|
TV="$REPO_ROOT/scripts/media-tv.py"
|
||||||
|
|
||||||
|
# probe_row FILE DUR [RES] [AUDIO] [SUBS] — one probed-TSV row: the twelve
|
||||||
|
# columns media-census.py probe writes (bytes, path, dur, vcodec, res, fps,
|
||||||
|
# vbit, pix, audio, subs, spare, fmt).
|
||||||
|
probe_row() {
|
||||||
|
local p="$1" dur="$2" res="${3:-1920x1080}" aud="${4:-aac+aac}" subs="${5:-0}"
|
||||||
|
printf '%s\t%s\t%s\thevc\t%s\t23.976\t5000000\t8bit\t%s\t%s\t0\tmatroska,webm\n' \
|
||||||
|
"$(stat -c %s "$p")" "$p" "$dur" "$res" "$aud" "$subs" >> "$PROBED"
|
||||||
|
}
|
||||||
|
|
||||||
|
# plex_row FILE TITLE YEAR — one Plex metadata row; the planner reads these
|
||||||
|
# three columns and treats a row without a plausible year as no match at all.
|
||||||
|
plex_row() {
|
||||||
|
printf '%s\t%s\t%s\n' "$1" "$2" "$3" >> "$PLEX"
|
||||||
|
}
|
||||||
|
|
||||||
|
# at PATH SIZE — a file with deterministic content, big enough to matter.
|
||||||
|
at() { mkdir -p "$(dirname "$1")"; head -c "$2" /dev/zero > "$1"; }
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
LIB="$BATS_TEST_TMPDIR/Movies"
|
||||||
|
ENUM="$BATS_TEST_TMPDIR/enum.tsv"
|
||||||
|
PROBED="$BATS_TEST_TMPDIR/probed.tsv"
|
||||||
|
PLEX="$BATS_TEST_TMPDIR/plex.tsv"
|
||||||
|
OUT="$BATS_TEST_TMPDIR/plan.tsv"
|
||||||
|
EXC="$BATS_TEST_TMPDIR/exceptions.txt"
|
||||||
|
TVLIB="$BATS_TEST_TMPDIR/TVShows"
|
||||||
|
TVPLAN="$BATS_TEST_TMPDIR/tv.tsv"
|
||||||
|
TVJ="$BATS_TEST_TMPDIR/tv.jsonl"
|
||||||
|
: > "$PROBED"
|
||||||
|
printf 'file\tleaf_title\tleaf_year\n' > "$PLEX"
|
||||||
|
|
||||||
|
# Two films that share a box set and a word, plus their bonus material:
|
||||||
|
# the shape that made one film's featurettes land in another's folder.
|
||||||
|
at "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" 4000
|
||||||
|
at "$LIB/Alien (1979)/Featurettes/A Bug And A Bite - Making Alien.mkv" 3300000
|
||||||
|
at "$LIB/Alien (1979)/Extras/Extra - Logo.mkv" 900000
|
||||||
|
at "$LIB/Alien (1979)/subs/3_Eng.srt" 40000
|
||||||
|
at "$LIB/Marnie (1964)/Marnie.1964.1080p.BluRay.x265.mkv" 5000000
|
||||||
|
at "$LIB/Marnie (1964)/Featurettes/The Trouble With Marnie.mkv" 2400000
|
||||||
|
# A long extra: over 45 minutes, so only the folder may call it bonus.
|
||||||
|
at "$LIB/Marnie (1964)/Featurettes/Commentary And Archives.mkv" 2600000
|
||||||
|
# A misspelled bonus folder: no bonus vocabulary, so only the folder that
|
||||||
|
# holds the feature can decide that this is not a film.
|
||||||
|
at "$LIB/Marnie (1964)/Exrtas/Marnie - A Closer Look.mkv" 2500000
|
||||||
|
# A Plex title that carries "/" and a Plex row that was never matched.
|
||||||
|
at "$LIB/VHS 2 (2013)/VHS.2.2013.1080p.BluRay.x265.mkv" 4200000
|
||||||
|
at "$LIB/AMERICAN POP/AMERICAN.POP.2023.1080p.WEB-DL.mkv" 3100000
|
||||||
|
# Junk the layout has to park somewhere other than a movie folder.
|
||||||
|
at "$LIB/.DS_Store" 6148
|
||||||
|
at "$LIB/Alien (1979)/._Alien.1979.1080p.BluRay.x265.10bit.mkv" 220
|
||||||
|
# A bonus file whose name is a number: never a movie folder named "1".
|
||||||
|
at "$LIB/Bruce Lee Collection 1971-1981/.Extra 1.mkv" 2700000
|
||||||
|
# A TV box set sitting in the movie library. This one names each file
|
||||||
|
# "Show - SxxEyy - Episode Title", so the episode title comes AFTER the
|
||||||
|
# marker; that convention is what made 124 episodes of one series look like
|
||||||
|
# 124 different films, each under its own threshold.
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
at "$LIB/Monk 2002 Seasons 1 to 8/Season 1/Monk - S01E$e - Mr Monk and the Rival.mkv" 3000000
|
||||||
|
done
|
||||||
|
# The same trouble named the other way round, with no episode title at all.
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
at "$LIB/Big Little Lies Season 1/Big Little Lies S01E$e.mp4" 3000000
|
||||||
|
done
|
||||||
|
# A film whose own name carries a bare "Part 2": one episode-shaped token is
|
||||||
|
# not a series, and it must not be dragged into the TV pile.
|
||||||
|
at "$LIB/The Godfather Part 2 (1974)/The.Godfather.Part.2.1974.1080p.BluRay.mkv" 9000000
|
||||||
|
|
||||||
|
probe_row "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" 4700
|
||||||
|
probe_row "$LIB/Alien (1979)/Featurettes/A Bug And A Bite - Making Alien.mkv" 2700
|
||||||
|
probe_row "$LIB/Alien (1979)/Extras/Extra - Logo.mkv" 30
|
||||||
|
probe_row "$LIB/Marnie (1964)/Marnie.1964.1080p.BluRay.x265.mkv" 4800
|
||||||
|
probe_row "$LIB/Marnie (1964)/Featurettes/The Trouble With Marnie.mkv" 1200
|
||||||
|
probe_row "$LIB/Marnie (1964)/Featurettes/Commentary And Archives.mkv" 3000
|
||||||
|
probe_row "$LIB/Marnie (1964)/Exrtas/Marnie - A Closer Look.mkv" 2600
|
||||||
|
probe_row "$LIB/VHS 2 (2013)/VHS.2.2013.1080p.BluRay.x265.mkv" 5700
|
||||||
|
probe_row "$LIB/AMERICAN POP/AMERICAN.POP.2023.1080p.WEB-DL.mkv" 6300
|
||||||
|
probe_row "$LIB/Bruce Lee Collection 1971-1981/.Extra 1.mkv" 5220
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
probe_row "$LIB/Monk 2002 Seasons 1 to 8/Season 1/Monk - S01E$e - Mr Monk and the Rival.mkv" 2600
|
||||||
|
probe_row "$LIB/Big Little Lies Season 1/Big Little Lies S01E$e.mp4" 3300
|
||||||
|
done
|
||||||
|
probe_row "$LIB/The Godfather Part 2 (1974)/The.Godfather.Part.2.1974.1080p.BluRay.mkv" 6200
|
||||||
|
plex_row "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" "Alien" "1979"
|
||||||
|
plex_row "$LIB/Marnie (1964)/Marnie.1964.1080p.BluRay.x265.mkv" "Marnie" "1964"
|
||||||
|
plex_row "$LIB/VHS 2 (2013)/VHS.2.2013.1080p.BluRay.x265.mkv" "V/H/S/2" "2013"
|
||||||
|
# An unmatched Plex row: the leaf title echoes the file and the year is 0.
|
||||||
|
plex_row "$LIB/AMERICAN POP/AMERICAN.POP.2023.1080p.WEB-DL.mkv" "AMERICAN POP" "0"
|
||||||
|
plex_row "$LIB/The Godfather Part 2 (1974)/The.Godfather.Part.2.1974.1080p.BluRay.mkv" \
|
||||||
|
"The Godfather Part 2" "1974"
|
||||||
|
|
||||||
|
export MEDIA_ROOT="$LIB"
|
||||||
|
run python3 "$CENSUS" enumerate --root "$LIB" --out "$ENUM"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
# plan — run the planner over the fixture, keeping its plan TSV, its exception
|
||||||
|
# report and the gate lines it prints (that stdout is the operator's evidence
|
||||||
|
# that the plan accounts for every file, so the tests read it too).
|
||||||
|
plan() {
|
||||||
|
PLANOUT="$BATS_TEST_TMPDIR/plan.out"
|
||||||
|
run python3 "$PLAN" --root "$LIB" --enum "$ENUM" --probed "$PROBED" \
|
||||||
|
--plex "$PLEX" --out "$OUT" --exceptions "$EXC"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
printf '%s\n' "$output" > "$PLANOUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
# dst_of SRC — the destination the plan gives one file.
|
||||||
|
dst_of() {
|
||||||
|
python3 -c '
|
||||||
|
import sys
|
||||||
|
for line in open(sys.argv[1]):
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if len(f) > 2 and f[1] == sys.argv[2]:
|
||||||
|
print(f[2]); break
|
||||||
|
' "$OUT" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# tv_plan — the TV mover over the fixture library; its stdout is the operator's
|
||||||
|
# census of what it called a series and what it refused to touch.
|
||||||
|
tv_plan() {
|
||||||
|
run python3 "$TV" --root "$LIB" --library "$TVLIB" --out "$TVPLAN"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
printf '%s\n' "$output" > "$BATS_TEST_TMPDIR/tv.out"
|
||||||
|
}
|
||||||
|
|
||||||
|
# tv_apply — that plan through the same executor. The destination is a sibling
|
||||||
|
# share, so the plan is applied at the root that holds both libraries.
|
||||||
|
tv_apply() {
|
||||||
|
run python3 "$APPLY" apply --plan "$TVPLAN" --root "$BATS_TEST_TMPDIR" \
|
||||||
|
--journal "$TVJ" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
# tv_dst_of SRC — the destination the TV plan gives one file, empty if none.
|
||||||
|
tv_dst_of() {
|
||||||
|
python3 -c '
|
||||||
|
import sys
|
||||||
|
for line in open(sys.argv[1]):
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if len(f) > 2 and f[1] == sys.argv[2]:
|
||||||
|
print(f[2]); break
|
||||||
|
' "$TVPLAN" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a film lands in Title (year)/Title (year).mkv, named by Plex" {
|
||||||
|
plan
|
||||||
|
[ "$(dst_of "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv")" \
|
||||||
|
= "$LIB/Alien (1979)/Alien (1979).mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a Plex title that carries a slash becomes one folder, not a nested tree" {
|
||||||
|
plan
|
||||||
|
local d; d="$(dst_of "$LIB/VHS 2 (2013)/VHS.2.2013.1080p.BluRay.x265.mkv")"
|
||||||
|
[ "$(dirname "$d")" = "$LIB/V-H-S-2 (2013)" ]
|
||||||
|
[ ! -e "$LIB/V" ]
|
||||||
|
# the folder must be ONE mkdir: an un-sanitized name would make the
|
||||||
|
# executor build Movies/V/H/S/2 (2013) instead
|
||||||
|
run grep -F "mkdir" "$OUT"
|
||||||
|
[[ "$output" == *"$(printf '%s' "$LIB/V-H-S-2 (2013)")"* ]]
|
||||||
|
run awk -F'\t' '$1=="mkdir"{print $3}' "$OUT"
|
||||||
|
[[ "$output" != *"$LIB/V/"* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an unmatched Plex row never names a folder with a zero year" {
|
||||||
|
plan
|
||||||
|
run awk -F'\t' '{print $3}' "$OUT"
|
||||||
|
[[ "$output" != *"(0)"* ]]
|
||||||
|
# the filename's own year carries the folder, the folder and the file share
|
||||||
|
# the name (Jellyfin's rule), and the film sits at the library root rather
|
||||||
|
# than in an unsorted pile
|
||||||
|
local d; d="$(dst_of "$LIB/AMERICAN POP/AMERICAN.POP.2023.1080p.WEB-DL.mkv")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB" ]
|
||||||
|
[[ "$(basename "$(dirname "$d")")" == *" (2023)" ]]
|
||||||
|
[ "$(basename "$d")" = "$(basename "$(dirname "$d")").mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a bonus folder keeps its featurettes inside its own film, not a neighbour's" {
|
||||||
|
plan
|
||||||
|
local d
|
||||||
|
d="$(dst_of "$LIB/Alien (1979)/Featurettes/A Bug And A Bite - Making Alien.mkv")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB/Alien (1979)" ]
|
||||||
|
d="$(dst_of "$LIB/Marnie (1964)/Featurettes/The Trouble With Marnie.mkv")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB/Marnie (1964)" ]
|
||||||
|
d="$(dst_of "$LIB/Alien (1979)/Extras/Extra - Logo.mkv")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB/Alien (1979)" ]
|
||||||
|
# the feature-length extra is bonus too: it sits in a bonus folder and no
|
||||||
|
# metadata matched it, so it may not become a movie of its own
|
||||||
|
d="$(dst_of "$LIB/Marnie (1964)/Featurettes/Commentary And Archives.mkv")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB/Marnie (1964)" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a folder inside a movie folder is bonus even when its name is misspelled" {
|
||||||
|
plan
|
||||||
|
local d; d="$(dst_of "$LIB/Marnie (1964)/Exrtas/Marnie - A Closer Look.mkv")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB/Marnie (1964)" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a subtitle follows the film of the folder it sits in" {
|
||||||
|
plan
|
||||||
|
local d; d="$(dst_of "$LIB/Alien (1979)/subs/3_Eng.srt")"
|
||||||
|
[ "$(dirname "$(dirname "$d")")" = "$LIB/Alien (1979)" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "macOS sidecars are parked in one pile, never inside a movie folder" {
|
||||||
|
plan
|
||||||
|
local d; d="$(dst_of "$LIB/Alien (1979)/._Alien.1979.1080p.BluRay.x265.10bit.mkv")"
|
||||||
|
[[ "$(dirname "$d")" == "$LIB"/_* ]]
|
||||||
|
d="$(dst_of "$LIB/.DS_Store")"
|
||||||
|
[[ "$(dirname "$d")" == "$LIB"/_* ]]
|
||||||
|
grep -q "AppleDouble" "$EXC"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a file whose name is a number becomes a review line, not a folder named 1" {
|
||||||
|
plan
|
||||||
|
run awk -F'\t' '$1=="mkdir"{print $3}' "$OUT"
|
||||||
|
[[ "$output" != "$LIB/1"$'\n'* ]] && [[ "$output" != "$LIB/1" ]]
|
||||||
|
[[ "$output" != *"$LIB/1/"* ]]
|
||||||
|
grep -q "UNNAMED" "$EXC"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a box set in the movie library is quarantined, not filed as movies" {
|
||||||
|
plan
|
||||||
|
local d
|
||||||
|
d="$(dst_of "$LIB/Monk 2002 Seasons 1 to 8/Season 1/Monk - S01E01 - Mr Monk and the Rival.mkv")"
|
||||||
|
[[ "$d" == "$LIB"/_TV\ needs\ a\ TV\ library\ -\ monk* ]]
|
||||||
|
d="$(dst_of "$LIB/Big Little Lies Season 1/Big Little Lies S01E01.mp4")"
|
||||||
|
[[ "$d" == "$LIB"/_TV\ needs\ a\ TV\ library\ -\ big\ little\ lies* ]]
|
||||||
|
# one series is one pile, whatever its episodes are called
|
||||||
|
run python3 -c '
|
||||||
|
import re, sys
|
||||||
|
pile = {}
|
||||||
|
for line in open(sys.argv[1]):
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if len(f) > 2 and f[0] == "move":
|
||||||
|
m = re.search(r"/_TV needs a TV library - ([^/]*)/", f[2] + "/")
|
||||||
|
if m:
|
||||||
|
pile[m.group(1)] = pile.get(m.group(1), 0) + 1
|
||||||
|
for k in sorted(pile):
|
||||||
|
print(f"{k}={pile[k]}")
|
||||||
|
' "$OUT"
|
||||||
|
[ "${#lines[@]}" -eq 2 ]
|
||||||
|
[[ "${lines[0]}" == big\ little\ lies*=5 ]]
|
||||||
|
[[ "${lines[1]}" == monk*=5 ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a film named Part 2 is not called a series" {
|
||||||
|
plan
|
||||||
|
[ "$(dst_of "$LIB/The Godfather Part 2 (1974)/The.Godfather.Part.2.1974.1080p.BluRay.mkv")" \
|
||||||
|
= "$LIB/The Godfather Part 2 (1974)/The Godfather Part 2 (1974).mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the TV mover files a box set into the TV library in Jellyfin shape" {
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
[ -e "$TVLIB/Monk (2002)/Season 01/Monk - S01E01 - Mr Monk and the Rival.mkv" ]
|
||||||
|
[ -e "$TVLIB/Big Little Lies/Season 01/Big Little Lies S01E01.mp4" ]
|
||||||
|
[ ! -e "$LIB/Monk 2002 Seasons 1 to 8/Season 1/Monk - S01E01 - Mr Monk and the Rival.mkv" ]
|
||||||
|
# films are not the TV mover's business
|
||||||
|
[ -e "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the TV mover leaves a film whose name only looks like an episode" {
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
local gf="$LIB/The Godfather Part 2 (1974)/The.Godfather.Part.2.1974.1080p.BluRay.mkv"
|
||||||
|
[ "$(tv_dst_of "$gf")" = "" ]
|
||||||
|
[ -e "$gf" ]
|
||||||
|
grep -q "Godfather.*not a series" "$BATS_TEST_TMPDIR/tv.out"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the TV mover keeps an episode's own subtitle with the episode" {
|
||||||
|
at "$LIB/Monk 2002 Seasons 1 to 8/Season 1/Monk - S01E01 - Mr Monk and the Rival.srt" 40000
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
[ -e "$TVLIB/Monk (2002)/Season 01/Monk - S01E01 - Mr Monk and the Rival.srt" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a second TV move finds nothing left to move" {
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
tv_plan
|
||||||
|
grep -q "0 moves" "$BATS_TEST_TMPDIR/tv.out"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a show whose own name carries the year is not given it twice" {
|
||||||
|
local d="$LIB/Leah Remini Scientology and the Aftermath (2016)/Season 1"
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
at "$d/Leah Remini Scientology and the Aftermath (2016) - S01E$e - Episode.mkv" 3000000
|
||||||
|
done
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
[ -e "$TVLIB/Leah Remini Scientology and the Aftermath (2016)/Season 01/Leah Remini Scientology and the Aftermath (2016) - S01E01 - Episode.mkv" ]
|
||||||
|
[ ! -e "$TVLIB/Leah Remini Scientology and the Aftermath (2016) (2016)" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an all-lowercase series name is written the way the library writes names" {
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
at "$LIB/downton abbey 2010/Season 1/downton abbey - s01e$e - episode title.mkv" 3000000
|
||||||
|
done
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
# the FOLDER is titled; the file keeps the name the library already holds
|
||||||
|
[ -e "$TVLIB/Downton Abbey (2010)/Season 01/downton abbey - s01e01 - episode title.mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the mover leaves an episode the TV library already holds" {
|
||||||
|
local src="$LIB/Monk 2002 Seasons 1 to 8/Season 1/Monk - S01E01 - Mr Monk and the Rival.mkv"
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
at "$TVLIB/Monk (2002)/Season 01/Monk - S01E$e - Mr Monk and the Rival.mkv" 3000000
|
||||||
|
done
|
||||||
|
tv_plan
|
||||||
|
[ -z "$(tv_dst_of "$src")" ]
|
||||||
|
[ -e "$src" ]
|
||||||
|
grep -q "Monk.*already in" "$BATS_TEST_TMPDIR/tv.out"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an episode the TV library holds in another encode is still moved" {
|
||||||
|
for e in 01 02 03 04 05; do
|
||||||
|
at "$TVLIB/Big Little Lies/Season 01/Big Little Lies S01E$e 1080p.mp4" 4000000
|
||||||
|
done
|
||||||
|
tv_plan
|
||||||
|
tv_apply
|
||||||
|
[ -e "$TVLIB/Big Little Lies/Season 01/Big Little Lies S01E01.mp4" ]
|
||||||
|
[ -e "$TVLIB/Big Little Lies/Season 01/Big Little Lies S01E01 1080p.mp4" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "every file in the census is placed or kept — nothing is dropped" {
|
||||||
|
plan
|
||||||
|
python3 - "$ENUM" "$OUT" <<'PY'
|
||||||
|
import sys
|
||||||
|
enum = {l.rstrip("\n").split("\t")[2] for l in open(sys.argv[1])}
|
||||||
|
placed = set()
|
||||||
|
for line in open(sys.argv[2]):
|
||||||
|
f = line.rstrip("\n").split("\t")
|
||||||
|
if f[0] in ("move", "keep"):
|
||||||
|
placed.add(f[1])
|
||||||
|
missing = enum - placed
|
||||||
|
assert not missing, f"{len(missing)} census files with no plan row: {sorted(missing)[:3]}"
|
||||||
|
PY
|
||||||
|
grep -q "left-unmoved=0" "$PLANOUT"
|
||||||
|
grep -q "double-claim=0" "$PLANOUT"
|
||||||
|
grep -q "occupied=0" "$PLANOUT"
|
||||||
|
grep -q "bad-names=0" "$PLANOUT"
|
||||||
|
grep -q "unplanned=0" "$EXC"
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- executor
|
||||||
|
|
||||||
|
# apply_plan — plan, then apply for real; the journal goes in the tmpdir.
|
||||||
|
apply_plan() {
|
||||||
|
JOURNAL="$BATS_TEST_TMPDIR/journal.jsonl"
|
||||||
|
run python3 "$APPLY" apply --plan "$OUT" --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a run without --apply says what it would do and touches nothing" {
|
||||||
|
plan
|
||||||
|
JOURNAL="$BATS_TEST_TMPDIR/journal.jsonl"
|
||||||
|
run python3 "$APPLY" apply --plan "$OUT" --root "$LIB" --journal "$JOURNAL"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == *"--apply"* ]]
|
||||||
|
[ -e "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" ]
|
||||||
|
[ ! -e "$LIB/Alien (1979)/Alien (1979).mkv" ]
|
||||||
|
# a dry run may not even build the folders: an operator must be able to
|
||||||
|
# run it against the live library with no --apply and change nothing
|
||||||
|
[ ! -d "$LIB/V-H-S-2 (2013)" ]
|
||||||
|
[ ! -e "$JOURNAL" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "apply moves every planned file, keeps its bytes, and journals each move" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ -e "$LIB/Alien (1979)/Alien (1979).mkv" ]
|
||||||
|
[ ! -e "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" ]
|
||||||
|
local planned moved
|
||||||
|
planned=$(awk -F'\t' '$1=="move"' "$OUT" | wc -l)
|
||||||
|
moved=$(python3 -c 'import json,sys
|
||||||
|
print(sum(1 for l in open(sys.argv[1]) if json.loads(l).get("op") == "move"))' "$JOURNAL")
|
||||||
|
[ "$moved" -eq "$planned" ]
|
||||||
|
# The media servers' sidecars live in the metadata overlay and did not follow
|
||||||
|
# these moves, so the run names that cleanup at the moment it is needed.
|
||||||
|
[[ "$output" == *"media-view.sh reap"* ]]
|
||||||
|
run python3 "$APPLY" verify --plan "$OUT" --root "$LIB" --journal "$JOURNAL"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a target already holding a different file stops the whole run" {
|
||||||
|
plan
|
||||||
|
JOURNAL="$BATS_TEST_TMPDIR/journal.jsonl"
|
||||||
|
at "$LIB/Alien (1979)/Alien (1979).mkv" 7
|
||||||
|
run python3 "$APPLY" apply --plan "$OUT" --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[[ "$output" == *"occupied"* ]]
|
||||||
|
# nothing moved at all: the other films are still where the census saw them
|
||||||
|
[ -e "$LIB/Marnie (1964)/Marnie.1964.1080p.BluRay.x265.mkv" ]
|
||||||
|
[ ! -e "$JOURNAL" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a plan row whose source has gone stops the run instead of inventing a move" {
|
||||||
|
plan
|
||||||
|
JOURNAL="$BATS_TEST_TMPDIR/journal.jsonl"
|
||||||
|
rm "$LIB/Marnie (1964)/Marnie.1964.1080p.BluRay.x265.mkv"
|
||||||
|
run python3 "$APPLY" apply --plan "$OUT" --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[[ "$output" == *"missing"* ]]
|
||||||
|
[ -e "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a run interrupted between the journal write and the move finishes the job" {
|
||||||
|
plan
|
||||||
|
JOURNAL="$BATS_TEST_TMPDIR/journal.jsonl"
|
||||||
|
# The journal is written before each move, so an interruption leaves two
|
||||||
|
# states and only two: an intent whose move happened, and an intent whose
|
||||||
|
# move never ran. Both are simulated here by hand.
|
||||||
|
python3 - "$OUT" "$JOURNAL" <<'PY'
|
||||||
|
import json, os, sys
|
||||||
|
moves = [r for r in (l.rstrip("\n").split("\t") for l in open(sys.argv[1]))
|
||||||
|
if r[0] == "move"][:3]
|
||||||
|
with open(sys.argv[2], "w") as fh:
|
||||||
|
for i, r in enumerate(moves):
|
||||||
|
fh.write(json.dumps({"op": "move", "src": r[1], "dst": r[2],
|
||||||
|
"bytes": os.path.getsize(r[1])}) + "\n")
|
||||||
|
if i < 2: # the first two moves actually ran
|
||||||
|
os.makedirs(os.path.dirname(r[2]), exist_ok=True)
|
||||||
|
os.rename(r[1], r[2])
|
||||||
|
PY
|
||||||
|
run python3 "$APPLY" apply --plan "$OUT" --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run python3 "$APPLY" verify --plan "$OUT" --root "$LIB" --journal "$JOURNAL"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
python3 - "$OUT" "$JOURNAL" <<'PY'
|
||||||
|
import json, os, sys
|
||||||
|
moves = [r for r in (l.rstrip("\n").split("\t") for l in open(sys.argv[1]))
|
||||||
|
if r[0] == "move"]
|
||||||
|
journal = [json.loads(l) for l in open(sys.argv[2])]
|
||||||
|
seen = [j["dst"] for j in journal]
|
||||||
|
assert len(seen) == len(set(seen)), "a destination was journalled twice"
|
||||||
|
for r in moves:
|
||||||
|
assert os.path.exists(r[2]) and not os.path.exists(r[1]), r[2]
|
||||||
|
PY
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "rollback puts every file back where the census found it" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run python3 "$APPLY" rollback --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ -e "$LIB/Alien (1979)/Alien.1979.1080p.BluRay.x265.10bit.mkv" ]
|
||||||
|
[ -e "$LIB/Marnie (1964)/Marnie.1964.1080p.BluRay.x265.mkv" ]
|
||||||
|
[ -e "$LIB/Alien (1979)/subs/3_Eng.srt" ]
|
||||||
|
[ ! -e "$LIB/Alien (1979)/Alien (1979).mkv" ]
|
||||||
|
# the folders the run created are empty afterwards and must not litter the
|
||||||
|
# library: this one never existed before the plan ran
|
||||||
|
[ -d "$LIB/VHS 2 (2013)" ]
|
||||||
|
[ ! -e "$LIB/V-H-S-2 (2013)" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "rollback refuses when a moved file has gone missing" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
rm "$LIB/Alien (1979)/Alien (1979).mkv"
|
||||||
|
run python3 "$APPLY" rollback --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[[ "$output" == *"missing"* ]]
|
||||||
|
# the refusal is all-or-nothing: another film did not move back
|
||||||
|
[ -e "$LIB/Marnie (1964)/Marnie (1964).mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------- sweep
|
||||||
|
#
|
||||||
|
# A relayout leaves the release folders it emptied standing at the library root:
|
||||||
|
# 1675 of them after the live run, each holding nothing but empty subfolders. The
|
||||||
|
# sweep pass removes only folders THIS RUN emptied — proven from the journal,
|
||||||
|
# never from "it looks empty" — so a folder someone made by hand is untouchable,
|
||||||
|
# and every removal rides the same rollback as the moves.
|
||||||
|
|
||||||
|
@test "sweep removes only the folders this run emptied" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
mkdir -p "$LIB/A Empty Placeholder" # someone's folder, never a move source
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == *"swept"* ]]
|
||||||
|
[ ! -e "$LIB/AMERICAN POP" ]
|
||||||
|
[ ! -e "$LIB/VHS 2 (2013)" ]
|
||||||
|
[ ! -e "$LIB/Alien (1979)/Featurettes" ]
|
||||||
|
[ ! -e "$LIB/Alien (1979)/subs" ]
|
||||||
|
[ ! -e "$LIB/Marnie (1964)/Exrtas" ]
|
||||||
|
# a folder the run never moved a file out of survives, empty or not
|
||||||
|
[ -d "$LIB/A Empty Placeholder" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a swept folder carries the empty junk folders inside it" {
|
||||||
|
mkdir -p "$LIB/AMERICAN POP/Screens" # screenshots, never a move source
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == *"0 left in place"* ]]
|
||||||
|
[ ! -e "$LIB/AMERICAN POP/Screens" ]
|
||||||
|
[ ! -e "$LIB/AMERICAN POP" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a sweep whose rmdir failed tries those folders again" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
mkdir -p "$LIB/AMERICAN POP/Screens"
|
||||||
|
chmod 500 "$LIB/AMERICAN POP" # readable, but nothing can be removed
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == *"left in place"* ]]
|
||||||
|
[ -e "$LIB/AMERICAN POP/Screens" ]
|
||||||
|
chmod 700 "$LIB/AMERICAN POP" # the operator cleared the blocker
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" != *"left in place: "* ]]
|
||||||
|
[ ! -e "$LIB/AMERICAN POP" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "sweep without --apply reports the husks and changes nothing" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
local before jlines
|
||||||
|
before=$(find "$LIB" -type d | wc -l)
|
||||||
|
jlines=$(wc -l < "$JOURNAL")
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == *"--apply"* ]]
|
||||||
|
[ "$(find "$LIB" -type d | wc -l)" -eq "$before" ]
|
||||||
|
[ "$(wc -l < "$JOURNAL")" -eq "$jlines" ]
|
||||||
|
[ -e "$LIB/AMERICAN POP" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a folder that still holds content is never swept" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
# the film folder, the folder the run created, and the piles all stay
|
||||||
|
[ -d "$LIB/Marnie (1964)" ]
|
||||||
|
[ -e "$LIB/Marnie (1964)/Marnie (1964).mkv" ]
|
||||||
|
[ -d "$LIB/V-H-S-2 (2013)" ]
|
||||||
|
[ -d "$LIB/_unsorted bonus/extras" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "rollback after a sweep brings the folders and the files back" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ ! -e "$LIB/AMERICAN POP" ]
|
||||||
|
run python3 "$APPLY" rollback --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ -e "$LIB/AMERICAN POP/AMERICAN.POP.2023.1080p.WEB-DL.mkv" ]
|
||||||
|
[ -e "$LIB/VHS 2 (2013)/VHS.2.2013.1080p.BluRay.x265.mkv" ]
|
||||||
|
[ -e "$LIB/Alien (1979)/Featurettes/A Bug And A Bite - Making Alien.mkv" ]
|
||||||
|
[ -e "$LIB/Alien (1979)/subs/3_Eng.srt" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a second sweep has nothing left to do and says so" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
local before
|
||||||
|
before=$(find "$LIB" -type d | wc -l)
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[[ "$output" == *"swept: 0"* ]]
|
||||||
|
[ "$(find "$LIB" -type d | wc -l)" -eq "$before" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "sweep never follows a symlink and never removes the folder it guards" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
mkdir -p "$LIB/Elsewhere"
|
||||||
|
at "$LIB/Elsewhere/keep.txt" 10
|
||||||
|
ln -s "$LIB/Elsewhere" "$LIB/AMERICAN POP/pointer"
|
||||||
|
run python3 "$APPLY" sweep --root "$LIB" --journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
# a link is content, not nothing: the husk it sits in survives untouched
|
||||||
|
[ -d "$LIB/AMERICAN POP" ]
|
||||||
|
[ -L "$LIB/AMERICAN POP/pointer" ]
|
||||||
|
[ -e "$LIB/Elsewhere/keep.txt" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "sweep refuses a journal that belongs to another library" {
|
||||||
|
plan
|
||||||
|
apply_plan
|
||||||
|
mkdir -p "$BATS_TEST_TMPDIR/Other"
|
||||||
|
run python3 "$APPLY" sweep --root "$BATS_TEST_TMPDIR/Other" \
|
||||||
|
--journal "$JOURNAL" --apply
|
||||||
|
[ "$status" -ne 0 ]
|
||||||
|
[[ "$output" == *"outside"* ]]
|
||||||
|
[ -d "$LIB/AMERICAN POP" ]
|
||||||
|
}
|
||||||
Executable
+307
@@ -0,0 +1,307 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
# Seam: the TV-library relayout — scripts/media-census.py jellyfin (the metadata
|
||||||
|
# authority export) and scripts/media-shows.py (the plan).
|
||||||
|
#
|
||||||
|
# The TV library is 52k files written in five different release layouts, and the
|
||||||
|
# one thing that makes a rename of that size survivable is that the plan is
|
||||||
|
# derived from what a metadata provider already matched, not from a guess at the
|
||||||
|
# file name. So every test here states which authority a file had — name only,
|
||||||
|
# provider only, both, neither — and asserts where the PLAN files it, never how
|
||||||
|
# the planner decided. A fixture Jellyfin database stands in for the live one and
|
||||||
|
# the TMDb cache is an input file, so no test touches the network.
|
||||||
|
#
|
||||||
|
# The two outcomes that cost a re-run of a 50k-file plan are a file filed as an
|
||||||
|
# episode that is not one (it steals an episode number from its real owner) and a
|
||||||
|
# move that lands on an existing file. Everything below is written to catch
|
||||||
|
# exactly those, plus the gate that matters most: the plan must be replayable by
|
||||||
|
# scripts/media-apply.py, whose preflight is the fleet's only overwrite guard.
|
||||||
|
|
||||||
|
load helpers
|
||||||
|
|
||||||
|
PLAN="$REPO_ROOT/scripts/media-shows.py"
|
||||||
|
CENSUS="$REPO_ROOT/scripts/media-census.py"
|
||||||
|
APPLY="$REPO_ROOT/scripts/media-apply.py"
|
||||||
|
|
||||||
|
# at PATH SIZE — a file with deterministic content.
|
||||||
|
at() { mkdir -p "$(dirname "$1")"; head -c "$2" /dev/zero > "$1"; }
|
||||||
|
|
||||||
|
# title_row FILE SERIES YEAR SEASON EPISODE [EXTRA] — one row of the authority
|
||||||
|
# export scripts/media-census.py jellyfin writes.
|
||||||
|
title_row() {
|
||||||
|
printf '%s\t%s\t%s\t%s\t%s\t%s\t\n' "$1" "$2" "$3" "${4:-}" "${5:-}" "${6:-}" \
|
||||||
|
>> "$TITLES"
|
||||||
|
}
|
||||||
|
|
||||||
|
# dst SRC — the destination the plan gives SRC, or nothing when it does not move.
|
||||||
|
dst() {
|
||||||
|
awk -F '\t' -v s="$1" '$1 == "move" && $2 == s {print $3}' "$OUT"
|
||||||
|
}
|
||||||
|
|
||||||
|
setup() {
|
||||||
|
LIB="$BATS_TEST_TMPDIR/TVShows"
|
||||||
|
PARK="$LIB/_unsorted"
|
||||||
|
MOVIES="$BATS_TEST_TMPDIR/Movies"
|
||||||
|
ENUM="$BATS_TEST_TMPDIR/enum.tsv"
|
||||||
|
PROBED="$BATS_TEST_TMPDIR/probed.tsv"
|
||||||
|
TITLES="$BATS_TEST_TMPDIR/titles.tsv"
|
||||||
|
CACHE="$BATS_TEST_TMPDIR/tmdb.json"
|
||||||
|
OUT="$BATS_TEST_TMPDIR/plan.tsv"
|
||||||
|
EXC="$BATS_TEST_TMPDIR/exceptions.txt"
|
||||||
|
: > "$PROBED"
|
||||||
|
|
||||||
|
SEASON="$LIB/The Examples (2010) Season 1 S01 (1080p BluRay x265 Silence)"
|
||||||
|
# An episode its own name states, with a subtitle, a season image and a
|
||||||
|
# trailer: four things that have to end up in four different places.
|
||||||
|
at "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.mkv" 9000
|
||||||
|
at "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.en.srt" 200
|
||||||
|
at "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.mkv.forced.srt" 250
|
||||||
|
# A scene pack buries its subtitles two levels down, in Subs/<release-name>/.
|
||||||
|
at "$SEASON/Subs/The.Examples.S01E01.Pilot.1080p.x265/The.Examples.S01E01.Pilot.1080p.x265.jpn.srt" 220
|
||||||
|
at "$SEASON/folder.jpg" 300
|
||||||
|
at "$SEASON/Trailers/teaser.mkv" 1000
|
||||||
|
at "$SEASON/tvshow.nfo" 400
|
||||||
|
# An anime pack: no episode marker anywhere in its name, numbered by the
|
||||||
|
# provider only. Its year is the one thing no authority states.
|
||||||
|
ANIME="$LIB/Anime Pack [grp] (1080p HEVC)"
|
||||||
|
at "$ANIME/[grp] Anime Pack - 05.mkv" 5000
|
||||||
|
# Two releases claiming one episode, and a file holding two episodes.
|
||||||
|
at "$LIB/Dupes/Show Two S01E01.mkv" 4000
|
||||||
|
at "$LIB/Dupes/Show.Two.S01E01.720p.other.mkv" 7000
|
||||||
|
at "$LIB/Dupes/Show.Two.S01E02-E03.720p.mkv" 7000
|
||||||
|
# A file no authority numbers, and a long file the movie provider knows.
|
||||||
|
at "$LIB/Orphan Tapes/clip001.mp4" 3000
|
||||||
|
at "$LIB/Longform/Some Documentary Special (2015).mkv" 8000
|
||||||
|
# A bonus folder inside a release pack whose own name is not the show's. The
|
||||||
|
# pack's rows name the show; the bonus folder may never invent a series.
|
||||||
|
at "$ANIME/01. Extras/NCOP 01.mkv" 1200
|
||||||
|
# A season folder whose rows are stale: the database numbers every file of this
|
||||||
|
# pack season 16 episode 20, four seasons' worth of them.
|
||||||
|
at "$LIB/SNL Pack/Season 19/01 Charles Barkley, Nirvana.avi" 6000
|
||||||
|
# One episode-shaped file loose at the library root.
|
||||||
|
at "$LIB/Loose.Show.S02E03.720p.WEB-DL.mkv" 4000
|
||||||
|
|
||||||
|
# The enum census lists every file; the planner reads that instead of walking
|
||||||
|
# the tree, so the fixture writes the same three columns.
|
||||||
|
python3 - "$LIB" "$ENUM" <<'PY'
|
||||||
|
import os, sys
|
||||||
|
root, out = sys.argv[1], sys.argv[2]
|
||||||
|
with open(out, "w") as fh:
|
||||||
|
for d, _, fs in os.walk(root):
|
||||||
|
for f in fs:
|
||||||
|
p = os.path.join(d, f)
|
||||||
|
fh.write(f"{os.stat(p).st_size}\t{os.path.splitext(f)[1]}\t{p}\n")
|
||||||
|
PY
|
||||||
|
|
||||||
|
printf 'file\tseries\tyear\tseason\tepisode\textra\ttmdb\n' > "$TITLES"
|
||||||
|
printf '{}\n' > "$CACHE"
|
||||||
|
title_row "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.mkv" \
|
||||||
|
"The Examples" 2010 1 1
|
||||||
|
title_row "$ANIME/[grp] Anime Pack - 05.mkv" "Anime Pack" "" 1 5
|
||||||
|
title_row "$LIB/Dupes/Show Two S01E01.mkv" "Show Two" 2016 1 1
|
||||||
|
title_row "$LIB/Dupes/Show.Two.S01E01.720p.other.mkv" "Show Two" 2016 1 1
|
||||||
|
title_row "$LIB/Dupes/Show.Two.S01E02-E03.720p.mkv" "Show Two" 2016 1 2
|
||||||
|
title_row "$LIB/Loose.Show.S02E03.720p.WEB-DL.mkv" "Loose Show" 2019 2 3
|
||||||
|
title_row "$LIB/SNL Pack/Season 19/01 Charles Barkley, Nirvana.avi" \
|
||||||
|
"SNL Pack" "" 16 20
|
||||||
|
}
|
||||||
|
|
||||||
|
plan() {
|
||||||
|
run python3 "$PLAN" --root "$LIB" --enum "$ENUM" --titles "$TITLES" \
|
||||||
|
--probed "$PROBED" --tmdb-cache "$CACHE" --movies "$MOVIES" \
|
||||||
|
--out "$OUT" --exceptions "$EXC"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the census exports the provider's episode numbering as a TSV" {
|
||||||
|
DB="$BATS_TEST_TMPDIR/jellyfin.db"
|
||||||
|
python3 - "$DB" "$SEASON" <<'PY'
|
||||||
|
import os, sqlite3, sys, uuid
|
||||||
|
db, season = sys.argv[1], sys.argv[2]
|
||||||
|
c = sqlite3.connect(db)
|
||||||
|
pre = "/data/TVShows/" + os.path.basename(season)
|
||||||
|
c.execute("""create table BaseItems (Id, ParentId, Type, Name, Path, IndexNumber,
|
||||||
|
ParentIndexNumber, ProductionYear, ExtraType,
|
||||||
|
IsVirtualItem, Data)""")
|
||||||
|
sid = str(uuid.uuid4())
|
||||||
|
c.execute("insert into BaseItems values (?,?,?,?,?,?,?,?,?,?,?)",
|
||||||
|
(sid, None, "MediaBrowser.Controller.Entities.TV.Series", "The Examples",
|
||||||
|
pre, None, None, 2010, None, 0,
|
||||||
|
'{"ProviderIds":{"Tmdb":"1234"}}'))
|
||||||
|
c.execute("insert into BaseItems values (?,?,?,?,?,?,?,?,?,?,?)",
|
||||||
|
(str(uuid.uuid4()), sid, "MediaBrowser.Controller.Entities.TV.Episode",
|
||||||
|
"Pilot", pre + "/The.Examples.S01E01.Pilot.1080p.x265.mkv", 1, 1,
|
||||||
|
None, None, 0, '{}'))
|
||||||
|
c.commit()
|
||||||
|
PY
|
||||||
|
run python3 "$CENSUS" jellyfin --root "$LIB" --db "$DB" \
|
||||||
|
--out "$BATS_TEST_TMPDIR/exp.tsv"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run head -1 "$BATS_TEST_TMPDIR/exp.tsv"
|
||||||
|
[ "${lines[0]}" = "file series year season episode extra tmdb" ]
|
||||||
|
tail -1 "$BATS_TEST_TMPDIR/exp.tsv" \
|
||||||
|
| grep -q "^$SEASON/The\.Examples\.S01E01\.Pilot\.1080p\.x265\.mkv The Examples 2010 1 1 1234$"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "an episode its own name states files as Show SxxEyy under Season NN" {
|
||||||
|
plan
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
[ "$(dst "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.mkv")" \
|
||||||
|
= "$LIB/The Examples (2010)/Season 01/The Examples S01E01.mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a subtitle follows its episode and keeps its language flag" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.en.srt")" \
|
||||||
|
= "$LIB/The Examples (2010)/Season 01/The Examples S01E01.en.srt" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a subtitle two folders down inside Subs/<release-name>/ still follows its episode" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$SEASON/Subs/The.Examples.S01E01.Pilot.1080p.x265/The.Examples.S01E01.Pilot.1080p.x265.jpn.srt")" \
|
||||||
|
= "$LIB/The Examples (2010)/Season 01/The Examples S01E01.jpn.srt" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a subtitle that carries the container's word in its own name still follows the episode" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$SEASON/The.Examples.S01E01.Pilot.1080p.x265.mkv.forced.srt")" \
|
||||||
|
= "$LIB/The Examples (2010)/Season 01/The Examples S01E01.forced.srt" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a season image keeps its own name; a series nfo keeps the series folder" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$SEASON/folder.jpg")" = "$LIB/The Examples (2010)/Season 01/folder.jpg" ]
|
||||||
|
[ "$(dst "$SEASON/tvshow.nfo")" = "$LIB/The Examples (2010)/tvshow.nfo" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "bonus material files as an extra, never as an episode" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$SEASON/Trailers/teaser.mkv")" = "$LIB/The Examples (2010)/trailers/teaser.mkv" ]
|
||||||
|
[ -z "$(awk -F '\t' '$3 ~ /Season .*teaser/ {print $3}' "$OUT")" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a file only the provider numbers files at the episode it is matched to" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$ANIME/[grp] Anime Pack - 05.mkv")" \
|
||||||
|
= "$LIB/Anime Pack/Season 01/Anime Pack S01E05.mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a bonus folder inside a release pack belongs to the pack's show, not to its own name" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$ANIME/01. Extras/NCOP 01.mkv")" = "$LIB/Anime Pack/extras/NCOP 01.mkv" ]
|
||||||
|
[ -z "$(awk -F '\t' '$3 ~ /\/01\. Extras\// {print $3}' "$OUT")" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a Season folder outvotes the stale row its files carry" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$LIB/SNL Pack/Season 19/01 Charles Barkley, Nirvana.avi")" \
|
||||||
|
= "$LIB/SNL Pack/Season 19/SNL Pack S19E01.avi" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@test "one file holding two episodes keeps both numbers in its name" {
|
||||||
|
plan
|
||||||
|
[ "$(dst "$LIB/Dupes/Show.Two.S01E02-E03.720p.mkv")" \
|
||||||
|
= "$LIB/Show Two (2016)/Season 01/Show Two S01E02-E03.mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "two files claiming one episode: one keeps the name, the other parks" {
|
||||||
|
plan
|
||||||
|
n=$(grep -c " $LIB/Show Two (2016)/Season 01/Show Two S01E01.mkv " "$OUT")
|
||||||
|
[ "$n" -eq 1 ]
|
||||||
|
run dst "$LIB/Dupes/Show Two S01E01.mkv"
|
||||||
|
[[ "$output" == "$PARK/"* ]]
|
||||||
|
run dst "$LIB/Dupes/Show.Two.S01E01.720p.other.mkv"
|
||||||
|
[[ "$output" == "$LIB/Show Two (2016)/Season 01/"* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a file no authority numbers is parked, never renamed into the library" {
|
||||||
|
plan
|
||||||
|
run dst "$LIB/Orphan Tapes/clip001.mp4"
|
||||||
|
[[ "$output" == "$PARK/Orphan Tapes/clip001.mp4" ]]
|
||||||
|
grep -q "clip001.mp4" "$EXC"
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a long file the movie provider knows leaves the TV library for the movie one" {
|
||||||
|
printf '8000\t%s\t7200\thevc\t1920x1080\t23.976\t4000000\tyuv420p10le\taac:eng\t0\t0\tmatroska,webm\n' \
|
||||||
|
"$LIB/Longform/Some Documentary Special (2015).mkv" > "$PROBED"
|
||||||
|
python3 -c 'import json, sys
|
||||||
|
json.dump({"movie:some documentary":
|
||||||
|
{"name": "Some Documentary Special", "year": 2015, "id": 55}},
|
||||||
|
open(sys.argv[1], "w"))' "$CACHE"
|
||||||
|
plan
|
||||||
|
[ "$(dst "$LIB/Longform/Some Documentary Special (2015).mkv")" \
|
||||||
|
= "$MOVIES/Some Documentary Special (2015)/Some Documentary Special (2015).mkv" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "a long file no movie provider knows is parked, not moved into Movies" {
|
||||||
|
printf '8000\t%s\t7200\thevc\t1920x1080\t23.976\t4000000\tyuv420p10le\taac:eng\t0\t0\tmatroska,webm\n' \
|
||||||
|
"$LIB/Longform/Some Documentary Special (2015).mkv" > "$PROBED"
|
||||||
|
plan
|
||||||
|
run dst "$LIB/Longform/Some Documentary Special (2015).mkv"
|
||||||
|
[[ "$output" == "$PARK/"* ]]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the plan creates every folder before the move that needs it" {
|
||||||
|
plan
|
||||||
|
python3 - "$OUT" "$LIB" "$MOVIES" "$PARK" <<'PY'
|
||||||
|
import os, sys
|
||||||
|
plan, known = sys.argv[1], set(sys.argv[2:])
|
||||||
|
made, bad = set(), []
|
||||||
|
for line in open(plan).read().splitlines()[1:]:
|
||||||
|
action, _src, dest, _kind = line.split("\t")
|
||||||
|
if action == "mkdir":
|
||||||
|
made.add(dest)
|
||||||
|
elif action == "move":
|
||||||
|
d = os.path.dirname(dest)
|
||||||
|
while d not in known and d != os.path.dirname(d):
|
||||||
|
if d not in made:
|
||||||
|
bad.append(f"{dest}: {d} was never made")
|
||||||
|
d = os.path.dirname(d)
|
||||||
|
print("\n".join(bad[:5]))
|
||||||
|
sys.exit(1 if bad else 0)
|
||||||
|
PY
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "no move lands outside the library or the movie library" {
|
||||||
|
plan
|
||||||
|
run awk -F '\t' -v l="$LIB" -v m="$MOVIES" \
|
||||||
|
'$1 == "move" && $3 !~ "^" l "/" && $3 !~ "^" m "/" {print $3}' "$OUT"
|
||||||
|
[ -z "$output" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "the executor preflights the plan and no target is claimed twice" {
|
||||||
|
plan
|
||||||
|
run python3 "$APPLY" apply --plan "$OUT" --root "$BATS_TEST_TMPDIR" \
|
||||||
|
--journal "$BATS_TEST_TMPDIR/journal.jsonl"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
dup=$(awk -F '\t' '$1 == "move" {print $3}' "$OUT" | sort | uniq -d)
|
||||||
|
[ -z "$dup" ]
|
||||||
|
}
|
||||||
|
|
||||||
|
@test "re-planning a library the plan already satisfies moves nothing" {
|
||||||
|
plan
|
||||||
|
# Applying the plan and re-censusing is exactly what the operator does next:
|
||||||
|
# every authority file is keyed by path, so all three are replayed through the
|
||||||
|
# plan the way a fresh census would see them. (The enum has no header; the
|
||||||
|
# titles export does, and its header row is not a path so it survives.)
|
||||||
|
python3 - "$OUT" "$ENUM" "$TITLES" "$PROBED" <<'PY'
|
||||||
|
import os, sys
|
||||||
|
plan = sys.argv[1]
|
||||||
|
moves = {}
|
||||||
|
for line in open(plan).read().splitlines()[1:]:
|
||||||
|
action, src, dest, _kind = line.split("\t")
|
||||||
|
if action == "move":
|
||||||
|
moves[src] = dest
|
||||||
|
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
||||||
|
os.rename(src, dest)
|
||||||
|
for path, col in ((sys.argv[2], 2), (sys.argv[3], 0), (sys.argv[4], 1)):
|
||||||
|
if not os.path.getsize(path):
|
||||||
|
continue
|
||||||
|
rows = [line.rstrip("\n").split("\t") for line in open(path)]
|
||||||
|
with open(path, "w") as fh:
|
||||||
|
for f in rows:
|
||||||
|
f[col] = moves.get(f[col], f[col])
|
||||||
|
fh.write("\t".join(f) + "\n")
|
||||||
|
PY
|
||||||
|
plan
|
||||||
|
[ -z "$(awk -F '\t' '$1 == "move" {print $2}' "$OUT")" ]
|
||||||
|
}
|
||||||
Reference in new issue
Block a user