Files

4.5 KiB

media-preload

Warm the VPS media cache before someone watches something.

webdav.godemperordoge.org is served by the shared VPS, which keeps a 512 GB rclone cache of the Movies and TV folders. The first play of a file streams over the home uplink. Once cached, it streams from the VPS. This service lets a client, such as the planned Discord bot, say "I will watch X", so X is cached before anyone presses play.

graph LR
  bot[Discord bot or any client] -->|"search, request (client token)"| api[preload-api on yveltal]
  api -->|"items, episodes, next up"| jf[Jellyfin]
  agent[preload-agent on the VPS] -->|"claim and report (agent token, over wg-home)"| api
  agent -->|"GET whole file"| rc["rclone serve, read-only, 127.0.0.1:18124"]
  rc -->|"cache miss: fetch over tunnel"| origin[rclone-webdav-vpscache on yveltal]
  • preload-api (cmd/preload-api) runs at home next to Jellyfin. It resolves a movie, a season, chosen episodes or "the next N unwatched episodes for user U" to file paths, and queues them in SQLite.
  • preload-agent (cmd/preload-agent) runs on the VPS as the rclone-media user. It polls the API, skips files already in the cache (it compares allocated blocks, since cache files are sparse), reads the rest at a capped rate, and reports back. No inbound port is opened on the VPS: the agent dials home over the tunnel.
  • Only /media/Videos/Movies/ (which includes Arabic Shows), /media2/Videos/tvshows/ and /media2/Videos/mxtvshows/ are cacheable. Anything else comes back as not_cacheable.

API

The contract is api/openapi.yaml (OpenAPI 3.0). A running server also serves it at GET /openapi.json. Generate a client for any language from it. For example, a Python bot:

uvx openapi-python-client generate --path api/openapi.yaml

Or a TypeScript or Go client with the OpenAPI generator or oapi-codegen. The Go server, client and models in internal/api/api.gen.go are generated by mise run generate; never edit that file by hand.

Typical bot flow:

API=http://127.0.0.1:8125; TOKEN=...   # PRELOAD_BOT_TOKEN in ziadpve ~/secrets.txt
curl -H "Authorization: Bearer $TOKEN" "$API/v1/search?q=dark%20matter&type=series"
curl -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -X POST "$API/v1/requests" \
     -d '{"itemId":"<id from search>","season":2,"requestedBy":"discord:doge"}'
curl -H "Authorization: Bearer $TOKEN" "$API/v1/requests/1"     # per-file progress

A series request takes either season (optionally with episodes) or next + jellyfinUser. It defaults to the next 3 unwatched episodes. Limits: 40 files and 150 GB per request (MAX_FILES, MAX_GB).

Auth is Authorization: Bearer <token>. CLIENT_TOKENS may use search and requests, and AGENT_TOKENS may only use /v1/agent/*. /v1/health and /openapi.json are open.

Development

Tooling is managed by mise (mise.toml: Go 1.27, oapi-codegen 2.8.2, CGO_ENABLED=0).

mise install          # toolchain
mise run generate     # regenerate internal/api from the spec
mise run build        # bin/preload-api, bin/preload-agent
mise run test

Deployment (as run on 2026-11-05)

Piece Where Config
preload-api yveltal, quadlet deploy/media-preload-api.container, image built from Containerfile /etc/media-preload/api.env (600): JELLYFIN_URL, JELLYFIN_API_KEY, CLIENT_TOKENS, AGENT_TOKENS; data in volume media-preload-data
listen 127.0.0.1:8125, 10.0.0.38:8125 (LAN), 10.77.0.2:8125 (tunnel) yveltal's wg_vps_guard allows 8125 from 10.77.0.1 only
preload-agent VPS /usr/local/bin/preload-agent, unit deploy/preload-agent.service /etc/preload-agent.env (root:rclone-media 640): PRELOAD_API_URL=http://10.77.0.2:8125, PRELOAD_AGENT_TOKEN

Clients in use: the dogebot Discord cog watch (d!watch, repo github RustyRaptor/doge_cogs, generated client in clients/media-preload-client). Rootless containers on yveltal must use http://host.containers.internal:8125, because 10.0.0.38 inside a rootless (pasta) network is the container itself. After an API change, regenerate that client.

The on-demand service accepts both legacy roots and /arrmedia/Movies, /arrmedia/TV, and /arrmedia/TV-Spanish. Dogebot acquisition completion reports an ARR import. Use d!watch after Jellyfin indexes the files to queue a VPS preload.

The nightly preload (edge-preload.timer on ziadpve, ~/handoffs/homelab/vps-edge/preload.py) is separate and still works on its own. It could later file its picks through this API instead.