SPB Git

Repository Public

m3u.ts 3.9 KB 71 lines typescript

1// M3U / extended M3U parser (the format published by IPTV-org and most public playlists).2export interface M3uEntry {3  tvgId: string | null; channelId: string | null; feed: string | null;4  name: string; rawTitle: string; logo: string | null; group: string[];5  quality: string | null; labels: string[]; url: string; referrer: string | null; userAgent: string | null;6  /** ISO 3166-1 alpha-2 from `tvg-country`, when the playlist gives one. */7  country: string | null;8}9export interface M3uResult { entries: M3uEntry[]; errors: string[]; epgUrls: string[] }1011const ATTR = /([a-zA-Z0-9-]+)="([^"]*)"/g;1213/** "TVA Sports (1080p) [Geo-blocked] [Not 24/7]" → name, quality, labels */14export function splitTitle(raw: string): { name: string; quality: string | null; labels: string[] } {15  const labels: string[] = [];16  // Free-TV markers: Ⓖ geo-blocked, Ⓢ not HD, Ⓨ YouTube, Ⓣ Twitch.17  if (raw.includes("Ⓖ")) labels.push("Geo-blocked");18  raw = raw.replace(/[ⓈⒼⓎⓉ]/g, " ").replace(/\s{2,}/g, " ").trim();19  let s = raw.replace(/\s*\[([^\]]+)\]/g, (_, l: string) => { labels.push(l.trim()); return ""; });20  let quality: string | null = null;21  s = s.replace(/\s*\((\d{3,4}[pi])\)\s*$/i, (_, q: string) => { quality = q.toLowerCase(); return ""; });22  return { name: s.trim() || raw.trim(), quality, labels };23}2425export function parseM3u(text: string): M3uResult {26  const out: M3uResult = { entries: [], errors: [], epgUrls: [] };27  const lines = text.replace(/^/, "").split(/\r?\n/);28  if (!lines[0]?.trim().toUpperCase().startsWith("#EXTM3U")) out.errors.push("En-tête #EXTM3U absent");29  else for (const m of lines[0].matchAll(ATTR)) if (m[1] === "x-tvg-url" || m[1] === "url-tvg") out.epgUrls.push(...m[2].split(",").map((s) => s.trim()).filter(Boolean));30  let cur: Omit<M3uEntry, "url"> | null = null;31  for (let i = 1; i < lines.length; i++) {32    const line = lines[i].trim();33    if (!line) continue;34    if (line.startsWith("#EXTINF")) {35      const attrs: Record<string, string> = {};36      // The display title follows the last comma that is outside the quoted attributes.37      let inQ = false, comma = -1;38      for (let k = 0; k < line.length; k++) { const c = line[k]; if (c === '"') inQ = !inQ; else if (c === "," && !inQ) { comma = k; break; } }39      const head = comma >= 0 ? line.slice(0, comma) : line;40      const rawTitle = comma >= 0 ? line.slice(comma + 1).trim() : "";41      for (const m of head.matchAll(ATTR)) attrs[m[1].toLowerCase()] = m[2].trim();42      const tvgId = attrs["tvg-id"] || null;43      const [channelId, feed] = tvgId ? tvgId.split("@") : [null, null];44      const t = splitTitle(rawTitle || attrs["tvg-name"] || "");45      if (!t.name) { out.errors.push(`Ligne ${i + 1} : entrée sans nom`); cur = null; continue; }46      cur = {47        tvgId, channelId: channelId || null, feed: feed || null, name: t.name, rawTitle, quality: t.quality, labels: t.labels,48        logo: attrs["tvg-logo"] || null,49        group: (attrs["group-title"] || "").split(";").map((s) => s.trim()).filter((s) => s && s.toLowerCase() !== "undefined"),50        referrer: attrs["http-referrer"] || null, userAgent: attrs["http-user-agent"] || null,51        country: /^[A-Za-z]{2}$/.test(attrs["tvg-country"] ?? "") ? attrs["tvg-country"].toUpperCase() : null,52      };53    } else if (line.startsWith("#EXTVLCOPT:")) {54      if (!cur) continue;55      const m = /^#EXTVLCOPT:http-(referrer|user-agent)=(.*)$/i.exec(line);56      if (m) { if (m[1].toLowerCase() === "referrer") cur.referrer ||= m[2].trim(); else cur.userAgent ||= m[2].trim(); }57    } else if (line.startsWith("#")) {58      continue;59    } else {60      if (!cur) { out.errors.push(`Ligne ${i + 1} : URL sans #EXTINF`); continue; }61      out.entries.push({ ...cur, url: line });62      cur = null;63    }64  }65  return out;66}6768export function slugKey(name: string): string {69  return name.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");70}71
Files main
Menu

Theme