From dd8ed20572adb1d507a7829e9d9cfbf0090cca37 Mon Sep 17 00:00:00 2001 From: EJClaw Date: Thu, 29 Oct 2026 10:45:51 +0900 Subject: [PATCH] =?UTF-8?q?=EC=9B=90=EA=B3=A1=EC=B0=BE=EA=B8=B0=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=20=ED=99=95=EC=A0=95=202=EC=B0=A8:=20?= =?UTF-8?q?=EC=A0=9C=EB=AA=A9+=EA=B0=80=EC=88=98=EA=B0=80=20=EB=A7=9E?= =?UTF-8?q?=EC=9C=BC=EB=A9=B4=20=EA=B3=A1=20=EA=B8=B8=EC=9D=B4=EA=B0=80=20?= =?UTF-8?q?=EB=8B=AC=EB=9D=BC=EB=8F=84=20=ED=99=95=EC=A0=95(=EA=B8=B8?= =?UTF-8?q?=EC=9D=B4=EA=B0=80=2015=EC=B4=88=20=EB=84=98=EA=B2=8C=20?= =?UTF-8?q?=EB=8B=A4=EB=A5=B4=EB=A9=B4=20MV=C2=B7inst.=C2=B7=EB=9D=BC?= =?UTF-8?q?=EC=9D=B4=EB=B8=8C=C2=B7=EC=BB=A4=EB=B2=84,=20=EB=85=B8?= =?UTF-8?q?=EB=9E=98=20=EC=A0=9C=EB=AA=A9=EC=97=90=20=EC=97=86=EB=8A=94=20?= =?UTF-8?q?=EB=AF=B9=EC=8A=A4/=EC=97=90=EB=94=94=ED=8A=B8/=EC=97=B0?= =?UTF-8?q?=EB=8F=84=20=ED=91=9C=EC=8B=9C,=20Intro=20=EA=B0=99=EC=9D=80=20?= =?UTF-8?q?=ED=9D=94=ED=95=9C=20=EC=A0=9C=EB=AA=A9=EC=9D=80=20=EB=B3=B4?= =?UTF-8?q?=EB=A5=98=20=E2=80=94=20=EC=8B=A4=EC=A0=9C=20=EC=98=81=EC=83=81?= =?UTF-8?q?=20=EC=A0=9C=EB=AA=A9=EB=8F=84=20=ED=99=95=EC=9D=B8,=20Topic=20?= =?UTF-8?q?=EC=95=84=EB=8B=8C=20=EC=98=81=EC=83=81=EC=9D=80=20=EC=A0=9C?= =?UTF-8?q?=EC=99=B8).=20"=ED=95=9C=EA=B8=80=20English"=20=ED=91=9C?= =?UTF-8?q?=EA=B8=B0(=EB=B6=81=EA=B7=B9=EC=84=B1=20Polaris,=20=ED=97=A4?= =?UTF-8?q?=EC=9D=B4=EC=A6=88=20Heize)=EB=A5=BC=20=EB=82=98=EB=88=A0=20?= =?UTF-8?q?=EB=B9=84=EA=B5=90(=EC=A0=9C=EB=AA=A9=EC=9D=80=20=EA=B0=80?= =?UTF-8?q?=EC=88=98=EB=8F=84=20=EB=A7=9E=EC=9D=84=20=EB=95=8C=EB=A7=8C,?= =?UTF-8?q?=20Part=202=C2=B7I=C2=B7=EB=A9=94=EB=93=A4=EB=A6=AC=EB=8A=94=20?= =?UTF-8?q?=EC=95=88=20=EB=82=98=EB=88=94).=20=ED=95=9C=EA=B8=80/=EC=98=81?= =?UTF-8?q?=EB=AC=B8=20=EA=B0=80=EC=88=98=20=EC=9D=B4=EB=A6=84=20=EB=B0=9C?= =?UTF-8?q?=EC=9D=8C=20=EB=B9=84=EA=B5=90(collector/sound.ts:=20=EC=9E=90?= =?UTF-8?q?=EC=9D=8C=20=EB=BC=88=EB=8C=80=20+=20=EB=A1=9C=EB=A7=88?= =?UTF-8?q?=EC=9E=90=20=ED=95=9C=EA=B5=AD=20=EC=9D=B4=EB=A6=84=EC=9D=80=20?= =?UTF-8?q?=EB=AA=A8=EC=9D=8C=20=EA=B3=84=EC=97=B4=EB=8F=84,=20=EC=84=B1?= =?UTF-8?q?=20=EB=92=A4=EB=A1=9C=C2=B7DJ=20DOC=20=EC=95=8C=ED=8C=8C?= =?UTF-8?q?=EB=B2=B3=20=EC=9D=BD=EA=B8=B0;=20=EB=B0=9C=EC=9D=8C=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=EB=A7=8C=20=EB=A7=9E=EC=9C=BC=EB=A9=B4=20=EC=A0=9C?= =?UTF-8?q?=EB=AA=A9=20=EA=B0=99=EA=B3=A0=208=EC=B4=88=20=EC=9D=B4?= =?UTF-8?q?=EB=82=B4=EB=A7=8C).=20=EA=B2=B0=EA=B3=BC=EC=97=90=20=EA=B7=B8?= =?UTF-8?q?=20=EA=B0=80=EC=88=98=20=EA=B3=A1=EC=9D=B4=20=EC=97=86=EC=9C=BC?= =?UTF-8?q?=EB=A9=B4=20=EC=A0=80=EC=9E=A5=EB=90=9C=20=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=B0=BE=EC=9D=80=20=EB=8B=A4=EB=A5=B8=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=20=EC=9D=B4=EB=A6=84(DJ=20DOC)=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=8B=A4=EC=8B=9C=20=EA=B2=80=EC=83=89=20=E2=80=94?= =?UTF-8?q?=20=EB=A7=A4=EC=9D=BC=20=EC=9B=90=EA=B3=A1=20=EC=B0=BE=EA=B8=B0?= =?UTF-8?q?=EC=97=90=20=ED=8F=AC=ED=95=A8,=20=EC=8C=93=EC=9D=B8=20?= =?UTF-8?q?=ED=9B=84=EB=B3=B4=EB=8A=94=20cli=20research-youtube(=EA=B7=B8?= =?UTF-8?q?=20=EA=B0=80=EC=88=98=20=EA=B3=A1=EB=A7=8C=20=ED=99=95=EC=A0=95?= =?UTF-8?q?).=20reconfirm-youtube=EA=B0=80=20=EB=AA=BB=20=EC=B0=BE?= =?UTF-8?q?=EC=9D=8C=20=EA=B3=A1=EB=8F=84=20=EB=8B=A4=EC=8B=9C=20=EB=B9=84?= =?UTF-8?q?=EA=B5=90.=20=EC=84=9C=EB=B2=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20150=EA=B0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- apps/server/src/collector/cli.ts | 15 +- apps/server/src/collector/collector.test.ts | 105 ++++++++-- apps/server/src/collector/match-youtube.ts | 209 +++++++++++++++----- apps/server/src/collector/other-names.ts | 52 +++++ apps/server/src/collector/score.ts | 83 +++++++- apps/server/src/collector/sound.ts | 173 ++++++++++++++++ docs/12-decisions.md | 7 + packages/shared/src/match-key.test.ts | 15 +- packages/shared/src/match-key.ts | 57 +++++- 9 files changed, 638 insertions(+), 78 deletions(-) create mode 100644 apps/server/src/collector/other-names.ts create mode 100644 apps/server/src/collector/sound.ts diff --git a/apps/server/src/collector/cli.ts b/apps/server/src/collector/cli.ts index 353e30f..60db2c2 100644 --- a/apps/server/src/collector/cli.ts +++ b/apps/server/src/collector/cli.ts @@ -4,6 +4,7 @@ * bun apps/server/src/collector/cli.ts import-sta * 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 reconfirm-youtube [--dry-run] [--delay ms] (후보 곡을 이미 저장된 검색 결과로 현재 기준에 다시 비교해 확정) + * bun apps/server/src/collector/cli.ts research-youtube [--dry-run] [--limit N] [--delay ms] [--deadline epochMs] (가수가 다른 문자로 적혀 결과에 그 가수가 없던 후보 곡을 영어/한글 이름으로 다시 검색; 쿠키가 풀렸으면 일반 유튜브 검색) * 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] @@ -18,7 +19,7 @@ import { join } from 'node:path'; import { openDb } from '../db'; import { importNorabose } from './norabose'; import { importSta } from './sta'; -import { matchYoutube, reconfirmCandidates } from './match-youtube'; +import { matchYoutube, reconfirmCandidates, researchYoutube } from './match-youtube'; import { pickSearcher } from './ytmusic'; import { catalogStats } from '../songs/store'; import { currentJob } from './catalog'; @@ -62,6 +63,16 @@ switch (cmd) { case 'reconfirm-youtube': console.log(await reconfirmCandidates(db, { dryRun: rest.includes('--dry-run'), delayMs: flag('delay'), log: console.log })); break; + case 'research-youtube': { + if (rest.includes('--dry-run')) { + console.log(await researchYoutube(db, { searchSongs: async () => [] }, { dryRun: true, limit: flag('limit'), log: console.log })); + break; + } + const searcher = await pickSearcher(process.env.YT_COOKIE_PATH ?? '/home/claude/.config/music_quiz/ytm_cookie.txt', console.log); + console.log(`search: ${searcher.kind}`); + console.log(await researchYoutube(db, searcher, { limit: flag('limit'), delayMs: flag('delay'), deadline: flag('deadline'), source: searcher.kind, log: console.log })); + break; + } case 'charts': { const i = rest.indexOf('--sources'); console.log(await collectCharts(db, { from: flag('from'), to: flag('to'), sources: i >= 0 ? rest[i + 1]!.split(',') : undefined, refetch: rest.includes('--refetch'), delayMs: flag('delay') })); @@ -89,7 +100,7 @@ switch (cmd) { console.log(JSON.stringify(catalogStats(db), null, 1)); break; default: - console.log('usage: cli.ts import-norabose|import-sta|match-youtube|reconfirm-youtube|charts|melon-detail|heatmap|loudness|age-audio|levels|fix-text|stats'); + console.log('usage: cli.ts import-norabose|import-sta|match-youtube|reconfirm-youtube|research-youtube|charts|melon-detail|heatmap|loudness|age-audio|levels|fix-text|stats'); process.exitCode = 1; } db.close(); diff --git a/apps/server/src/collector/collector.test.ts b/apps/server/src/collector/collector.test.ts index 611dee3..f84384e 100644 --- a/apps/server/src/collector/collector.test.ts +++ b/apps/server/src/collector/collector.test.ts @@ -5,8 +5,9 @@ import { openDb } from '../db'; import { Catalog } from './catalog'; import { genresOf, provisionalLevel } from './norabose'; import { importSta } from './sta'; -import { matchYoutube, reconfirmCandidates } from './match-youtube'; +import { matchYoutube, reconfirmCandidates, researchYoutube } from './match-youtube'; import { isConfident, notConfidentReasons, scoreResult } from './score'; +import { soundsLike } from './sound'; import { learnArtistAliases } from './artist-aliases'; import { parseDuration, parseSearch, type YtmSong } from './ytmusic'; import { catalogStats, listSongs } from '../songs/store'; @@ -37,13 +38,24 @@ test('YouTube Music search parsing', () => { expect(parseDuration('1:02:03')).toBe(3723); }); -test('scoring: title+artist confident, MR/cover not, wrong length not', () => { +test('scoring: title+artist confident whatever the length, MR/cover/MV/other mix not', () => { const song = { title: '광 (狂)', artist: 'SG워너비', durationSec: 237 }; const yt = (title: string, artists: string, durationSec: number): YtmSong => ({ videoId: 'x', title, artists, album: null, durationSec }); expect(isConfident(scoreResult(song, yt('광 (狂)', 'SG워너비', 237), 0))).toBe(true); expect(isConfident(scoreResult(song, yt('광', 'SG Wannabe', 236), 1))).toBe(true); // title + exact length expect(isConfident(scoreResult(song, yt('광 (Inst.)', 'SG워너비', 237), 0))).toBe(false); - expect(isConfident(scoreResult(song, yt('광', 'SG워너비', 300), 0))).toBe(false); + // a different take or a wrong Melon length: title + artist is enough … + expect(isConfident(scoreResult(song, yt('광', 'SG워너비', 300), 0))).toBe(true); + expect(isConfident(scoreResult(song, yt('광 (2021 Remastered)', 'SG워너비', 300), 0))).toBe(true); + // … unless the result is an MV, another mix/year, or not an official track + expect(isConfident(scoreResult(song, yt('광 (Official MV)', 'SG워너비', 300), 0))).toBe(false); + expect(isConfident(scoreResult(song, yt('광 (Sleep Mix)', 'SG워너비', 3600), 0))).toBe(false); + expect(isConfident(scoreResult(song, yt('광 (2021)', 'SG워너비', 300), 0))).toBe(false); + expect(isConfident(scoreResult(song, { ...yt('광', 'SG워너비', 300), topic: false }, 0))).toBe(false); + // an MV with the same length still counts as before (score path) + expect(isConfident(scoreResult(song, yt('광 (Official MV)', 'SG워너비', 238), 0))).toBe(true); + // a title artists reuse album after album + expect(isConfident(scoreResult({ title: 'Intro', artist: '나얼', durationSec: 214 }, yt('Intro', '나얼 Naul', 56), 0))).toBe(false); expect(isConfident(scoreResult(song, yt('다른 노래', 'SG워너비', 237), 0))).toBe(false); }); @@ -53,14 +65,80 @@ test('scoring: accents, 및, "Radio Edit Version" and the length limit', () => { expect(isConfident(scoreResult({ title: 'Forever Love', artist: 'X JAPAN, YOSHIKI', durationSec: null }, yt('Forever Love', 'X JAPAN 및 YOSHIKI', 300), 0))).toBe(true); expect(isConfident(scoreResult({ title: 'Set Me On Fire', artist: 'Flashlight Brown', durationSec: 180 }, yt('Set Me On Fire (Radio Edit Version)', 'Flashlight Brown', 182), 0))).toBe(true); expect(isConfident(scoreResult({ title: '심장이 없어', artist: '에이트', durationSec: 208 }, yt('심장이 없어 (Rough Ver.)', '에이트', 208), 0))).toBe(false); - // title + artist but 18 seconds off: never auto-confirmed - const off = scoreResult({ title: 'Human', artist: 'The Human League', durationSec: 248 }, yt('Human', 'Human League', 266), 0); - expect(isConfident(off)).toBe(false); - expect(notConfidentReasons(off)).toEqual(['길이 18초 차이']); + // title + artist 18 seconds off: confirmed; a title alone that far off is not + expect(isConfident(scoreResult({ title: 'Human', artist: 'The Human League', durationSec: 248 }, yt('Human', 'Human League', 266), 0))).toBe(true); + const off = scoreResult({ title: 'Human', artist: 'The Human League', durationSec: 248 }, yt('Human', 'Someone', 266), 0); + expect(notConfidentReasons(off)).toEqual(['가수 다름: Someone', '길이 18초 차이']); + const mix = scoreResult({ title: 'Human', artist: 'The Human League', durationSec: 248 }, yt('Human (Extended Mix)', 'Human League', 400), 0); + expect(notConfidentReasons(mix)).toEqual(['길이 152초 차이 · 믹스·연도 등 다른 버전 표시']); const other = scoreResult({ title: 'Flower Dance', artist: 'DJ Okawari', durationSec: null }, yt('Flower Dance', 'Ashiyane', 277), 0); expect(notConfidentReasons(other)).toEqual(['가수 다름: Ashiyane']); }); +test('Hangul and Latin artist names that sound the same', () => { + const same = [ + ['디제이디오씨', 'DJ DOC'], ['에스이에스', 'S.E.S.'], ['플라이투더스카이', 'Fly to the Sky'], ['조규찬', 'Cho Kyu Chan'], ['강타', 'KANGTA'], + ['백예린', 'Yerin Baek'], ['김보경', 'Bo Kyung Kim'], ['이승철', 'Lee Seung Chul'], ['서태지', 'Seotaiji'], ['클로버', 'CLOVER'], ['윤미래', 'Yoonmirae'], + ]; + for (const [a, b] of same) expect([a, b, soundsLike(a!, b!)]).toEqual([a, b, true]); + // romanized Korean names must have the same vowels too; ㅎ is not silent + const different = [['박정현', 'Park Jin Young'], ['이정현', 'CHEN'], ['이정현', 'JENNIE'], ['이상은', 'San E'], ['김현정', 'Kim Won Jun'], ['박혜경', 'BAEKHYUN'], ['도끼', 'GD&TOP'], ['태연', 'TAEYEON']]; + for (const [a, b] of different) expect([a, b, soundsLike(a!, b!)]).toEqual([a, b, a === '태연']); +}); + +test('scoring: "한글 English" titles and sound-alike artists only with a close length', () => { + const yt = (title: string, artists: string, durationSec: number | null): YtmSong => ({ videoId: 'x', title, artists, album: null, durationSec }); + const promise = { title: '약속', artist: '플라이투더스카이', durationSec: 249 }; + expect(isConfident(scoreResult(promise, yt('약속 The Promise', 'Fly to the Sky', 247), 0))).toBe(true); + const far = scoreResult(promise, yt('약속 The Promise', 'Fly to the Sky', 269), 0); + expect(isConfident(far)).toBe(false); + expect(notConfidentReasons(far)).toEqual(['가수 이름 발음만 같음: Fly to the Sky', '길이 20초 차이']); + expect(isConfident(scoreResult({ title: '북극성', artist: '강타', durationSec: 243 }, yt('북극성 Polaris', 'KANGTA', 244), 0))).toBe(true); + expect(isConfident(scoreResult({ title: '2년만에', artist: '김동률', durationSec: 300 }, yt('2년만에 After Two Years', '김동률', 301), 0))).toBe(true); + expect(isConfident(scoreResult({ title: '그대', artist: '더클래식', durationSec: 200 }, yt('그대', '더 클래식 The Classic', 201), 0))).toBe(true); + // the split title needs the artist: another singer's "작은 촛불 Little Flame" with the same length is not it + expect(isConfident(scoreResult({ title: '작은 촛불', artist: '이주연', durationSec: 247 }, yt('작은 촛불 Little Flame', '정바름 JEONG BARUM', 248), 0))).toBe(false); + // a Latin part that numbers the song is not split off + expect(isConfident(scoreResult({ title: '너에게 쓰는 편지', artist: '엠씨더맥스', durationSec: 250 }, yt('너에게 쓰는 편지 Part 2', '엠씨더맥스', 250), 0))).toBe(false); +}); + +test('YouTube matching searches again by the artist\'s other-script name', async () => { + const db = freshDb(); + const c = new Catalog(db); + // another song's saved results know the name "DJ DOC" + const [other] = c.upsert({ title: 'DOC와 춤을', artists: [{ name: 'DJ DOC' }], durationSec: 200, source: 't', ref: 'o' }); + db.query("UPDATE songs SET youtube_id = 'docdance001', youtube_status = 'matched', youtube_source = 'ytmusic' WHERE id = ?").run(other); + db.query("INSERT INTO youtube_candidates (song_id, video_id, title, channel, duration_sec, score, source) VALUES (?, 'docdance001', 'DOC와 춤을', 'DJ DOC', 200, 100, 'ytmusic')").run(other); + const [murphy] = c.upsert({ title: '머피의 법칙', artists: [{ name: '디제이디오씨' }], durationSec: 230, source: 't', ref: 'm' }); + const asked: string[] = []; + const searchSongs = async (q: string): Promise => { + asked.push(q); + if (q === 'DJ DOC 머피의 법칙') return [{ videoId: 'murphy00001', title: '머피의 법칙', artists: 'DJ DOC', album: null, durationSec: 232 }]; + return [{ videoId: 'wrong000001', title: '머피의 법칙', artists: '다른 가수', album: null, durationSec: 180 }]; + }; + const ok = async (id: string) => ({ state: 'ok' as const, author: 'DJ DOC - Topic', title: id }); + expect(await matchYoutube(db, { searchSongs }, { delayMs: 0 }, ok)).toMatchObject({ matched: 1 }); + expect(asked).toEqual(['디제이디오씨 머피의 법칙', 'DJ DOC 머피의 법칙']); + const st = (id: number) => db.query<{ youtube_status: string; youtube_id: string }, [number]>('SELECT youtube_status, youtube_id FROM songs WHERE id = ?').get(id)!; + expect(st(murphy!)).toEqual({ youtube_status: 'matched', youtube_id: 'murphy00001' }); + + // songs already left as 후보 by an earlier search: research-youtube searches them again once + const [everybody] = c.upsert({ title: 'Everybody', artists: [{ name: '디제이디오씨' }], durationSec: 240, source: 't', ref: 'e' }); + db.query("UPDATE songs SET youtube_id = 'fan', youtube_status = 'candidate', youtube_source = 'norabose' WHERE id = ?").run(everybody); + db.query("INSERT INTO youtube_candidates (song_id, video_id, title, channel, duration_sec, score, source) VALUES (?, 'roola000001', 'EVERYBODY', '룰라', 230, 50, 'ytmusic')").run(everybody); + // the same title by another singer with the exact length comes first, but only DJ DOC's song counts + const res: Record = { + 'DJ DOC Everybody': [ + { videoId: 'cover000001', title: 'Everybody', artists: 'KY Noraebang', album: null, durationSec: 240 }, + { videoId: 'everybody01', title: 'Everybody', artists: 'DJ DOC', album: null, durationSec: 241 }, + ], + }; + expect(await researchYoutube(db, { searchSongs: async () => [] }, { dryRun: true })).toMatchObject({ songs: 1 }); + expect(await researchYoutube(db, { searchSongs: async (q) => res[q] ?? [] }, { delayMs: 0 }, ok)).toMatchObject({ songs: 1, matched: 1 }); + expect(st(everybody!)).toEqual({ youtube_status: 'matched', youtube_id: 'everybody01' }); + expect((await researchYoutube(db, { searchSongs: async () => [] }, { delayMs: 0 }, ok)).songs).toBe(0); +}); + test('artist names learnt from confirmed songs (2+ songs) confirm waiting candidates', async () => { const db = freshDb(); const c = new Catalog(db); @@ -82,16 +160,19 @@ test('artist names learnt from confirmed songs (2+ songs) confirm waiting candid }; const happy = waiting('Happy', '태연', 220, { video: 'happyvid001', title: 'Happy', artist: 'TAEYEON', dur: 225 }); const flower = waiting('Flower Dance', 'DJ Okawari', null, { video: 'ashiyane001', title: 'Flower Dance', artist: 'Ashiyane', dur: 277 }); + // 30 seconds off: confirmed by title + artist, unless the video itself is an MV const far = waiting('Weekend', '태연', 230, { video: 'weekend0001', title: 'Weekend', artist: 'TAEYEON', dur: 260 }); + const farMv = waiting('Fine', '태연', 230, { video: 'finemv00001', title: 'Fine', artist: 'TAEYEON', dur: 260 }); const dry = await reconfirmCandidates(db, { dryRun: true }); - expect(dry.matched).toBe(1); - expect(db.query<{ n: number }, []>("SELECT COUNT(*) n FROM songs WHERE youtube_status = 'candidate'").get()!.n).toBe(3); // dry run writes nothing - const stats = await reconfirmCandidates(db, { delayMs: 0 }, async (id) => ({ state: 'ok', author: 'TAEYEON - Topic', title: id })); - expect(stats.matched).toBe(1); + expect(dry.matched).toBe(3); + expect(db.query<{ n: number }, []>("SELECT COUNT(*) n FROM songs WHERE youtube_status = 'candidate'").get()!.n).toBe(4); // dry run writes nothing + const stats = await reconfirmCandidates(db, { delayMs: 0 }, async (id) => ({ state: 'ok', author: 'TAEYEON - Topic', title: id === 'finemv00001' ? "TAEYEON 태연 'Fine' MV" : id })); + expect(stats).toMatchObject({ matched: 2, unconfirmed: 1 }); const st = (id: number) => db.query<{ youtube_status: string; youtube_id: string }, [number]>('SELECT youtube_status, youtube_id FROM songs WHERE id = ?').get(id)!; expect(st(happy)).toEqual({ youtube_status: 'matched', youtube_id: 'happyvid001' }); expect(st(flower).youtube_status).toBe('candidate'); - expect(st(far).youtube_status).toBe('candidate'); + expect(st(far)).toEqual({ youtube_status: 'matched', youtube_id: 'weekend0001' }); + expect(st(farMv).youtube_status).toBe('candidate'); }); function freshDb() { diff --git a/apps/server/src/collector/match-youtube.ts b/apps/server/src/collector/match-youtube.ts index f91f1ef..3861cdc 100644 --- a/apps/server/src/collector/match-youtube.ts +++ b/apps/server/src/collector/match-youtube.ts @@ -6,8 +6,9 @@ import type { Database } from 'bun:sqlite'; import { Job, sleep } from './catalog'; import { checkEmbed } from './oembed'; -import { isConfident, scoreResult, type Scored } from './score'; +import { confirmedDespiteLength, isConfident, isJunkTitle, isVideoTitle, namesOtherCut, scoreResult, type Scored } from './score'; import { learnArtistAliases } from './artist-aliases'; +import { otherScriptNames } from './other-names'; import { CookieError, type YtMusic, type YtmSong } from './ytmusic'; interface Row { @@ -41,6 +42,59 @@ function setAutoVideo(db: Database, id: number, status: 'matched' | 'candidate', ); } +/** + * A result confirmed despite a length difference (title + artist) whose actual video turns out to be a music video, + * inst., live, another mix … (the YouTube title, not the search row): left as 후보. + */ +function isOtherCut(c: Scored, videoTitle: string, songTitle: string): boolean { + if (!confirmedDespiteLength(c)) return false; + return (isVideoTitle(videoTitle) && !isVideoTitle(songTitle)) || (isJunkTitle(videoTitle) && !isJunkTitle(songTitle)) || namesOtherCut(videoTitle, songTitle); +} + +async function searchRetry(ytm: Pick, query: string, job: Job | null): Promise { + try { + return await ytm.searchSongs(query); + } catch (e) { + if (e instanceof CookieError) throw e; + job?.log(`search failed (${(e as Error).message}), waiting 60s`); + await sleep(60_000); + return ytm.searchSongs(query); + } +} + +/** Results of another search added to the ones we have (the same video once, its better score kept). */ +function mergeScored(a: Scored[], b: Scored[]): Scored[] { + const byId = new Map(a.map((c) => [c.videoId, c])); + for (const c of b) if (!byId.has(c.videoId) || byId.get(c.videoId)!.score < c.score) byId.set(c.videoId, c); + return [...byId.values()]; +} + +/** Keeps the best 5 results (already sorted) for the admin and for reconfirm-youtube. */ +function saveCandidates(db: Database, songId: number, scored: Scored[], src: string) { + for (const c of scored.slice(0, 5)) { + db.query( + `INSERT INTO youtube_candidates (song_id, video_id, title, channel, duration_sec, score, source) VALUES (?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(song_id, video_id) DO UPDATE SET title = excluded.title, channel = excluded.channel, duration_sec = excluded.duration_sec, score = excluded.score`, + ).run(songId, c.videoId, c.title, c.artists, c.durationSec, c.score, src); + } +} + +/** + * Confirms the best confident result that plays (and is not an MV/other cut when the length is off). + * Returns null when none did, true when the song was changed, false when an admin had settled it meanwhile. + */ +async function confirmFirst(db: Database, s: Row, scored: Scored[], src: string, embed: typeof checkEmbed, delayMs = 0): Promise { + for (const c of scored.filter(isConfident)) { + const e = await embed(c.videoId); + if (delayMs) await sleep(delayMs); + if (e.state !== 'ok' || isOtherCut(c, e.title, s.title)) continue; + const changed = setAutoVideo(db, s.id, 'matched', { videoId: c.videoId, author: e.author, title: e.title }, src); + if (changed && !s.duration_sec && c.durationSec) db.query('UPDATE songs SET duration_sec = ? WHERE id = ?').run(c.durationSec, s.id); + return changed; + } + return null; +} + export async function matchYoutube(db: Database, ytm: Pick, opts: MatchOptions = {}, embed = checkEmbed) { const retryBefore = Date.now() - (opts.retryDays ?? 7) * 86400_000; // Tries are remembered per search method: songs the weaker fallback could not settle are retried once the cookie works. @@ -60,6 +114,7 @@ export async function matchYoutube(db: Database, ytm: Pick scoreResult({ title: s.title, artist: s.artist, durationSec: s.duration_sec }, r, rank, aliases)); - scored.sort((a, b) => b.score - a.score); - for (const c of scored.slice(0, 5)) { - db.query( - `INSERT INTO youtube_candidates (song_id, video_id, title, channel, duration_sec, score, source) VALUES (?, ?, ?, ?, ?, ?, ?) - ON CONFLICT(song_id, video_id) DO UPDATE SET title = excluded.title, channel = excluded.channel, duration_sec = excluded.duration_sec, score = excluded.score`, - ).run(s.id, c.videoId, c.title, c.artists, c.durationSec, c.score, src); - } - let done = false; - for (const c of scored.filter(isConfident)) { - const e = await embed(c.videoId); - if (e.state === 'ok') { - done = true; - if (setVideo(s.id, 'matched', { videoId: c.videoId, author: e.author, title: e.title }, src)) { - if (!s.duration_sec && c.durationSec) db.query('UPDATE songs SET duration_sec = ? WHERE id = ?').run(c.durationSec, s.id); - stats.matched++; - } - break; + const song = { title: s.title, artist: s.artist, durationSec: s.duration_sec }; + let scored = (await searchRetry(ytm, `${s.artist.split(', ')[0]} ${s.title}`, job)).slice(0, 8).map((r, rank) => scoreResult(song, r, rank, aliases)); + // Melon wrote the artist in the other script ("디제이디오씨" for DJ DOC) and no result is by them: search again by that name + if (!scored.some(isConfident) && !scored.some((c) => c.artistOk || c.artistSound)) { + for (const name of otherNames(s.artist)) { + await sleep(opts.delayMs ?? 2000); + // only that artist's songs count from this search: the same title by someone else is not the song + const byArtist = (await searchRetry(ytm, `${name} ${s.title}`, job)).slice(0, 8).map((r, rank) => scoreResult(song, r, rank, aliases)).filter((c) => c.artistOk || c.artistSound); + scored = mergeScored(scored, byArtist); + if (scored.some(isConfident)) break; } } + scored.sort((a, b) => b.score - a.score); + saveCandidates(db, s.id, scored, src); + const confirmed = await confirmFirst(db, s, scored, src, embed); + let done = confirmed !== null; + if (confirmed) stats.matched++; if (!done && adminOwned(s.id)) done = true; // an admin settled it meanwhile if (!done) { // Not sure: keep the best playable guess as a candidate for the admin (title match first, then norabose's video). @@ -139,8 +181,9 @@ export async function matchYoutube(db: Database, ytm: Pick void } = {}, embed = checkEmbed) { @@ -148,7 +191,7 @@ export async function reconfirmCandidates(db: Database, opts: { dryRun?: boolean const songs = db .query( `SELECT id, title, artist, duration_sec FROM songs - WHERE deleted_at IS NULL AND youtube_status = 'candidate' AND COALESCE(youtube_source, '') <> 'admin' ORDER BY id`, + WHERE deleted_at IS NULL AND youtube_status IN ('candidate','none') AND COALESCE(youtube_source, '') <> 'admin' ORDER BY id`, ) .all(); const cands = db.query<{ video_id: string; title: string | null; channel: string | null; duration_sec: number | null }, [number]>( @@ -156,7 +199,7 @@ export async function reconfirmCandidates(db: Database, opts: { dryRun?: boolean ); const job = opts.dryRun ? null : new Job(db, 'reconfirm-youtube', songs.length); job?.log(`start ${songs.length} candidate songs, ${aliases.size} learnt artist names`); - const stats = { checked: songs.length, matched: 0, unplayable: 0, aliases: aliases.size }; + const stats = { checked: songs.length, matched: 0, unconfirmed: 0, aliases: aliases.size }; try { for (const [i, s] of songs.entries()) { const scored = cands @@ -164,23 +207,17 @@ export async function reconfirmCandidates(db: Database, opts: { dryRun?: boolean .map((c) => scoreResult({ title: s.title, artist: s.artist, durationSec: s.duration_sec }, { videoId: c.video_id, title: c.title ?? '', artists: c.channel ?? '', album: null, durationSec: c.duration_sec }, -1, aliases)) .filter(isConfident) .sort((a, b) => b.score - a.score); - for (const c of scored) { - if (opts.dryRun) { + // unconfirmed: none of them plays, or the video turned out to be an MV/another cut + if (opts.dryRun) { + const c = scored[0]; + if (c) { stats.matched++; opts.log?.(`${s.id}\t${s.artist} - ${s.title}\t→ ${c.artists} - ${c.title} (${c.lengthDiff ?? '?'}s)`); - break; } - const e = await embed(c.videoId); - await sleep(opts.delayMs ?? 300); - if (e.state !== 'ok') { - stats.unplayable++; - continue; - } - if (setAutoVideo(db, s.id, 'matched', { videoId: c.videoId, author: e.author, title: e.title }, 'ytmusic')) { - if (!s.duration_sec && c.durationSec) db.query('UPDATE songs SET duration_sec = ? WHERE id = ?').run(c.durationSec, s.id); - stats.matched++; - } - break; + } else if (scored.length) { + const r = await confirmFirst(db, s, scored, 'ytmusic', embed, opts.delayMs ?? 300); + if (r) stats.matched++; + else if (r === null) stats.unconfirmed++; } job?.progress(i + 1); } @@ -193,3 +230,81 @@ export async function reconfirmCandidates(db: Database, opts: { dryRun?: boolean } return stats; } + +/** + * Searches again, by the artist's name in the other script, the 후보/못 찾음 songs whose saved results have no song by + * that artist at all ("디제이디오씨 Everybody" → "DJ DOC Everybody"). Only confirms: an unsure song keeps its 후보 video. + * Each song is searched again once per search method (signal yt_research_). + */ +export async function researchYoutube( + db: Database, + ytm: Pick, + opts: { limit?: number; delayMs?: number; deadline?: number; dryRun?: boolean; source?: 'ytmusic' | 'youtube'; log?: (s: string) => void } = {}, + embed = checkEmbed, +) { + const src = opts.source ?? 'ytmusic'; + const kind = `yt_research_${src}`; + const aliases = learnArtistAliases(db); + const otherNames = otherScriptNames(db); + const cands = db.query<{ video_id: string; title: string | null; channel: string | null; duration_sec: number | null }, [number]>( + "SELECT video_id, title, channel, duration_sec FROM youtube_candidates WHERE song_id = ? AND source IN ('ytmusic','youtube')", + ); + const targets: (Row & { names: string[] })[] = []; + for (const s of db + .query( + `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 ('candidate','none') AND COALESCE(s.youtube_source, '') <> 'admin' AND t.value IS NULL + ORDER BY l.level IS NULL, l.level, s.id`, + ) + .all(kind)) { + const song = { title: s.title, artist: s.artist, durationSec: s.duration_sec }; + const saved = cands.all(s.id).map((c) => scoreResult(song, { videoId: c.video_id, title: c.title ?? '', artists: c.channel ?? '', album: null, durationSec: c.duration_sec }, -1, aliases)); + if (saved.some((c) => isConfident(c) || c.artistOk || c.artistSound)) continue; + const names = otherNames(s.artist); + if (names.length) targets.push({ ...s, names }); + if (opts.limit !== undefined && targets.length >= opts.limit) break; + } + const stats = { songs: targets.length, searches: 0, matched: 0 }; + if (opts.dryRun) { + for (const s of targets) opts.log?.(`${s.id}\t${s.artist} - ${s.title}\t→ ${s.names.join(' | ')}`); + return stats; + } + const job = new Job(db, 'research-youtube', targets.length); + job.log(`start ${targets.length} songs via ${src}, searching again by the artist's other-script name`); + try { + for (const [i, s] of targets.entries()) { + if (opts.deadline !== undefined && Date.now() >= opts.deadline) { + job.log(`deadline reached after ${i}/${targets.length}`); + break; + } + const song = { title: s.title, artist: s.artist, durationSec: s.duration_sec }; + let scored: Scored[] = []; + for (const name of s.names) { + // only that artist's songs: the same title by someone else is not the song + const found = (await searchRetry(ytm, `${name} ${s.title}`, job)).slice(0, 8).map((r, rank) => scoreResult(song, r, rank, aliases)).filter((c) => c.artistOk || c.artistSound); + stats.searches++; + scored = mergeScored(scored, found); + await sleep(opts.delayMs ?? 2000); + if (scored.some(isConfident)) break; + } + scored.sort((a, b) => b.score - a.score); + saveCandidates(db, s.id, scored, src); + if (await confirmFirst(db, s, scored, src, embed)) { + stats.matched++; + opts.log?.(`${s.id}\t${s.artist} - ${s.title}\t→ ${scored.find(isConfident)?.artists} - ${scored.find(isConfident)?.title}`); + } + db.query('INSERT INTO signals (song_id, kind, value, fetched_at) VALUES (?, ?, ?, ?) ON CONFLICT(song_id, kind) DO UPDATE SET value = excluded.value, fetched_at = excluded.fetched_at').run(s.id, kind, Date.now(), Date.now()); + job.progress(i + 1); + if ((i + 1) % 25 === 0) job.log(`research ${i + 1}/${targets.length} matched ${stats.matched}`); + } + job.log(`research done: matched ${stats.matched} of ${targets.length} (${stats.searches} searches)`); + job.finish('done'); + } catch (e) { + job.log(`stopped: ${(e as Error).message}`); + job.finish(e instanceof CookieError ? 'stopped' : 'failed'); + throw e; + } + return stats; +} diff --git a/apps/server/src/collector/other-names.ts b/apps/server/src/collector/other-names.ts new file mode 100644 index 0000000..2d67fff --- /dev/null +++ b/apps/server/src/collector/other-names.ts @@ -0,0 +1,52 @@ +/** + * An artist's name in the other script, for searching again: Melon writes "디제이디오씨" while YouTube Music knows + * "DJ DOC", so "디제이디오씨 Everybody" finds nothing. Names come from artist names already seen in saved YouTube Music + * results, kept when they sound the same (sound.ts); the most often seen first. + */ +import type { Database } from 'bun:sqlite'; +import { artistParts, compareKey } from '@mq/shared'; +import { hangulSkeletons, latinSkeletons, soundsLike } from './sound'; + +const onlyHangul = (s: string) => /\p{Script=Hangul}/u.test(s) && !/[A-Za-z]/.test(s); +const onlyLatin = (s: string) => /[A-Za-z]/.test(s) && !/\p{Script=Hangul}/u.test(s); +const skeletons = (name: string) => (onlyHangul(name) ? [...new Set([...hangulSkeletons(name, false), ...hangulSkeletons(name, true)])] : latinSkeletons(name)); + +export function otherScriptNames(db: Database): (artist: string, max?: number) => string[] { + const seen = new Map>(); + const rows = db.query<{ song_id: number; channel: string }, []>("SELECT song_id, channel FROM youtube_candidates WHERE source = 'ytmusic' AND channel IS NOT NULL").all(); + for (const r of rows) { + for (const p of artistParts(r.channel)) { + const name = p.trim(); + if (!(onlyHangul(name) || onlyLatin(name)) || name.length > 40) continue; + let ids = seen.get(name); + if (!ids) seen.set(name, (ids = new Set())); + ids.add(r.song_id); + } + } + const index = new Map(); + for (const name of seen.keys()) + for (const k of skeletons(name)) { + if (k.length < 2) continue; + const list = index.get(k); + if (list) list.push(name); + else index.set(k, [name]); + } + return (artist, max = 2) => { + const out = new Map(); + for (const part of artistParts(artist)) { + if (!onlyHangul(part) && !onlyLatin(part)) continue; + for (const k of skeletons(part)) + for (const name of index.get(k) ?? []) { + if (compareKey(name) === compareKey(part) || !soundsLike(part, name)) continue; + out.set(name, Math.max(out.get(name) ?? 0, seen.get(name)!.size)); + } + } + // one spelling per sound ("DJ DOC" and "DJ. DOC"): the most seen + const byKey = new Map(); + for (const [name, n] of out) { + const k = compareKey(name); + if ((byKey.get(k)?.[1] ?? -1) < n) byKey.set(k, [name, n]); + } + return [...byKey.values()].sort((a, b) => b[1] - a[1]).slice(0, max).map(([name]) => name); + }; +} diff --git a/apps/server/src/collector/score.ts b/apps/server/src/collector/score.ts index 838be59..e9590eb 100644 --- a/apps/server/src/collector/score.ts +++ b/apps/server/src/collector/score.ts @@ -1,12 +1,23 @@ /** Scores YouTube Music results against a song (docs/08 매칭 순서 4~5). */ -import { artistKeys, keysOverlap, titleKeys } from '@mq/shared'; +import { artistKeys, artistParts, keysOverlap, titleKeys, titleRunKeys } from '@mq/shared'; +import { soundsLike } from './sound'; import type { YtmSong } from './ytmusic'; const JUNK = /\b(inst|instrumental|mr|karaoke|cover|remix|sped ?up|slowed|live|ver|version|acoustic|made famous by|in the style of|originally performed|tribute)\b|노래방|커버|라이브|반주/i; /** "Radio Edit Version", "Original Version", "7\" Version" are the same recording, not a different take. */ const BENIGN = /\b(?:radio edit|original|album|single|mono|stereo|remaster(?:ed)?)\s*(?:ver\.?|version)?|\d{1,2}"\s*(?:ver\.?|version)|\bedit\b/gi; -/** Auto-confirm only within this length difference (when both lengths are known). */ +/** Without the artist, a title only confirms with a length this close (when both lengths are known). */ export const MAX_LENGTH_DIFF = 15; +/** An artist that only sounds the same in the other script (디제이디오씨 ~ DJ DOC) confirms only this close. */ +export const MAX_SOUND_LENGTH_DIFF = 8; +/** A music video or another cut of the song: never confirmed by title and artist alone when the length is off. */ +/** Words of a different cut or recording: a mix, an edit, a dated re-recording … (only used when the length is off too). */ +const CUT = /\b(?:mix|edit|ver|version|edition|demo|medley|reprise|session|take|cut|re-?recorded|piano|orchestra|strings|sleep|lo-?fi|extended|short|full|intro|outro)\b|\b(?:19|20)\d{2}\b|버전|믹스/gi; +/** A remaster is the same recording: "(2021 Remastered)" is not a dated re-recording. */ +const REMASTER = /\b(?:(?:19|20)\d{2}\s+)?remaster(?:ed)?(?:\s+(?:19|20)\d{2})?\b|\bradio edit\b|\b(?:original|album|single)\s+(?:ver\.?|version)\b/gi; +/** Titles an artist reuses album after album ("Intro"): the same title says nothing when the length is off. */ +const GENERIC_TITLE = /^\s*(?:intro|outro|interlude|skit|prologue|epilogue|opening|ending|theme|bonus\s+track|hidden\s+track)\s*\d*\s*$/i; +const VIDEO = /\b(?:m\/?v|music\s+video|official\s+video|performance\s+video|lyrics?\s+video|visualizer|teaser|dance\s+practice|special\s+clip|full\s+ver)\b|뮤직\s*비디오|뮤비|직캠/i; /** Song-side artist key → YouTube-side keys learnt from already confirmed songs ("태연" → "taeyeon"). */ export type ArtistAliases = Map>; @@ -15,7 +26,15 @@ export interface Scored extends YtmSong { score: number; titleOk: boolean; artistOk: boolean; + /** The artist is only written in the other script and sounds the same (디제이디오씨 ~ DJ DOC). */ + artistSound: boolean; junk: boolean; + /** Looks like a music video, lyric video, stage clip etc. */ + video: boolean; + /** Names a mix, edit, year … that the song's title does not: another cut when the length is off too. */ + otherCut: boolean; + /** The song's title is one artists reuse ("Intro"). */ + genericTitle: boolean; /** |song length − result length| in seconds, null when either is unknown. */ lengthDiff: number | null; } @@ -24,6 +43,27 @@ export function isJunkTitle(title: string): boolean { return JUNK.test(title.replace(BENIGN, ' ')); } +/** A music video, lyric video, live stage … or a cover/inst./remix (isJunkTitle): not the plain recording. */ +export function isVideoTitle(title: string): boolean { + return VIDEO.test(title); +} + +/** Some name of the song's artist sounds like some name of the result's, across Hangul and Latin. */ +export function artistsSoundAlike(songArtist: string, resultArtists: string): boolean { + const b = artistParts(resultArtists); + return artistParts(songArtist).some((x) => b.some((y) => soundsLike(x, y))); +} + +function cutWords(title: string): string[] { + return (title.replace(REMASTER, ' ').match(CUT) ?? []).map((w) => w.toLowerCase()); +} + +/** The result names a mix, edit, version, year … that the song's own title does not ("(Sleep Mix)", "2021"). */ +export function namesOtherCut(resultTitle: string, songTitle: string): boolean { + const own = cutWords(songTitle); + return cutWords(resultTitle).some((w) => !own.includes(w)); +} + export function artistsMatch(songArtist: string, resultArtists: string, aliases?: ArtistAliases): boolean { const a = artistKeys(songArtist); const b = artistKeys(resultArtists); @@ -32,8 +72,10 @@ export function artistsMatch(songArtist: string, resultArtists: string, aliases? } export function scoreResult(song: { title: string; artist: string; durationSec: number | null }, r: YtmSong, rank: number, aliases?: ArtistAliases): Scored { - const titleOk = keysOverlap(titleKeys(song.title), titleKeys(r.title)); const artistOk = artistsMatch(song.artist, r.artists, aliases); + const artistSound = !artistOk && artistsSoundAlike(song.artist, r.artists); + // "약속" = "약속 The Promise" only for the same artist: title-only matches stay strict + const titleOk = keysOverlap(titleKeys(song.title), titleKeys(r.title)) || ((artistOk || artistSound) && keysOverlap(titleRunKeys(song.title), titleRunKeys(r.title))); const lengthDiff = song.durationSec && r.durationSec ? Math.abs(song.durationSec - r.durationSec) : null; let score = 0; if (titleOk) score += 50; @@ -49,15 +91,31 @@ export function scoreResult(song: { title: string; artist: string; durationSec: // Junk words only count when the original title doesn't have them (a real "Remix" song stays fine). const junk = isJunkTitle(r.title) && !isJunkTitle(song.title); if (junk) score -= 40; - return { ...r, score, titleOk, artistOk, junk, lengthDiff }; + const video = isVideoTitle(r.title) && !isVideoTitle(song.title); + const otherCut = namesOtherCut(r.title, song.title); + return { ...r, score, titleOk, artistOk, artistSound, junk, video, otherCut, genericTitle: GENERIC_TITLE.test(song.title), lengthDiff }; } /** - * Auto-confirm: title match plus artist (80) or exact length (70); a far-off length or junk words drop it below, - * and a length more than MAX_LENGTH_DIFF seconds off never confirms. + * Auto-confirm, any of: + * - title + artist, whatever the length (a different take or a wrong Melon length), unless the result is a cover/inst./live + * (junk), a music video or not an official Topic track — and when the length is off, unless it names another mix/edit/year + * or the title is one artists reuse ("Intro"); + * - title + artist written in the other script that sounds the same, within MAX_SOUND_LENGTH_DIFF seconds; + * - title + a close length (score 70+, e.g. without the artist), within MAX_LENGTH_DIFF seconds. */ export function isConfident(s: Scored): boolean { - return s.titleOk && s.score >= 70 && (s.lengthDiff === null || s.lengthDiff <= MAX_LENGTH_DIFF); + if (!s.titleOk) return false; + const plain = !s.junk && !s.video && s.topic !== false; + const lengthOff = s.lengthDiff !== null && s.lengthDiff > MAX_LENGTH_DIFF; + if (s.artistOk && plain && !(lengthOff && (s.otherCut || s.genericTitle))) return true; + if (s.artistSound && plain && s.lengthDiff !== null && s.lengthDiff <= MAX_SOUND_LENGTH_DIFF) return true; + return s.score >= 70 && (s.lengthDiff === null || s.lengthDiff <= MAX_LENGTH_DIFF); +} + +/** Confirmed only because title and artist match while the length is off: the video itself must not be an MV or another cut. */ +export function confirmedDespiteLength(s: Scored): boolean { + return isConfident(s) && s.lengthDiff !== null && s.lengthDiff > MAX_LENGTH_DIFF; } /** Why a result was not confirmed, in plain words for the admin list ("가수 다름: Ashiyane"). Empty when confident. */ @@ -65,9 +123,16 @@ export function notConfidentReasons(s: Scored): string[] { if (isConfident(s)) return []; const out: string[] = []; if (!s.titleOk) out.push(`제목 다름: ${s.title}`); - if (!s.artistOk) out.push(`가수 다름: ${s.artists || '알 수 없음'}`); - if (s.lengthDiff !== null && s.lengthDiff > MAX_LENGTH_DIFF) out.push(`길이 ${s.lengthDiff}초 차이`); + if (s.artistSound) out.push(`가수 이름 발음만 같음: ${s.artists}`); + else if (!s.artistOk) out.push(`가수 다름: ${s.artists || '알 수 없음'}`); + const lengthLimit = s.artistOk ? null : s.artistSound ? MAX_SOUND_LENGTH_DIFF : MAX_LENGTH_DIFF; + if (s.lengthDiff !== null && lengthLimit !== null && s.lengthDiff > lengthLimit) out.push(`길이 ${s.lengthDiff}초 차이`); + if (s.artistOk && s.lengthDiff !== null && s.lengthDiff > MAX_LENGTH_DIFF) { + if (s.otherCut) out.push(`길이 ${s.lengthDiff}초 차이 · 믹스·연도 등 다른 버전 표시`); + else if (s.genericTitle) out.push(`길이 ${s.lengthDiff}초 차이 · 흔한 제목(Intro 등)`); + } if (s.junk) out.push('커버·노래방·라이브 등 다른 버전'); + if (s.video) out.push('뮤직비디오·가사 영상 등'); if (s.topic === false) out.push('공식 음원 채널 아님'); if (!out.length && s.lengthDiff === null) out.push('곡 길이 정보 없음'); if (!out.length) out.push('점수 부족'); diff --git a/apps/server/src/collector/sound.ts b/apps/server/src/collector/sound.ts new file mode 100644 index 0000000..5a895f1 --- /dev/null +++ b/apps/server/src/collector/sound.ts @@ -0,0 +1,173 @@ +/** + * Does a Hangul artist name sound like a Latin one ("디제이디오씨" ↔ "DJ DOC", "조규찬" ↔ "Cho Kyu Chan")? + * Both sides become a consonant skeleton (vowels dropped, similar consonants folded); same skeleton = same sound. + * Romanized Korean names ("Park Jung Hyun") must also have the same vowels. Still loose, so a sound match only confirms + * with the same title and a close length (score.ts). + */ + +// 19 initials ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ and 28 finals (none, ㄱ … ㅎ) +const INITIAL = ['K', 'K', 'N', 'T', 'T', 'L', 'M', 'P', 'P', 'S', 'S', '', 'J', 'J', 'J', 'K', 'T', 'P', 'H']; +const FINAL = ['', 'K', 'K', 'KS', 'N', 'NJ', 'N', 'T', 'L', 'LK', 'LM', 'LP', 'LS', 'LT', 'LP', 'L', 'M', 'P', 'PS', 'T', 'T', 'N', 'T', 'T', 'K', 'T', 'P', '']; +/** Latin letters read one by one in Korean ("DJ" → 디제이). */ +const LETTER: Record = { + a: '에이', b: '비', c: '씨', d: '디', e: '이', f: '에프', g: '지', h: '에이치', i: '아이', j: '제이', k: '케이', l: '엘', m: '엠', + n: '엔', o: '오', p: '피', q: '큐', r: '알', s: '에스', t: '티', u: '유', v: '브이', w: '더블유', x: '엑스', y: '와이', z: '제트', +}; +const DIGIT = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; +const squeeze = (s: string) => s.replace(/(.)\1+/g, '$1'); + +export function hangulSkeleton(s: string): string { + let out = ''; + for (const ch of s) { + const c = ch.codePointAt(0)! - 0xac00; + if (c < 0 || c > 11171) continue; + out += INITIAL[Math.floor(c / 588)]! + FINAL[c % 28]!; + } + return squeeze(out); +} + +export function latinSkeleton(word: string): string { + const s = word + .toLowerCase() + .replace(/[0-9]/g, (d) => DIGIT[Number(d)]!) + .replace(/[^a-z]/g, '') + .replace(/r(?![aeiouy])/g, '') + .replace(/ng/g, 'N') + .replace(/t?ch/g, 'J') + .replace(/sh/g, 'S') + .replace(/th/g, 'T') + .replace(/ph/g, 'P') + .replace(/h(?=$|[^aeiouwy])/g, '') + .replace(/h/g, 'H') + .replace(/ck/g, 'K') + .replace(/x/g, 'KS') + .replace(/q/g, 'K') + .replace(/c(?=[eiy])/g, 'S') + .replace(/[cgk]/g, 'K') + .replace(/[bpfv]/g, 'P') + .replace(/[dt]/g, 'T') + .replace(/[lr]/g, 'L') + .replace(/[jz]/g, 'J') + .replace(/[smn]/g, (x) => x.toUpperCase()) + .replace(/[aeiouwyh]/g, ''); + return squeeze(s); +} + +/** A Latin name's skeletons: words as read, short all-caps words spelled letter by letter (DJ DOC, S.E.S.). */ +export function latinSkeletons(name: string): string[] { + let acc = ['']; + for (const t of name.split(/[\s.\-·]+/).filter(Boolean)) { + const opts = new Set([latinSkeleton(t)]); + if (/^[A-Za-z]{1,5}$/.test(t) && (t === t.toUpperCase() || t.length <= 2)) opts.add(hangulSkeleton([...t.toLowerCase()].map((c) => LETTER[c]).join(''))); + const next: string[] = []; + for (const a of acc) for (const o of opts) next.push(a + o); + acc = next.slice(0, 64); + } + return [...new Set(acc.map(squeeze))]; +} + +/** Vowel class per Hangul vowel ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ: romanized names blur ㅓ/ㅗ/ㅜ/ㅡ ("Jung", "Jun") and ㅐ/ㅔ. */ +const VOWEL_CLASS = 'AEAEOEOEOAEEOOOEIOOII'; +/** Romanized vowels (with their glides) and their class, as Korean names are spelt in Latin letters. */ +const LATIN_VOWEL: [string, string][] = ( + [ + ['A', 'a ah ya wa'], + ['E', 'ae e eh ye yae we wae oe oi ai ay'], + ['O', 'eo yeo o oh yo u uh oo ou wo woo yu you yoo eu'], + ['I', 'i ee wi ui wee'], + ] as const +).flatMap(([c, vs]) => vs.split(' ').map((v) => [v, c] as [string, string])); +const ONSET = ['', 'b', 'ch', 'd', 'g', 'h', 'j', 'jj', 'k', 'kk', 'l', 'm', 'n', 'p', 'pp', 'r', 's', 'sh', 'ss', 't', 'tt']; +const CODA = ['', 'k', 'n', 'ng', 't', 'l', 'm', 'p', 'r', 'rk', 'h']; + +/** Vowel classes of a word read as a romanized Korean name ("hyun" → O, "taeyeon" → EO); empty when it isn't one. */ +function romanizedVowels(word: string): string[] { + const memo = new Map(); + // afterVowel: the syllable before ended in a vowel, so a bare vowel would have joined it ("baek" is not ba-ek) + const from = (i: number, afterVowel: boolean): string[] => { + if (i === word.length) return ['']; + const key = `${i}${afterVowel}`; + const hit = memo.get(key); + if (hit) return hit; + const out = new Set(); + for (const on of ONSET) { + if (!word.startsWith(on, i)) continue; + for (const [v, c] of LATIN_VOWEL) { + const j = i + on.length; + if (!word.startsWith(v, j) || (afterVowel && !on && !/^[wy]/.test(v))) continue; + for (const co of CODA) { + if (!word.startsWith(co, j + v.length)) continue; + for (const rest of from(j + v.length + co.length, !co)) if (out.size < 16) out.add(c + rest); + } + } + } + memo.set(key, [...out]); + return [...out]; + }; + return from(0, false); +} + +/** + * Vowel classes of a Latin name when every word reads as a romanized Korean name ("Park Jung Hyun" → AOO), else null: + * English words and spelt-out letters ("Fly to the Sky", "DJ DOC") are compared by consonants only. + */ +export function latinVowels(name: string): string[] | null { + let acc = ['']; + for (const t of name.toLowerCase().split(/[\s.\-·]+/).filter(Boolean)) { + const vs = /^[a-z]+$/.test(t) ? romanizedVowels(t) : []; + if (!vs.length) return null; + acc = acc.flatMap((a) => vs.map((v) => a + v)).slice(0, 32); + } + return acc; +} + +/** + * A Hangul name's forms: consonant skeleton and vowel classes, as written and, for Western-order names, with the surname + * last (김보경 ↔ Bo Kyung Kim); 이/임/유/류 may start with L (Lee). + */ +export function hangulForms(name: string, surnameLast: boolean): { skeleton: string; vowels: string }[] { + const h = [...name].filter((c) => /\p{Script=Hangul}/u.test(c)); + if (!h.length) return []; + const form = (syl: string[]) => ({ + skeleton: hangulSkeleton(syl.join('')), + vowels: syl.map((c) => { + const n = c.codePointAt(0)! - 0xac00; + return n < 0 || n > 11171 ? '' : VOWEL_CLASS[Math.floor((n % 588) / 28)]!; + }).join(''), + }); + const out = [form(h)]; + if (surnameLast && h.length >= 2 && h.length <= 4) { + const rotated = form([...h.slice(1), h[0]!]); + out.push(rotated); + if (/^[이임유류리림]/.test(h[0]!)) { + out.push({ ...out[0]!, skeleton: 'L' + out[0]!.skeleton }, { ...rotated, skeleton: squeeze(hangulSkeleton(h.slice(1).join('')) + 'L' + hangulSkeleton(h[0]!)) }); + } + } + return out.map((f) => ({ ...f, skeleton: squeeze(f.skeleton) })); +} + +/** Consonant skeletons of a Hangul name (as hangulForms), for indexing. */ +export function hangulSkeletons(name: string, surnameLast: boolean): string[] { + return [...new Set(hangulForms(name, surnameLast).map((f) => f.skeleton))]; +} + +const onlyHangul = (s: string) => /\p{Script=Hangul}/u.test(s) && !/[A-Za-z]/.test(s); +const onlyLatin = (s: string) => /[A-Za-z]/.test(s) && !/\p{Script=Hangul}/u.test(s); + +/** + * One name in Hangul, the other in Latin letters, and they sound the same: same consonants, and for a romanized Korean + * name also the same vowels (박정현 is not Park Jin Young). + */ +export function soundsLike(a: string, b: string): boolean { + let h: string; + let l: string; + if (onlyHangul(a) && onlyLatin(b)) [h, l] = [a, b]; + else if (onlyHangul(b) && onlyLatin(a)) [h, l] = [b, a]; + else return false; + // spelt letter by letter: "DJ DOC" = 디제이디오씨, "S.E.S." = 에스이에스 + const tokens = l.split(/[\s.\-·]+/).filter(Boolean); + if (tokens.every((t) => /^[A-Z]{1,5}$/.test(t)) && tokens.map((t) => [...t.toLowerCase()].map((c) => LETTER[c]).join('')).join('') === h.replace(/\s/g, '')) return true; + const skeletons = latinSkeletons(l); + const vowels = latinVowels(l); + return hangulForms(h, /[A-Za-z]\s+[A-Za-z]/.test(l)).some((f) => f.skeleton.length >= 2 && skeletons.includes(f.skeleton) && (!vowels || vowels.includes(f.vowels))); +} diff --git a/docs/12-decisions.md b/docs/12-decisions.md index 38cfabc..6ee37de 100644 --- a/docs/12-decisions.md +++ b/docs/12-decisions.md @@ -193,3 +193,10 @@ ## 원곡 찾기 자동 확정 기준 (2026-10-22) 1. 제목 일치 + 가수 일치(라틴 악센트 무시 JAŸ-Z=JAY-Z, `및`·with·feat 구분, 앞 The 무시, 확정곡 2곡 이상에서 확인된 한글/영문 이름 짝 태연=TAEYEON) + 길이 차이 15초 이내 + 커버·노래방·라이브·다른 버전 아님. "Radio Edit/Original Version"은 같은 녹음으로 본다 2. 쌓인 후보는 `cli.ts reconfirm-youtube`로 저장된 검색 결과를 다시 비교했다(2,313곡 중 282곡 확정). 관리자 원곡 찾기 목록은 채널 이름 대신 실제 가수와 확정 안 된 이유를 보여 준다 + +## 원곡 찾기 자동 확정 기준 2차 (2026-10-29, 사용자 확인) +1. **제목 + 가수가 맞으면 곡 길이가 달라도 확정**(다른 녹음·멜론 길이 오류). 단 길이가 15초 넘게 다를 때는 결과가 MV·가사 영상·inst.·라이브·커버이거나, 노래 제목에 없는 믹스/에디트/버전/연도(`(Sleep Mix)`, `(2021)`)가 붙었거나, 제목이 Intro·Outro처럼 흔하면 보류. 실제 영상 제목(oEmbed)도 같은 기준으로 한 번 더 본다. 리마스터는 같은 녹음. 공식 Topic이 아닌 일반 유튜브 영상은 길이 무시 안 함 +2. **"한글 English" 표기 나눠 비교**: `북극성 Polaris`, `헤이즈 Heize`, `더 클래식 The Classic`(괄호 없이 띄어쓰기로 붙은 것). 제목을 나눠 맞춘 것은 가수도 맞을 때만 인정(다른 가수의 `작은 촛불 Little Flame`과 길이가 같다고 확정하지 않음). 뒤 영어가 몇 번째 곡을 뜻하면(`Part 2`, `I`, `Vol.1`) 나누지 않고, `/`·`+`로 여러 곡을 이은 메들리도 나누지 않음 +3. **한글/영문 가수 이름은 발음으로 비교**(`collector/sound.ts`): 자음 뼈대가 같고, `Park Jung Hyun`처럼 로마자 한국 이름이면 모음 계열(아/에/오/이)도 같아야 함. 성이 뒤로 간 이름(`Bo Kyung Kim`), 알파벳 읽기(`DJ DOC`=디제이디오씨, `S.E.S.`=에스이에스) 포함. 발음으로만 맞은 곡은 제목이 같고 길이 차이 8초 이내일 때만 확정 +4. **다른 문자 이름으로 다시 검색**: 결과에 그 가수의 곡이 하나도 없으면, 저장된 검색 결과에서 발음이 같은 다른 문자 이름(`DJ DOC`)을 찾아 `이름 제목`으로 한 번 더 검색(매일 원곡 찾기에 포함). 이 검색 결과는 그 가수 곡일 때만 확정. 이미 쌓인 후보는 `cli.ts research-youtube`(검색 방법별 한 번씩, `yt_research_`) +5. 운영 적용: `reconfirm-youtube`가 후보·못 찾음 곡까지 다시 비교 diff --git a/packages/shared/src/match-key.test.ts b/packages/shared/src/match-key.test.ts index 7d4dc52..9cb6984 100644 --- a/packages/shared/src/match-key.test.ts +++ b/packages/shared/src/match-key.test.ts @@ -1,5 +1,5 @@ import { expect, test } from 'bun:test'; -import { artistKeys, keysOverlap, looseKey, songMatchKey, titleKeys } from './match-key'; +import { artistKeys, keysOverlap, looseKey, scriptRuns, songMatchKey, titleKeys, titleRunKeys } from './match-key'; test('loose keys ignore case, spaces and punctuation', () => { expect(looseKey('Bubble Pop!')).toBe('bubblepop'); @@ -33,3 +33,16 @@ test('compare keys fold Latin accents and split 및 / with', () => { expect(keysOverlap(artistKeys('Human League'), artistKeys('The Human League'))).toBe(true); expect(titleKeys('が')).not.toContain('か'); // kana marks stay }); + +test('"한글 English" names split into both parts, numbered parts and medleys do not', () => { + expect(scriptRuns('북극성 Polaris')).toEqual(['북극성', 'Polaris']); + expect(scriptRuns('2년만에 After Two Years')).toEqual(['2년만에', 'After Two Years']); + expect(scriptRuns('너에게 쓰는 편지 Part 2')).toEqual([]); + expect(scriptRuns('사랑도 추억도 I')).toEqual([]); + expect(scriptRuns('Overture/추억속의 재회/마도요')).toEqual([]); + expect(scriptRuns('북극성')).toEqual([]); + expect(artistKeys('헤이즈 Heize')).toEqual(expect.arrayContaining(['헤이즈', 'heize'])); + expect(artistKeys('더 클래식 The Classic')).toEqual(expect.arrayContaining(['더클래식', 'classic'])); + expect(titleRunKeys('약속 The Promise')).toContain('약속'); + expect(titleKeys('약속 The Promise')).not.toContain('약속'); // titleKeys stays strict +}); diff --git a/packages/shared/src/match-key.ts b/packages/shared/src/match-key.ts index f9edadb..4c309e5 100644 --- a/packages/shared/src/match-key.ts +++ b/packages/shared/src/match-key.ts @@ -35,17 +35,60 @@ export function titleKeys(title: string): string[] { return [...out]; } -/** Artist variants: split on , & / x 및 with feat and "(…)" so "RESCENE (리센느)" matches both names. */ -export function artistKeys(artist: string): string[] { - const out = new Set(); +/** + * Title variants of a "한글 English" title written without brackets ("북극성 Polaris" → 북극성, polaris), plus titleKeys. + * Looser than titleKeys: only use it together with a matching artist. + */ +export function titleRunKeys(title: string): string[] { + const bare = title.replace(TAIL, ' ').replace(/\s*[([{【][^)\]}】]*[)\]}】]/g, ' '); + return [...new Set([...titleKeys(title), ...scriptRuns(bare).map(compareKey).filter(Boolean)])]; +} + +const hasHangul = (s: string) => /\p{Script=Hangul}/u.test(s); +const hasLatin = (s: string) => /[A-Za-z]/.test(s); +/** A Latin part that says which song or part it is ("Part 2", "Vol.1", "II", "A"): splitting it off would join different songs. */ +const ORDINAL = /^(?:part|pt|vol|no|ver|chapter|ep|season)\b|^#|^[A-Z]$|^[IVX]+$|^[\d\s.]+$/i; + +/** + * Splits a name written in both scripts without brackets into its Hangul and Latin parts (other marks between them dropped): + * "북극성 Polaris" → ["북극성", "Polaris"], "헤이즈 Heize" → ["헤이즈", "Heize"]. Empty unless both scripts are there, + * and empty when a Latin part numbers the song ("편지 Part 2", "사랑도 추억도 I"). + */ +export function scriptRuns(s: string): string[] { + if (!hasHangul(s) || !hasLatin(s)) return []; + const RUN = /[\p{Script=Hangul}\d][\p{Script=Hangul}\d\s'’′`‘"“”!?.,~&…-]*|[\p{Script=Latin}\d][\p{Script=Latin}\d\s'’′`‘"“”!?.,~&…-]*/gu; + // several songs or names joined ("Overture/추억속의 재회/마도요", a medley) are not one name in two scripts + if (/[/+]/.test(s)) return []; + const runs = (s.match(RUN) ?? []).map((r) => r.trim()); + if (runs.some((r) => ORDINAL.test(r) || /\b(?:part|pt)\.?\s*\d/i.test(r))) return []; + // one Latin letter alone ("K") is too short to be a name or a title + return runs.filter((r) => /\p{Script=Hangul}/u.test(r) || r.replace(/[^A-Za-z\d]/g, '').length >= 2); +} + +/** + * The names inside an artist string: split on , & / x 및 with feat, "(…)" and "한글 English" so "RESCENE (리센느)" and + * "헤이즈 Heize" give both names. + */ +export function artistParts(artist: string): string[] { + const out: string[] = []; const parts = artist.split(/\s*(?:,|&|\/|×|\sx\s|\band\b|\s및\s|\bwith\b|\bvs\.?(?=\s)|·|\bfeat\.?|\bft\.)\s*/i); for (const p of [artist, ...parts]) { - const k = compareKey(p); - if (k) out.add(k); + out.push(p); + const inner = /^(.*?)\s*\(([^)]+)\)\s*$/.exec(p); + const names = inner ? [inner[1]!, inner[2]!] : [p]; + if (inner) out.push(...names); + for (const n of names) out.push(...scriptRuns(n)); + } + return out.filter((p) => compareKey(p)); +} + +/** Artist variants for comparing: each name of artistParts, also without a leading "The". */ +export function artistKeys(artist: string): string[] { + const out = new Set(); + for (const p of artistParts(artist)) { + out.add(compareKey(p)); const noThe = compareKey(p.replace(/^\s*the\s+/i, '')); if (noThe) out.add(noThe); - const inner = /^(.*?)\s*\(([^)]+)\)\s*$/.exec(p); - if (inner) for (const q of [inner[1]!, inner[2]!]) if (compareKey(q)) out.add(compareKey(q)); } return [...out]; }