import { readFile } from "node:fs/promises"; export type ArchiveContents = { ids: Set; byExtractor: Map>; }; // yt-dlp archive lines have the form ` ` (e.g., `youtube abc123`, // `lbry 9720133ae3a206487321f21eb5bdcc29f40828fc`). export function parseArchive(text: string): ArchiveContents { const ids = new Set(); const byExtractor = new Map>(); for (const rawLine of text.split("\n")) { const line = rawLine.trim(); if (!line) continue; const spaceIdx = line.indexOf(" "); if (spaceIdx <= 0) continue; const extractor = line.slice(0, spaceIdx); const id = line.slice(spaceIdx + 1).trim(); if (!id) continue; ids.add(id); let set = byExtractor.get(extractor); if (!set) { set = new Set(); byExtractor.set(extractor, set); } set.add(id); } return { ids, byExtractor }; } export async function readArchive(filePath: string): Promise { let text: string; try { text = await readFile(filePath, "utf8"); } catch { return { ids: new Set(), byExtractor: new Map() }; } return parseArchive(text); }