원곡찾기 후보 자동 확정: 가수·제목 비교 오류 수정(라틴 악센트 JAŸ-Z=JAY-Z, '및'·with 구분, 앞 The 무시, 'X JAPAN'을 x로 자르던 것), 'Radio Edit/Original Version'은 같은 녹음으로, 'ver.' 뒤 괄호에서 못 잡던 버전 표시 수정. 이미 확정된 곡에서 2곡 이상 확인된 한글/영문 가수 이름(태연=TAEYEON)을 배워 씀(레이블·컴필레이션 제외). 길이 15초 넘게 다르면 자동 확정 안 함. cli reconfirm-youtube(저장된 검색 결과로 다시 비교, --dry-run). 관리자 원곡 찾기 목록에 채널 이름 대신 실제 가수와 확정 안 된 이유('가수 다름: Ashiyane', '길이 40초 차이')

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-22 08:08:50 +09:00
parent 4f888711b1
commit e3cb623773
12 changed files with 327 additions and 42 deletions

View File

@@ -1,5 +1,6 @@
/** Song catalog queries for the admin pages (docs/02 노래 관리 화면). */
import type { Database } from 'bun:sqlite';
import { explainCandidate } from './youtube-review';
import { chosung, songMatchKey } from '@mq/shared';
export function catalogStats(db: Database) {
@@ -126,8 +127,11 @@ export function getSong(db: Database, id: number) {
lyrics: db.query<{ text: string; source: string }, [number]>('SELECT text, source FROM lyrics WHERE song_id = ?').get(id),
sources: db.query<{ source: string; ref: string }, [number]>('SELECT source, ref FROM song_sources WHERE song_id = ?').all(id),
candidates: db
.query<Record<string, unknown>, [number]>('SELECT video_id, title, channel, duration_sec, score, source FROM youtube_candidates WHERE song_id = ? ORDER BY score DESC')
.all(id),
.query<{ video_id: string; title: string | null; channel: string | null; duration_sec: number | null; score: number; source: string }, [number]>(
'SELECT video_id, title, channel, duration_sec, score, source FROM youtube_candidates WHERE song_id = ? ORDER BY score DESC',
)
.all(id)
.map((c) => ({ ...c, reasons: explainCandidate(db, song as { title: string; artist: string; duration_sec: number | null }, c) })),
signals: db.query<{ kind: string; value: number }, [number]>('SELECT kind, value FROM signals WHERE song_id = ?').all(id),
};
}

View File

