Repository Public
1import { all, bumpCatalog, bumpTitles, get, log, now, run, tx } from "./db";2import { parseM3u, type M3uEntry } from "./m3u";3import { open as unseal, redact, seal } from "./secrets";4import { assertPublicUrl, fetchText, UrlRejected } from "./ssrf";5import { probeStream } from "./checker";6import { setMedia, upsertEpisode, upsertTitle } from "./vod";7import * as arte from "./connectors/arte";8import * as jf from "./connectors/jellyfin";9import * as nasa from "./connectors/nasa";10import * as xt from "./connectors/xtream";1112// Universal source engine. Every way content enters SP TV is a connector with the same contract — describe itself,13// test a configuration, import — so a source is added from the administration, never by changing code:14//15// live m3u-url · m3u-file · stream (one HLS / DASH address) · xtream (authorised provider) · built-in playlists16// guide xmltv · provider guide17// vod wikidata (public domain / free licences) · onf · arte · nasa · jellyfin · xtream18// library folders of this machine or a mounted NAS19//20// Live connectors all end in the same pipeline (sync.ts: filters, de-duplication, grouping, availability tests); VOD21// connectors all write titles / episodes / media. Storage stays in the existing tables; this module is the single22// entry point that knows them all.2324export type Family = "live" | "guide" | "vod" | "library";25export interface Field { key: string; label: string; type: "text" | "url" | "password" | "file" | "country" | "select"; required?: boolean; placeholder?: string; help?: string; options?: [string, string][] }26export interface ConnectorType { type: string; family: Family; label: string; description: string; fields: Field[]; consent?: string; builtin?: boolean }2728export const TYPES: ConnectorType[] = [29 { type: "m3u-url", family: "live", label: "Playlist M3U / M3U8 (adresse)", description: "Une playlist publiée à une adresse http(s), relue à chaque synchronisation.", consent: "Je suis autorisé à utiliser cette playlist pour mon usage personnel.",30 fields: [{ key: "name", label: "Nom", type: "text", required: true }, { key: "url", label: "Adresse de la playlist", type: "url", required: true, placeholder: "https://exemple.org/playlist.m3u" }, { key: "country", label: "Pays par défaut", type: "country" }] },31 { type: "m3u-file", family: "live", label: "Playlist M3U (fichier importé)", description: "Le contenu d'un fichier .m3u, conservé dans la base.", consent: "Je suis autorisé à utiliser cette playlist pour mon usage personnel.",32 fields: [{ key: "name", label: "Nom", type: "text", required: true }, { key: "content", label: "Fichier .m3u / .m3u8", type: "file", required: true }, { key: "country", label: "Pays par défaut", type: "country" }] },33 { type: "stream", family: "live", label: "Flux HLS ou MPEG-DASH (une chaîne)", description: "Une seule adresse .m3u8 ou .mpd non protégée, ajoutée comme chaîne.", consent: "Ce flux est public ou j'ai le droit de le regarder.",34 fields: [{ key: "name", label: "Nom de la chaîne", type: "text", required: true }, { key: "url", label: "Adresse du flux", type: "url", required: true, placeholder: "https://exemple.org/direct/master.m3u8" }, { key: "country", label: "Pays", type: "country" }, { key: "logo", label: "Logo (adresse d'image)", type: "url" }] },35 { type: "loop", family: "live", label: "Chaîne maison (vos fichiers en boucle)", description: "Une chaîne continue faite d'un film ou d'une série de votre bibliothèque personnelle, diffusée par le moteur de relais : un seul encodage, plusieurs écrans.",36 consent: "Ces fichiers m'appartiennent ou leur licence m'autorise à les diffuser chez moi.",37 fields: [{ key: "name", label: "Nom de la chaîne", type: "text", required: true }, { key: "match", label: "Titre dans votre bibliothèque (film ou série)", type: "text", required: true, placeholder: "Big Buck Bunny" }, { key: "logo", label: "Logo (adresse d'image)", type: "url" }] },38 { type: "xtream", family: "live", label: "Fournisseur IPTV (API compatible Xtream)", description: "Chaînes, films, séries et guide d'un fournisseur auquel vous êtes abonné et qui autorise les lecteurs tiers. Vos identifiants sont chiffrés et ne quittent pas le serveur.",39 consent: "Je suis abonné à ce fournisseur, il détient les droits de ce qu'il diffuse et il autorise l'usage de mes identifiants dans une application tierce.",40 fields: [{ key: "name", label: "Nom", type: "text", required: true }, { key: "server", label: "Adresse du portail", type: "url", required: true, placeholder: "https://portail.fournisseur.example" }, { key: "username", label: "Identifiant", type: "text", required: true }, { key: "password", label: "Mot de passe", type: "password", required: true }] },41 { type: "xmltv", family: "guide", label: "Guide TV (XMLTV / EPG)", description: "Un fichier XMLTV (.xml ou .xml.gz) dont les identifiants de chaînes suivent IPTV-org ou ceux d'une de vos playlists.",42 fields: [{ key: "name", label: "Nom", type: "text", required: true }, { key: "url", label: "Adresse du guide", type: "url", required: true, placeholder: "https://exemple.org/guide.xml.gz" }] },43 { type: "jellyfin", family: "vod", label: "Bibliothèque Jellyfin", description: "Les films et séries d'un serveur Jellyfin que vous exploitez (réseau de la maison ou adresse publique). La lecture va directement de l'appareil à Jellyfin.",44 fields: [{ key: "name", label: "Nom", type: "text", required: true }, { key: "server", label: "Adresse du serveur", type: "url", required: true, placeholder: "http://192.168.2.10:8096" }, { key: "publicUrl", label: "Adresse vue par vos appareils, si différente", type: "url", placeholder: "https://tv.spboucher.ai/jellyfin" },45 { key: "username", label: "Utilisateur Jellyfin", type: "text", required: true }, { key: "password", label: "Mot de passe", type: "password", required: true }] },46 { type: "wikidata", family: "vod", builtin: true, label: "Films et séries libres (Wikidata × Internet Archive / Commons)", description: "Œuvres du domaine public ou sous licence libre, droits documentés par Wikidata.", fields: [] },47 { type: "onf", family: "vod", builtin: true, label: "ONF — Office national du film du Canada", description: "Films gratuits dans le lecteur officiel intégré.", fields: [] },48 { type: "arte", family: "vod", builtin: true, label: "ARTE", description: "Documentaires, films et séries disponibles au Canada, dans le lecteur officiel intégré ; catalogue tournant, droits datés.", fields: [] },49 { type: "nasa", family: "vod", builtin: true, label: "NASA", description: "Vidéos de la bibliothèque de la NASA (fichiers MP4 directs).", fields: [] },50 { type: "folder", family: "library", label: "Dossier local ou NAS monté", description: "Géré dans l'onglet Bibliothèque.", fields: [] },51];5253// --- live: loading the entries of any live source -------------------------------------------------------------------------54export interface LiveSource { id: number; name: string; url: string; country: string | null; kind: string; config: string | null; secret: string | null; content: string | null }55export interface Loaded { entries: M3uEntry[]; errors: string[]; total: number }56export async function loadLive(s: LiveSource, readBuiltin: (name: string) => string): Promise<Loaded> {57 if (s.kind === "xtream") {58 const cfg = JSON.parse(s.config ?? "{}") as xt.XtreamConfig; const pw = unseal(s.secret); if (!pw) throw new Error("Identifiants illisibles (APP_SECRET modifié ?)");59 const acc = await xt.account(cfg, pw); if (!acc.ok) throw new Error(acc.message);60 const entries = await xt.liveEntries(cfg, pw, s.id); return { entries, errors: [], total: entries.length };61 }62 if (s.kind === "loop") {63 const c = JSON.parse(s.config ?? "{}") as { logo?: string };64 return { total: 1, errors: [], entries: [{ tvgId: null, channelId: null, feed: null, name: s.name, rawTitle: s.name, logo: c.logo || null, group: [], quality: null, labels: [], url: `provider://loop/${s.id}/1`, referrer: null, userAgent: null, country: s.country }] };65 }66 if (s.kind === "stream") {67 const c = JSON.parse(s.config ?? "{}") as { logo?: string };68 return { total: 1, errors: [], entries: [{ tvgId: null, channelId: null, feed: null, name: s.name, rawTitle: s.name, logo: c.logo || null, group: [], quality: null, labels: [], url: s.url, referrer: null, userAgent: null, country: s.country }] };69 }70 const text = s.kind === "file" ? s.content ?? "" : s.url.startsWith("builtin:") ? readBuiltin(s.url.slice(8)) : await fetchText(s.url, 60 * 1024 * 1024, 120_000);71 const p = parseM3u(text);72 if (!p.entries.length) throw new Error(p.errors[0] ?? "Aucune entrée #EXTINF dans la playlist");73 return { entries: p.entries, errors: p.errors, total: p.entries.length };74}7576// --- provider addresses: opaque in the database, rebuilt on the server at play time ------------------------------------------77/** `provider://live/<source>/<file>` · `provider://xvod/<vodSource>/(movie|series)/<file>` · `provider://jf/<vodSource>/<item>` → real address. */78export async function resolveProvider(ref: string, opts: { hls?: boolean } = {}): Promise<string> {79 if (ref.startsWith("provider://loop/")) throw new Error("Chaîne maison : lecture par le moteur de relais uniquement");80 const m = /^provider:\/\/(live|xvod|jf)\/(\d+)\/(.+)$/.exec(ref); if (!m) throw new Error("Référence de fournisseur invalide");81 const [, kind, id, rest] = m;82 if (kind === "live") {83 const s = get<{ config: string | null; secret: string | null; enabled: number }>("SELECT config, secret, enabled FROM sources WHERE id = ? AND kind = 'xtream'", Number(id));84 const pw = unseal(s?.secret); if (!s || !pw || !s.enabled) throw new Error("Fournisseur absent ou désactivé");85 return xt.liveUrl(JSON.parse(s.config ?? "{}"), pw, rest);86 }87 const v = get<{ kind: string; config: string | null; secret: string | null; enabled: number }>("SELECT kind, config, secret, enabled FROM vod_sources WHERE id = ?", Number(id));88 const pw = unseal(v?.secret); if (!v || !pw || !v.enabled) throw new Error("Source absente ou désactivée");89 if (kind === "xvod") { const [type, file] = rest.split("/"); return type === "series" ? xt.episodeUrl(JSON.parse(v.config ?? "{}"), pw, file) : xt.movieUrl(JSON.parse(v.config ?? "{}"), pw, file); }90 const cfg = JSON.parse(v.config ?? "{}") as jf.JellyfinConfig; const s = await jf.login(cfg, pw);91 return opts.hls ? jf.hlsUrl(cfg, s, rest) : jf.directUrl(cfg, s, rest);92}93export const isProvider = (url: string | null | undefined): url is string => !!url && url.startsWith("provider://");9495// --- VOD imports -------------------------------------------------------------------------------------------------------96const vodSource = (kind: string, name: string, ref: string, as: string, note: string) =>97 run("INSERT INTO vod_sources(kind, name, ref, as_kind, rights_note, builtin, created_at) VALUES(?,?,?,?,?,1,?) ON CONFLICT(kind, ref) DO NOTHING", kind, name, ref, as, note, now());98const vodEnabled = (kind: string) => !!get("SELECT 1 FROM vod_sources WHERE kind = ? AND enabled = 1", kind);99const vodDone = (kind: string, ref: string, n: number, err?: string) => run("UPDATE vod_sources SET last_sync_at = ?, last_status = ?, last_error = ?, title_count = ? WHERE kind = ? AND ref = ?", now(), err ? "error" : "ok", err ?? null, n, kind, ref);100const countActive = (origin: string) => get<{ n: number }>("SELECT COUNT(*) n FROM titles t WHERE origin = ? AND active = 1 AND EXISTS(SELECT 1 FROM media m WHERE m.title_id = t.id)", origin)!.n;101102export function ensureBuiltinVod() {103 vodSource("arte", "ARTE — documentaires, films et séries (lecteur officiel intégré)", "fr", "movie", "Lecteur intégré d'ARTE, seulement quand la configuration du lecteur l'autorise (allowEmbed), sans restriction géographique pour ce serveur et dans la période de droits");104 vodSource("nasa", "NASA — bibliothèque vidéo", "videos", "movie", "Médias de la NASA, généralement libres de droits ; fichiers MP4 lus directement sur les serveurs de la NASA");105}106107export interface ArteResult { listed: number; films: number; series: number; episodes: number; refused: Record<string, number>; expired: number; errors: number }108export async function importArte(): Promise<ArteResult> {109 const res: ArteResult = { listed: 0, films: 0, series: 0, episodes: 0, refused: {}, expired: 0, errors: 0 };110 const started = now();111 const list = await arte.listing(); res.listed = list.length;112 const refuse = (r: string) => { res.refused[r] = (res.refused[r] ?? 0) + 1; };113 const until = (t: number | null) => (t ? ` ; disponible jusqu'au ${new Intl.DateTimeFormat("fr-CA", { dateStyle: "long", timeZone: "America/Toronto" }).format(t)}` : "");114 const common = (it: arte.ArteItem, section: string, documentary: boolean, p: arte.ArtePlay | null, kind: "movie" | "series") => ({115 kind, origin: "arte", originId: it.programId, title: kind === "movie" && it.subtitle ? `${it.title} — ${it.subtitle}` : it.title, year: null,116 synopsis: it.shortDescription || p?.description || it.teaserText || null, synopsisSource: "ARTE", genres: [...new Set([...(documentary ? ["Documentaire"] : []), section])], directors: [] as string[], cast: [] as string[], languages: ["fr"], countries: [] as string[],117 poster: arte.img(it.mainImage?.url, "940x530") ?? p?.image ?? null, backdrop: arte.img(it.mainImage?.url, "1920x1080") ?? p?.image ?? null, documentary, animation: false,118 rightsStatus: "official", rightsBasis: `Diffusion gratuite par ARTE dans son lecteur officiel intégré (intégration autorisée par la configuration du lecteur)${until(p?.end ?? null)}`, rightsUrl: "https://www.arte.tv/sites/corporate/fr/conditions-generales-dutilisation/",119 sourceName: "ARTE", sourceUrl: it.url,120 });121 for (const { item, section, documentary } of list) {122 try {123 const code = item.kind?.code;124 if (code === "BONUS" || code === "TOPIC" || code === "MAGAZINE" && !item.kind?.isCollection && !item.duration) { refuse("extrait, dossier ou magazine sans vidéo"); continue; }125 if (item.kind?.isCollection || code === "TV_SERIES") {126 const eps = (await arte.collection(item.programId)).slice(0, 60); const ok: { e: arte.ArteItem; p: arte.ArtePlay }[] = [];127 for (const e of eps) { const p = await arte.playable(e.programId); if (p.ok) ok.push({ e, p }); else refuse(p.reason ?? "indisponible"); }128 if (ok.length < 2) { if (ok.length === 1) refuse("collection à un seul épisode disponible"); continue; }129 const end = Math.min(...ok.map((x) => x.p.end ?? Infinity));130 const t = upsertTitle({ ...common(item, section, documentary, ok[0].p, "series"), runtime: null, short: false });131 run("UPDATE titles SET expires_at = ? WHERE id = ?", Number.isFinite(end) ? end : null, t.id);132 tx(() => {133 const keep: number[] = [];134 ok.forEach(({ e, p }, i) => {135 const eid = upsertEpisode(t.id, e.episodeInfo?.season || 1, e.episodeInfo?.episode || i + 1, e.subtitle || e.title, e.shortDescription || p.description || null);136 keep.push(eid); setMedia(t.id, eid, { kind: "embed", url: arte.embedUrl(e.programId), container: "embed", duration: e.duration ?? p.duration, playback: "embed" });137 });138 run(`DELETE FROM episodes WHERE title_id = ? AND id NOT IN (${keep.map(() => "?").join(",")})`, t.id, ...keep);139 });140 res.series++; res.episodes += ok.length; continue;141 }142 const p = await arte.playable(item.programId);143 if (!p.ok) { refuse(p.reason ?? "indisponible"); continue; }144 const seconds = item.duration ?? p.duration;145 const t = upsertTitle({ ...common(item, section, documentary, p, "movie"), runtime: seconds, short: !!seconds && seconds < 40 * 60 });146 run("UPDATE titles SET expires_at = ? WHERE id = ?", p.end, t.id);147 setMedia(t.id, null, { kind: "embed", url: arte.embedUrl(item.programId), container: "embed", duration: seconds, playback: "embed" });148 res.films++;149 } catch { res.errors++; }150 }151 // Out of ARTE's current offer, or past its end date: no longer listed.152 if (res.films + res.series > 0) res.expired = Number(run("UPDATE titles SET active = 0 WHERE origin = 'arte' AND active = 1 AND (updated_at < ? OR (expires_at IS NOT NULL AND expires_at < ?))", started, now()).changes);153 return res;154}155/** Hourly safety net between two imports: a title whose rights ended stops being offered. */156export function expireTitles(): number {157 const n = Number(run("UPDATE titles SET active = 0 WHERE active = 1 AND expires_at IS NOT NULL AND expires_at < ?", now()).changes);158 if (n) bumpTitles(); return n;159}160161export interface NasaResult { found: number; added: number; skipped: number; errors: number; total: number }162export async function importNasa(budget = 500): Promise<NasaResult> {163 const res: NasaResult = { found: 0, added: 0, skipped: 0, errors: 0, total: 0 };164 const known = new Set(all<{ origin_id: string }>("SELECT origin_id FROM titles WHERE origin = 'nasa'").map((r) => r.origin_id));165 const seen = new Set<string>();166 for (const [q, topic] of nasa.TOPICS) {167 if (res.added >= budget) break;168 for (const it of await nasa.search(q, topic)) {169 if (seen.has(it.id)) continue; seen.add(it.id); res.found++;170 if (known.has(it.id) || res.added >= budget) continue;171 if (/©|copyright|courtesy of/i.test(it.description ?? "")) { res.skipped++; continue; } // third-party material: left out172 try {173 const f = await nasa.files(it.assets); if (!f) { res.skipped++; continue; }174 const t = upsertTitle({175 kind: "movie", origin: "nasa", originId: it.id, title: it.title, year: it.year, synopsis: it.description, synopsisSource: "NASA", runtime: null, genres: ["Documentaire", "Espace", it.topic], directors: [], cast: [], languages: ["en"], countries: ["US"],176 poster: it.thumb, backdrop: it.thumb, documentary: true, animation: false, short: true, rightsStatus: "pd",177 rightsBasis: "Média de la NASA, généralement non soumis au droit d'auteur selon ses lignes directrices ; aucune mention de droits de tiers dans la notice", rightsUrl: "https://www.nasa.gov/nasa-brand-center/images-and-media/",178 sourceName: "NASA", sourceUrl: `https://images.nasa.gov/details/${encodeURIComponent(it.id)}`,179 });180 setMedia(t.id, null, { kind: "remote", url: f.video, container: "mp4", vcodec: "h264", height: f.height, subtitles: f.subtitles ? [{ label: "English", lang: "en", url: f.subtitles }] : [] });181 res.added++;182 } catch { res.errors++; }183 }184 }185 res.total = countActive("nasa");186 return res;187}188189const ticks = (t: number | undefined) => (t ? Math.round(t / 10_000_000) : null);190export async function importJellyfin(src: { id: number; name: string; config: string | null; secret: string | null }): Promise<{ movies: number; series: number; episodes: number }> {191 const cfg = JSON.parse(src.config ?? "{}") as jf.JellyfinConfig; const pw = unseal(src.secret); if (!pw) throw new Error("Identifiants illisibles (APP_SECRET modifié ?)");192 const s = await jf.login(cfg, pw); const origin = `jellyfin:${src.id}`; const started = now(); const res = { movies: 0, series: 0, episodes: 0 };193 const people = (i: jf.JfItem, type: string) => (i.People ?? []).filter((p) => p.Type === type).map((p) => p.Name);194 const media = (i: jf.JfItem) => {195 const m = i.MediaSources?.[0]; const v = m?.MediaStreams?.find((x) => x.Type === "Video"), a = m?.MediaStreams?.filter((x) => x.Type === "Audio") ?? [];196 return { kind: "remote" as const, url: `provider://jf/${src.id}/${i.Id}`, container: m?.Container?.split(",")[0] ?? null, vcodec: v?.Codec ?? null, acodec: a[0]?.Codec ?? null, width: v?.Width ?? null, height: v?.Height ?? null,197 duration: ticks(i.RunTimeTicks), size: m?.Size ?? null, playback: jf.directPlayable(i) ? "direct" : "transcode", audio: a.map((x, n) => ({ index: n, label: x.DisplayTitle ?? `Piste ${n + 1}`, lang: (x.Language ?? "und").slice(0, 2) })) };198 };199 const base = (i: jf.JfItem, kind: "movie" | "series") => ({200 kind, origin, originId: i.Id, title: i.Name, original: i.OriginalTitle && i.OriginalTitle !== i.Name ? i.OriginalTitle : null, year: i.ProductionYear ?? null, synopsis: i.Overview ?? null, synopsisSource: i.Overview ? "Jellyfin" : null,201 genres: i.Genres ?? [], directors: people(i, "Director"), cast: people(i, "Actor"), languages: [] as string[], countries: [] as string[], poster: i.ImageTags?.Primary ? jf.image(cfg, i.Id, "Primary", 500) : null,202 backdrop: i.BackdropImageTags?.length ? jf.image(cfg, i.Id, "Backdrop", 1280) : null, documentary: (i.Genres ?? []).some((g) => /documentar/i.test(g)), animation: (i.Genres ?? []).some((g) => /animation/i.test(g)),203 rightsStatus: "personal", rightsBasis: `Votre serveur Jellyfin « ${src.name} »`, sourceName: src.name, imdb: i.ProviderIds?.Imdb ?? null,204 });205 for (const m of await jf.items(cfg, s, "Movie")) {206 if (!m.MediaSources?.length) continue;207 const d = ticks(m.RunTimeTicks); const t = upsertTitle({ ...base(m, "movie"), runtime: d, short: !!d && d < 40 * 60 }); setMedia(t.id, null, media(m)); res.movies++;208 }209 for (const sr of await jf.items(cfg, s, "Series")) {210 const eps = (await jf.episodes(cfg, s, sr.Id)).filter((e) => e.MediaSources?.length); if (!eps.length) continue;211 const t = upsertTitle({ ...base(sr, "series"), runtime: null, short: false });212 tx(() => { const keep: number[] = []; eps.forEach((e, i) => { const eid = upsertEpisode(t.id, e.ParentIndexNumber ?? 1, e.IndexNumber ?? i + 1, e.Name, e.Overview ?? null, e.PremiereDate?.slice(0, 10) ?? null); keep.push(eid); setMedia(t.id, eid, media(e)); });213 run(`DELETE FROM episodes WHERE title_id = ? AND id NOT IN (${keep.map(() => "?").join(",")})`, t.id, ...keep); });214 res.series++; res.episodes += eps.length;215 }216 run("UPDATE titles SET active = 0 WHERE origin = ? AND active = 1 AND updated_at < ?", origin, started);217 return res;218}219220export async function importXtreamVod(src: { id: number; name: string; config: string | null; secret: string | null }): Promise<{ movies: number; series: number; episodes: number }> {221 const cfg = JSON.parse(src.config ?? "{}") as xt.XtreamConfig; const pw = unseal(src.secret); if (!pw) throw new Error("Identifiants illisibles (APP_SECRET modifié ?)");222 const origin = `xtream:${src.id}`; const started = now(); const res = { movies: 0, series: 0, episodes: 0 };223 const rights = { rightsStatus: "provider", rightsBasis: `Catalogue du fournisseur « ${src.name} », que vous avez déclaré être autorisé à utiliser`, sourceName: src.name } as const;224 const browser = (ext: string) => (["mp4", "m4v", "mov", "webm"].includes(ext) ? "direct" : "unsupported");225 for (const v of await xt.vodList(cfg, pw)) {226 const t = upsertTitle({ kind: "movie", origin, originId: `m${v.id}`, title: v.name.replace(/\s*\(\d{4}\)\s*$/, ""), year: v.year, synopsis: v.plot, synopsisSource: v.plot ? src.name : null, runtime: null, genres: v.category ? [v.category] : [], directors: [], cast: [], languages: [], countries: [],227 poster: v.icon, backdrop: null, documentary: /documenta/i.test(v.category ?? ""), animation: /anim/i.test(v.category ?? ""), short: false, ...rights });228 setMedia(t.id, null, { kind: "remote", url: `provider://xvod/${src.id}/movie/${v.id}.${v.ext}`, container: v.ext, playback: browser(v.ext) }); res.movies++;229 }230 // Episode lists cost one request per series: done for the series not yet known, a bounded number per run.231 let budget = 150;232 for (const sr of await xt.seriesList(cfg, pw)) {233 const prev = get<{ id: number }>("SELECT id FROM titles WHERE origin = ? AND origin_id = ?", origin, `s${sr.id}`);234 if (prev) { run("UPDATE titles SET active = 1, updated_at = ? WHERE id = ?", now(), prev.id); res.series++; continue; }235 if (budget-- <= 0) continue;236 const eps = await xt.seriesEpisodes(cfg, pw, sr.id).catch(() => []); if (!eps.length) continue;237 const t = upsertTitle({ kind: "series", origin, originId: `s${sr.id}`, title: sr.name, year: sr.year, synopsis: sr.plot, synopsisSource: sr.plot ? src.name : null, runtime: null, genres: sr.category ? [sr.category] : [], directors: [], cast: [], languages: [], countries: [],238 poster: sr.cover, backdrop: null, documentary: false, animation: false, short: false, ...rights });239 tx(() => eps.forEach((e) => { const eid = upsertEpisode(t.id, e.season, e.episode, e.title || null, e.plot); setMedia(t.id, eid, { kind: "remote", url: `provider://xvod/${src.id}/series/${e.id}.${e.ext}`, container: e.ext, duration: e.duration, playback: browser(e.ext) }); }));240 res.series++; res.episodes += eps.length;241 }242 run("UPDATE titles SET active = 0 WHERE origin = ? AND active = 1 AND updated_at < ?", origin, started);243 return res;244}245246export interface SourcesResult { arte?: ArteResult; nasa?: NasaResult; instances: { name: string; type: string; ok: boolean; detail: string }[]; errors: string[]; durationMs: number }247/** Daily refresh of the catalogues that move: ARTE (rotating rights), NASA (new videos), and the instances you added. */248export async function refreshSources(): Promise<SourcesResult> {249 const t0 = now(); const res: SourcesResult = { instances: [], errors: [], durationMs: 0 };250 ensureBuiltinVod();251 // Your own sources first (quick), then the public catalogues.252 for (const s of all<{ id: number; kind: string; name: string; ref: string; config: string | null; secret: string | null }>("SELECT id, kind, name, ref, config, secret FROM vod_sources WHERE enabled = 1 AND kind IN ('jellyfin','xtream')")) {253 try {254 const r = s.kind === "jellyfin" ? await importJellyfin(s) : await importXtreamVod(s);255 vodDone(s.kind, s.ref, r.movies + r.series); res.instances.push({ name: s.name, type: s.kind, ok: true, detail: `${r.movies} films, ${r.series} séries, ${r.episodes} épisodes` });256 } catch (e) { const msg = redact((e as Error).message, unseal(s.secret)); vodDone(s.kind, s.ref, 0, msg); res.instances.push({ name: s.name, type: s.kind, ok: false, detail: msg }); }257 }258 bumpTitles();259 if (vodEnabled("arte")) try { res.arte = await importArte(); vodDone("arte", "fr", countActive("arte")); } catch (e) { res.errors.push(`ARTE : ${(e as Error).message}`); vodDone("arte", "fr", countActive("arte"), (e as Error).message); }260 if (vodEnabled("nasa")) try { res.nasa = await importNasa(); vodDone("nasa", "videos", res.nasa.total); } catch (e) { res.errors.push(`NASA : ${(e as Error).message}`); vodDone("nasa", "videos", countActive("nasa"), (e as Error).message); }261 expireTitles(); bumpTitles();262 res.durationMs = now() - t0;263 log(res.errors.length || res.instances.some((i) => !i.ok) ? "warn" : "info", "sources", `Sources actualisées : ARTE ${res.arte ? res.arte.films + res.arte.series : "—"} titres, NASA ${res.nasa?.total ?? "—"} vidéos`, res);264 return res;265}266267// --- administration façade ------------------------------------------------------------------------------------------------268export interface Instance { uid: string; type: string; family: Family; name: string; detail: string; enabled: boolean; builtin: boolean; status: string | null; error: string | null; lastSyncAt: number | null; priority?: number;269 metrics: { label: string; value: number }[] }270271export function instances(): Instance[] {272 const out: Instance[] = [];273 const live = all<{ id: number; kind: string; name: string; url: string; enabled: number; builtin: number; last_status: string | null; last_error: string | null; last_sync_at: number | null; priority: number; stream_count: number; config: string | null }>("SELECT * FROM sources ORDER BY priority, builtin DESC, id");274 // What a source really brings: streams kept after de-duplication, channels, channels only it provides, and how many of those answered the last test.275 const m = new Map(all<{ source_id: number; streams: number; up: number; channels: number }>("SELECT source_id, COUNT(*) streams, SUM(status = 'up') up, COUNT(DISTINCT channel_id) channels FROM streams GROUP BY source_id").map((r) => [r.source_id, r]));276 const uniq = new Map(all<{ source_id: number; n: number; playable: number }>(`SELECT source_id, COUNT(*) n, SUM(up > 0) playable FROM (SELECT channel_id, MIN(source_id) source_id, COUNT(DISTINCT source_id) k, SUM(status = 'up') up FROM streams GROUP BY channel_id HAVING k = 1) GROUP BY source_id`).map((r) => [r.source_id, r]));277 for (const s of live) {278 const type = s.kind === "xtream" ? "xtream" : s.kind === "loop" ? "loop" : s.kind === "file" ? "m3u-file" : s.kind === "stream" ? "stream" : "m3u-url";279 out.push({ uid: `live:${s.id}`, type, family: "live", name: s.name, detail: s.kind === "xtream" ? "identifiants chiffrés" : s.kind === "loop" ? `${((JSON.parse(s.config ?? "{}") as { mediaIds?: number[] }).mediaIds ?? []).length} fichier(s) de votre bibliothèque, par le relais` : s.kind === "file" ? "fichier importé" : s.url, enabled: !!s.enabled, builtin: !!s.builtin, status: s.last_status, error: s.last_error, lastSyncAt: s.last_sync_at, priority: s.priority,280 metrics: [{ label: "entrées", value: s.stream_count }, { label: "flux retenus", value: m.get(s.id)?.streams ?? 0 }, { label: "chaînes", value: m.get(s.id)?.channels ?? 0 }, { label: "chaînes uniques à cette source", value: uniq.get(s.id)?.n ?? 0 }, { label: "dont lisibles au dernier test", value: uniq.get(s.id)?.playable ?? 0 }] });281 }282 for (const e of all<{ id: number; name: string; url: string; enabled: number; builtin: number; last_status: string | null; last_error: string | null; last_sync_at: number | null; program_count: number }>("SELECT * FROM epg_sources ORDER BY id"))283 out.push({ uid: `guide:${e.id}`, type: "xmltv", family: "guide", name: e.name, detail: isProvider(e.url) ? "guide du fournisseur" : e.url, enabled: !!e.enabled, builtin: !!e.builtin, status: e.last_status, error: e.last_error, lastSyncAt: e.last_sync_at, metrics: [{ label: "programmes", value: e.program_count }] });284 for (const v of all<{ id: number; kind: string; name: string; ref: string; enabled: number; builtin: number; last_status: string | null; last_error: string | null; last_sync_at: number | null; rights_note: string | null }>("SELECT * FROM vod_sources ORDER BY builtin DESC, id")) {285 const origin = v.kind === "jellyfin" || v.kind === "xtream" ? `${v.kind}:${v.id}` : v.kind;286 const c = get<{ movies: number; series: number }>("SELECT SUM(kind = 'movie') movies, SUM(kind = 'series') series FROM titles t WHERE origin = ? AND active = 1 AND EXISTS(SELECT 1 FROM media m WHERE m.title_id = t.id)" + (v.kind === "wikidata" ? ` AND kind = '${v.ref === "series" ? "series" : "movie"}'` : ""), origin);287 out.push({ uid: `vod:${v.id}`, type: v.kind, family: "vod", name: v.name, detail: v.rights_note ?? (v.kind === "jellyfin" || v.kind === "xtream" ? "identifiants chiffrés" : ""), enabled: !!v.enabled, builtin: !!v.builtin, status: v.last_status, error: v.last_error, lastSyncAt: v.last_sync_at,288 metrics: [{ label: "films", value: c?.movies ?? 0 }, { label: "séries", value: c?.series ?? 0 }] });289 }290 for (const d of all<{ id: number; label: string; path: string; enabled: number; last_status: string | null; last_error: string | null; last_scan_at: number | null; file_count: number }>("SELECT * FROM library_dirs ORDER BY id"))291 out.push({ uid: `library:${d.id}`, type: "folder", family: "library", name: d.label, detail: d.path, enabled: !!d.enabled, builtin: false, status: d.last_status, error: d.last_error, lastSyncAt: d.last_scan_at, metrics: [{ label: "fichiers", value: d.file_count }] });292 return out;293}294295export class EngineError extends Error {}296const normalizeKey = (s: string) => s.normalize("NFKD").replace(/[\u0300-\u036f]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");297/** Media files of the personal library whose title matches, in play order (episodes by season and number). */298export function loopMedia(match: string): { id: number; title: string; label: string }[] {299 const q = match.trim(); if (q.length < 2) return [];300 const t = get<{ id: number; title: string }>("SELECT id, title FROM titles WHERE library_dir IS NOT NULL AND active = 1 AND title = ? COLLATE NOCASE ORDER BY id LIMIT 1", q)301 ?? get<{ id: number; title: string }>("SELECT id, title FROM titles WHERE library_dir IS NOT NULL AND active = 1 AND title LIKE ? ORDER BY LENGTH(title), id LIMIT 1", `%${q.replace(/[%_]/g, "")}%`);302 if (!t) return [];303 return all<{ id: number; path: string; season: number | null; episode: number | null }>("SELECT m.id, m.path, e.season, e.episode FROM media m LEFT JOIN episodes e ON e.id = m.episode_id WHERE m.title_id = ? AND m.path IS NOT NULL ORDER BY e.season, e.episode, m.id LIMIT 200", t.id)304 .map((m) => ({ id: m.id, title: t.title, label: m.season !== null ? `S${m.season}E${m.episode}` : m.path.split("/").pop() ?? "" }));305}306const publicUrl = (raw: string) => { try { return assertPublicUrl(raw.trim()).toString(); } catch (e) { throw new EngineError(e instanceof UrlRejected ? e.message : "Adresse invalide"); } };307const anyUrl = (raw: string) => { let u: URL; try { u = new URL(raw.trim()); } catch { throw new EngineError("Adresse invalide"); } if (u.protocol !== "http:" && u.protocol !== "https:") throw new EngineError("Adresse http(s) attendue"); return u.toString().replace(/\/+$/, ""); };308309/** Checks a configuration without saving anything. */310export async function test(type: string, f: Record<string, string>): Promise<{ ok: boolean; message: string; details?: Record<string, unknown> }> {311 try {312 if (type === "m3u-url") { const p = parseM3u(await fetchText(publicUrl(f.url), 60 * 1024 * 1024, 60_000)); return p.entries.length ? { ok: true, message: `${p.entries.length} entrées lues`, details: { exemples: p.entries.slice(0, 5).map((e) => e.name), avertissements: p.errors.length } } : { ok: false, message: p.errors[0] ?? "Aucune entrée #EXTINF" }; }313 if (type === "m3u-file") { const p = parseM3u(f.content ?? ""); return p.entries.length ? { ok: true, message: `${p.entries.length} entrées lues`, details: { exemples: p.entries.slice(0, 5).map((e) => e.name) } } : { ok: false, message: p.errors[0] ?? "Aucune entrée #EXTINF" }; }314 if (type === "loop") { const m = loopMedia(f.match ?? ""); return m.length ? { ok: true, message: `${m.length} fichier(s) trouvé(s) pour « ${m[0].title} »`, details: { fichiers: m.slice(0, 6).map((x) => x.label) } } : { ok: false, message: "Aucun film ou série de votre bibliothèque personnelle ne porte ce titre" }; }315 if (type === "stream") { const r = await probeStream({ id: 0, url: publicUrl(f.url), referrer: null, user_agent: null }, 12_000); return { ok: r.status === "up", message: r.status === "up" ? `Le flux répond (${r.contentType ?? "type inconnu"})${r.cors ? ", lisible dans tous les navigateurs (CORS)" : ", sans en-tête CORS : lecteur natif de Safari seulement"}` : r.error ?? "Le flux ne répond pas" }; }316 if (type === "xmltv") { const head = (await fetchText(publicUrl(f.url), 300 * 1024 * 1024, 90_000).catch((e) => { if (/gz|trop volumineuse/i.test((e as Error).message)) return "<tv"; throw e; })).slice(0, 4000); return /<tv[\s>]/.test(head) || f.url.endsWith(".gz") ? { ok: true, message: "Guide XMLTV accessible" } : { ok: false, message: "Ce fichier ne ressemble pas à un guide XMLTV" }; }317 if (type === "xtream") { const a = await xt.account({ server: publicUrl(f.server).replace(/\/+$/, ""), username: f.username }, f.password); return { ok: a.ok, message: a.message, details: a.ok ? { statut: a.status, expiration: a.expires ? new Date(a.expires).toISOString().slice(0, 10) : "non indiquée", connexions: a.maxConnections } : undefined }; }318 if (type === "jellyfin") { const cfg = { server: anyUrl(f.server), username: f.username }; const info = await jf.serverInfo(cfg); jf.forget(cfg); await jf.login(cfg, f.password); return { ok: true, message: `Connecté à « ${info.name} » (Jellyfin ${info.version})` }; }319 return { ok: false, message: "Ce type de source ne se teste pas" };320 } catch (e) { return { ok: false, message: redact((e as Error).message, f.password) }; }321}322323/** Adds a source from the administration. Returns the unified identifier of what was created. */324export function create(type: string, f: Record<string, string>): string {325 const def = TYPES.find((t) => t.type === type && !t.builtin && t.type !== "folder"); if (!def) throw new EngineError("Type de source inconnu");326 for (const fd of def.fields) if (fd.required && !f[fd.key]?.trim()) throw new EngineError(`Champ requis : ${fd.label}`);327 const name = f.name.trim().slice(0, 80); const country = /^[A-Za-z]{2}$/.test(f.country ?? "") ? f.country.toUpperCase() : null;328 const dup = (sql: string, v: string) => { if (get(sql, v)) throw new EngineError("Cette source existe déjà"); };329 if (type === "m3u-url") { const url = publicUrl(f.url); dup("SELECT 1 FROM sources WHERE url = ?", url); return `live:${run("INSERT INTO sources(kind, name, url, country, created_at) VALUES('m3u', ?, ?, ?, ?)", name, url, country, now()).lastInsertRowid}`; }330 if (type === "m3u-file") {331 const p = parseM3u(f.content ?? ""); if (!p.entries.length) throw new EngineError(p.errors[0] ?? "Aucune entrée #EXTINF dans ce fichier");332 if (f.content.length > 20 * 1024 * 1024) throw new EngineError("Fichier trop volumineux (20 Mo au plus)");333 return `live:${run("INSERT INTO sources(kind, name, url, country, content, created_at) VALUES('file', ?, ?, ?, ?, ?)", name, `upload:${now().toString(36)}`, country, f.content, now()).lastInsertRowid}`;334 }335 if (type === "loop") {336 const m = loopMedia(f.match ?? ""); if (!m.length) throw new EngineError("Aucun film ou série de votre bibliothèque personnelle ne porte ce titre");337 const key = `loop:${normalizeKey(name)}`; dup("SELECT 1 FROM sources WHERE url = ?", key);338 // Own files: the relay is what plays them, so the authorisation is recorded with the source.339 const relay = JSON.stringify({ allowed: true, basis: "Fichiers de votre bibliothèque personnelle (déclaration faite à la création de la chaîne)", share: true, record: true, maxConnections: null, declaredAt: now() });340 return `live:${run("INSERT INTO sources(kind, name, url, country, config, relay, priority, created_at) VALUES('loop', ?, ?, 'CA', ?, ?, 10, ?)", name, key, JSON.stringify({ mediaIds: m.map((x) => x.id), title: m[0].title, logo: f.logo ? publicUrl(f.logo) : null }), relay, now()).lastInsertRowid}`;341 }342 if (type === "stream") { const url = publicUrl(f.url); dup("SELECT 1 FROM sources WHERE url = ?", url); return `live:${run("INSERT INTO sources(kind, name, url, country, config, created_at) VALUES('stream', ?, ?, ?, ?, ?)", name, url, country, JSON.stringify({ logo: f.logo ? publicUrl(f.logo) : null }), now()).lastInsertRowid}`; }343 if (type === "xmltv") { const url = publicUrl(f.url); dup("SELECT 1 FROM epg_sources WHERE url = ?", url); return `guide:${run("INSERT INTO epg_sources(name, url, created_at) VALUES(?,?,?)", name, url, now()).lastInsertRowid}`; }344 if (type === "xtream") {345 const cfg = JSON.stringify({ server: publicUrl(f.server).replace(/\/+$/, ""), username: f.username.trim() }); const key = `xtream:${cfg}`; dup("SELECT 1 FROM sources WHERE url = ?", key);346 return tx(() => {347 const id = Number(run("INSERT INTO sources(kind, name, url, config, secret, created_at) VALUES('xtream', ?, ?, ?, ?, ?)", name, key, cfg, seal(f.password), now()).lastInsertRowid);348 // The same subscription also feeds the film / series catalogue and the guide.349 run("INSERT INTO vod_sources(kind, name, ref, as_kind, rights_note, config, secret, created_at) VALUES('xtream', ?, ?, 'movie', 'Catalogue de votre fournisseur', ?, ?, ?)", name, `live:${id}`, cfg, seal(f.password), now());350 run("INSERT INTO epg_sources(name, url, created_at) VALUES(?, ?, ?)", `Guide — ${name}`, `provider://live/${id}/xmltv`, now());351 return `live:${id}`;352 });353 }354 const cfg = JSON.stringify({ server: anyUrl(f.server), username: f.username.trim(), publicUrl: f.publicUrl?.trim() ? anyUrl(f.publicUrl) : null }); dup("SELECT 1 FROM vod_sources WHERE kind = 'jellyfin' AND ref = ?", cfg);355 return `vod:${run("INSERT INTO vod_sources(kind, name, ref, as_kind, config, secret, created_at) VALUES('jellyfin', ?, ?, 'movie', ?, ?, ?)", name, cfg, cfg, seal(f.password), now()).lastInsertRowid}`;356}357358export function setEnabled(uid: string, on: boolean) { const [fam, id] = uid.split(":"); const table = ({ live: "sources", guide: "epg_sources", vod: "vod_sources", library: "library_dirs" } as Record<string, string>)[fam]; if (!table) throw new EngineError("Source inconnue"); run(`UPDATE ${table} SET enabled = ? WHERE id = ?`, Number(on), Number(id)); }359export function setPriority(uid: string, priority: number) { const [fam, id] = uid.split(":"); if (fam !== "live") throw new EngineError("La priorité concerne les sources de chaînes"); run("UPDATE sources SET priority = ? WHERE id = ?", Math.max(1, Math.min(999, Math.round(priority))), Number(id)); bumpCatalog(); }360export function remove(uid: string) {361 const [fam, idS] = uid.split(":"); const id = Number(idS);362 if (fam === "live") {363 const s = get<{ builtin: number; kind: string }>("SELECT builtin, kind FROM sources WHERE id = ?", id); if (!s) throw new EngineError("Source inconnue"); if (s.builtin) throw new EngineError("Une source intégrée se désactive, elle ne se supprime pas");364 tx(() => {365 if (s.kind === "xtream") { for (const v of all<{ id: number }>("SELECT id FROM vod_sources WHERE kind = 'xtream' AND ref = ?", `live:${id}`)) { run("DELETE FROM titles WHERE origin = ?", `xtream:${v.id}`); run("DELETE FROM vod_sources WHERE id = ?", v.id); } run("DELETE FROM epg_sources WHERE url = ?", `provider://live/${id}/xmltv`); }366 run("DELETE FROM sources WHERE id = ?", id); run("UPDATE channels SET active = EXISTS(SELECT 1 FROM streams s WHERE s.channel_id = channels.id)");367 });368 bumpCatalog(); bumpTitles();369 } else if (fam === "guide") { const e = get<{ builtin: number }>("SELECT builtin FROM epg_sources WHERE id = ?", id); if (e?.builtin) throw new EngineError("Une source intégrée se désactive, elle ne se supprime pas"); run("DELETE FROM programs WHERE origin = ?", `xmltv:${id}`); run("DELETE FROM epg_sources WHERE id = ?", id); }370 else if (fam === "vod") { const v = get<{ builtin: number; kind: string }>("SELECT builtin, kind FROM vod_sources WHERE id = ?", id); if (!v) throw new EngineError("Source inconnue"); if (v.builtin) throw new EngineError("Une source intégrée se désactive, elle ne se supprime pas"); run("DELETE FROM titles WHERE origin = ?", `${v.kind}:${id}`); run("DELETE FROM vod_sources WHERE id = ?", id); bumpTitles(); }371 else throw new EngineError("Les dossiers se gèrent dans l'onglet Bibliothèque");372}373