first commit
build / test (push) Canceled after 0s
build / deb (amd64) (push) Canceled after 0s
build / deb (arm64) (push) Canceled after 0s
build / deb (armhf) (push) Canceled after 0s

This commit is contained in:
Mateusz Gruszczyński committed 2026-11-16 16:19:21 +01:00
commit c30064b531
93 files changed
+26367

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# All architectures: amd64, arm64, armhf -> dist/
set -euo pipefail
for a in amd64 arm64 armhf; do
"$(dirname "$0")/build-deb.sh" "$a"
done
ls -la "${OUT_DIR:-dist}"
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# .deb package for x86_64 (PC, server, NAS).
exec "$(dirname "$0")/build-deb.sh" amd64 "$@"
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# .deb package for Raspberry Pi 3/4/5 with a 64-bit OS (Raspberry Pi OS 64-bit, Debian, Ubuntu).
exec "$(dirname "$0")/build-deb.sh" arm64 "$@"
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# .deb package for Raspberry Pi with 32-bit Raspberry Pi OS (armv7).
exec "$(dirname "$0")/build-deb.sh" armhf "$@"
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env bash
# Builds the simply-dvb .deb package for the selected architecture.
#
# scripts/build-deb.sh amd64 # PC / x86_64 server
# scripts/build-deb.sh arm64 # Raspberry Pi 3/4/5 with 64-bit Raspberry Pi OS / Debian / Ubuntu
# scripts/build-deb.sh armhf # Raspberry Pi 2/3/4 with 32-bit Raspberry Pi OS
#
# By default the build itself runs in a Docker container (image scripts/docker/builder.Dockerfile),
# so the host only needs Docker – no Rust, zig or cargo-deb. The result lands in dist/ on the host.
# BUILD_IN_DOCKER=0 build directly on the host (e.g. in CI that already runs in a container)
# BUILDER_IMAGE=... builder image name (default simply-dvb-builder:1)
# DOCKER_ARGS="..." extra arguments for `docker run` (e.g. --network host)
# DOCKER_BUILD_ARGS="..." extra arguments for `docker build` of the builder image
#
# Build method inside (BUILD_METHOD variable):
# auto (default) – zigbuild (fixed glibc 2.31 baseline for all architectures), otherwise
# native (when host = target), cross or cross-gcc
# native – cargo build on the same architecture (e.g. directly on an RPi)
# zigbuild – cargo-zigbuild (zig as the linker, selectable glibc version: GLIBC=2.31)
# cross – cross-rs (Docker containers with ready-made toolchains)
# gcc – system cross-gcc (aarch64-linux-gnu-gcc / arm-linux-gnueabihf-gcc)
#
# Other variables:
# GLIBC=2.31 minimum glibc version for zigbuild (2.31 = Debian 11 / RPi OS bullseye and newer)
# OUT_DIR=dist output directory
set -euo pipefail
cd "$(dirname "$0")/.."
ARCH="${1:-}"
case "$ARCH" in
amd64|x86_64) DEB_ARCH=amd64; TARGET=x86_64-unknown-linux-gnu; GCC_PREFIX=x86_64-linux-gnu ;;
arm64|aarch64) DEB_ARCH=arm64; TARGET=aarch64-unknown-linux-gnu; GCC_PREFIX=aarch64-linux-gnu ;;
armhf|armv7) DEB_ARCH=armhf; TARGET=armv7-unknown-linux-gnueabihf; GCC_PREFIX=arm-linux-gnueabihf ;;
*) echo "Usage: $0 {amd64|arm64|armhf}" >&2; exit 2 ;;
esac
have() { command -v "$1" >/dev/null 2>&1; }
# ---------------------------------------------------------------- build in a container
if [ -z "${SIMPLY_DVB_IN_DOCKER:-}" ] && [ "${BUILD_IN_DOCKER:-1}" != 0 ]; then
have docker || { echo "Docker not found. Install it or run with BUILD_IN_DOCKER=0." >&2; exit 1; }
IMAGE="${BUILDER_IMAGE:-simply-dvb-builder:1}"
echo "==> builder image $IMAGE"
# shellcheck disable=SC2086
docker build ${DOCKER_BUILD_ARGS:-} -t "$IMAGE" -f scripts/docker/builder.Dockerfile scripts/docker
OUT="${OUT_DIR:-dist}"
mkdir -p .cache/cargo .cache/home "$OUT"
echo "==> building $ARCH in a container"
# Output files belong to the current user; crate and build caches stay in .cache/ and target/docker/.
# shellcheck disable=SC2086
exec docker run --rm \
--user "$(id -u):$(id -g)" \
-v "$PWD:/src" -w /src \
-e HOME=/src/.cache/home \
-e CARGO_HOME=/src/.cache/cargo \
-e CARGO_TARGET_DIR=/src/target/docker \
-e ZIG_GLOBAL_CACHE_DIR=/src/.cache/home/zig \
-e BUILD_METHOD="${BUILD_METHOD:-zigbuild}" \
-e GLIBC="${GLIBC:-2.31}" \
-e OUT_DIR="$OUT" \
-e HTTP_PROXY -e HTTPS_PROXY -e NO_PROXY -e http_proxy -e https_proxy -e no_proxy \
${DOCKER_ARGS:-} \
"$IMAGE" scripts/build-deb.sh "$ARCH"
fi
METHOD="${BUILD_METHOD:-auto}"
GLIBC="${GLIBC:-2.31}"
OUT_DIR="${OUT_DIR:-dist}"
VERSION=$(grep -m1 '^version' Cargo.toml | cut -d'"' -f2)
host_arch() {
case "$(uname -m)" in
x86_64) echo amd64 ;;
aarch64|arm64) echo arm64 ;;
armv7l|armv6l) echo armhf ;;
*) uname -m ;;
esac
}
if [ "$METHOD" = auto ]; then
if have cargo-zigbuild && (have zig || python3 -c 'import ziglang' 2>/dev/null); then METHOD=zigbuild
elif [ "$(host_arch)" = "$DEB_ARCH" ]; then METHOD=native
elif have cross; then METHOD=cross
elif have "${GCC_PREFIX}-gcc"; then METHOD=gcc
else
echo "No cross-compilation tools for $DEB_ARCH. Install one of:" >&2
echo " cargo install cargo-zigbuild && pip install ziglang (recommended)" >&2
echo " cargo install cross (requires Docker)" >&2
echo " apt install gcc-${GCC_PREFIX} (cross-gcc)" >&2
exit 1
fi
fi
echo "==> simply-dvb $VERSION · $DEB_ARCH ($TARGET) · method: $METHOD"
have cargo-deb || cargo install cargo-deb --locked
if [ -z "${SIMPLY_DVB_IN_DOCKER:-}" ] && [ "$METHOD" != native ] && [ "$METHOD" != cross ] && have rustup; then
rustup target add "$TARGET" >/dev/null
fi
case "$METHOD" in
native)
cargo build --release --locked --target "$TARGET"
;;
zigbuild)
cargo zigbuild --release --locked --target "${TARGET}.${GLIBC}"
;;
cross)
cross build --release --locked --target "$TARGET"
;;
gcc)
upper=$(echo "$TARGET" | tr 'a-z-' 'A-Z_')
export "CARGO_TARGET_${upper}_LINKER=${GCC_PREFIX}-gcc"
export "CC_${TARGET//-/_}=${GCC_PREFIX}-gcc"
export "AR_${TARGET//-/_}=${GCC_PREFIX}-ar"
cargo build --release --locked --target "$TARGET"
;;
*) echo "Unknown method: $METHOD" >&2; exit 2 ;;
esac
TARGET_DIR="${CARGO_TARGET_DIR:-target}"
BIN="$TARGET_DIR/$TARGET/release/simply-dvb"
[ -x "$BIN" ] || { echo "Missing file $BIN" >&2; exit 1; }
# cargo-deb: the binary is already built; target/release/* paths are mapped to target/<triple>/release/*
DEB=$(cargo deb --no-build --no-strip --target "$TARGET" 2>&1 | tail -n1)
mkdir -p "$OUT_DIR"
cp "$DEB" "$OUT_DIR/"
echo "==> $(ls -1 "$OUT_DIR"/simply-dvb_*_"$DEB_ARCH".deb)"
dpkg-deb -f "$OUT_DIR/$(basename "$DEB")" Package Version Architecture Depends 2>/dev/null || true
+17
View File
@@ -0,0 +1,17 @@
# Builder image for simply-dvb .deb packages (amd64, arm64, armhf) – used by scripts/build-deb.sh.
# Contains: Rust + targets, zig (cross-compilation with a fixed glibc baseline), cargo-zigbuild, cargo-deb.
FROM rust:1-bookworm
RUN apt-get update \
&& apt-get install -y --no-install-recommends python3-pip dpkg-dev \
&& rm -rf /var/lib/apt/lists/* \
&& pip3 install --no-cache-dir --break-system-packages ziglang \
&& printf '#!/bin/sh\nexec python3 -m ziglang "$@"\n' > /usr/local/bin/zig \
&& chmod 755 /usr/local/bin/zig \
&& rustup target add x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu armv7-unknown-linux-gnueabihf \
&& cargo install --locked cargo-deb cargo-zigbuild \
&& rm -rf /usr/local/cargo/registry /usr/local/cargo/git \
&& chmod -R a+rX /usr/local/cargo /usr/local/rustup
ENV SIMPLY_DVB_IN_DOCKER=1
WORKDIR /src
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env node
// Language pack helper (langs/<locale>.json).
// node scripts/i18n-missing.js pl_PL – keys from en_US.json missing in langs/pl_PL.json
// node scripts/i18n-missing.js --sources – UI texts found in web/ that are not in en_US.json
// node scripts/i18n-missing.js --new de_DE "Deutsch" "German"
// – creates langs/de_DE.json with all keys (empty values)
// Empty values fall back to English at runtime, so a pack can be translated incrementally.
'use strict';
const fs = require('fs');
const path = require('path');
const root = path.join(__dirname, '..');
const langsDir = path.join(root, 'langs');
const load = (code) => JSON.parse(fs.readFileSync(path.join(langsDir, code + '.json'), 'utf8'));
const en = load('en_US');
const arg = process.argv[2] || '--sources';
if (arg === '--new') {
const [code, nativeName, name] = process.argv.slice(3);
if (!code || !/^[a-z]{2,3}_[A-Z]{2}$/.test(code)) { console.error('usage: --new xx_YY "Native name" "English name"'); process.exit(2); }
const file = path.join(langsDir, code + '.json');
if (fs.existsSync(file)) { console.error(`${file} already exists`); process.exit(1); }
const tr = {};
for (const k of Object.keys(en.translations)) tr[k] = '';
const pack = { meta: { locale: code, name: name || code, native_name: nativeName || name || code, direction: 'ltr' }, translations: tr, patterns: [] };
fs.writeFileSync(file, JSON.stringify(pack, null, 2) + '\n');
console.error(`created ${file} (${Object.keys(tr).length} keys); see langs/pl_PL.json for pattern examples`);
process.exit(0);
}
let missing;
if (arg === '--sources') {
// Heuristic: human-readable fragments of string literals / HTML text in the panel sources.
const files = fs.readdirSync(path.join(root, 'web', 'js')).filter((f) => /\.js$/.test(f) && !/^10-/.test(f))
.map((f) => path.join(root, 'web', 'js', f)).concat([path.join(root, 'web', 'index.html')]);
const src = files.map((f) => fs.readFileSync(f, 'utf8')).join('\n');
const found = new Set();
for (const m of src.matchAll(/'((?:[^'\\\n]|\\.)*)'|>([^<>{}\n]+)</g)) {
for (const part of (m[1] || m[2] || '').replace(/<[^>]*>/g, '\n').split('\n')) {
const t = part.replace(/\\'/g, "'").trim();
if (/[A-Za-z]{3,}.*\s.*[A-Za-z]/.test(t) && !/[{}=;$]|^[\w-]+(\s[\w-]+)*=|https?:|\/api\//.test(t)) found.add(t);
}
}
missing = [...found].filter((k) => !(k in en.translations));
} else {
const pack = load(arg);
missing = Object.keys(en.translations).filter((k) => !pack.translations[k]);
}
missing.forEach((k) => console.log(k));
console.error(`${arg}: missing entries: ${missing.length}`);
+518
View File
@@ -0,0 +1,518 @@
#!/usr/bin/env python3
"""Copy the channel list layout from tvheadend to simply-dvb.
Reads tvheadend channels (number, name, enabled, tags) either from a running tvheadend
(HTTP API) or from its configuration directory, matches them with simply-dvb channels and
applies numbers – optionally also names, groups (from tags) and the enabled state.
Matching runs in passes, most reliable first (a weaker match never takes a channel that a
stronger one would get):
1. service by network ids: ONID + TSID + SID (works even if the transmitter frequency differs),
2. service: SID on the same frequency (±1 MHz),
3. exact channel/service name (case, diacritics, spaces, "HD"/"SD" ignored),
4. loose name without quality/test/prefix words ("Zet" = "Radio ZET", "PR1" = "PR1 emisja testowa"),
5. similar name (typos, spacing; --fuzzy threshold, numbers must agree: "PR1" never "PR2"),
6. SID alone when it is unique.
tvheadend duplicates (two tvh channels for one service) are listed separately and skipped.
Nothing is changed without --apply (dry run prints the plan).
Examples:
# tvheadend still running (stop it later, or run simply-dvb's stream port elsewhere)
scripts/import-tvheadend.py --tvh http://192.168.1.10:9981 --tvh-user admin \\
--url http://192.168.1.10:9980 --user admin
# tvheadend already stopped: read its configuration directory
sudo scripts/import-tvheadend.py --tvh-config /var/lib/tvheadend --url http://localhost:9980 --user admin
# apply, including names, groups and enabled/disabled state; disable channels tvheadend did not have
scripts/import-tvheadend.py --tvh-config ~/tvh-backup --user admin --names --groups --enabled \\
--unmatched disable --apply
Passwords: --tvh-pass / --password, the TVH_PASSWORD / SIMPLY_DVB_PASSWORD environment
variables, or an interactive prompt. Only the Python 3 standard library is needed.
"""
import argparse
import getpass
import gzip
import http.cookiejar
import json
import os
import re
import sys
import unicodedata
import urllib.error
import urllib.parse
import urllib.request
import zlib
# ---------------------------------------------------------------- helpers
def norm_name(s):
"""Same normalization as simply-dvb's XMLTV matching: "TVP 1 HD" == "tvp1"."""
s = unicodedata.normalize("NFKD", (s or "").lower().replace("ł", "l"))
s = "".join(c for c in s if c.isalnum() and not unicodedata.combining(c))
for suf in ("uhd", "fhd", "hd", "sd", "pl"):
if len(s) > len(suf) + 1 and s.endswith(suf):
s = s[: -len(suf)]
break
return s
def to_number(v):
try:
n = float(v)
except (TypeError, ValueError):
return 0
return int(round(n)) if n > 0 else 0
def mhz(hz):
try:
return round(float(hz) / 1e6)
except (TypeError, ValueError):
return None
def die(msg):
print("error: " + msg, file=sys.stderr)
sys.exit(1)
# ---------------------------------------------------------------- tvheadend sources
class Tvh:
"""tvheadend data: channels [{name, number, enabled, tags:[names], services:[(sid, mhz, name)]}]."""
def __init__(self):
self.channels = []
def tvh_from_api(base, user, password):
base = base.rstrip("/")
handlers = []
if user:
mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
mgr.add_password(None, base, user, password)
# tvheadend uses digest or basic auth depending on its configuration
handlers += [urllib.request.HTTPDigestAuthHandler(mgr), urllib.request.HTTPBasicAuthHandler(mgr)]
opener = urllib.request.build_opener(*handlers)
def grid(path):
url = base + path + ("&" if "?" in path else "?") + "start=0&limit=999999"
try:
with opener.open(url, timeout=30) as r:
return json.load(r).get("entries", [])
except urllib.error.HTTPError as e:
if e.code in (401, 403):
die(f"tvheadend refused {path} (HTTP {e.code}) – check --tvh-user/--tvh-pass and the user's admin rights")
die(f"tvheadend {path}: HTTP {e.code}")
except urllib.error.URLError as e:
die(f"cannot reach tvheadend at {base}: {e.reason}")
tags = {t["uuid"]: t.get("name", "") for t in grid("/api/channeltag/grid")}
muxes = {m["uuid"]: m for m in grid("/api/mpegts/mux/grid")}
services = {}
for s in grid("/api/mpegts/service/grid"):
m = muxes.get(s.get("multiplex_uuid"), {})
services[s["uuid"]] = (s.get("sid"), mhz(m.get("frequency")), s.get("svcname", ""), m.get("onid"), m.get("tsid"))
t = Tvh()
for c in grid("/api/channel/grid"):
t.channels.append({
"name": c.get("name", ""),
"number": to_number(c.get("number")),
"enabled": bool(c.get("enabled", True)),
"autoname": bool(c.get("autoname", False)),
"tags": [tags[x] for x in c.get("tags", []) if tags.get(x)],
"services": [services[x] for x in c.get("services", []) if x in services],
})
return t
def read_tvh_file(path):
"""tvheadend config files are JSON, optionally compressed ("\\xff\\xffGZIP0x" header)."""
with open(path, "rb") as f:
data = f.read()
if data.startswith(b"\xff\xffGZIP"):
body = data[12:]
try:
data = gzip.decompress(body)
except OSError:
data = zlib.decompress(body)
try:
return json.loads(data.decode("utf-8"))
except ValueError:
return None
def tvh_from_config(root):
root = os.path.expanduser(root)
if not os.path.isdir(os.path.join(root, "channel", "config")):
die(f"{root}/channel/config not found – point --tvh-config to tvheadend's configuration directory "
"(usually /var/lib/tvheadend or ~hts/.hts/tvheadend)")
tags = {}
tag_dir = os.path.join(root, "channel", "tag")
if os.path.isdir(tag_dir):
for u in os.listdir(tag_dir):
j = read_tvh_file(os.path.join(tag_dir, u))
if j:
tags[u] = j.get("name", "")
services = {}
nets = os.path.join(root, "input", "dvb", "networks")
if os.path.isdir(nets):
for net in os.listdir(nets):
mdir = os.path.join(nets, net, "muxes")
if not os.path.isdir(mdir):
continue
for mux in os.listdir(mdir):
mcfg = read_tvh_file(os.path.join(mdir, mux, "config")) or {}
freq = mhz(mcfg.get("frequency"))
onid, tsid = mcfg.get("onid"), mcfg.get("tsid")
sdir = os.path.join(mdir, mux, "services")
if not os.path.isdir(sdir):
continue
for svc in os.listdir(sdir):
j = read_tvh_file(os.path.join(sdir, svc)) or {}
services[svc] = (j.get("sid"), freq, j.get("svcname", ""), onid, tsid)
t = Tvh()
cdir = os.path.join(root, "channel", "config")
for u in os.listdir(cdir):
c = read_tvh_file(os.path.join(cdir, u))
if not c:
continue
t.channels.append({
"name": c.get("name", ""),
"number": to_number(c.get("number")),
"enabled": bool(c.get("enabled", True)),
"autoname": bool(c.get("autoname", False)),
"tags": [tags[x] for x in c.get("tags", []) if tags.get(x)],
"services": [services[x] for x in c.get("services", []) if x in services],
})
return t
# ---------------------------------------------------------------- simply-dvb API
class SimplyDvb:
def __init__(self, base, user, password):
self.base = base.rstrip("/")
self.opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar()))
self.call("POST", "/api/login", {"username": user, "password": password})
def call(self, method, path, body=None):
data = json.dumps(body).encode() if body is not None else None
req = urllib.request.Request(self.base + path, data=data, method=method)
req.add_header("X-Requested-With", "simply-dvb")
if data is not None:
req.add_header("Content-Type", "application/json")
try:
with self.opener.open(req, timeout=60) as r:
txt = r.read().decode()
return json.loads(txt) if txt else None
except urllib.error.HTTPError as e:
msg = e.read().decode(errors="replace")
try:
msg = json.loads(msg).get("error", msg)
except ValueError:
pass
die(f"simply-dvb {method} {path}: HTTP {e.code} {msg}")
except urllib.error.URLError as e:
die(f"cannot reach simply-dvb at {self.base}: {e.reason}")
# ---------------------------------------------------------------- matching
# words that do not identify a channel (quality, test transmissions, prefixes)
NOISE = re.compile(
r"\b(u?hd|fhd|sd|4k|pl|polska|poland|tv|telewizja|radio|polskie|kanal|channel|"
r"emisja|testowa|test|em|testowy|eksperymentalna|plus)\b"
)
def loose_name(s):
"""Name reduced to its identifying part: "Radio ZET em.test." -> "zet", "TVN 24 HD" -> "tvn24"."""
s = unicodedata.normalize("NFKD", (s or "").lower().replace("ł", "l"))
s = "".join(c for c in s if not unicodedata.combining(c))
s = s.replace("+", " plus ").replace("&", " and ")
s = re.sub(r"[^a-z0-9]+", " ", s)
s = NOISE.sub(" ", s)
s = re.sub(r"\s+", "", s)
return s or norm_name(s)
def digits(s):
"""Numbers in a name must agree: "PR1" never matches "PR2", "TVP3" never "TVP".."""
return re.findall(r"\d+", unicodedata.normalize("NFKD", s or ""))
def similar(a, b):
from difflib import SequenceMatcher
la, lb = loose_name(a), loose_name(b)
if not la or not lb or digits(a) != digits(b):
return 0.0
if la == lb:
return 1.0
return SequenceMatcher(None, la, lb).ratio()
def build_plan(tvh, channels, services, muxes, fuzzy=0.86):
"""Matches tvheadend channels with simply-dvb channels in passes, most reliable first,
so a weaker match can never take a channel that a stronger one would get."""
mux = {m["id"]: m for m in muxes}
chans = [c for c in channels]
used = {} # simply-dvb channel id -> tvheadend channel
result = {} # id(tvh channel) -> (simply-dvb channel, how)
def svc_of(c):
s = c.get("service") or {}
m = mux.get(s.get("mux_id"), {})
return s, m
def assign(t, c, how):
used[c["id"]] = t
result[id(t)] = (c, how)
order = sorted(tvh.channels, key=lambda x: (x["number"] == 0, x["number"], x["name"].lower()))
def run(match, how):
for t in order:
if id(t) in result:
continue
c = match(t)
if c is not None and c["id"] not in used:
assign(t, c, how)
# 1. network identity: ONID + TSID + SID (independent of the transmitter frequency)
def by_triplet(t):
for sid, _, _, onid, tsid in t["services"]:
if sid is None or onid is None or tsid is None:
continue
for c in chans:
s, m = svc_of(c)
if c["id"] not in used and s.get("sid") == sid and m.get("onid") == onid and m.get("ts_id") == tsid:
return c
return None
# 2. SID on the same frequency (±1 MHz)
def by_sid_freq(t):
for sid, f, *_ in t["services"]:
for c in chans:
s, m = svc_of(c)
cf = mhz(m.get("frequency_hz"))
if c["id"] not in used and s.get("sid") == sid and f is not None and cf is not None and abs(cf - f) <= 1:
return c
return None
# 3. exact (normalized) channel or service name
def by_name(t):
keys = {norm_name(t["name"])} | {norm_name(x[2]) for x in t["services"] if x[2]}
for c in chans:
if c["id"] in used:
continue
s, _ = svc_of(c)
if norm_name(c["name"]) in keys or (s.get("name") and norm_name(s["name"]) in keys):
return c
return None
# 4. loose name: without quality/test/prefix words ("Zet" == "Radio ZET", "PR1" == "PR1 emisja testowa")
def by_loose(t):
keys = {loose_name(t["name"])} | {loose_name(x[2]) for x in t["services"] if x[2]}
keys.discard("")
hits = [c for c in chans if c["id"] not in used and
(loose_name(c["name"]) in keys or loose_name((c.get("service") or {}).get("name", "")) in keys)
and digits(c["name"]) == digits(t["name"])]
return hits[0] if len(hits) == 1 else None
# 5. similar name (typos, spacing) – only a clear single best candidate
def by_similar(t):
if fuzzy >= 1:
return None
best, best_score, second = None, 0.0, 0.0
for c in chans:
if c["id"] in used:
continue
sc = max(similar(t["name"], c["name"]), similar(t["name"], (c.get("service") or {}).get("name", "")))
if sc > best_score:
best, best_score, second = c, sc, best_score
elif sc > second:
second = sc
return best if best_score >= fuzzy and best_score - second >= 0.05 else None
# 6. SID alone when it is unique in simply-dvb (frequency changed, no NIT ids in tvheadend)
def by_sid(t):
for sid, *_ in t["services"]:
hits = [c for c in chans if (c.get("service") or {}).get("sid") == sid]
if len(hits) == 1 and hits[0]["id"] not in used and similar(t["name"], hits[0]["name"]) >= 0.5:
return hits[0]
return None
run(by_triplet, "service (network ids)")
run(by_sid_freq, "service (SID + frequency)")
run(by_name, "name")
run(by_loose, "similar name")
run(by_similar, "similar name")
run(by_sid, "service (SID)")
# services without a channel (can be created with --create)
free = [s for s in services if s.get("channel_id") is None]
plan, missing, dups = [], [], []
for t in order:
if id(t) in result:
c, how = result[id(t)]
plan.append((t, c, how))
continue
# a tvheadend duplicate: same service or name as a channel already taken by another tvh channel
twin = None
for c in chans:
s, m = svc_of(c)
same_svc = any(s.get("sid") == sid and (f is None or abs((mhz(m.get("frequency_hz")) or 0) - f) <= 1)
for sid, f, *_ in t["services"])
if c["id"] in used and (same_svc or similar(t["name"], c["name"]) >= 0.95):
twin = c
break
if twin is not None:
dups.append((t, twin, used[twin["id"]]))
continue
create = None
for sid, f, *_ in t["services"]:
for sv in free:
if sv.get("sid") == sid and abs((mhz(mux.get(sv.get("mux_id"), {}).get("frequency_hz")) or 0) - (f or 0)) <= 1:
create = sv
break
if create:
break
if not create:
for sv in free:
if similar(t["name"], sv.get("name", "")) >= fuzzy:
create = sv
break
if create:
free.remove(create)
plan.append((t, None, "create", create))
else:
missing.append(t)
return plan, missing, dups, set(used)
def main():
ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
src = ap.add_mutually_exclusive_group(required=True)
src.add_argument("--tvh", metavar="URL", help="tvheadend address, e.g. http://192.168.1.10:9981")
src.add_argument("--tvh-config", metavar="DIR", help="tvheadend configuration directory (e.g. /var/lib/tvheadend)")
ap.add_argument("--tvh-user", help="tvheadend user (admin rights needed for the API)")
ap.add_argument("--tvh-pass", help="tvheadend password (or TVH_PASSWORD)")
ap.add_argument("--url", default="http://127.0.0.1:9980", help="simply-dvb panel address (default %(default)s)")
ap.add_argument("--user", default="admin", help="simply-dvb administrator (default %(default)s)")
ap.add_argument("--password", help="simply-dvb password (or SIMPLY_DVB_PASSWORD)")
ap.add_argument("--names", nargs="?", const="custom", choices=["custom", "all"],
help="copy channel names: 'custom' (default for --names) only names set by hand in "
"tvheadend (autoname off), 'all' every name – note tvheadend names may be stale "
"when a broadcaster renamed a channel")
ap.add_argument("--reset-names", action="store_true",
help="set every simply-dvb channel name back to its current broadcast (service) name")
ap.add_argument("--fuzzy", type=float, default=0.86,
help="similarity threshold 0–1 for similar names (default %(default)s, 1 = exact names only)")
ap.add_argument("--groups", action="store_true", help="set the group from the first tvheadend tag")
ap.add_argument("--enabled", action="store_true", help="copy the enabled/disabled state")
ap.add_argument("--create", action="store_true",
help="create channels for tvheadend channels whose service exists in simply-dvb but has no channel")
ap.add_argument("--unmatched", choices=["keep", "after", "disable"], default="after",
help="simply-dvb channels tvheadend does not have: keep numbers, number them after the "
"imported ones (default), or disable them (also numbered after)")
ap.add_argument("--apply", action="store_true", help="make the changes (default: dry run)")
a = ap.parse_args()
if a.tvh:
tpass = a.tvh_pass or os.environ.get("TVH_PASSWORD")
if a.tvh_user and tpass is None:
tpass = getpass.getpass(f"tvheadend password for {a.tvh_user}: ")
tvh = tvh_from_api(a.tvh, a.tvh_user, tpass or "")
else:
tvh = tvh_from_config(a.tvh_config)
if not tvh.channels:
die("no channels found in tvheadend")
pw = a.password or os.environ.get("SIMPLY_DVB_PASSWORD") or getpass.getpass(f"simply-dvb password for {a.user}: ")
sd = SimplyDvb(a.url, a.user, pw)
channels = sd.call("GET", "/api/channels")
services = sd.call("GET", "/api/services")
muxes = sd.call("GET", "/api/muxes")
plan, missing, dups, used = build_plan(tvh, channels, services, muxes, a.fuzzy)
print(f"tvheadend: {len(tvh.channels)} channels · simply-dvb: {len(channels)} channels, {len(services)} services\n")
print(f"{'tvh no.':>7} {'tvheadend name':<28} {'→':1} {'simply-dvb':<28} match")
print("-" * 82)
for item in plan:
t, c, how = item[0], item[1], item[2]
target = c["name"] if c else f"(new) {item[3]['name']}"
note = ""
if c and how.startswith("service") and similar(t["name"], c["name"]) < 0.6:
note = " (renamed by the broadcaster?)"
print(f"{t['number'] or '-':>7} {t['name'][:28]:<28} → {target[:28]:<28} {how}{note}")
if dups:
print("\nDuplicates in tvheadend (same service/name as a channel above – skipped):")
for t, c, first in dups:
print(f"{t['number'] or '-':>7} {t['name'][:28]:<28} = #{first['number']} {first['name']} → {c['name']}")
if missing:
print("\nNot found in simply-dvb (not received by the scan, or a different service):")
for t in missing:
print(f"{t['number'] or '-':>7} {t['name'][:28]:<28} ✗")
rest = [c for c in channels if c["id"] not in used]
if rest:
print(f"\nsimply-dvb channels without a tvheadend counterpart ({a.unmatched}): " +
", ".join(c["name"] for c in sorted(rest, key=lambda c: c["number"])))
if not a.apply:
print("\nDry run – nothing changed. Add --apply to make the changes.")
return
# imported channels keep tvheadend numbers; channels without a number go after the highest one
top = max([t["number"] for t, *_ in plan] + [0])
changed = created = 0
for item in plan:
t, c = item[0], item[1]
number = t["number"]
if not number:
top += 1
number = top
if c is None:
r = sd.call("POST", "/api/channels", {"service_ids": [item[3]["id"]]})
ids = (r or {}).get("ids") or []
if not ids:
print(f" could not create a channel for {t['name']}")
continue
c = {"id": ids[0], "service": item[3]}
created += 1
body = {"number": number}
if a.names and t["name"] and not t["name"].startswith("{") and (a.names == "all" or not t.get("autoname")):
body["name"] = t["name"]
elif a.reset_names and c.get("service", {}).get("name"):
body["name"] = c["service"]["name"]
if a.groups and t["tags"]:
body["grp"] = t["tags"][0]
if a.enabled:
body["enabled"] = t["enabled"]
sd.call("PUT", f"/api/channels/{c['id']}", body)
changed += 1
if a.unmatched != "keep":
for c in sorted(rest, key=lambda c: c["number"]):
top += 1
body = {"number": top}
if a.reset_names and (c.get("service") or {}).get("name"):
body["name"] = c["service"]["name"]
if a.unmatched == "disable":
body["enabled"] = False
sd.call("PUT", f"/api/channels/{c['id']}", body)
print(f"\nDone: {changed} channels updated ({created} created), {len(dups)} duplicates skipped, {len(missing)} not found"
+ (f", {len(rest)} others {'disabled and ' if a.unmatched == 'disable' else ''}numbered from {top - len(rest) + 1}"
if rest and a.unmatched != "keep" else "") + ".")
if __name__ == "__main__":
main()