/** * Watched-source sync (Pinchflat-style; ROADMAP-PINCHFLAT.md Phase J). Re-indexes * every watched Source and reports the videos that are new since its last index. * A YouTube RSS feed is used as a cheap pre-check so a source with no new uploads * is skipped before the (more expensive) full `yt-dlp` re-index. */ import { listSources, listMediaItems } from './sources' import { indexSource } from './indexer' import { parseRssVideoIds } from './indexerCore' import type { IndexProgress, MediaItem, SyncResult } from '@shared/ipc' /** Fetch a YouTube RSS feed and return its recent video ids. Throws on failure. */ async function fetchFeedIds(feedUrl: string): Promise { const res = await fetch(feedUrl, { signal: AbortSignal.timeout(15_000) }) if (!res.ok) throw new Error(`feed responded ${res.status}`) return parseRssVideoIds(await res.text()) } /** * Re-index every watched source and collect the videos new since the last index. * RSS pre-check: if the feed shows only ids the source already knows, the full * re-index is skipped. `onProgress` is forwarded from the underlying indexSource. */ export async function syncWatchedSources( onProgress: (p: IndexProgress) => void ): Promise { try { const watched = listSources().filter((s) => s.watched) const newItems: MediaItem[] = [] for (const src of watched) { // Cheap freshness check — skip the full re-index when nothing is new. if (src.feedUrl) { const known = new Set(listMediaItems(src.id).map((m) => m.videoId)) const recent = await fetchFeedIds(src.feedUrl).catch(() => null) if (recent && recent.length > 0 && recent.every((id) => known.has(id))) continue } const before = new Set(listMediaItems(src.id).map((m) => m.videoId)) const res = await indexSource(src.url, onProgress) if (res.ok) { for (const it of listMediaItems(src.id)) { if (!before.has(it.videoId)) newItems.push(it) } } } return { ok: true, newItems } } catch (e) { return { ok: false, newItems: [], error: e instanceof Error ? e.message : String(e) } } }