128 lines
7.7 KiB
Markdown
128 lines
7.7 KiB
Markdown
# Telegram sticker importer
|
|
|
|
Server-side CLI. No deployment, Telegram messages or player interaction.
|
|
Only `getStickerSet`, `getFile` and file downloads are used. Real Telegram
|
|
credential verification is pending; tests use an independent HTTP fixture
|
|
and generated WEBP, vector TGS and alpha VP9 WEBM.
|
|
|
|
From repository root:
|
|
|
|
```sh
|
|
mkdir -p /tmp/wowmine-stickers-importer
|
|
nix build --impure --expr \
|
|
'import ./script/wowmine-stickers/importer/default.nix {}' \
|
|
--out-link /tmp/wowmine-stickers-importer/result
|
|
/tmp/wowmine-stickers-importer/result/bin/wowmine-stickers-import \
|
|
--token-file /operator/provided/bot-token \
|
|
--pack https://t.me/addstickers/PackShortName --output /srv/sticker-cache
|
|
|
|
nix-shell --impure script/wowmine-stickers/importer/shell.nix --run \
|
|
'python -m pytest -q -o cache_dir=/tmp/wowmine-stickers-importer/pytest-cache --basetemp=/tmp/wowmine-stickers-importer/tests script/wowmine-stickers/importer/tests'
|
|
```
|
|
|
|
Python runtime attrs: `python3Packages.pillow`, `python3Packages.lottie`
|
|
(python-lottie 0.7.2), `python3Packages.cairosvg` (Cairo renderer),
|
|
`python3Packages.pydantic`, `python3Packages.typer`, `python3Packages.httpx`; executable attr:
|
|
`ffmpeg` (includes ffprobe and libvpx). Test/check attrs: `python3Packages.pytest`,
|
|
`ruff`, `basedpyright`. Both Nix files pin repository's Nixpkgs revision
|
|
`bfc1b8a4574108ceef22f02bafcf6611380c100d`; accept explicit `pkgs` override.
|
|
HTTPX 0.28.1 is selected explicitly because this pinned Nixpkgs provides it;
|
|
httpx2 is absent. No packages install into shared Python environments.
|
|
|
|
Token comes only from the explicit file (max 256 bytes). Keep file readable only
|
|
by importer operator. CLI prints structured status, never response
|
|
content, token, traceback or credential-bearing URL. Tokens are not subprocess
|
|
arguments. Environment proxies and HTTP redirects are disabled. `--api-base`
|
|
supports an operator's local Bot API server: HTTPS origin or loopback HTTP,
|
|
without path, query or user information. Returned file paths must be safe
|
|
relative paths; absolute paths from Bot API `--local` mode are intentionally
|
|
rejected. Configure local server to serve relative file downloads over HTTP.
|
|
Only configured origin receives token; pack URLs are parsed, never fetched.
|
|
|
|
Catalog exactly matches shared version 1 schema:
|
|
|
|
```json
|
|
{"version":1,"packs":[{"id":"PackShortName","title":"Pack title","stickers":[{"id":"lowercase64hexSHA256","emoji":"","frames":1}]}]}
|
|
```
|
|
|
|
`id` is SHA-256 of actual PNG bytes; assets live at `assets/<id>.png`.
|
|
Every PNG contains deterministic text chunk `wowmine_frames` with decimal
|
|
frame count. Identical pixels at different timings therefore have distinct
|
|
hash identities. Catalog also rejects conflicting frame counts for one hash.
|
|
RGBA cells are aspect-preserving, centered, transparent 128x128. Atlas is
|
|
row-major, `min(8,frames)` columns, enough rows; unused cells stay transparent.
|
|
Static WEBP produces one frame. TGS and VP8/VP9 WEBM sample at 20 fps, retaining
|
|
at most first 60 frames (three seconds). VPX software decoder preserves alpha.
|
|
Opaque source pixels remain opaque. TGS renders through python-lottie/Cairo;
|
|
external/embedded raster images, text/font resources and expressions are rejected.
|
|
Complex effects unsupported by python-lottie may not match Telegram perfectly.
|
|
|
|
Limits: PNG 2 MiB, catalog 512 KiB, 64 packs, 200 stickers/pack, 2048 overall.
|
|
Title max 256 and emoji max 64 UTF-16 code units, matching Java.
|
|
Identical PNGs within imported pack retain first emoji. Existing duplicate pack
|
|
IDs or sticker hashes inside a pack reject catalog. Import replaces one pack by
|
|
canonical Telegram name, preserving other packs. Existing catalog fields are
|
|
strictly parsed; unknown fields reject. Empty existing packs/catalog are allowed.
|
|
|
|
Additional safety budgets: 4 MiB/download; 128 MiB total network bytes and
|
|
generated PNG bytes per import; Bot API response 512 KiB; source dimensions
|
|
at most 512x512; TGS expanded JSON 2 MiB, depth 32, 50,000 nodes, rate at most
|
|
120 fps and frame timeline at most 7200. Reads/connects time out after 10/5
|
|
seconds, requests enforce 30 seconds checked per received chunk (a pending
|
|
socket read adds at most 10 seconds). Worker wall time 45 seconds, CPU 35
|
|
seconds, address space 768 MiB, file size 8 MiB; ffmpeg decode 30 seconds.
|
|
Imports are sequential; no retries. Bounded conversions run without token
|
|
input, network protocols or arbitrary file resource loading.
|
|
|
|
Pack URL/name is untrusted ordinary-player input. No OP requirement exists in
|
|
this CLI; main server owns authorization policy, queue, rate limits and request
|
|
deduplication. `--api-base`, `--token-file` and `--output` are operator configuration,
|
|
never player-controlled. Invoke with an argument array, never a shell string.
|
|
|
|
Status is JSON Lines: stdout emits `{"status":"fetching"}`, then
|
|
`{"status":"converting","completed":0,"total":3}` before each conversion and
|
|
`{"status":"complete","pack":"PackShortName","stickers":3}` after publication.
|
|
Counts in progress track source stickers; final count is unique PNGs.
|
|
Exit 0 means complete. Runtime errors emit
|
|
`{"status":"error","code":"import_failed"}` on stderr and exit 1.
|
|
Whole-job deadline emits code `job_timeout` instead. Fetch/conversion deadline
|
|
is 270 seconds, leaving server's proposed 300-second kill deadline cleanup
|
|
headroom. Deadline disarms for atomic publication and bounded final cleanup.
|
|
300 seconds cannot guarantee importing every 200-sticker animated pack:
|
|
individual conversion CPU caps alone permit 200 * 35 = 7000 CPU seconds.
|
|
Keep 300 seconds if timeout failures are acceptable; do not promise every
|
|
public pack finishes within that budget.
|
|
Argument syntax errors remain Typer usage errors (exit 2); server must provide
|
|
all required options. No exception text or remote metadata enters error status.
|
|
|
|
Output directory is trusted operator-owned local storage; keep other writers
|
|
out. **Single-writer contract:** main server serializes all imports globally.
|
|
Importer does not provide a queue or concurrent writer coordination.
|
|
Import stages in `.wowmine-stickers-importer-v1/job-*`, publishes hash assets
|
|
then fsyncs and atomically replaces catalog. Input/conversion/validation failures
|
|
retain old catalog. Capacity checks run before conversion when definitely full,
|
|
and complete 2048-entry validation happens before publishing any asset.
|
|
|
|
Owned staging has a format marker and per-job asset journal written atomically
|
|
before publication. Normal failure cleans staging and any journaled unreferenced
|
|
assets. Next import recovers interrupted jobs before allocating new staging.
|
|
Replacement reclaims obsolete assets of replaced packs. Every deletion excludes
|
|
all IDs referenced by current catalog; a candidate must also have matching
|
|
SHA-256 contents and be a regular non-symlink file. Files absent from ownership
|
|
journals and other directories are preserved, including unrelated files inside
|
|
cache. Output's marked staging namespace is reserved for importer scratch.
|
|
Malformed journals or invalid catalog stop recovery rather than guess ownership.
|
|
Failed recovery leaves its journal and prevents further staging growth.
|
|
|
|
Managed cache contains at most 2048 referenced PNGs (2 MiB each), plus bounded
|
|
in-flight pack (128 MiB generated PNG budget, one 4 MiB input and one transient
|
|
atlas). Interruption may leave journaled unreferenced blobs, reclaimed on next
|
|
import. This bound excludes unrelated pre-existing operator files, which importer
|
|
must not delete. No global garbage collection of unknown hash-named files occurs.
|
|
Process interruption before catalog rename keeps old catalog; power-loss
|
|
durability of directory entries is filesystem-dependent.
|
|
|
|
Sources consulted: official Bot API `getStickerSet`/`getFile` method docs and
|
|
python-lottie Cairo exporter API; installed `Animation.load(lottiedict)` and
|
|
`export_png(animation, fp, frame=0, dpi=96)` signatures verified.
|