Repository Public
1import { execFile } from "node:child_process";2import fs from "node:fs";3import path from "node:path";4import zlib from "node:zlib";5import { all, db, get, getNum, getSetting, log, now, run, tx } from "./db";6import { env } from "./env";7import { fetchBuffer } from "./ssrf";89// Real programme data only. Two origins:10// - the official IPTV-org EPG grabber (https://github.com/iptv-org/epg), run locally for a bounded list of channels;11// - XMLTV files added by the administrator.12// A channel without data simply has no rows in `programs`; the UI then says « Programme indisponible ».1314export interface Programme { channelId: string; start: number; stop: number; title: string; subtitle: string | null; description: string | null; category: string | null }1516const ENT: Record<string, string> = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };17const decode = (s: string) => s.replace(/&(#x[0-9a-f]+|#\d+|[a-z]+);/gi, (m, e: string) =>18 e[0] === "#" ? String.fromCodePoint(e[1].toLowerCase() === "x" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10)) : ENT[e.toLowerCase()] ?? m).trim();1920export function parseXmltvTime(s: string): number {21 const m = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?\s*([+-]\d{4})?/.exec(s.trim());22 if (!m) return NaN;23 const utc = Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +(m[6] ?? 0));24 if (!m[7]) return utc;25 const off = (parseInt(m[7].slice(1, 3), 10) * 60 + parseInt(m[7].slice(3), 10)) * (m[7][0] === "-" ? -1 : 1);26 return utc - off * 60_000;27}2829export function parseXmltv(xml: string): Programme[] {30 const out: Programme[] = [];31 const re = /<programme\b([^>]*)>([\s\S]*?)<\/programme>/g;32 const attr = (h: string, n: string) => new RegExp(`\\b${n}="([^"]*)"`).exec(h)?.[1];33 const tag = (b: string, n: string) => { const m = new RegExp(`<${n}\\b[^>]*>([\\s\\S]*?)</${n}>`).exec(b); return m ? decode(m[1].replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1")) || null : null; };34 for (let m; (m = re.exec(xml)); ) {35 const ch = attr(m[1], "channel"), start = parseXmltvTime(attr(m[1], "start") ?? ""), stop = parseXmltvTime(attr(m[1], "stop") ?? "");36 const title = tag(m[2], "title");37 if (!ch || !title || !Number.isFinite(start) || !Number.isFinite(stop) || stop <= start) continue;38 out.push({ channelId: decode(ch), start, stop, title, subtitle: tag(m[2], "sub-title"), description: tag(m[2], "desc"), category: tag(m[2], "category") });39 }40 return out;41}4243/** Store programmes for catalogue channels (XMLTV id « Channel.cc » or « Channel.cc@Feed »). Returns programmes kept per channel. */44export function ingestProgrammes(list: Programme[], origin: string): Map<string, number> {45 const known = new Set(all<{ id: string }>("SELECT id FROM channels").map((r) => r.id));46 // A guide written for a playlist uses that playlist's identifiers: resolve them through the streams that carry them.47 const viaStream = new Map(all<{ tvg_id: string; channel_id: string }>("SELECT tvg_id, channel_id FROM streams WHERE tvg_id IS NOT NULL").map((r) => [r.tvg_id, r.channel_id]));48 const lo = now() - 12 * 3600_000, hi = now() + 4 * 86400_000;49 const by = new Map<string, Programme[]>();50 for (const p of list) {51 const id = known.has(p.channelId) ? p.channelId : viaStream.get(p.channelId) ?? p.channelId.split("@")[0];52 if (!known.has(id) || p.stop < lo || p.start > hi) continue;53 const l = by.get(id); if (l) l.push(p); else by.set(id, [p]);54 }55 const counts = new Map<string, number>();56 const d = db();57 tx(() => {58 const ins = d.prepare("INSERT OR REPLACE INTO programs(channel_id, start, stop, title, subtitle, description, category, origin) VALUES(?,?,?,?,?,?,?,?)");59 for (const [id, progs] of by) {60 progs.sort((a, b) => a.start - b.start);61 run("DELETE FROM programs WHERE channel_id = ? AND start >= ?", id, progs[0].start);62 let lastStop = 0, n = 0;63 for (const p of progs) {64 if (p.start < lastStop) continue; // overlapping duplicates from multi-feed files65 ins.run(id, p.start, p.stop, p.title.slice(0, 300), p.subtitle?.slice(0, 300) ?? null, p.description?.slice(0, 2000) ?? null, p.category?.slice(0, 100) ?? null, origin);66 lastStop = p.stop; n++;67 }68 counts.set(id, n);69 }70 run("DELETE FROM programs WHERE stop < ?", now() - 24 * 3600_000);71 });72 return counts;73}7475// --- IPTV-org grabber -----------------------------------------------------------------------------------------------76const GRABBER_REPO = "https://github.com/iptv-org/epg";77const LANG2: Record<string, string> = { fra: "fr", eng: "en", spa: "es", deu: "de", ita: "it", por: "pt", ara: "ar", nld: "nl", rus: "ru", zho: "zh", jpn: "ja", kor: "ko", tur: "tr", pol: "pl" };78// Sites observed to answer reliably without credentials; the rest keep their order from the IPTV-org guide list.79const SITE_RANK = ["tvhebdo.com", "canalplus.com", "tvpassport.com", "tv.sfr.fr", "programme-tv.net", "tvtv.us", "i.mjh.nz", "plex.tv", "ontvtonight.com", "pluto.tv"];8081function exec(cmd: string, args: string[], opts: { cwd?: string; timeoutMs: number }): Promise<string> {82 return new Promise((resolve, reject) => {83 execFile(cmd, args, { cwd: opts.cwd, timeout: opts.timeoutMs, maxBuffer: 32 * 1024 * 1024, env: { ...process.env, PATH: `/opt/homebrew/bin:/usr/local/bin:${process.env.PATH}`, CI: "1", HUSKY: "0" } },84 (err, stdout, stderr) => err ? reject(new Error(`${cmd} ${args[0]} : ${(stderr || err.message).toString().slice(-600)}`)) : resolve(stdout.toString()));85 });86}8788export async function ensureGrabber(): Promise<string> {89 const dir = path.join(env.dataDir, "epg-grabber");90 const stamp = path.join(dir, ".sptv-installed");91 if (!fs.existsSync(path.join(dir, "package.json"))) {92 fs.rmSync(dir, { recursive: true, force: true });93 log("info", "epg", "Installation du grabber IPTV-org (première exécution)");94 await exec("git", ["clone", "--depth", "1", GRABBER_REPO, dir], { timeoutMs: 180_000 });95 } else if (fs.existsSync(stamp) && now() - fs.statSync(stamp).mtimeMs > 7 * 86400_000) {96 // Site parsers break when the scraped sites change: refresh weekly. A failed update keeps the installed version.97 try { await exec("git", ["pull", "--ff-only", "--depth", "1"], { cwd: dir, timeoutMs: 120_000 }); fs.rmSync(stamp); }98 catch (e) { log("warn", "epg", "Mise à jour du grabber impossible, version installée conservée", { error: (e as Error).message }); fs.utimesSync(stamp, new Date(), new Date()); }99 }100 if (!fs.existsSync(stamp)) {101 await exec("npm", ["install", "--no-audit", "--no-fund"], { cwd: dir, timeoutMs: 600_000 });102 fs.writeFileSync(stamp, new Date().toISOString());103 }104 return dir;105}106107interface GuideRow { channel_id: string; feed: string | null; site: string; site_id: string; site_name: string | null; lang: string | null }108109/** Ordered guide candidates for a channel: matching language first, then sites known to work. */110export function rankGuides(rows: GuideRow[], languages: string[]): GuideRow[] {111 const want = new Set(languages.map((l) => LANG2[l] ?? l.slice(0, 2)));112 const score = (g: GuideRow) => (want.has(g.lang ?? "") ? 0 : 100) + (SITE_RANK.indexOf(g.site) >= 0 ? SITE_RANK.indexOf(g.site) : 50);113 const seen = new Set<string>();114 return [...rows].sort((a, b) => score(a) - score(b)).filter((g) => (seen.has(g.site) ? false : (seen.add(g.site), true)));115}116117export function guideChannelIds(): string[] {118 const max = getNum("epg_max_channels");119 const countries = (getSetting("epg_countries") ?? "").split(",").map((s) => s.trim().toUpperCase()).filter(Boolean);120 const personal = all<{ id: string }>(`SELECT c.id FROM channels c WHERE c.has_guide = 1 AND c.active = 1121 AND c.id IN (SELECT channel_id FROM list_items UNION SELECT channel_id FROM (SELECT channel_id FROM history ORDER BY last_watched_at DESC LIMIT 40))`).map((r) => r.id);122 const ids = new Set(personal);123 if (countries.length) {124 const rows = all<{ id: string }>(`SELECT c.id FROM channels c WHERE c.has_guide = 1 AND c.active = 1 AND c.country IN (${countries.map(() => "?").join(",")})125 AND c.id NOT IN (SELECT channel_id FROM hidden)126 ORDER BY EXISTS(SELECT 1 FROM streams s WHERE s.channel_id = c.id AND s.status = 'up') DESC, c.name`, ...countries);127 for (const r of rows) { if (ids.size >= max) break; ids.add(r.id); }128 }129 return [...ids].slice(0, Math.max(max, personal.length));130}131132const xa = (s: string) => s.replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">");133134export interface EpgResult { channels: number; withData: number; programmes: number; sources: { name: string; ok: boolean; programmes: number; error?: string }[]; grabber?: string; durationMs: number }135136export async function refreshEpg(): Promise<EpgResult> {137 const t0 = now();138 const res: EpgResult = { channels: 0, withData: 0, programmes: 0, sources: [], durationMs: 0 };139140 // 1. Administrator XMLTV sources.141 for (const s of all<{ id: number; name: string; url: string }>("SELECT id, name, url FROM epg_sources WHERE enabled = 1")) {142 try {143 // A provider's guide address carries its credentials: it is rebuilt here and never stored.144 const target = s.url.startsWith("provider://") ? await providerGuide(s.url) : s.url;145 let buf = await fetchBuffer(target, 200 * 1024 * 1024);146 if (buf[0] === 0x1f && buf[1] === 0x8b) buf = zlib.gunzipSync(buf, { maxOutputLength: 800 * 1024 * 1024 });147 const counts = ingestProgrammes(parseXmltv(buf.toString("utf8")), `xmltv:${s.id}`);148 const n = [...counts.values()].reduce((a, b) => a + b, 0);149 run("UPDATE epg_sources SET last_sync_at = ?, last_status = 'ok', last_error = NULL, program_count = ? WHERE id = ?", now(), n, s.id);150 res.sources.push({ name: s.name, ok: true, programmes: n }); res.programmes += n;151 } catch (e) {152 const msg = (e as Error).message.replace(/(username|password)=[^&\s]+/gi, "$1=•••");153 run("UPDATE epg_sources SET last_sync_at = ?, last_status = 'error', last_error = ? WHERE id = ?", now(), msg, s.id);154 res.sources.push({ name: s.name, ok: false, programmes: 0, error: msg });155 log("error", "epg", `Source XMLTV en échec : ${s.name}`, { error: msg });156 }157 }158159 // 2. IPTV-org grabber for the guide list.160 const ids = guideChannelIds();161 res.channels = ids.length;162 if (ids.length) {163 try {164 const dir = await ensureGrabber();165 const work = path.join(env.dataDir, "epg"); fs.mkdirSync(work, { recursive: true });166 const chosen = new Map<string, { site: string; n: number; idx: number }>();167 const lines = ['<?xml version="1.0" encoding="UTF-8"?>', "<channels>"];168 for (const id of ids) {169 const c = get<{ languages: string }>("SELECT languages FROM channels WHERE id = ?", id)!;170 const cands = rankGuides(all<GuideRow>("SELECT * FROM guides WHERE channel_id = ?", id), c.languages.split(";").filter(Boolean));171 if (!cands.length) continue;172 const st = get<{ candidate: number }>("SELECT candidate FROM epg_state WHERE channel_id = ?", id);173 const idx = (st?.candidate ?? 0) % cands.length; const g = cands[idx];174 chosen.set(id, { site: g.site, n: cands.length, idx });175 lines.push(` <channel site="${xa(g.site)}" lang="${xa(g.lang ?? "")}" xmltv_id="${xa(id)}" site_id="${xa(g.site_id)}">${xa(g.site_name ?? id)}</channel>`);176 }177 lines.push("</channels>");178 const chFile = path.join(work, "custom.channels.xml"), outFile = path.join(work, "guide.xml");179 fs.writeFileSync(chFile, lines.join("\n")); fs.rmSync(outFile, { force: true });180 const out = await exec("node", ["-r", "@swc-node/register", "scripts/commands/epg/grab.ts", `--channels=${chFile}`, `--output=${outFile}`, `--days=${getNum("epg_days")}`, "--maxConnections=2", "--timeout=30000"],181 { cwd: dir, timeoutMs: 60 * 60_000 });182 res.grabber = out.split("\n").filter((l) => /done in|found \d+ channel/.test(l)).join(" · ").replace(/[^\x20-\x7eÀ-ÿ·]/g, "").trim();183 const counts = fs.existsSync(outFile) ? ingestProgrammes(parseXmltv(fs.readFileSync(outFile, "utf8")), "iptv-org") : new Map<string, number>();184 tx(() => {185 for (const [id, ch] of chosen) {186 const n = counts.get(id) ?? 0;187 if (n) { res.withData++; res.programmes += n; }188 // No data from this site: the next run tries the next candidate site for this channel.189 run(`INSERT INTO epg_state(channel_id, candidate, site, last_try_at, last_ok_at, last_count) VALUES(?,?,?,?,?,?)190 ON CONFLICT(channel_id) DO UPDATE SET candidate = excluded.candidate, site = excluded.site, last_try_at = excluded.last_try_at,191 last_ok_at = COALESCE(excluded.last_ok_at, last_ok_at), last_count = excluded.last_count`,192 id, n ? ch.idx : (ch.idx + 1) % ch.n, ch.site, now(), n ? now() : null, n);193 }194 });195 } catch (e) {196 res.sources.push({ name: "Grabber IPTV-org", ok: false, programmes: 0, error: (e as Error).message });197 log("error", "epg", "Grabber IPTV-org en échec", { error: (e as Error).message });198 }199 }200 res.durationMs = now() - t0;201 log("info", "epg", `Guide TV mis à jour : ${res.withData}/${res.channels} chaînes avec données, ${res.programmes} programmes`, res);202 return res;203}204205async function providerGuide(ref: string): Promise<string> {206 const id = Number(/^provider:\/\/live\/(\d+)\/xmltv$/.exec(ref)?.[1]);207 const src = get<{ config: string | null; secret: string | null }>("SELECT config, secret FROM sources WHERE id = ? AND kind = 'xtream' AND enabled = 1", id);208 const { open } = await import("./secrets"); const { xmltvUrl } = await import("./connectors/xtream");209 const pw = open(src?.secret); if (!src || !pw) throw new Error("Fournisseur absent ou désactivé");210 return xmltvUrl(JSON.parse(src.config ?? "{}"), pw);211}212