#!/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()