/** * 플레이리스트 가져오기 (관리자 → 퀴즈): YouTube / YouTube Music / Spotify playlists through the .6 Lavalink * (music_bot_v2's; Spotify's own API no longer lists playlist tracks without a user login). Each track becomes a quiz song: * - already in the server DB (same video, or same title+artist key) → that song and its video * - a YouTube track → its own video (title cleaned of "Official MV" etc.) * - a Spotify track → YouTube Music song search, confirmed with the same rules as 원곡 찾기 (else "확인 필요"/"못 찾음") * Runs as a background job (searches are paced); the editor polls it and adds what the admin keeps. */ import type { Database } from 'bun:sqlite'; import { randomUUID } from 'node:crypto'; import { songMatchKey } from '@mq/shared'; import { isConfident, scoreResult } from '../collector/score'; import type { YtmSong } from '../collector/ytmusic'; import type { EmbedState } from '../collector/oembed'; import { aliasesOf } from '../songs/youtube-review'; import { cleanTitle } from '@mq/shared'; import { songAnswersBatch } from '../answers'; import type { JaReader } from '../answers/ja-reading'; import type { WikiLookup } from '../answers/wiki'; export interface LavalinkTrack { identifier: string; title: string; author: string; length: number; sourceName: string; isrc?: string | null; } export interface Lavalink { url: string; password: string; } export async function loadPlaylist(ll: Lavalink, url: string, fetchImpl: typeof fetch = fetch): Promise<{ name: string; tracks: LavalinkTrack[] } | string> { let res: Response; try { res = await fetchImpl(`${ll.url.replace(/\/$/, '')}/v4/loadtracks?identifier=${encodeURIComponent(url)}`, { headers: { Authorization: ll.password }, signal: AbortSignal.timeout(90_000) }); } catch (e) { return `플레이리스트 서버(Lavalink)에 연결하지 못했어요: ${(e as Error).message}`; } if (!res.ok) return `플레이리스트 서버 오류(${res.status})`; const j = (await res.json()) as { loadType: string; data: any }; if (j.loadType === 'playlist') return { name: j.data.info?.name ?? '', tracks: (j.data.tracks ?? []).map((t: { info: LavalinkTrack }) => t.info) }; if (j.loadType === 'track') return { name: '', tracks: [j.data.info] }; if (j.loadType === 'empty') return '플레이리스트를 찾지 못했어요. 공개(또는 일부 공개) 플레이리스트 주소인지 확인해 주세요.'; if (j.loadType === 'error') return `플레이리스트를 불러오지 못했어요: ${j.data?.message ?? '알 수 없는 오류'}`; return '플레이리스트 주소를 알아볼 수 없어요.'; } const NOISE = /\s*[([【][^)\]】]*(official|m\/?v|music video|lyric|audio|visuali[sz]er|performance|teaser|live clip|가사|뮤직비디오)[^)\]】]*[)\]】]\s*/gi; /** * "아이유(IU) - 좋은 날 (Official MV)" → 좋은 날 / 아이유(IU); "IVE 아이브 'LOVE DIVE' MV" → LOVE DIVE / IVE 아이브; Topic channels are the artist. * `guessed`: the title had neither form, so the whole title is the song and the channel name the artist (worth a look). */ export function cleanVideoTitle(title: string, channel: string): { title: string; artist: string; guessed: boolean } { const t = title.replace(NOISE, ' ').replace(/\s+(M\/?V|MV|Official Video)$/i, '').replace(/\s+/g, ' ').trim(); if (/ - Topic$/.test(channel)) return { title: t, artist: channel.replace(/ - Topic$/, ''), guessed: false }; const dash = /^(.+?)\s+[-–—]\s+(.+)$/.exec(t); if (dash) return { artist: dash[1]!.trim(), title: dash[2]!.trim(), guessed: false }; const quoted = /^(.+?)\s*['"‘“『「](.+?)['"’”』」]/.exec(t); if (quoted) return { artist: quoted[1]!.trim(), title: quoted[2]!.trim(), guessed: false }; return { title: t, artist: channel.replace(/(Official|VEVO|공식)$/i, '').trim() || channel, guessed: true }; } export type ImportStatus = 'db' | 'video' | 'found' | 'check' | 'missing' | 'blocked'; export interface ImportRow { n: number; /** As the playlist has it. */ source: { title: string; artist: string }; title: string; artist: string; /** Accepted answers besides 제목·가수 (docs/13): other names, DB aliases, 한글 발음 … */ titleAnswers: string[]; artistAnswers: string[]; durationSec: number | null; videoId: string | null; songId: number | null; status: ImportStatus; note: string; } export interface ImportJob { id: string; owner: string; url: string; name: string; status: 'running' | 'done' | 'failed'; done: number; total: number; rows: ImportRow[]; error: string | null; startedAt: number; } const jobs = new Map(); const JOB_TTL = 3600_000; export function getImportJob(id: string, owner: string): ImportJob | null { const j = jobs.get(id); return j && j.owner === owner ? j : null; } export interface ImportDeps { db: Database; lavalink: Lavalink | null; search: (q: string) => Promise; embed: (videoId: string) => Promise; delayMs?: number; fetchImpl?: typeof fetch; /** J-POP 한글 이름 (위키백과) and 일본어 읽기 for the answers (docs/13). */ wiki?: WikiLookup; read?: JaReader; } /** Starts an import job and returns it at once (rows fill in while it runs). */ export async function startImport(d: ImportDeps, owner: string, url: string, now = Date.now()): Promise { for (const [k, j] of jobs) if (now - j.startedAt > JOB_TTL) jobs.delete(k); if (!d.lavalink) return '플레이리스트 서버(Lavalink)가 설정되지 않았어요(LAVALINK_URL·LAVALINK_PW).'; const pl = await loadPlaylist(d.lavalink, url, d.fetchImpl); if (typeof pl === 'string') return pl; if (!pl.tracks.length) return '플레이리스트에 곡이 없어요.'; const job: ImportJob = { id: randomUUID(), owner, url, name: pl.name, status: 'running', done: 0, total: pl.tracks.length, rows: [], error: null, startedAt: now }; jobs.set(job.id, job); void runImport(d, job, pl.tracks).catch((e) => { job.status = 'failed'; job.error = (e as Error).message; }); return job; } async function runImport(d: ImportDeps, job: ImportJob, tracks: LavalinkTrack[]): Promise { const byVideo = d.db.query<{ id: number; title: string; artist: string; youtube_id: string | null; youtube_status: string; duration_sec: number | null }, [string]>( "SELECT id, title, artist, youtube_id, youtube_status, duration_sec FROM songs WHERE youtube_id = ? AND deleted_at IS NULL ORDER BY youtube_status = 'matched' DESC LIMIT 1", ); const byKey = d.db.query<{ id: number; title: string; artist: string; youtube_id: string | null; youtube_status: string; duration_sec: number | null }, [string]>( 'SELECT id, title, artist, youtube_id, youtube_status, duration_sec FROM songs WHERE match_key = ? AND deleted_at IS NULL ORDER BY youtube_status = \'matched\' DESC LIMIT 1', ); const aliases = aliasesOf(d.db); for (const [i, t] of tracks.entries()) { const dur = t.length ? Math.round(t.length / 1000) : null; const yt = t.sourceName === 'youtube'; const named = yt ? cleanVideoTitle(t.title, t.author) : { title: t.title, artist: t.author, guessed: false }; const row: ImportRow = { n: i + 1, source: { title: t.title, artist: t.author }, title: named.title, artist: named.artist, titleAnswers: [], artistAnswers: [], durationSec: dur, videoId: null, songId: null, status: 'missing', note: '' }; const dbSong = (yt ? byVideo.get(t.identifier) : null) ?? byKey.get(songMatchKey(named.title, named.artist)); // the video must play on other sites, the DB song's own video too (the DB state may be old) const playable = async (videoId: string) => { const e = await d.embed(videoId); if (e.state !== 'ok') Object.assign(row, { status: 'blocked', note: e.state === 'noembed' ? '다른 사이트 재생이 막힌 영상' : e.state === 'dead' ? '없는 영상' : '영상 확인 실패' }); }; if (yt) { row.videoId = t.identifier; if (dbSong) Object.assign(row, { songId: dbSong.id, title: dbSong.title, artist: dbSong.artist, status: 'db', note: '서버 DB 곡 (플레이리스트 영상 사용)' }); else Object.assign(row, { status: 'video', note: named.guessed ? '제목·가수 확인 필요 (영상 제목 형식이 달라 채널 이름을 가수로 씀)' : '' }); await playable(t.identifier); } else if (dbSong?.youtube_id && dbSong.youtube_status === 'matched') { Object.assign(row, { songId: dbSong.id, videoId: dbSong.youtube_id, title: dbSong.title, artist: dbSong.artist, durationSec: dbSong.duration_sec ?? dur, status: 'db', note: '서버 DB 곡' }); await playable(dbSong.youtube_id); } else { // Spotify (or any non-YouTube source): find the official audio like 원곡 찾기 let results: YtmSong[] = []; try { results = await d.search(`${t.author.split(', ')[0]} ${t.title}`); } catch (e) { row.note = `검색 실패: ${(e as Error).message}`; } const scored = results.slice(0, 8).map((r, rank) => scoreResult({ title: t.title, artist: t.author, durationSec: dur }, r, rank, aliases)).sort((a, b) => b.score - a.score); const best = scored.find(isConfident) ?? scored.find((s) => s.titleOk) ?? null; if (best) { const e = await d.embed(best.videoId); if (e.state === 'ok') { Object.assign(row, { videoId: best.videoId, status: isConfident(best) ? 'found' : 'check', note: isConfident(best) ? `찾음: ${best.artists} - ${best.title}` : `확인 필요: ${best.artists} - ${best.title}` }); if (dbSong) row.songId = dbSong.id; } else row.note = '찾은 영상이 다른 사이트 재생이 막혀 있어요'; } else if (!row.note) row.note = '유튜브에서 못 찾았어요'; if (i < tracks.length - 1) await Bun.sleep(d.delayMs ?? 700); } // shown title without brackets / feat. / version marks (the answers come at the end, docs/13) row.title = cleanTitle(row.title).title; job.rows.push(row); job.done = i + 1; } // answers for every song at once: 한글 발음, 일본어 읽기 (one process), J-POP 한글 이름 (위키백과) const answers = await songAnswersBatch( d.db, job.rows.map((r) => ({ title: r.title, artist: r.artist, songId: r.songId })), { korean: true, wiki: d.wiki, read: d.read }, ); job.rows.forEach((r, i) => { const a = answers[i]!; Object.assign(r, { title: a.title, artist: a.artist, titleAnswers: a.titleAnswers, artistAnswers: a.artistAnswers }); if (a.note) r.note = [r.note, a.note].filter(Boolean).join(' · '); if (a.note?.includes('확인 필요') && r.status !== 'blocked' && r.status !== 'missing') r.status = 'check'; }); job.status = 'done'; }