SPB Git

Repository Public

catalog.ts 16.3 KB 246 lines typescript

1import { all, get, getSetting, now, run, tx } from "./db";2import { eligibility } from "./relay/access";3import { LOOP_REF } from "./relay/sources";4import { sign } from "./auth";56export type Status = "up" | "down" | "unknown";7export interface Channel {8  id: string; name: string; country: string | null; categories: string[]; languages: string[]; logo: string | null;9  status: Status; streams: number; web: boolean; hasGuide: boolean; firstSeen: number;10  fav?: boolean; hidden?: boolean;11}12interface Indexed extends Channel { norm: string; words: string[]; alt: string[] }1314export const normalize = (s: string) => s.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();1516type G = typeof globalThis & { __sptvIndex?: { version: string; checkedAt: number; list: Indexed[]; byId: Map<string, Indexed> } };1718function build(version: string) {19  const rows = all<{ id: string; name: string; alt_names: string; country: string | null; categories: string; languages: string; logo: string | null; has_guide: number; first_seen: number; n: number; up: number; down: number; web: number }>(`20    SELECT c.id, c.name, c.alt_names, c.country, c.categories, c.languages, c.logo, c.has_guide, c.first_seen,21      COUNT(s.id) n, SUM(s.status = 'up') up, SUM(s.status = 'down') down,22      SUM(s.url LIKE 'https:%' AND s.referrer IS NULL AND s.user_agent IS NULL AND s.status != 'down') web23    FROM channels c JOIN streams s ON s.channel_id = c.id WHERE c.active = 1 GROUP BY c.id`);24  const list: Indexed[] = rows.map((r) => {25    const norm = normalize(r.name);26    return {27      id: r.id, name: r.name, country: r.country, categories: r.categories ? r.categories.split(";") : [], languages: r.languages ? r.languages.split(";") : [],28      logo: r.logo, status: r.up > 0 ? "up" : r.down === r.n ? "down" : "unknown", streams: r.n, web: r.web > 0, hasGuide: !!r.has_guide, firstSeen: r.first_seen,29      norm, words: norm.split(" "), alt: r.alt_names ? r.alt_names.split("|").map(normalize).filter(Boolean) : [],30    };31  });32  list.sort((a, b) => a.name.localeCompare(b.name, "fr"));33  return { version, checkedAt: now(), list, byId: new Map(list.map((c) => [c.id, c])) };34}3536/** In-memory catalogue shared by all requests; rebuilt when the worker publishes a new catalogue version (checked at most every 3 s). */37export function index() {38  const g = globalThis as G;39  const cur = g.__sptvIndex;40  if (cur && now() - cur.checkedAt < 3000) return cur;41  const version = getSetting("catalog_version") ?? "0";42  if (cur && cur.version === version) { cur.checkedAt = now(); return cur; }43  return (g.__sptvIndex = build(version));44}45export function resetIndex() { (globalThis as G).__sptvIndex = undefined; }4647// Bounded Damerau–Levenshtein: returns max+1 as soon as the distance exceeds `max`.48export function editDistance(a: string, b: string, max: number): number {49  if (Math.abs(a.length - b.length) > max) return max + 1;50  let prev2: number[] = [], prev = Array.from({ length: b.length + 1 }, (_, i) => i);51  for (let i = 1; i <= a.length; i++) {52    const cur = [i]; let rowMin = i;53    for (let j = 1; j <= b.length; j++) {54      const cost = a[i - 1] === b[j - 1] ? 0 : 1;55      let v = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + cost);56      if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) v = Math.min(v, prev2[j - 2] + 1);57      cur[j] = v; if (v < rowMin) rowMin = v;58    }59    if (rowMin > max) return max + 1;60    prev2 = prev; prev = cur;61  }62  return prev[b.length];63}6465const tolerance = (len: number) => (len <= 3 ? 0 : len <= 5 ? 1 : len <= 9 ? 2 : 3);6667/** Score of one query token against a channel: exact/prefix/substring first, then typo-tolerant match on each word (and its prefix). 0 = no match. */68export function tokenScore(tok: string, c: { words: string[]; norm: string; alt: string[] }): number {69  let best = 0;70  for (const w of c.words) {71    if (w === tok) return 100;72    if (w.startsWith(tok)) best = Math.max(best, 85);73    else if (w.includes(tok)) best = Math.max(best, 65);74  }75  if (best) return best;76  if (c.norm.replace(/ /g, "").includes(tok)) return 60;77  for (const a of c.alt) if (a.includes(tok)) return 55;78  const max = tolerance(tok.length);79  if (!max) return 0;80  for (const w of c.words) {81    const d = Math.min(editDistance(tok, w, max), w.length > tok.length ? editDistance(tok, w.slice(0, tok.length), max) + 0.5 : 99);82    if (d <= max) best = Math.max(best, 50 - d * 10);83  }84  return best;85}8687export interface Query {88  q?: string; country?: string; category?: string; language?: string; status?: Status; fav?: boolean; list?: number;89  hidden?: "only" | "include"; guide?: boolean; web?: boolean; recent?: boolean; sort?: "name" | "relevance" | "recent" | "priority";90  offset?: number; limit?: number;91}9293export function userSets() {94  return {95    fav: new Set(all<{ channel_id: string }>("SELECT DISTINCT channel_id FROM list_items").map((r) => r.channel_id)),96    hidden: new Set(all<{ channel_id: string }>("SELECT channel_id FROM hidden").map((r) => r.channel_id)),97  };98}99const strip = (c: Indexed, sets: ReturnType<typeof userSets>): Channel => ({100  id: c.id, name: c.name, country: c.country, categories: c.categories, languages: c.languages, logo: c.logo, status: c.status, streams: c.streams,101  web: c.web, hasGuide: c.hasGuide, firstSeen: c.firstSeen, fav: sets.fav.has(c.id), hidden: sets.hidden.has(c.id) || undefined,102});103const homeRank = (c: Channel) => (c.country === "CA" ? (c.languages.includes("fra") ? 0 : 1) : c.languages.includes("fra") ? 2 : 3);104const statusRank = (c: Channel) => (c.status === "up" ? (c.web ? 0 : 1) : c.status === "unknown" ? 2 : 3);105106export function search(query: Query): { total: number; items: Channel[]; tookMs: number } {107  const t0 = performance.now();108  const { list } = index();109  const sets = userSets();110  const listIds = query.list ? new Set(all<{ channel_id: string }>("SELECT channel_id FROM list_items WHERE list_id = ?", query.list).map((r) => r.channel_id)) : null;111  const initial = query.recent ? Number(getSetting("initial_sync_at") ?? 0) : 0;112  const toks = query.q ? normalize(query.q).split(" ").filter(Boolean) : [];113  const hits: { c: Indexed; score: number }[] = [];114  for (const c of list) {115    const isHidden = sets.hidden.has(c.id);116    if (query.hidden === "only" ? !isHidden : isHidden && query.hidden !== "include") continue;117    if (query.country && c.country !== query.country) continue;118    if (query.category && !c.categories.includes(query.category)) continue;119    if (query.language && !c.languages.includes(query.language)) continue;120    if (query.status && c.status !== query.status) continue;121    if (query.fav && !sets.fav.has(c.id)) continue;122    if (listIds && !listIds.has(c.id)) continue;123    if (query.guide && !c.hasGuide) continue;124    if (query.web && !c.web) continue;125    if (query.recent && !(initial && c.firstSeen > initial)) continue;126    let score = 0;127    if (toks.length) {128      let ok = true;129      for (const t of toks) { const s = tokenScore(t, c); if (!s) { ok = false; break; } score += s; }130      if (!ok) continue;131      score = score / toks.length + (c.norm === toks.join(" ") ? 30 : c.norm.startsWith(toks.join(" ")) ? 10 : 0);132    }133    hits.push({ c, score });134  }135  // Default order of the live catalogue: Canadian French-language channels, then Canadian, then French-language, then the rest.136  const sort = query.sort ?? (toks.length ? "relevance" : query.country || query.language ? "name" : "priority");137  if (sort === "priority") hits.sort((a, b) => homeRank(a.c) - homeRank(b.c) || a.c.name.localeCompare(b.c.name, "fr"));138  if (sort === "relevance") hits.sort((a, b) => b.score - a.score || statusRank(a.c) - statusRank(b.c) || a.c.name.localeCompare(b.c.name, "fr"));139  else if (sort === "recent") hits.sort((a, b) => b.c.firstSeen - a.c.firstSeen || a.c.name.localeCompare(b.c.name, "fr"));140  const offset = Math.max(0, query.offset ?? 0), limit = Math.min(200, Math.max(1, query.limit ?? 60));141  return { total: hits.length, items: hits.slice(offset, offset + limit).map((h) => strip(h.c, sets)), tookMs: Math.round((performance.now() - t0) * 10) / 10 };142}143144export function channelsByIds(ids: string[]): Channel[] {145  const { byId } = index(); const sets = userSets();146  return ids.map((id) => byId.get(id)).filter((c): c is Indexed => !!c).map((c) => strip(c, sets));147}148149/** Home rows: verified and browser-playable channels first, never a hidden channel. */150export function row(filter: (c: Channel) => boolean, limit = 24, prefer?: (c: Channel) => number): Channel[] {151  const { list } = index(); const sets = userSets();152  return list.filter((c) => !sets.hidden.has(c.id) && c.status !== "down" && filter(c))153    .sort((a, b) => statusRank(a) - statusRank(b) || (prefer ? prefer(a) - prefer(b) : 0) || Number(!a.logo) - Number(!b.logo) || a.name.localeCompare(b.name, "fr"))154    .slice(0, limit).map((c) => strip(c, sets));155}156157export interface StreamInfo {158  id: number; url: string; quality: string | null; labels: string[]; feed: string | null; status: Status; httpStatus: number | null; checkError: string | null;159  checkedAt: number | null; cors: boolean | null; contentType: string | null; insecure: boolean; needsHeaders: boolean; kind: "hls" | "dash" | "other"; playOk: boolean | null; playAt: number | null; source: string;160  /** Served through the relay engine (source authorised for it); `relayOnly` when there is no other way to play it. */161  relay: boolean; relayOnly: boolean;162}163export function streamKind(url: string, contentType: string | null): StreamInfo["kind"] {164  let p = url; try { p = new URL(url).pathname; } catch { /* keep raw */ }165  if (/\.m3u8?$/i.test(p) || /mpegurl/i.test(contentType ?? "")) return "hls";166  if (/\.mpd$/i.test(p) || /dash\+xml/i.test(contentType ?? "")) return "dash";167  return "other";168}169/** Streams of a channel in playback order: verified, HTTPS, no special headers, CORS-enabled, best quality first. */170export function channelStreams(channelId: string): StreamInfo[] {171  const rows = all<{ id: number; url: string; quality: string | null; labels: string; feed: string | null; status: Status; http_status: number | null; check_error: string | null; checked_at: number | null; cors: number | null; content_type: string | null; referrer: string | null; user_agent: string | null; play_ok: number | null; play_at: number | null; source: string; priority: number; relay_policy: string | null; src_enabled: number }>(172    `SELECT s.*, src.name source, src.priority priority, src.relay relay_policy, src.enabled src_enabled FROM streams s JOIN sources src ON src.id = s.source_id WHERE s.channel_id = ?`, channelId);173  const q = (s: string | null) => parseInt(s ?? "0", 10) || 0;174  const prio = new Map(rows.map((r) => [r.id, r.priority]));175  return rows.map((r): StreamInfo => ({176    relay: eligibility({ url: r.url, referrer: r.referrer, user_agent: r.user_agent, enabled: r.src_enabled, relay: r.relay_policy }).ok, relayOnly: LOOP_REF.test(r.url),177    // A provider stream reaches the browser as a redirect issued by this server, never as stored data.178    id: r.id, url: LOOP_REF.test(r.url) ? "" : r.url.startsWith("provider://") ? `/api/provider/stream/${r.id}?t=${encodeURIComponent(sign(`s:${r.id}`, 12 * 3600))}` : r.url, quality: r.quality, labels: r.labels ? r.labels.split(";") : [], feed: r.feed, status: r.status, httpStatus: r.http_status, checkError: r.check_error,179    checkedAt: r.checked_at, cors: r.cors === null ? null : !!r.cors, contentType: r.content_type, insecure: r.url.startsWith("http:"), needsHeaders: !!(r.referrer || r.user_agent),180    kind: r.url.startsWith("provider://") ? "hls" : streamKind(r.url, r.content_type), playOk: r.play_ok === null ? null : !!r.play_ok, playAt: r.play_at, source: r.source,181  })).sort((a, b) => {182    const rank = (s: StreamInfo) => (s.playOk ? -4 : 0) + (s.status === "up" ? 0 : s.status === "unknown" ? 4 : 8) + (s.insecure ? 2 : 0) + (s.needsHeaders ? 2 : 0) + (s.cors === false ? 1 : 0) + (s.kind === "hls" ? 0 : 1);183    // Same availability: the source priority chosen in the administration decides, then the technical criteria.184    const cls = (s: StreamInfo) => (s.playOk ? 0 : s.status === "up" ? 1 : s.status === "unknown" ? 2 : 3);185    return cls(a) - cls(b) || (prio.get(a.id) ?? 100) - (prio.get(b.id) ?? 100) || rank(a) - rank(b) || q(b.quality) - q(a.quality);186  });187}188189// --- user state -----------------------------------------------------------------------------------------------------190export function lists() {191  return all<{ id: number; name: string; position: number; count: number }>(192    "SELECT l.id, l.name, l.position, (SELECT COUNT(*) FROM list_items i WHERE i.list_id = l.id) count FROM lists l ORDER BY l.position, l.id");193}194export function listChannels(listId: number): Channel[] {195  const ids = all<{ channel_id: string }>("SELECT channel_id FROM list_items WHERE list_id = ? ORDER BY position, added_at", listId).map((r) => r.channel_id);196  const found = channelsByIds(ids); const have = new Set(found.map((c) => c.id));197  // A favourite whose streams all left the sources stays listed, shown as unavailable, until the user removes it.198  const gone = ids.filter((id) => !have.has(id)).map((id) => get<{ id: string; name: string; country: string | null; logo: string | null; first_seen: number }>("SELECT id, name, country, logo, first_seen FROM channels WHERE id = ?", id))199    .filter((c) => !!c).map((c): Channel => ({ id: c!.id, name: c!.name, country: c!.country, categories: [], languages: [], logo: c!.logo, status: "down", streams: 0, web: false, hasGuide: false, firstSeen: c!.first_seen, fav: true }));200  const order = new Map(ids.map((id, i) => [id, i]));201  return [...found, ...gone].sort((a, b) => order.get(a.id)! - order.get(b.id)!);202}203export function setFavorite(channelId: string, listId: number, on: boolean) {204  if (!get("SELECT 1 FROM channels WHERE id = ?", channelId) || !get("SELECT 1 FROM lists WHERE id = ?", listId)) return false;205  if (on) {206    const pos = get<{ p: number | null }>("SELECT MAX(position) p FROM list_items WHERE list_id = ?", listId)?.p ?? -1;207    run("INSERT OR IGNORE INTO list_items(list_id, channel_id, position, added_at) VALUES(?,?,?,?)", listId, channelId, pos + 1, now());208  } else run("DELETE FROM list_items WHERE list_id = ? AND channel_id = ?", listId, channelId);209  return true;210}211export function reorderList(listId: number, ids: string[]) {212  tx(() => ids.forEach((id, i) => run("UPDATE list_items SET position = ? WHERE list_id = ? AND channel_id = ?", i, listId, id)));213}214export function setHidden(channelId: string, on: boolean) {215  if (on) run("INSERT OR IGNORE INTO hidden(channel_id, hidden_at) VALUES(?, ?)", channelId, now()); else run("DELETE FROM hidden WHERE channel_id = ?", channelId);216}217export function recordWatch(channelId: string) {218  if (!get("SELECT 1 FROM channels WHERE id = ?", channelId)) return;219  run("INSERT INTO history(channel_id, last_watched_at) VALUES(?, ?) ON CONFLICT(channel_id) DO UPDATE SET last_watched_at = excluded.last_watched_at, watch_count = watch_count + 1", channelId, now());220}221export function historyChannels(limit = 24): Channel[] {222  return channelsByIds(all<{ channel_id: string }>("SELECT channel_id FROM history WHERE channel_id NOT IN (SELECT channel_id FROM hidden) ORDER BY last_watched_at DESC LIMIT ?", limit).map((r) => r.channel_id));223}224225// --- programmes -----------------------------------------------------------------------------------------------------226export interface Prog { start: number; stop: number; title: string; subtitle: string | null; description: string | null; category: string | null }227export function nowNext(channelIds: string[], at = now()): Record<string, Prog[]> {228  const out: Record<string, Prog[]> = {};229  for (const id of channelIds) {230    const rows = all<Prog>("SELECT start, stop, title, subtitle, description, category FROM programs WHERE channel_id = ? AND stop > ? ORDER BY start LIMIT 3", id, at);231    if (rows.length) out[id] = rows;232  }233  return out;234}235export function programmes(channelIds: string[], from: number, to: number): Record<string, Prog[]> {236  const out: Record<string, Prog[]> = {};237  for (const id of channelIds) {238    const rows = all<Prog>("SELECT start, stop, title, subtitle, description, category FROM programs WHERE channel_id = ? AND stop > ? AND start < ? ORDER BY start", id, from, to);239    if (rows.length) out[id] = rows;240  }241  return out;242}243export function channelsWithProgrammes(): Set<string> {244  return new Set(all<{ channel_id: string }>("SELECT DISTINCT channel_id FROM programs WHERE stop > ?", now()).map((r) => r.channel_id));245}246
Files main
Menu

Theme