diff --git a/apps/server/src/collector/cli.ts b/apps/server/src/collector/cli.ts index 6c7468e..dc21493 100644 --- a/apps/server/src/collector/cli.ts +++ b/apps/server/src/collector/cli.ts @@ -2,7 +2,7 @@ * Song catalog collector (docs/07 처리 순서). Runs next to the app on the same DB (WAL): * bun apps/server/src/collector/cli.ts import-norabose [--limit N] * bun apps/server/src/collector/cli.ts import-sta - * bun apps/server/src/collector/cli.ts match-youtube [--limit N] [--delay ms] [--require-cookie] (default pace: 2s apart; cron caps a day at 3000) + * bun apps/server/src/collector/cli.ts match-youtube [--limit N] [--delay ms] [--deadline epochMs] [--require-cookie] (default pace: 2s apart; cron caps a day at 3000; most popular level first) * bun apps/server/src/collector/cli.ts charts [--from YYYY] [--to YYYY] [--sources melon-ye,genie-ye,tj-ye,melon-rt,bugs-rt] [--refetch] * bun apps/server/src/collector/cli.ts melon-detail [--limit N] * bun apps/server/src/collector/cli.ts heatmap [--limit N] @@ -48,7 +48,7 @@ switch (cmd) { case 'match-youtube': { const searcher = await pickSearcher(process.env.YT_COOKIE_PATH ?? '/home/claude/.config/music_quiz/ytm_cookie.txt', console.log, rest.includes('--require-cookie')); console.log(`search: ${searcher.kind}`); - console.log(await matchYoutube(db, searcher, { limit: flag('limit'), delayMs: flag('delay'), source: searcher.kind })); + console.log(await matchYoutube(db, searcher, { limit: flag('limit'), delayMs: flag('delay'), deadline: flag('deadline'), source: searcher.kind })); break; } case 'charts': { diff --git a/apps/server/src/collector/collector.test.ts b/apps/server/src/collector/collector.test.ts index c9c32f1..b819cf0 100644 --- a/apps/server/src/collector/collector.test.ts +++ b/apps/server/src/collector/collector.test.ts @@ -111,3 +111,22 @@ test('YouTube matching: confident → matched, unsure → candidate fallback, no expect(await matchYoutube(db, { searchSongs: async () => { throw new Error('should not search'); } }, { delayMs: 0 })).toEqual({ matched: 0, candidate: 0, none: 0 }); expect(listSongs(db, { youtube: 'matched' }).total).toBe(1); }); + +test('YouTube matching asks the most popular level first and stops at the deadline', async () => { + const db = freshDb(); + const c = new Catalog(db); + const [rare] = c.upsert({ title: '숨은곡', artists: [{ name: '가' }], source: 't', ref: '1' }); + const [hit] = c.upsert({ title: '대표곡', artists: [{ name: '나' }], source: 't', ref: '2' }); + const [mid] = c.upsert({ title: '중간곡', artists: [{ name: '다' }], source: 't', ref: '3' }); + c.autoLevel(rare!, 'all', 9); + c.autoLevel(hit!, 'all', 1); + c.autoLevel(mid!, 'all', 5); + const asked: string[] = []; + const searchSongs = async (q: string) => (asked.push(q), []); + const noEmbed = async () => ({ state: 'noembed' as const }); + await matchYoutube(db, { searchSongs }, { delayMs: 0, limit: 2 }, noEmbed); + expect(asked).toEqual(['나 대표곡', '다 중간곡']); + await matchYoutube(db, { searchSongs }, { delayMs: 0, deadline: Date.now() - 1 }, noEmbed); + expect(asked.length).toBe(2); + expect(db.query<{ status: string }, []>("SELECT status FROM collect_jobs WHERE kind = 'match-youtube' ORDER BY id DESC LIMIT 1").get()!.status).toBe('done'); +}); diff --git a/apps/server/src/collector/match-youtube.ts b/apps/server/src/collector/match-youtube.ts index fe5681c..67def9e 100644 --- a/apps/server/src/collector/match-youtube.ts +++ b/apps/server/src/collector/match-youtube.ts @@ -20,6 +20,8 @@ export interface MatchOptions { limit?: number; delayMs?: number; retryDays?: number; + /** Stop cleanly (job done) once this time (epoch ms) has passed, e.g. before the nightly cron. */ + deadline?: number; /** Where results come from: recorded on songs and candidates. */ source?: 'ytmusic' | 'youtube'; log?: (s: string) => void; @@ -33,13 +35,15 @@ export async function matchYoutube(db: Database, ytm: Pick( `SELECT s.id, s.title, s.artist, s.duration_sec FROM songs s LEFT JOIN signals t ON t.song_id = s.id AND t.kind = ? + LEFT JOIN song_levels l ON l.song_id = s.id AND l.scope = 'all' WHERE s.deleted_at IS NULL AND s.youtube_status IN ('none','candidate') AND COALESCE(s.youtube_source, '') <> 'admin' AND (t.value IS NULL OR t.value < ?) - ORDER BY s.id LIMIT ?`, + ORDER BY l.level IS NULL, l.level, s.id LIMIT ?`, ) .all(triedKind, retryBefore, opts.limit ?? 1_000_000); const job = new Job(db, 'match-youtube', rows.length); const stats = { matched: 0, candidate: 0, none: 0 }; + let tried = 0; const src = opts.source ?? 'ytmusic'; /** * Writes the result only if the song is still open for automatic matching: an admin may have saved a video @@ -57,6 +61,10 @@ export async function matchYoutube(db: Database, ytm: Pick("SELECT COUNT(*) n FROM songs WHERE id = ? AND COALESCE(youtube_source, '') = 'admin'").get(id)!.n > 0; try { for (const [i, s] of rows.entries()) { + if (opts.deadline !== undefined && Date.now() >= opts.deadline) { + job.log(`deadline reached after ${i}/${rows.length}`); + break; + } let results: YtmSong[]; const query = `${s.artist.split(', ')[0]} ${s.title}`; try { @@ -105,11 +113,12 @@ export async function matchYoutube(db: Database, ytm: Pick