SPB Git

Repository Public

tmdb.ts 5.9 KB 71 lines typescript

1import { all, bumpTitles, log, now, run, setSetting } from "./db";2import { env } from "./env";3import { fetchText } from "./ssrf";45// TMDB is used for artwork and French synopses only — never as a source of video, and never to decide that a title can6// be watched. Terms of use: non-commercial, attribution shown in the application, data not kept beyond six months7// (the weekly import refreshes it). https://www.themoviedb.org/api-terms-of-use8const IMG = "https://image.tmdb.org/t/p";9const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));10let last = 0;11async function api<T>(path: string): Promise<T | null> {12  const w = last + 60 - Date.now(); if (w > 0) await sleep(w); last = Date.now(); // ≈ 16 requests per second, well under TMDB's limit13  try { return JSON.parse(await fetchText(`https://api.themoviedb.org/3${path}${path.includes("?") ? "&" : "?"}api_key=${encodeURIComponent(env.tmdbKey)}`, 2 * 1024 * 1024, 20_000)) as T; }14  catch (e) { if (/HTTP 401/.test((e as Error).message)) throw new Error("Clé TMDB refusée"); return null; }15}16interface Hit { id: number; title?: string; name?: string; original_title?: string; original_name?: string; release_date?: string; first_air_date?: string; poster_path?: string | null; backdrop_path?: string | null; overview?: string }17const norm = (s: string) => s.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, " ").trim();1819export interface TmdbMatch { poster: string | null; backdrop: string | null; overview: string | null; id: number; type: "movie" | "tv" }20const shape = (h: Hit, type: "movie" | "tv"): TmdbMatch => ({ id: h.id, type, poster: h.poster_path ? `${IMG}/w500${h.poster_path}` : null, backdrop: h.backdrop_path ? `${IMG}/w1280${h.backdrop_path}` : null, overview: h.overview?.trim() || null });2122/** Exact identification through the IMDb identifier Wikidata gives. */23export async function byImdb(imdb: string): Promise<TmdbMatch | null> {24  if (!/^tt\d+$/.test(imdb)) return null;25  const d = await api<{ movie_results?: Hit[]; tv_results?: Hit[] }>(`/find/${imdb}?external_source=imdb_id&language=fr-FR`);26  return d?.movie_results?.[0] ? shape(d.movie_results[0], "movie") : d?.tv_results?.[0] ? shape(d.tv_results[0], "tv") : null;27}28/** Search by title: accepted only when the title is the same and the year matches (± 1) — a wrong poster is worse than none. */29export async function byTitle(title: string, year: number | null, kind: "movie" | "series"): Promise<TmdbMatch | null> {30  if (!year) return null;31  const type = kind === "movie" ? "movie" : "tv";32  const d = await api<{ results?: Hit[] }>(`/search/${type}?query=${encodeURIComponent(title)}&language=fr-FR&${type === "movie" ? "year" : "first_air_date_year"}=${year}`);33  const want = norm(title);34  const hit = (d?.results ?? []).find((h) => { const y = Number((h.release_date ?? h.first_air_date ?? "").slice(0, 4)); return Math.abs(y - year) <= 1 && [h.title, h.name, h.original_title, h.original_name].some((t) => t && norm(t) === want); });35  return hit ? shape(hit, type) : null;36}3738export interface TmdbResult { examined: number; matched: number; posters: number; synopses: number; durationMs: number }39/**40 * Completes titles that lack a proper poster or a French synopsis. Wikidata titles are identified by IMDb id; ONF and41 * library titles by exact title + year. Sources that bring their own artwork and texts (ARTE, NASA) are left alone.42 */43export async function enrichTmdb(budget = 6000): Promise<TmdbResult> {44  const t0 = now(); const res: TmdbResult = { examined: 0, matched: 0, posters: 0, synopses: 0, durationMs: 0 };45  if (!env.tmdbKey) return res;46  const stale = now() - 150 * 86400_000; // refreshed well within the six months TMDB allows data to be kept47  const rows = all<{ id: number; kind: "movie" | "series"; origin: string; title: string; original_title: string | null; year: number | null; imdb_id: string | null; poster: string | null; synopsis: string | null; synopsis_source: string | null }>(`48    SELECT id, kind, origin, title, original_title, year, imdb_id, poster, synopsis, synopsis_source FROM titles49    WHERE active = 1 AND (origin IN ('wikidata', 'onf', 'library')) AND (tmdb_checked_at IS NULL OR tmdb_checked_at < ?)50    ORDER BY (poster IS NULL) DESC, (synopsis IS NULL) DESC, id LIMIT ?`, stale, budget);51  for (const t of rows) {52    res.examined++;53    let m: TmdbMatch | null = null;54    try { m = t.imdb_id ? await byImdb(t.imdb_id) : null; m ??= await byTitle(t.title, t.year, t.kind) ?? (t.original_title ? await byTitle(t.original_title, t.year, t.kind) : null); }55    catch (e) { log("warn", "tmdb", "Enrichissement interrompu", { error: (e as Error).message }); break; }56    if (m) {57      res.matched++;58      // A real poster replaces a film still, an archive thumbnail or a landscape image; a French synopsis replaces a missing or English one.59      const betterPoster = !!m.poster && (!t.poster || !/nfbcdn\.ca\/medias\/norman\/jackets\//.test(t.poster));60      const betterText = !!m.overview && (!t.synopsis || t.synopsis_source === "Wikipedia (en)");61      run(`UPDATE titles SET poster = CASE WHEN ? THEN ? ELSE poster END, backdrop = COALESCE(?, backdrop), synopsis = CASE WHEN ? THEN ? ELSE synopsis END, synopsis_source = CASE WHEN ? THEN 'TMDB' ELSE synopsis_source END, tmdb_id = ?, tmdb_checked_at = ? WHERE id = ?`,62        Number(betterPoster), m.poster, m.backdrop, Number(betterText), m.overview, Number(betterText), `${m.type}/${m.id}`, now(), t.id);63      if (betterPoster) res.posters++; if (betterText) res.synopses++;64    } else run("UPDATE titles SET tmdb_checked_at = ? WHERE id = ?", now(), t.id);65    if (res.examined % 500 === 0) { bumpTitles(); log("info", "tmdb", `TMDB : ${res.examined}/${rows.length} titres examinés, ${res.matched} reconnus`); }66  }67  res.durationMs = now() - t0; setSetting("last_tmdb_at", String(now())); bumpTitles();68  log("info", "tmdb", `TMDB : ${res.matched}/${res.examined} titres reconnus, ${res.posters} affiches et ${res.synopses} synopsis ajoutés`, res);69  return res;70}71
Files main
Menu

Theme