SPB Git

Repository Public

xtream.ts 6.1 KB 56 lines typescript

1import { fetchText } from "../ssrf";2import { redact } from "../secrets";3import type { M3uEntry } from "../m3u";45// Adapter for providers that expose the « Xtream Codes » player API (player_api.php) and allow their subscribers to6// use third-party players. It only calls the documented endpoints with the subscriber's own credentials.7export interface XtreamConfig { server: string; username: string }8const base = (c: XtreamConfig) => c.server.replace(/\/+$/, "");9const api = (c: XtreamConfig, pw: string, action?: string, extra = "") => `${base(c)}/player_api.php?username=${encodeURIComponent(c.username)}&password=${encodeURIComponent(pw)}${action ? `&action=${action}` : ""}${extra}`;1011async function call<T>(c: XtreamConfig, pw: string, action?: string, extra = ""): Promise<T> {12  try { return JSON.parse(await fetchText(api(c, pw, action, extra), 80 * 1024 * 1024, 90_000)) as T; }13  catch (e) { throw new Error(redact((e as Error).message, pw, c.username)); } // an error text must never carry the credentials14}1516export interface XtreamAccount { ok: boolean; status: string; expires: number | null; maxConnections: number | null; message: string }17export async function account(c: XtreamConfig, pw: string): Promise<XtreamAccount> {18  const d = await call<{ user_info?: { auth?: number | string; status?: string; exp_date?: string | null; max_connections?: string | number; message?: string } }>(c, pw);19  const u = d.user_info; const ok = !!u && String(u.auth) === "1";20  return { ok, status: u?.status ?? "inconnu", expires: u?.exp_date ? Number(u.exp_date) * 1000 : null, maxConnections: u?.max_connections ? Number(u.max_connections) : null, message: ok ? "Compte reconnu par le fournisseur" : u?.message || "Identifiants refusés par le fournisseur" };21}2223interface Cat { category_id: string; category_name: string }24interface Live { stream_id: number; name: string; stream_icon?: string; epg_channel_id?: string | null; category_id?: string | null }25/** Live channels as playlist entries. The address stored is an opaque reference; the real one is rebuilt at play time. */26export async function liveEntries(c: XtreamConfig, pw: string, sourceId: number): Promise<M3uEntry[]> {27  const cats = new Map((await call<Cat[]>(c, pw, "get_live_categories")).map((x) => [String(x.category_id), x.category_name]));28  const list = await call<Live[]>(c, pw, "get_live_streams");29  if (!Array.isArray(list)) throw new Error("Réponse inattendue du fournisseur (get_live_streams)");30  return list.filter((s) => s.stream_id && s.name).map((s) => ({31    tvgId: s.epg_channel_id || null, channelId: s.epg_channel_id ? s.epg_channel_id.split("@")[0] : null, feed: null, name: s.name.trim(), rawTitle: s.name.trim(), logo: s.stream_icon || null,32    group: s.category_id && cats.get(String(s.category_id)) ? [cats.get(String(s.category_id))!] : [], quality: null, labels: [], url: `provider://live/${sourceId}/${s.stream_id}.m3u8`, referrer: null, userAgent: null, country: null,33  }));34}35export interface XtreamVod { id: number; name: string; icon: string | null; ext: string; category: string | null; year: number | null; plot: string | null }36export async function vodList(c: XtreamConfig, pw: string): Promise<XtreamVod[]> {37  const cats = new Map((await call<Cat[]>(c, pw, "get_vod_categories")).map((x) => [String(x.category_id), x.category_name]));38  const list = await call<{ stream_id: number; name: string; stream_icon?: string; container_extension?: string; category_id?: string; year?: string; plot?: string }[]>(c, pw, "get_vod_streams");39  return (Array.isArray(list) ? list : []).filter((v) => v.stream_id && v.name).map((v) => ({ id: v.stream_id, name: v.name.trim(), icon: v.stream_icon || null, ext: (v.container_extension || "mp4").toLowerCase(), category: cats.get(String(v.category_id)) ?? null, year: Number(v.year) || Number(/\((\d{4})\)/.exec(v.name)?.[1]) || null, plot: v.plot || null }));40}41export interface XtreamSeries { id: number; name: string; cover: string | null; plot: string | null; year: number | null; category: string | null }42export async function seriesList(c: XtreamConfig, pw: string): Promise<XtreamSeries[]> {43  const cats = new Map((await call<Cat[]>(c, pw, "get_series_categories")).map((x) => [String(x.category_id), x.category_name]));44  const list = await call<{ series_id: number; name: string; cover?: string; plot?: string; releaseDate?: string; category_id?: string }[]>(c, pw, "get_series");45  return (Array.isArray(list) ? list : []).filter((v) => v.series_id && v.name).map((v) => ({ id: v.series_id, name: v.name.trim(), cover: v.cover || null, plot: v.plot || null, year: Number((v.releaseDate ?? "").slice(0, 4)) || null, category: cats.get(String(v.category_id)) ?? null }));46}47export async function seriesEpisodes(c: XtreamConfig, pw: string, seriesId: number): Promise<{ id: string; season: number; episode: number; title: string; ext: string; duration: number | null; plot: string | null }[]> {48  const d = await call<{ episodes?: Record<string, { id: string; episode_num: number | string; title?: string; container_extension?: string; season?: number; info?: { duration_secs?: number; plot?: string } }[]> }>(c, pw, "get_series_info", `&series_id=${seriesId}`);49  return Object.entries(d.episodes ?? {}).flatMap(([season, eps]) => (eps ?? []).map((e) => ({ id: String(e.id), season: Number(e.season ?? season) || 1, episode: Number(e.episode_num) || 1, title: e.title ?? "", ext: (e.container_extension || "mp4").toLowerCase(), duration: e.info?.duration_secs ?? null, plot: e.info?.plot ?? null })));50}51/** Real addresses, built server-side only. */52export const liveUrl = (c: XtreamConfig, pw: string, file: string) => `${base(c)}/live/${encodeURIComponent(c.username)}/${encodeURIComponent(pw)}/${file}`;53export const movieUrl = (c: XtreamConfig, pw: string, file: string) => `${base(c)}/movie/${encodeURIComponent(c.username)}/${encodeURIComponent(pw)}/${file}`;54export const episodeUrl = (c: XtreamConfig, pw: string, file: string) => `${base(c)}/series/${encodeURIComponent(c.username)}/${encodeURIComponent(pw)}/${file}`;55export const xmltvUrl = (c: XtreamConfig, pw: string) => `${base(c)}/xmltv.php?username=${encodeURIComponent(c.username)}&password=${encodeURIComponent(pw)}`;56
Files main
Menu

Theme