@@ -3,6 +3,31 @@
* Tabs split "searched but nothing found" from "not searched yet" (both are youtube_status 'none').
*/
import type { Database } from 'bun:sqlite';
import { learnArtistAliases } from '../collector/artist-aliases';
import { notConfidentReasons, scoreResult, type ArtistAliases } from '../collector/score';
let aliasCache: { db: Database; at: number; map: ArtistAliases } | null = null;
/** Learnt artist names change only when songs get confirmed: re-learn at most every 10 minutes. */
function aliasesOf(db: Database, now = Date.now()): ArtistAliases {
if (!aliasCache || aliasCache.db !== db || now - aliasCache.at > 600_000) aliasCache = { db, at: now, map: learnArtistAliases(db) };
return aliasCache.map;
}
type CandInfo = { video_id: string; title: string | null; channel: string | null; duration_sec: number | null; source: string };
/**
* Why the matcher would not confirm this result, in plain words ("가수 다름: Ashiyane", "길이 40초 차이");
* empty when it would. norabose videos carry no title/artist/length, so they are only labelled as such.
*/
export function explainCandidate(db: Database, song: { title: string; artist: string; duration_sec: number | null }, c: CandInfo): string[] {
if (c.source === 'norabose' && !c.title) return ['노라보세 영상이라 제목·가수·길이 정보 없음'];
const scored = scoreResult(
{ title: song.title, artist: song.artist, durationSec: song.duration_sec },
{ videoId: c.video_id, title: c.title ?? '', artists: c.channel ?? '', album: null, durationSec: c.duration_sec },
-1,
aliasesOf(db),
);
return notConfidentReasons(scored);
}
export const REVIEW_TABS = ['matched', 'candidate', 'missing', 'noorig', 'dead', 'pending'] as const;
export type ReviewTab = (typeof REVIEW_TABS)[number];
@@ -138,7 +163,11 @@ export interface ReviewRow {
score: number | null;
note: string | null;
touched: number;
candidates: { video_id: string; title: string | null; channel: string | null; duration_sec: number | null; score: number; source: string }[];
/** The real artist of the attached video (YouTube Music's artist field), not the channel name ("Release - Topic"). */
video_artist: string | null;
/** 후보 only: why the attached video was not confirmed automatically. */
reasons: string[];
candidates: { video_id: string; title: string | null; channel: string | null; duration_sec: number | null; score: number; source: string; reasons: string[] }[];
}
export function reviewList(db: Database, tab: ReviewTab, opts: { q?: string; page?: number } = {}) {
@@ -155,7 +184,7 @@ export function reviewList(db: Database, tab: ReviewTab, opts: { q?: string; pag
// 대기: in the order the matcher will take them (most popular level first); the rest: latest first.
const order = tab === 'pending' ? 'lv.level IS NULL, lv.level, s.id' : tab === 'dead' ? 's.updated_at DESC, s.id DESC' : 'touched DESC, s.id DESC';
const rows = db
.query<Omit<ReviewRow, 'candidates'>, string[]>(
.query<Omit<ReviewRow, 'candidates' | 'video_artist' | 'reasons'>, string[]>(
`SELECT s.id, s.title, s.artist, s.year, lv.level, s.duration_sec, s.youtube_id, s.youtube_status, s.youtube_source, s.youtube_channel, s.youtube_title,
(SELECT c.score FROM youtube_candidates c WHERE c.song_id = s.id AND c.video_id = s.youtube_id) score, s.note,
${tab === 'dead' ? 's.updated_at' : TOUCHED} touched
@@ -163,20 +192,37 @@ export function reviewList(db: Database, tab: ReviewTab, opts: { q?: string; pag
WHERE ${w} ORDER BY ${order} LIMIT ${REVIEW_PAGE} OFFSET ${page * REVIEW_PAGE}`,
)
.all(...args);
const cands = new Map<number, ReviewRow['candidates']>();
type C = Omit<ReviewRow['candidates'][number], 'reasons'>;
const all = new Map<number, C[]>();
if (rows.length) {
for (const c of db
.query<ReviewRow['candidates'][number] & { song_id: number }, number[]>(
.query<C & { song_id: number }, number[]>(
`SELECT song_id, video_id, title, channel, duration_sec, score, source FROM youtube_candidates
WHERE song_id IN (${rows.map(() => '?').join(',')}) ORDER BY score DESC`,
)
.all(...rows.map((r) => r.id))) {
const list = cands.get(c.song_id) ?? [];
if (list.length < 6) list.push({ video_id: c.video_id, title: c.title, channel: c.channel, duration_sec: c.duration_sec, score: c.score, source: c.source });
cands.set(c.song_id, list);
const list = all.get(c.song_id) ?? [];
list.push({ video_id: c.video_id, title: c.title, channel: c.channel, duration_sec: c.duration_sec, score: c.score, source: c.source });
all.set(c.song_id, list);
}
}
return { total, page, pageSize: REVIEW_PAGE, rows: rows.map((r) => ({ ...r, candidates: cands.get(r.id) ?? [] })) };
return {
total,
page,
pageSize: REVIEW_PAGE,
rows: rows.map((r): ReviewRow => {
const list = all.get(r.id) ?? [];
// the attached video's own row: prefer YouTube Music's (it knows the artist), else norabose's
const own = list.filter((c) => c.video_id === r.youtube_id).sort((a, b) => Number(b.source === 'ytmusic') - Number(a.source === 'ytmusic'))[0];
const explain = (c: C) => (r.youtube_status === 'candidate' ? explainCandidate(db, r, c) : []);
return {
...r,
video_artist: own?.source === 'ytmusic' ? own.channel : null,
reasons: own ? explain(own) : r.youtube_status === 'candidate' && r.youtube_id ? ['찾기 결과에 없는 영상이라 정보 없음'] : [],
candidates: list.slice(0, 6).map((c) => ({ ...c, reasons: explain(c) })),
};
}),
};
}
/**