| … | … | @@ -0,0 +1,135 @@ |
| 1 | #!/usr/bin/env python3 |
| 2 | """Fetch subtitles for library titles this addon did not ingest. |
| 3 | |
| 4 | `_run_ingest` fetches subtitles for anything it copies, including the |
| 5 | already-in-library path -- but its scope is the INGEST QUEUE, and the queue is |
| 6 | ~20 titles against a library of 226. The rest arrived by other means and has |
| 7 | none: measured 2026-09-05, exactly ONE item in the whole library had an external |
| 8 | subtitle, and that was the one fetched an hour earlier. |
| 9 | |
| 10 | A script rather than a route, for one reason: the ids. The library naming |
| 11 | convention carries `[tmdbid-...]` for only 6 of 81 movie folders, so the id has |
| 12 | to come from Jellyfin, which has already identified this library and holds |
| 13 | Tmdb/Imdb for 66 movies and every episode's series. Teaching the daemon to |
| 14 | authenticate against Jellyfin -- for a job that is essentially one-time -- would |
| 15 | be a permanent dependency bought for a temporary need. New content does not need |
| 16 | this: it comes in through _run_ingest, which already fetches subtitles. |
| 17 | |
| 18 | It imports addon rather than reimplementing anything. _wyzie_tracks and |
| 19 | _write_sidecars are the tested path; a second copy of that logic here would be |
| 20 | the thing that drifts. |
| 21 | |
| 22 | Idempotent and resumable: _write_sidecars skips any sidecar already on disk, so |
| 23 | a re-run costs one lookup per title and no downloads. |
| 24 | |
| 25 | The work list is built separately (Jellyfin has the ids, and the token lives on |
| 26 | the Pi) and passed in as JSON: |
| 27 | |
| 28 | [{"kind": "movie"|"series", "name": ..., "path": "/mnt/usb/...", |
| 29 | "ident": "639988", "season": 0, "episode": 0}] |
| 30 | |
| 31 | Usage: |
| 32 | .venv/bin/python3 backfill_subtitles.py work.json [--dry-run] [--limit N] |
| 33 | """ |
| 34 | |
| 35 | import argparse |
| 36 | import json |
| 37 | import os |
| 38 | import sys |
| 39 | import time |
| 40 | |
| 41 | import addon |
| 42 | |
| 43 | # Politeness. The per-title fetch already runs 8 downloads in parallel; this is |
| 44 | # the gap BETWEEN titles, so a 225-title run does not arrive as one burst on a |
| 45 | # service doing us a favour for free. |
| 46 | PAUSE_BETWEEN_TITLES = 0.5 |
| 47 | |
| 48 | |
| 49 | def load_work(path): |
| 50 | with open(path) as f: |
| 51 | work = json.load(f) |
| 52 | if not isinstance(work, list): |
| 53 | raise SystemExit("work list must be a JSON array") |
| 54 | out = [] |
| 55 | for w in work: |
| 56 | if not w.get("path") or not w.get("ident"): |
| 57 | continue |
| 58 | out.append(w) |
| 59 | return out |
| 60 | |
| 61 | |
| 62 | def needs_work(item): |
| 63 | """True if the media file exists and has no sidecar beside it yet. |
| 64 | |
| 65 | Checked on DISK rather than trusting the work list: the list is a snapshot, |
| 66 | and a re-run should see what actually happened. |
| 67 | """ |
| 68 | path = item["path"] |
| 69 | if not os.path.exists(path): |
| 70 | return False |
| 71 | stem = os.path.splitext(os.path.basename(path))[0] |
| 72 | folder = os.path.dirname(path) |
| 73 | try: |
| 74 | siblings = os.listdir(folder) |
| 75 | except OSError: |
| 76 | return False |
| 77 | return not any(f.startswith(stem + ".") and f.endswith(".srt") for f in siblings) |
| 78 | |
| 79 | |
| 80 | def main(): |
| 81 | ap = argparse.ArgumentParser() |
| 82 | ap.add_argument("work") |
| 83 | ap.add_argument("--dry-run", action="store_true", |
| 84 | help="look up tracks and report, write nothing") |
| 85 | ap.add_argument("--limit", type=int, default=0) |
| 86 | args = ap.parse_args() |
| 87 | |
| 88 | work = load_work(args.work) |
| 89 | todo = [w for w in work if needs_work(w)] |
| 90 | skipped = len(work) - len(todo) |
| 91 | if args.limit: |
| 92 | todo = todo[:args.limit] |
| 93 | |
| 94 | print("work list: %d already done or missing: %d to process: %d" |
| 95 | % (len(work), skipped, len(todo))) |
| 96 | if args.dry_run: |
| 97 | print("DRY RUN -- nothing will be written") |
| 98 | |
| 99 | started = time.time() |
| 100 | hits = misses = failures = written = 0 |
| 101 | for i, item in enumerate(todo, 1): |
| 102 | name = item.get("name") or os.path.basename(item["path"]) |
| 103 | try: |
| 104 | tracks = addon._wyzie_tracks(item["ident"], |
| 105 | season=item.get("season") or 0, |
| 106 | episode=item.get("episode") or 0) |
| 107 | except Exception as exc: |
| 108 | failures += 1 |
| 109 | print(" [%3d/%d] %-52s LOOKUP FAILED: %s" % (i, len(todo), name[:52], exc)) |
| 110 | continue |
| 111 | |
| 112 | if not tracks: |
| 113 | misses += 1 |
| 114 | print(" [%3d/%d] %-52s no subtitles" % (i, len(todo), name[:52])) |
| 115 | time.sleep(PAUSE_BETWEEN_TITLES) |
| 116 | continue |
| 117 | |
| 118 | hits += 1 |
| 119 | if args.dry_run: |
| 120 | print(" [%3d/%d] %-52s %2d tracks (dry run)" % (i, len(todo), name[:52], len(tracks))) |
| 121 | else: |
| 122 | paths = addon._write_sidecars(item["path"], tracks) |
| 123 | written += len(paths) |
| 124 | print(" [%3d/%d] %-52s %2d tracks -> %2d files" |
| 125 | % (i, len(todo), name[:52], len(tracks), len(paths))) |
| 126 | time.sleep(PAUSE_BETWEEN_TITLES) |
| 127 | |
| 128 | print() |
| 129 | print("done in %.0fs: %d with subtitles, %d without, %d lookup failures, %d files written" |
| 130 | % (time.time() - started, hits, misses, failures, written)) |
| 131 | return 0 if failures == 0 else 1 |
| 132 | |
| 133 | |
| 134 | if __name__ == "__main__": |
| 135 | sys.exit(main()) |