Files
AeroFetch/src/main/sync.ts
T
debont80 47da3e6746 Add Pinchflat-style media manager: index channels into playlist folders
Index an entire YouTube channel or playlist once, then download it into
organized <Channel>/<Playlist>/<NNN> - <Title> folders, with incremental
re-sync. Built in six rebuild-gated phases (F-K); see ROADMAP-PINCHFLAT.md.

- F: channel-walk indexer (/playlists + /videos) -> persisted Source +
  MediaItem JSON stores; pure classify/dedup logic in indexerCore.ts
- G: Windows-safe folder paths + dir sanitizer (collectionOutputTemplate)
- H: Library tab + source tree + "Download pending" into the existing queue
- I: state-preserving re-index merge + persist-downloaded-on-complete
- J: watched sources, RSS fast-check, Task Scheduler + --sync launch
  (the OS-level scheduling/RSS need a real-install smoke test)
- K: .info.json / thumbnail / .description sidecars for media servers

Also gitignore .gitea-token. 106 unit tests pass; typecheck + build clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 20:50:13 -04:00

51 lines
2.1 KiB
TypeScript

/**
* 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<string[]> {
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<SyncResult> {
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) }
}
}