연령제한 곡 재생: 유튜브가 퍼가기 플레이어에서 막는(오류 150) 연령제한 영상은 서버가 성인 인증된 계정 쿠키로 소리만 받아 두고(/data/audio, yt-dlp) 게임은 그 파일을 숨긴 <audio>로 재생. 찾기는 소리 크기·하이라이트 수집(로그인 없는 페이지의 성인 인증 화면) + 게임 중 오류 시 age-audio 작업이 다시 확인해 되돌림. 받기 전에는 출제 안 함. prepare·catchup에 videoId 대신 문제마다 새 무작위 주소(/api/audio/<토큰>, Range 지원)라 영상 id가 안 보임. 같은 시각 시작·위치 정지·새로고침 이어 듣기·소리 크기(유튜브 -14 보정 없이) 그대로. 관리자 수집 화면·cli age-audio, Dockerfile에 yt-dlp(버전·체크섬 고정), 수집 CLI가 서버 DB 경로를 물려받게(테스트가 기본 DB를 건드리던 것). 실제 크롬 e2e로 받은 음원+유튜브 곡 섞어 재생 확인

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-20 08:11:55 +09:00
parent 37c1440892
commit 4f888711b1
26 changed files with 705 additions and 45 deletions

View File

@@ -190,7 +190,7 @@ describe('admin artists, genres, stats, rooms, collect, room defaults', () => {
test('collect control: one run per kind, stop marks a dead process stopped', async () => {
s = boot();
const { collectorControl } = await import('./collector/control');
const ctl = collectorControl(s.db, () => {}, [{ id: 'sleep', label: 't' }], () => ['stats']);
const ctl = collectorControl(s.db, () => {}, s.config.dbPath, [{ id: 'sleep', label: 't' }], () => ['stats']);
const id = Number(s.db.query("INSERT INTO collect_jobs (kind, status, total, started_at, pid) VALUES ('sleep', 'running', 0, ?, ?)").run(Date.now(), process.pid).lastInsertRowid);
expect(ctl.run('sleep')).toContain('이미');
expect(ctl.run('nope')).toContain('없는');

View File

@@ -0,0 +1,123 @@
import { describe, expect, test } from 'bun:test';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { DEFAULT_ROOM_CONFIG } from '@mq/shared';
import { openDb } from '../db';
import { audioDir, audioFile } from '../songs/audio';
import { countPool } from '../songs/pool';
import { loudnessGain, quizSong } from '../songs/quiz';
import { tempDbPath } from '../test-utils';
import { cookieForms, collectAgeAudio } from './age-audio';
import { Catalog } from './catalog';
import { Blocked } from './charts';
import { collectLoudness, isAgeGated } from './loudness';
const fx = (f: string) => Bun.file(join(import.meta.dir, 'fixtures', f)).text();
const gate = '"playabilityStatus":{"status":"LOGIN_REQUIRED","reason":"성인 인증하려면 로그인하세요"}';
const ok = (lufs: number) => `"playabilityStatus":{"status":"OK"},"audioConfig":{"trackAbsoluteLoudnessLkfs":${lufs},"loudnessTargetLkfs":-14}`;
function setup(n: number) {
const path = tempDbPath();
const db = openDb(path);
const c = new Catalog(db);
const ids = Array.from({ length: n }, (_, i) => c.upsert({ title: `t${i}`, artists: [{ name: 'x' }], source: 't', ref: `r${i}` })[0]!);
for (const id of ids) c.autoLevel(id, 'all', 3);
db.query("UPDATE songs SET youtube_status = 'matched', embeddable = 1, youtube_id = 'v' || id, duration_sec = 200").run();
return { db, ids, dir: audioDir(path) };
}
const cfg = { ...DEFAULT_ROOM_CONFIG, level: 3 as const };
describe('연령제한 곡', () => {
test('tells an age gate from a playable or Premium-only page (real anonymous page of an age-restricted video)', async () => {
expect(isAgeGated(await fx('youtube-age-gate.txt'))).toBe(true);
expect(isAgeGated(ok(-10))).toBe(false);
expect(isAgeGated('"playabilityStatus":{"status":"UNPLAYABLE","reason":"Music Premium 회원만 시청할 수 있는 동영상입니다."}')).toBe(false);
expect(isAgeGated('"playabilityStatus":{"status":"LOGIN_REQUIRED","reason":"Sign in to confirm your age"}')).toBe(true);
});
test('cookie file as a header or as cookies.txt → both forms', () => {
const h = cookieForms('SID=a; SAPISID=b=c\n');
expect(h.header).toBe('SID=a; SAPISID=b=c');
expect(h.netscape).toContain('.youtube.com\tTRUE\t/\tTRUE\t');
expect(h.netscape).toContain('\tSAPISID\tb=c');
const n = cookieForms('# Netscape HTTP Cookie File\n.youtube.com\tTRUE\t/\tTRUE\t0\tSID\ta\n.google.com\tTRUE\t/\tTRUE\t0\tNID\tz\n');
expect(n.header).toBe('SID=a');
expect(n.netscape).toContain('NID');
});
test('the loudness job finds them; the game leaves them out until the audio is downloaded, then plays the file', async () => {
const { db, ids, dir } = setup(3);
const [a, b] = ids as [number, number, number];
await collectLoudness(db, { delayMs: 0, fetchPage: async (id) => (id === `v${a}` ? gate : ok(-12)) });
expect(quizSong(db, a)).toMatchObject({ ageRestricted: true, audio: false, loudness: null });
expect(quizSong(db, b)).toMatchObject({ ageRestricted: false, audio: false, loudness: -12 });
expect(countPool(db, cfg)).toEqual({ playable: 2, total: 3 });
const asked: string[] = [];
const r = await collectAgeAudio(db, dir, {
delayMs: 0,
fetchPage: async () => gate,
fetchSignedIn: async (id) => (asked.push(id), ok(-8)),
download: async (id, out) => writeFileSync(out, `audio of ${id}`),
});
expect(r).toEqual({ back: 0, downloaded: 1, failed: 0, removed: 0 });
expect(asked).toEqual([`v${a}`]);
expect(readFileSync(audioFile(dir, `v${a}`), 'utf8')).toBe(`audio of v${a}`);
// loudness from the signed-in page; our file gets no YouTube turn-down
expect(quizSong(db, a)).toMatchObject({ ageRestricted: true, audio: true, loudness: -8 });
expect(loudnessGain(-8, true)).toBeCloseTo(10 ** (-10 / 20), 3);
expect(loudnessGain(-8, true)).toBeLessThan(loudnessGain(-8));
expect(loudnessGain(null, true)).toBe(loudnessGain(-9, true));
expect(countPool(db, cfg)).toEqual({ playable: 3, total: 3 });
// the admin picks another video: the old file is no longer used and goes away
db.query("UPDATE songs SET youtube_id = 'other' WHERE id = ?").run(a);
db.query('UPDATE songs SET audio_video = NULL WHERE id = ?').run(a);
expect(quizSong(db, a)).toMatchObject({ ageRestricted: false, audio: false });
expect((await collectAgeAudio(db, dir, { delayMs: 0, fetchPage: async () => gate })).removed).toBe(1);
expect(existsSync(audioFile(dir, `v${a}`))).toBe(false);
});
test('a song a player could not play (150) comes back when it is age-restricted; others stay 재생 불가', async () => {
const { db, ids, dir } = setup(2);
const [a, b] = ids as [number, number];
db.query("UPDATE songs SET youtube_status = 'dead', note = '[재생 오류 150]'").run();
// a broken old file of song a is replaced
mkdirSync(dir, { recursive: true });
writeFileSync(audioFile(dir, `v${a}`), 'broken');
db.query('UPDATE songs SET audio_video = youtube_id, age_video = youtube_id WHERE id = ?').run(a);
const r = await collectAgeAudio(db, dir, {
delayMs: 0,
fetchPage: async (id) => (id === `v${a}` ? gate : '"playabilityStatus":{"status":"ERROR","reason":"동영상을 재생할 수 없음"}'),
fetchSignedIn: async () => ok(-9),
download: async (id, out) => writeFileSync(out, 'fresh'),
});
expect(r).toMatchObject({ back: 1, downloaded: 1 });
expect(db.query<{ youtube_status: string; note: string | null }, [number]>('SELECT youtube_status, note FROM songs WHERE id = ?').get(a)).toEqual({ youtube_status: 'matched', note: null });
expect(readFileSync(audioFile(dir, `v${a}`), 'utf8')).toBe('fresh');
expect(db.query<{ youtube_status: string }, [number]>('SELECT youtube_status FROM songs WHERE id = ?').get(b)!.youtube_status).toBe('dead');
// checked once a month, not every run
let calls = 0;
await collectAgeAudio(db, dir, { delayMs: 0, fetchPage: async () => (calls++, gate) });
expect(calls).toBe(0);
});
test('stops when the cookie account cannot watch them, or at a block signal; a failed download waits a day', async () => {
const { db, ids, dir } = setup(2);
db.query('UPDATE songs SET age_video = youtube_id').run();
let downloads = 0;
const cookieGone = await collectAgeAudio(db, dir, { delayMs: 0, fetchSignedIn: async () => gate, download: async () => void downloads++ });
expect(cookieGone.downloaded).toBe(0);
expect(downloads).toBe(0);
expect(db.query<{ status: string; log: string }, []>("SELECT status, log FROM collect_jobs WHERE kind = 'age-audio' ORDER BY id DESC").get()).toMatchObject({ status: 'failed', log: expect.stringContaining('성인 인증') });
await collectAgeAudio(db, dir, { delayMs: 0, fetchSignedIn: async () => ok(-9), download: async () => (downloads++, Promise.reject(new Blocked('HTTP Error 429'))) });
expect(downloads).toBe(1);
const r = await collectAgeAudio(db, dir, { delayMs: 0, fetchSignedIn: async () => ok(-9), download: async () => Promise.reject(new Error('Requested format is not available')) });
expect(r).toMatchObject({ downloaded: 0, failed: 2 });
expect((await collectAgeAudio(db, dir, { delayMs: 0, fetchSignedIn: async () => ok(-9), download: async (_, out) => writeFileSync(out, 'x') })).downloaded).toBe(0);
expect(ids.every((id) => !quizSong(db, id)!.audio)).toBe(true);
expect(existsSync(dirname(audioFile(dir, 'x')))).toBe(true);
});
});

View File

@@ -0,0 +1,182 @@
/**
* 연령제한 곡 (docs/08 §2-1): YouTube refuses age-restricted videos in every embedded player (error 150), even for adults who
* are signed in. The server downloads their audio once with the adult-verified account's cookie and the game plays that
* file (songs/audio.ts). One song at a time, a few seconds apart, stops at the first block signal:
* 1. songs a player could not play (재생 오류): still age-restricted? → back in the game once the audio is here
* 2. age-restricted songs without audio: the signed-in watch page (loudness and 하이라이트 — the anonymous page has neither),
* then yt-dlp downloads the audio-only m4a (3~5MB) → AUDIO_DIR/<video>.m4a
* 3. files no song uses any more are deleted
* Age-restricted songs are found by the loudness / heatmap jobs (their anonymous page) and by step 1.
*/
import type { Database } from 'bun:sqlite';
import { existsSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { audioFile } from '../songs/audio';
import { Catalog, Job, sleep } from './catalog';
import { Blocked } from './charts';
import { parseHeatmap, pickHighlight } from './heatmap';
import { fetchWatchPage, isAgeGated, parseLoudness, saveLoudness } from './loudness';
/** The cookie file holds either a request header (`a=1; b=2`) or a Netscape cookies.txt; returns both forms. */
export function cookieForms(raw: string): { header: string; netscape: string } {
const text = raw.trim();
if (text.includes('\t')) {
const rows = text
.split(/\r?\n/)
.filter((l) => l && (!l.startsWith('#') || l.startsWith('#HttpOnly_')))
.map((l) => l.split('\t'))
.filter((p) => p.length >= 7 && /youtube\.com$/.test(p[0]!));
return { header: rows.map((p) => `${p[5]}=${p[6]!.trim()}`).join('; '), netscape: `${text}\n` };
}
const exp = Math.floor(Date.now() / 1000) + 365 * 86400;
const pairs = text
.split(/;\s*/)
.map((p) => [p.slice(0, p.indexOf('=')).trim(), p.slice(p.indexOf('=') + 1).trim()] as const)
.filter(([k]) => k);
const lines = pairs.map(([k, v]) => ['.youtube.com', 'TRUE', '/', 'TRUE', exp, k, v].join('\t'));
return { header: pairs.map(([k, v]) => `${k}=${v}`).join('; '), netscape: ['# Netscape HTTP Cookie File', ...lines, ''].join('\n') };
}
/** yt-dlp: the best m4a audio-only stream of the video into `out` (no ffmpeg in the image: browsers play the DASH m4a as is). */
export async function ytDlpAudio(videoId: string, out: string, cookieFile: string, bin = process.env.YTDLP_BIN ?? 'yt-dlp'): Promise<void> {
const tmp = `${out}.tmp`;
rmSync(tmp, { force: true });
const proc = Bun.spawn(
[bin, '--cookies', cookieFile, '--js-runtimes', 'bun', '-f', 'bestaudio[ext=m4a]', '--fixup', 'never', '--no-playlist', '--no-progress', '--no-part', '--max-filesize', '40M', '-q', '--no-warnings', '-o', tmp, '--', videoId],
{ stdin: 'ignore', stdout: 'ignore', stderr: 'pipe' },
);
const timer = setTimeout(() => proc.kill(), 180_000);
const [code, err] = await Promise.all([proc.exited, new Response(proc.stderr).text()]);
clearTimeout(timer);
if (code !== 0 || !existsSync(tmp)) {
rmSync(tmp, { force: true });
const msg = err.trim().split('\n').pop() || `yt-dlp exit ${code}`;
if (/not a bot|HTTP Error 429|Too Many Requests/i.test(err)) throw new Blocked(msg);
throw new Error(msg);
}
renameSync(tmp, out);
}
/** The cookie belongs to an account YouTube does not let watch age-restricted videos (expired, or not adult-verified). */
export class CookieProblem extends Error {}
export interface AgeAudioOptions {
limit?: number;
delayMs?: number;
cookiePath?: string;
/** Tests inject these; by default the anonymous / signed-in watch page and yt-dlp. */
fetchPage?: (id: string) => Promise<string>;
fetchSignedIn?: (id: string) => Promise<string>;
download?: (id: string, out: string) => Promise<void>;
}
export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioOptions = {}) {
mkdirSync(dir, { recursive: true });
const now = Date.now();
const recheck = db
.query<{ id: number; youtube_id: string }, [number]>(
`SELECT s.id, s.youtube_id FROM songs s WHERE s.deleted_at IS NULL AND s.youtube_status = 'dead' AND s.youtube_id IS NOT NULL
AND s.note LIKE '%[재생 오류 %'
AND NOT EXISTS (SELECT 1 FROM signals g WHERE g.song_id = s.id AND g.kind = 'yt_age_checked' AND g.value > ?)
ORDER BY s.id`,
)
.all(now - 30 * 86400_000);
const pending = () =>
db
.query<{ id: number; youtube_id: string; highlight_source: string | null }, [number, number]>(
`SELECT s.id, s.youtube_id, s.highlight_source FROM songs s
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 = 'matched' AND s.youtube_id IS NOT NULL
AND s.age_video = s.youtube_id AND COALESCE(s.audio_video, '') <> s.youtube_id
AND NOT EXISTS (SELECT 1 FROM signals g WHERE g.song_id = s.id AND g.kind = 'yt_audio_tried' AND g.value > ?)
ORDER BY l.level IS NULL, l.level, s.id LIMIT ?`,
)
.all(Date.now() - 86400_000, opts.limit ?? 100);
const fetchPage = opts.fetchPage ?? fetchWatchPage;
const job = new Job(db, 'age-audio', recheck.length);
const cat = new Catalog(db);
const stats = { back: 0, downloaded: 0, failed: 0, removed: 0 };
let tmp: string | null = null;
let done = 0;
try {
// 1. a player could not play it: still age-restricted → back to 'matched' (left out until the audio is here)
for (const s of recheck) {
try {
if (isAgeGated(await fetchPage(s.youtube_id))) {
// the audio file may have been what failed (404 / broken): download it again
rmSync(audioFile(dir, s.youtube_id), { force: true });
const r = db
.query(
`UPDATE songs SET youtube_status = 'matched', age_video = youtube_id, audio_video = NULL, updated_at = ?,
note = NULLIF(trim(replace(replace(replace(note, '[재생 오류 150]', ''), '[재생 오류 101]', ''), '[재생 오류 100]', '')), '')
WHERE id = ? AND youtube_status = 'dead' AND youtube_id = ?`,
)
.run(Date.now(), s.id, s.youtube_id);
if (r.changes) (stats.back++, job.log(`${s.youtube_id} age-restricted: back in the game once downloaded`));
} else cat.signal(s.id, 'yt_age_checked', Date.now());
} catch (e) {
stats.failed++;
if (e instanceof Blocked) throw e;
job.log(`${s.youtube_id} check failed: ${(e as Error).message}`);
}
job.progress(++done);
await sleep(opts.delayMs ?? 3000);
}
// 2. download with the adult-verified account
const todo = pending();
job.progress(done, recheck.length + todo.length);
let fetchSignedIn = opts.fetchSignedIn;
let download = opts.download;
if (todo.length && (!fetchSignedIn || !download)) {
const path = opts.cookiePath ?? process.env.YT_COOKIE_PATH ?? '/home/claude/.config/music_quiz/ytm_cookie.txt';
if (!existsSync(path)) throw new CookieProblem(`쿠키 파일이 없어요: ${path}`);
const c = cookieForms(await Bun.file(path).text());
tmp = mkdtempSync(join(tmpdir(), 'mq-cookie-'));
const cookieFile = join(tmp, 'cookies.txt');
writeFileSync(cookieFile, c.netscape, { mode: 0o600 });
fetchSignedIn ??= (id) => fetchWatchPage(id, c.header);
download ??= (id, out) => ytDlpAudio(id, out, cookieFile);
}
for (const s of todo) {
try {
const page = await fetchSignedIn!(s.youtube_id);
if (isAgeGated(page)) throw new CookieProblem('쿠키 계정으로도 성인 인증 페이지가 나와요(쿠키가 만료됐거나 성인 인증이 안 된 계정)');
// the anonymous page of an age-restricted video has no loudness / heatmap: take them from this one
saveLoudness(db, s.id, s.youtube_id, parseLoudness(page));
const sec = s.highlight_source === 'admin' || s.highlight_source === 'heatmap' ? null : pickHighlight(parseHeatmap(page));
if (sec !== null)
db.query("UPDATE songs SET highlight_sec = ?, highlight_source = 'heatmap', updated_at = ? WHERE id = ? AND COALESCE(highlight_source, '') <> 'admin'").run(sec, Date.now(), s.id);
await download!(s.youtube_id, audioFile(dir, s.youtube_id));
db.query('UPDATE songs SET audio_video = ?, updated_at = ? WHERE id = ? AND youtube_id = ?').run(s.youtube_id, Date.now(), s.id, s.youtube_id);
stats.downloaded++;
job.log(`${s.youtube_id} downloaded`);
} catch (e) {
if (e instanceof Blocked || e instanceof CookieProblem) throw e;
stats.failed++;
cat.signal(s.id, 'yt_audio_tried', Date.now());
job.log(`${s.youtube_id} download failed: ${(e as Error).message}`);
}
job.progress(++done);
await sleep(opts.delayMs ?? 3000);
}
// 3. files of videos no song plays any more (the admin changed the video, the song was purged)
const used = new Set(db.query<{ v: string }, []>('SELECT audio_video v FROM songs WHERE audio_video IS NOT NULL').all().map((r) => r.v));
for (const f of readdirSync(dir)) {
if (used.has(f.replace(/\.m4a$/, '')) && f.endsWith('.m4a')) continue;
rmSync(join(dir, f), { force: true });
stats.removed++;
}
job.log(`age-audio done: back ${stats.back}, downloaded ${stats.downloaded}, failed ${stats.failed}, removed ${stats.removed}`);
job.finish('done');
} catch (e) {
job.log(`stopped: ${(e as Error).message}`);
job.finish('failed');
if (!(e instanceof Blocked || e instanceof CookieProblem)) throw e;
} finally {
if (tmp) rmSync(tmp, { recursive: true, force: true });
}
return stats;
}

View File

@@ -7,6 +7,7 @@
* bun apps/server/src/collector/cli.ts melon-detail [--limit N]
* bun apps/server/src/collector/cli.ts heatmap [--limit N]
* bun apps/server/src/collector/cli.ts loudness [--limit N] [--delay ms] [--deadline epochMs] (소리 크기: YouTube's LUFS per matched video, most popular level first)
* bun apps/server/src/collector/cli.ts age-audio [--limit N] [--delay ms] (연령제한 곡: download their audio with the adult-verified cookie, docs/08 §2-1)
* bun apps/server/src/collector/cli.ts levels
* bun apps/server/src/collector/cli.ts fix-text (one-off: decode `&#x27;`/`&amp;` saved in titles, let 원곡 찾기 retry them)
* bun apps/server/src/collector/cli.ts stats
@@ -24,6 +25,8 @@ import { collectCharts, fillMelonDetails } from './charts';
import { collectHeatmaps } from './heatmap';
import { collectLoudness } from './loudness';
import { computeLevels } from './levels';
import { collectAgeAudio } from './age-audio';
import { audioDir } from '../songs/audio';
import { fixEntities } from './text';
const [cmd, ...rest] = process.argv.slice(2);
@@ -69,6 +72,9 @@ switch (cmd) {
case 'loudness':
console.log(await collectLoudness(db, { limit: flag('limit'), delayMs: flag('delay'), deadline: flag('deadline') }));
break;
case 'age-audio':
console.log(await collectAgeAudio(db, audioDir(dbPath), { limit: flag('limit'), delayMs: flag('delay') }));
break;
case 'levels':
console.log(computeLevels(db));
break;
@@ -79,7 +85,7 @@ switch (cmd) {
console.log(JSON.stringify(catalogStats(db), null, 1));
break;
default:
console.log('usage: cli.ts import-norabose|import-sta|match-youtube|charts|melon-detail|heatmap|loudness|levels|fix-text|stats');
console.log('usage: cli.ts import-norabose|import-sta|match-youtube|charts|melon-detail|heatmap|loudness|age-audio|levels|fix-text|stats');
process.exitCode = 1;
}
db.close();

View File

@@ -13,6 +13,7 @@ export const COLLECT_KINDS: CollectControl['kinds'] = [
{ id: 'melon-detail', label: '멜론 곡 정보 채우기 (발매일·장르·가사)', limit: true },
{ id: 'heatmap', label: '하이라이트 찾기 (유튜브 많이 다시 본 구간)', limit: true },
{ id: 'loudness', label: '소리 크기 재기 (유튜브가 잰 곡별 크기)', limit: true },
{ id: 'age-audio', label: '연령제한 곡 음원 받기 (성인 인증 계정 쿠키)', limit: true },
{ id: 'levels', label: '난이도 다시 계산' },
];
@@ -27,7 +28,8 @@ function alive(pid: number): boolean {
}
}
export function collectorControl(db: Database, log: LogFn, kinds = COLLECT_KINDS, args: (kind: string, limit?: number) => string[] = defaultArgs): CollectControl {
/** `dbPath`: the server's DB, so the CLI never falls back to its default path (tests and e2e configure the DB in code). */
export function collectorControl(db: Database, log: LogFn, dbPath: string, kinds = COLLECT_KINDS, args: (kind: string, limit?: number) => string[] = defaultArgs): CollectControl {
const running = (kind: string) =>
db.query<{ id: number; pid: number | null }, [string]>("SELECT id, pid FROM collect_jobs WHERE kind = ? AND status = 'running' ORDER BY id DESC LIMIT 1").get(kind);
return {
@@ -37,7 +39,7 @@ export function collectorControl(db: Database, log: LogFn, kinds = COLLECT_KINDS
if (!k) return '없는 작업이에요.';
const r = running(kind);
if (r && r.pid && alive(r.pid)) return '이미 돌고 있는 작업이에요. 끝나거나 멈춘 뒤에 다시 실행해 주세요.';
const proc = Bun.spawn([process.execPath, CLI, ...args(kind, k.limit ? limit : undefined)], { stdout: 'ignore', stderr: 'ignore', stdin: 'ignore', env: process.env });
const proc = Bun.spawn([process.execPath, CLI, ...args(kind, k.limit ? limit : undefined)], { stdout: 'ignore', stderr: 'ignore', stdin: 'ignore', env: { ...process.env, DB_PATH: dbPath } });
proc.unref();
log('info', 'collector started', { kind, limit, pid: proc.pid });
return null;

View File

@@ -0,0 +1,2 @@
var ytInitialPlayerResponse = {"responseContext"
"playabilityStatus":{"status":"LOGIN_REQUIRED","reason":"성인 인증하려면 로그인하세요","errorScreen":{"playerInterstitialRenderer":{"content":{"interstitialViewModel":{"title":{"content":"성인 인증하려면 로그인하세요"},"description":{"content":"이 동영상은 일부 사용자에게는 부적절할 수 있습니다."},"primaryButton":{"buttonViewModel":{"onTap":{"innertubeCommand":{"clickTrackingParams":"CAUQtOoKIhMI6uzUxcnGlwMVFLUpAx0Sewg8ygEED2O7CQ==","commandMetadata":{"webCommandMetadata":{"url":"https://accounts.google.com/ServiceLogin?service=youtube\u0026uilel=3\u0026passive=true\u0026continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26app%3Ddesktop%26hl%3Dko%26next%3D%252Fwatch%253Fv%253D6kLq3WMV1nU%2526hl%253Dko\u0026hl=ko","webPageType":"WEB_PAGE_TYPE_UNKNOWN","rootVe":83769}},"signInEndpoint":{"nextEndpoint":{"clickTrackingParams":"CAUQtOoKIhMI6uzUxcnGlwMVFLUpAx0Sewg8ygEED2O7CQ==","commandMetadata":{"webCommandMetadata":{"url":"/watch?v=6kLq3WMV1nU\u0026hl=ko","webPageType":"WEB_PAGE_TYPE_UNKNOWN","rootVe":83769}},"urlEndpoint":{"url":"/watch?v=6kLq3WMV1nU\u0026hl=ko"}}}}},"style":"BUTTON_VIEW_MODEL_STYLE_OVERLAY","trackingParams":"CAUQtOoKIhMI6uzUxcnGlwMVFLUpAx0Sewg8","isFullWidth":false,"type":"BUTTON_VIEW_MODEL_TYPE_FILLED","buttonSize":"BUTTON_VIEW_MODEL_SIZE_LARGE","accessibilityId":"id.interstitial_sign_in_button","titleFormatted":{"content":"로그인"},"useYoutubeLoggingDirectives":true,"shouldLogGestures":true,"loggingDirectives":{"trackingParams":"CAU

View File

@@ -6,7 +6,7 @@
import type { Database } from 'bun:sqlite';
import { Catalog, Job, sleep } from './catalog';
import { Blocked } from './charts';
import { fetchWatchPage, parseLoudness, saveLoudness } from './loudness';
import { fetchWatchPage, isAgeGated, markAgeGated, parseLoudness, saveLoudness } from './loudness';
export interface Marker {
startMs: number;
@@ -59,6 +59,7 @@ export async function collectHeatmaps(db: Database, opts: { limit?: number; dela
for (const [i, s] of rows.entries()) {
try {
const page = await fetchPage(s.youtube_id);
if (isAgeGated(page)) markAgeGated(db, s.id, s.youtube_id);
// same page: keep its loudness too, so the loudness job can skip this song
saveLoudness(db, s.id, s.youtube_id, parseLoudness(page));
const sec = pickHighlight(parseHeatmap(page));

View File

@@ -38,7 +38,7 @@ describe('소리 크기 맞추기', () => {
const asked: string[] = [];
const lufs: Record<string, number> = { [`v${a}`]: -9, [`v${b}`]: -20 };
const r = await collectLoudness(db, { delayMs: 0, fetchPage: async (id) => (asked.push(id), id in lufs ? page(lufs[id]!) : 'no config') });
expect(r).toEqual({ set: 2, none: 1, failed: 0 });
expect(r).toEqual({ set: 2, none: 1, age: 0, failed: 0 });
expect(asked[0]).toBe(`v${b}`);
expect(quizSong(db, a!)!.loudness).toBe(-9);
expect(quizSong(db, b!)!.loudness).toBe(-20);
@@ -48,7 +48,7 @@ describe('소리 크기 맞추기', () => {
// the admin picks another video: the old number no longer applies, and the song is measured again
db.query("UPDATE songs SET youtube_id = 'new' WHERE id = ?").run(a!);
expect(quizSong(db, a!)!.loudness).toBeNull();
expect(await collectLoudness(db, { delayMs: 0, fetchPage: async () => page(-12) })).toEqual({ set: 1, none: 0, failed: 0 });
expect(await collectLoudness(db, { delayMs: 0, fetchPage: async () => page(-12) })).toEqual({ set: 1, none: 0, age: 0, failed: 0 });
expect(quizSong(db, a!)!.loudness).toBe(-12);
});
@@ -58,7 +58,7 @@ describe('소리 크기 맞추기', () => {
const [a] = ['a', 'b'].map((t) => c.upsert({ title: t, artists: [{ name: 'x' }], source: 't', ref: t })[0]!);
db.query("UPDATE songs SET youtube_status = 'matched', youtube_id = 'v' || id").run();
let calls = 0;
expect(await collectLoudness(db, { delayMs: 0, fetchPage: async () => (calls++, Promise.reject(new Blocked('youtube 429'))) })).toEqual({ set: 0, none: 0, failed: 1 });
expect(await collectLoudness(db, { delayMs: 0, fetchPage: async () => (calls++, Promise.reject(new Blocked('youtube 429'))) })).toEqual({ set: 0, none: 0, age: 0, failed: 1 });
expect(calls).toBe(1);
await collectHeatmaps(db, { delayMs: 0, fetchPage: async () => page(-15) });
expect(quizSong(db, a!)!.loudness).toBe(-15);

View File

@@ -8,15 +8,31 @@ import type { Database } from 'bun:sqlite';
import { Catalog, Job, sleep } from './catalog';
import { BROWSER_UA, Blocked } from './charts';
/** The watch page (heatmap markers, audioConfig loudness); throws Blocked on a block signal. */
export async function fetchWatchPage(id: string): Promise<string> {
const res = await fetch(`https://www.youtube.com/watch?v=${id}&hl=ko`, { headers: { 'user-agent': BROWSER_UA, 'accept-language': 'ko-KR,ko;q=0.9' }, signal: AbortSignal.timeout(20_000) });
/** The watch page (heatmap markers, audioConfig loudness); throws Blocked on a block signal. `cookie`: signed in (연령제한 곡). */
export async function fetchWatchPage(id: string, cookie?: string): Promise<string> {
const headers: Record<string, string> = { 'user-agent': BROWSER_UA, 'accept-language': 'ko-KR,ko;q=0.9', ...(cookie ? { cookie } : {}) };
const res = await fetch(`https://www.youtube.com/watch?v=${id}&hl=ko`, { headers, signal: AbortSignal.timeout(20_000) });
if (res.status === 429 || res.status === 403) throw new Blocked(`youtube ${res.status}`);
const text = await res.text();
if (/consent\.youtube\.com|google\.com\/sorry/.test(res.url) || (/g-recaptcha|unusual traffic/i.test(text) && text.length < 200_000)) throw new Blocked('youtube bot check');
return text;
}
/**
* 연령제한: the anonymous watch page asks to sign in to confirm age. YouTube then refuses the video in every embedded player
* (error 150), so the game plays downloaded audio instead (collector/age-audio.ts).
*/
export function isAgeGated(html: string): boolean {
const m = /"playabilityStatus":\{"status":"([A-Z_]+)"(?:,"reason":"([^"]*)")?/.exec(html);
if (!m) return false;
return m[1]!.startsWith('AGE_') || (m[1] === 'LOGIN_REQUIRED' && /성인|연령|age/i.test(m[2] ?? ''));
}
/** Remember that the song's current video is age-restricted (the game leaves it out until its audio is downloaded). */
export function markAgeGated(db: Database, songId: number, videoId: string): boolean {
return db.query("UPDATE songs SET age_video = ? WHERE id = ? AND youtube_id = ? AND COALESCE(age_video, '') <> ?").run(videoId, songId, videoId, videoId).changes === 1;
}
/** Integrated loudness (LUFS) of the video from the watch page, or null when the page has none. */
export function parseLoudness(html: string): number | null {
const cfg = /"audioConfig":\{([^}]*)/.exec(html)?.[1];
@@ -52,7 +68,7 @@ export async function collectLoudness(
const fetchPage = opts.fetchPage ?? fetchWatchPage;
const job = new Job(db, 'loudness', rows.length);
const cat = new Catalog(db);
const stats = { set: 0, none: 0, failed: 0 };
const stats = { set: 0, none: 0, age: 0, failed: 0 };
job.log(`start ${rows.length} songs${opts.deadline !== undefined ? ` deadline ${new Date(opts.deadline).toISOString()}` : ''}`);
try {
for (const [i, s] of rows.entries()) {
@@ -61,7 +77,9 @@ export async function collectLoudness(
break;
}
try {
if (saveLoudness(db, s.id, s.youtube_id, parseLoudness(await fetchPage(s.youtube_id)))) stats.set++;
const page = await fetchPage(s.youtube_id);
if (isAgeGated(page) && markAgeGated(db, s.id, s.youtube_id)) stats.age++;
if (saveLoudness(db, s.id, s.youtube_id, parseLoudness(page))) stats.set++;
else (stats.none++, cat.signal(s.id, 'yt_loudness_tried', Date.now()));
} catch (e) {
stats.failed++;
@@ -75,7 +93,7 @@ export async function collectLoudness(
if ((i + 1) % 100 === 0) job.log(`loudness ${i + 1}/${rows.length}: set ${stats.set}, no data ${stats.none}`);
await sleep(opts.delayMs ?? 3000);
}
job.log(`loudness done: set ${stats.set}, no data ${stats.none}, failed ${stats.failed}`);
job.log(`loudness done: set ${stats.set}, no data ${stats.none}, age-restricted ${stats.age}, failed ${stats.failed}`);
job.finish('done');
} catch (e) {
job.log(`stopped: ${(e as Error).message}`);

View File

@@ -0,0 +1,6 @@
-- 연령제한 곡: YouTube refuses age-restricted videos in every embedded player (error 150, even for signed-in adults).
-- age_video: the video found age-restricted (its anonymous watch page asks to sign in to confirm age).
-- audio_video: the video whose audio the server downloaded with the adult-verified account (AUDIO_DIR/<id>.m4a);
-- the game plays that file instead of the YouTube player. Both are tied to the video, like loudness_video.
ALTER TABLE songs ADD COLUMN age_video TEXT;
ALTER TABLE songs ADD COLUMN audio_video TEXT;

View File

@@ -6,6 +6,8 @@ import { afterEach, describe, expect, test } from 'bun:test';
import type { ChatLine, GameView, ServerMessage } from '@mq/shared';
import { Catalog } from './collector/catalog';
import { loudnessGain } from './songs/quiz';
import { audioDir, audioFile } from './songs/audio';
import { mkdirSync, writeFileSync } from 'node:fs';
import { Client, api, boot, discordLogin, guest, sleep } from './test-utils';
type Msg<T extends ServerMessage['t']> = Extract<ServerMessage, { t: T }>;
@@ -60,8 +62,13 @@ function seedLater(fn: () => void) {
pendingSeed.push(fn);
}
/** Collector runs the server asked for (no real CLI in tests). */
let collectRuns: string[] = [];
const fakeCollect = { kinds: [], run: (kind: string) => (collectRuns.push(kind), null), stop: () => null };
async function room(players: string[], config: Record<string, unknown> = {}, timing: Record<string, number> = {}) {
s = boot({ gameTiming: { ...TIMING, ...timing }, discordExchange: fakeDiscord }, { DISCORD_CLIENT_ID: 'cid', DISCORD_CLIENT_SECRET: 'sec' });
collectRuns = [];
s = boot({ gameTiming: { ...TIMING, ...timing }, discordExchange: fakeDiscord, collect: fakeCollect }, { DISCORD_CLIENT_ID: 'cid', DISCORD_CLIENT_SECRET: 'sec' });
for (const f of pendingSeed) f();
pendingSeed = [];
const users = [];
@@ -120,7 +127,7 @@ describe('game flow', () => {
expect(playA.startAt).toBe(playB.startAt);
expect(playA.atSec).toBe(0);
// answering during the 3·2·1 is just chat
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(pa.videoId)!.title;
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(pa.videoId!)!.title;
b.send({ t: 'chat', text: title, n: 'early' });
await b.waitFor((m) => m.t === 'chat' && m.line.n === 'early');
await sleep(Math.max(0, playA.startAt - Date.now()) + 10);
@@ -219,7 +226,7 @@ describe('game flow', () => {
const g = await gameWhere(a, (x) => x.excluded.includes(users[1]!.id));
expect(g.phase).toBe('play');
// excluded players cannot answer this question
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(last(a, 'prepare')!.videoId)!.title;
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(last(a, 'prepare')!.videoId!)!.title;
await sleep(80);
b.send({ t: 'chat', text: title, n: 'x' });
await a.waitFor((m) => m.t === 'chat' && m.line.n === 'x');
@@ -264,7 +271,7 @@ describe('game flow', () => {
expect(a.messages.slice(fa).some((m) => m.t === 'pause' || m.t === 'prepare')).toBe(false);
expect(a.messages.slice(fa).some((m) => m.t === 'chat' && m.line.text.includes('들어왔어요'))).toBe(false);
// still a player of this question: the answer counts
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(prep.videoId)!.title;
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(prep.videoId!)!.title;
b2.send({ t: 'chat', text: title });
expect((await gameWhere(a, (g) => g.phase === 'reveal')).history[0]).toMatchObject({ winnerId: users[1]!.id, how: 'correct' });
stop();
@@ -307,8 +314,50 @@ describe('game flow', () => {
const again = await nextOf(a, 'prepare', fa);
expect(again.n).toBe(1);
expect(again.videoId).not.toBe(first.videoId);
expect(s.db.query<{ youtube_status: string }, [string]>('SELECT youtube_status FROM songs WHERE youtube_id = ?').get(first.videoId)!.youtube_status).toBe('dead');
expect(s.db.query<{ youtube_status: string }, [string]>('SELECT youtube_status FROM songs WHERE youtube_id = ?').get(first.videoId!)!.youtube_status).toBe('dead');
expect((await gameWhere(a, (g) => g.n === 1)).total).toBe(2);
// maybe 연령제한: the collector looks at it
expect(collectRuns).toEqual(['age-audio']);
});
test('연령제한 곡: plays our downloaded file by a short-lived address (never the video id); without the file it is left out', async () => {
seedLater(() => {
const [x, y] = seed(3);
s.db.query("UPDATE songs SET age_video = youtube_id, audio_video = youtube_id, loudness_lufs = -8, loudness_video = youtube_id WHERE id = ?").run(x!);
s.db.query('UPDATE songs SET age_video = youtube_id WHERE id = ?').run(y!);
mkdirSync(audioDir(s.config.dbPath), { recursive: true });
writeFileSync(audioFile(audioDir(s.config.dbPath), 'vid1'), Buffer.alloc(1000, 7));
});
const { users, cls, code } = await room(['방장', '철수']);
const [a, b] = cls as [Client, Client];
expect(last(a, 'room')!.room.pool.playable).toBe(2);
a.send({ t: 'start' });
const p1 = await nextOf(a, 'prepare', 0);
for (const cl of cls) cl.send({ t: 'audio', n: 1, ev: 'ready' });
await nextOf(a, 'play', 0);
a.send({ t: 'vote', kind: 'skip' });
await gameWhere(a, (g) => g.phase === 'reveal');
a.send({ t: 'vote', kind: 'next' });
b.send({ t: 'vote', kind: 'next' });
const p2 = await nextOf(a, 'prepare', 0, (m) => m.n === 2);
const seen = [p1, p2];
expect((await gameWhere(a, (g) => g.n === 2)).total).toBe(2);
const file = seen.find((p) => p.audio)!;
const yt = seen.find((p) => !p.audio)!;
expect(file).toMatchObject({ videoId: null, startSec: 0, gain: loudnessGain(-8, true) });
expect(file.audio).toMatch(/^\/api\/audio\/[\w-]{24}$/);
expect(yt.videoId).toBe('vid3');
// seeking works (Range); a made-up address is not found
const r = await fetch(s.base + file.audio, { headers: { range: 'bytes=10-19' } });
expect(r.status).toBe(206);
expect((await r.arrayBuffer()).byteLength).toBe(10);
expect((await fetch(`${s.base}/api/audio/nope`)).status).toBe(404);
// a refresh mid-question gets the same source back (the same address for the file)
b.close();
const b2 = await new Client(s.base, users[1]!.cookie).connect();
b2.send({ t: 'join', code });
expect(await nextOf(b2, 'prepare', 0)).toMatchObject({ n: 2, audio: p2.audio, videoId: p2.videoId });
b2.close();
});
test('skip votes over the connected players (1/2 of 2 → 1), hint votes when 직접선택, replay restarts the clip for all', async () => {
@@ -431,7 +480,7 @@ describe('game flow', () => {
for (let n = 1; ; n++) {
const p = await nextOf(a, 'play', 0, (m) => m.n === n && !a.messages.some((x) => x.t === 'answer' && false));
await sleep(Math.max(0, p.startAt - Date.now()) + 10);
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(last(a, 'prepare')!.videoId)!.title;
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(last(a, 'prepare')!.videoId!)!.title;
a.send({ t: 'chat', text: title });
const g = await gameWhere(a, (x) => x.history.length === n);
if (g.n >= g.total) return g;
@@ -454,10 +503,10 @@ describe('game flow', () => {
const asked: string[] = [];
for (let n = 1; n <= 2; n++) {
const pr = await nextOf(a, 'prepare', fa, (m) => m.n === n);
asked.push(pr.videoId);
asked.push(pr.videoId!);
const p = await nextOf(a, 'play', fa, (m) => m.n === n);
await sleep(Math.max(0, p.startAt - Date.now()) + 10);
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(pr.videoId)!.title;
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(pr.videoId!)!.title;
a.send({ t: 'chat', text: title });
await gameWhere(a, (x) => x.history.length === n);
}
@@ -494,7 +543,7 @@ describe('game flow', () => {
const order: string[] = [];
for (let n = 1; n <= 6; n++) {
const pr = await nextOf(a, 'prepare', 0, (m) => m.n === n);
order.push(s.db.query<{ artist: string }, [string]>('SELECT artist FROM songs WHERE youtube_id = ?').get(pr.videoId)!.artist);
order.push(s.db.query<{ artist: string }, [string]>('SELECT artist FROM songs WHERE youtube_id = ?').get(pr.videoId!)!.artist);
a.send({ t: 'vote', kind: 'skip' });
await gameWhere(a, (x) => x.history.length === n);
}
@@ -543,7 +592,7 @@ describe('퀴즈 저장·불러오기 (docs/12 Q8)', () => {
a.send({ t: 'start' });
const p = await nextOf(a, 'play', 0, (m) => m.n === 1);
await sleep(Math.max(0, p.startAt - Date.now()) + 10);
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(last(a, 'prepare')!.videoId)!.title;
const title = s.db.query<{ title: string }, [string]>('SELECT title FROM songs WHERE youtube_id = ?').get(last(a, 'prepare')!.videoId!)!.title;
a.send({ t: 'chat', text: title });
await gameWhere(a, (x) => x.history.length === 1);
a.send({ t: 'save', name: '중간' });

View File

@@ -58,6 +58,8 @@ export class Game {
total: number;
song: QuizSong | null = null;
private clip: { startSec: number; endSec: number | null } = { startSec: 0, endSec: null };
/** 연령제한 곡: this question's address of our audio file (songs/audio.ts), null = the YouTube player. */
private audioSrc: string | null = null;
participants = new Set<string>();
excluded = new Set<string>();
private ready = new Set<string>();
@@ -157,7 +159,7 @@ export class Game {
let song: QuizSong | null = null;
while (this.n <= this.total) {
song = quizSong(this.room.deps.db, this.list[this.n - 1]!);
if (song && (this.listening ? song.videoId : song.lyrics)) break;
if (song && (this.listening ? song.videoId && (!song.ageRestricted || (song.audio && this.room.deps.audio)) : song.lyrics)) break;
this.list.splice(this.n - 1, 1);
this.total = c.count ? Math.min(c.count, this.list.length) : this.list.length;
song = null;
@@ -165,6 +167,7 @@ export class Game {
if (!song) return this.finish();
const now = this.now();
this.song = song;
this.audioSrc = this.listening && song.ageRestricted ? this.room.deps.audio!.link(song.videoId!) : null;
this.clip = this.listening ? clipOf(song, c) : { startSec: 0, endSec: null };
this.participants = new Set(this.room.online());
this.excluded.clear();
@@ -206,7 +209,14 @@ export class Game {
this.ready.clear();
this.waitingSince.clear();
for (const id of this.active()) this.waitingSince.set(id, now);
this.toActive({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song!.videoId!, startSec: atSec, endSec: this.clip.endSec, gain: loudnessGain(this.song!.loudness) });
this.toActive({ t: 'prepare', code: this.room.code, n: this.n, ...this.source(), startSec: atSec, endSec: this.clip.endSec });
}
/** What the players load: the YouTube video, or our file for a 연령제한 곡 (never the video id then). */
private source(): { videoId: string | null; audio: string | null; gain: number } {
const s = this.song!;
if (this.audioSrc) return { videoId: null, audio: this.audioSrc, gain: loudnessGain(s.loudness, true) };
return { videoId: s.videoId!, audio: null, gain: loudnessGain(s.loudness) };
}
private checkReady(): void {
@@ -414,7 +424,9 @@ export class Game {
}
if (ev === 'error' && (code === 100 || code === 101 || code === 150) && (this.phase === 'prepare' || this.phase === 'play') && this.song) {
// The video is gone or may not be embedded: swap the song for everyone, same question number (docs/12 Q1).
// 연령제한 곡 also land here (150): the collector checks it and brings the song back with downloaded audio.
markUnplayable(this.room.deps.db, this.song.id, code);
this.room.deps.onUnplayable?.(this.song.id);
this.room.say(null, '이 곡은 재생할 수 없어서 다른 곡으로 바꿨어요');
this.toActive({ t: 'pause', code: this.room.code, n: this.n });
this.list.splice(this.n - 1, 1);
@@ -515,10 +527,10 @@ export class Game {
if (this.awaitingReady) {
this.ready.delete(userId);
if (!this.waitingSince.has(userId)) this.waitingSince.set(userId, this.now());
send({ t: 'prepare', code: this.room.code, n: this.n, videoId: this.song.videoId, startSec: this.resumeAt, endSec: this.clip.endSec, gain: loudnessGain(this.song.loudness) });
send({ t: 'prepare', code: this.room.code, n: this.n, ...this.source(), startSec: this.resumeAt, endSec: this.clip.endSec });
this.broadcast();
} else if (this.playing && !this.clipOver) {
send({ t: 'catchup', code: this.room.code, n: this.n, videoId: this.song.videoId, clipStart: this.clip.startSec, startAt: this.audioStartAt, atSec: this.audioAtSec, endSec: this.clip.endSec, gain: loudnessGain(this.song.loudness) });
send({ t: 'catchup', code: this.room.code, n: this.n, ...this.source(), clipStart: this.clip.startSec, startAt: this.audioStartAt, atSec: this.audioAtSec, endSec: this.clip.endSec });
}
}

View File

@@ -14,6 +14,7 @@ import type { SettingsStore } from '../admin/settings';
import type { Database } from 'bun:sqlite';
import type { RoomManager } from '../rooms/manager';
import { genreList } from '../songs/store';
import type { AudioLinks } from '../songs/audio';
import { countPool, searchArtists, yearBounds } from '../songs/pool';
import { DEFAULT_ROOM_CONFIG, applyConfigPatch, type RoomConfig } from '@mq/shared';
@@ -34,6 +35,8 @@ export interface HttpDeps {
admin: Omit<AdminDeps, 'users' | 'sessions' | 'roles' | 'settings'>;
/** Deletes a guest account everywhere (sockets, DB). */
removeGuest: (userId: string) => void;
/** 연령제한 곡: the game's short-lived addresses of downloaded audio (songs/audio.ts). */
audio?: AudioLinks;
}
type Env = { Variables: { userId: string | null } };
@@ -86,6 +89,14 @@ export function createHttpApp(d: HttpDeps) {
return c.json({ build: d.build?.() ?? null });
});
// 연령제한 곡: our downloaded audio by the question's random address (Bun answers Range requests for seeking).
app.get('/api/audio/:token', async (c) => {
const path = d.audio?.file(c.req.param('token'));
const f = path ? Bun.file(path) : null;
if (!f || !(await f.exists())) return c.json({ error: '없는 음원이에요.' }, 404);
return new Response(f, { headers: { 'content-type': 'audio/mp4', 'cache-control': 'private, max-age=3600' } });
});
app.get('/api/config', (c) => {
const site = d.settings.getSite();
return c.json({ discord: d.config.discord !== null, siteName: site.siteName, announcement: site.announcement, maintenance: site.maintenance });

View File

@@ -8,6 +8,7 @@ import { DEFAULT_ROOM_CONFIG, applyConfigPatch, configSummary, type ChatLine, ty
import { DEFAULT_TIMING, Game, newQuiz, pickSongs, type GameTiming, type Quiz } from '../game/engine';
import { SavedQuizzes } from '../game/saved';
import { artistsByIds, countPool } from '../songs/pool';
import type { AudioLinks } from '../songs/audio';
import type { WsData } from '../ws/gateway';
/** Back within this long after a disconnect: keeps the place (and later the score). */
@@ -53,7 +54,7 @@ export class Room {
readonly code: string,
hostId: string,
config: RoomConfig,
readonly deps: { db: Database; user: PublicUserLookup; limitSec?: () => number },
readonly deps: { db: Database; user: PublicUserLookup; limitSec?: () => number; audio?: AudioLinks; onUnplayable?: (songId: number) => void },
) {
this.hostId = hostId;
this.config = config;
@@ -160,6 +161,10 @@ export class RoomManager {
timing?: Partial<GameTiming>;
/** Site room defaults and locked settings (admin 사이트 설정). */
roomPolicy?: () => { defaults: Record<string, unknown>; locks: string[] };
/** 연령제한 곡: addresses of downloaded audio files (songs/audio.ts). */
audio?: AudioLinks;
/** A player could not play a song (deleted / not embeddable / 연령제한): the collector looks at it. */
onUnplayable?: (songId: number) => void;
},
) {
this.saved = new SavedQuizzes(deps.db);

View File

@@ -14,6 +14,7 @@ import { RoomManager } from './rooms/manager';
import { toPublicUser } from './auth/users';
import { purgeTrash } from './songs/admin-ops';
import { collectorControl } from './collector/control';
import { AudioLinks, audioDir } from './songs/audio';
import type { CollectControl } from './http/admin-catalog';
/** A guest who closed the site and stayed away this long is deleted (docs/02). */
@@ -55,6 +56,7 @@ export function startServer(opts: StartOptions) {
let genreNames = new Map<string, string>();
const loadGenres = () => (genreNames = new Map(db.query<{ id: string; name: string }, []>('SELECT id, name FROM genres').all().map((g) => [g.id, g.name])));
loadGenres();
const audio = new AudioLinks(audioDir(config.dbPath));
const rooms = new RoomManager({
db,
user: (id) => {
@@ -66,8 +68,14 @@ export function startServer(opts: StartOptions) {
limitSec: () => settings.getSite().questionLimitSec,
timing: opts.gameTiming,
roomPolicy: () => ({ defaults: settings.getSite().roomDefaults, locks: settings.getSite().roomLocks }),
audio,
// A player could not play a song: maybe 연령제한 — the collector checks it and downloads its audio (one run at a time).
onUnplayable: (songId) => {
const err = collect.run('age-audio');
log('info', 'unplayable song', { songId, ageAudio: err ?? 'started' });
},
});
const collect = opts.collect ?? collectorControl(db, log);
const collect = opts.collect ?? collectorControl(db, log, config.dbPath);
const staticDir = opts.staticDir === undefined ? join(import.meta.dir, '../../web/dist') : opts.staticDir;
const serveStatic = !!staticDir && existsSync(staticDir);
const build = serveStatic ? webBuildId(staticDir!) : null;
@@ -110,6 +118,7 @@ export function startServer(opts: StartOptions) {
build: () => build,
health: () => ({ ok: ready, connections: gateway.connectionCount, rooms: rooms.rooms.size }),
removeGuest,
audio,
admin: {
audit,
db,

View File

@@ -0,0 +1,37 @@
/**
* 연령제한 곡 (docs/08 §2-1): YouTube refuses age-restricted videos in every embedded player (error 150), whoever watches.
* The collector downloads their audio with the adult-verified account (collector/age-audio.ts); the game plays the file
* from here through a random short-lived address, so the page never sees the video id.
*/
import { randomBytes } from 'node:crypto';
import { dirname, join } from 'node:path';
/** Where downloaded audio lives: AUDIO_DIR, else `audio/` next to the DB (the /data volume in the container). */
export function audioDir(dbPath: string, env: Record<string, string | undefined> = process.env): string {
return env.AUDIO_DIR ?? join(dirname(dbPath), 'audio');
}
export const audioFile = (dir: string, videoId: string) => join(dir, `${videoId}.m4a`);
export class AudioLinks {
private links = new Map<string, { file: string; exp: number }>();
constructor(
readonly dir: string,
private ttlMs = 6 * 3600_000,
private now: () => number = Date.now,
) {}
/** A new address for this video's file (one per question; replays and rejoins reuse it). */
link(videoId: string): string {
const now = this.now();
for (const [k, v] of this.links) if (v.exp < now) this.links.delete(k);
const token = randomBytes(18).toString('base64url');
this.links.set(token, { file: audioFile(this.dir, videoId), exp: now + this.ttlMs });
return `/api/audio/${token}`;
}
file(token: string): string | null {
const l = this.links.get(token);
return l && l.exp >= this.now() ? l.file : null;
}
}

View File

@@ -2,7 +2,8 @@
* Which songs a room's settings select (docs/04, docs/12 Q7·Q9).
* - 장르: same group "또는", different groups "그리고"; empty = 전체
* - 난이도 N: a song qualifies when any applicable scope has level N: 'all', each chosen genre, each decade the year range touches
* - 재생 가능: listening modes need a confirmed (matched, embeddable) YouTube video; the lyrics mode needs lyrics
* - 재생 가능: listening modes need a confirmed (matched, embeddable) YouTube video — an age-restricted one only once the
* server has its audio (연령제한 곡, songs/audio.ts); the lyrics mode needs lyrics
*/
import type { Database } from 'bun:sqlite';
import type { RoomConfig } from '@mq/shared';
@@ -46,7 +47,7 @@ function where(db: Database, c: RoomConfig): { sql: string; args: Args } {
}
function playable(c: RoomConfig): string {
return c.mode === 'lyrics' ? 'EXISTS (SELECT 1 FROM lyrics y WHERE y.song_id = s.id)' : "s.youtube_status = 'matched' AND s.embeddable = 1";
return c.mode === 'lyrics' ? 'EXISTS (SELECT 1 FROM lyrics y WHERE y.song_id = s.id)' : "s.youtube_status = 'matched' AND s.embeddable = 1 AND (COALESCE(s.age_video, '') <> s.youtube_id OR s.audio_video = s.youtube_id)";
}
/** playable = songs a game can use now; total = also those still waiting for a YouTube match (lyrics mode: same). */

View File

@@ -14,6 +14,9 @@ export interface QuizSong {
/** YouTube's loudness of that video (LUFS), null when not measured (collector/loudness.ts). */
loudness: number | null;
highlightSec: number | null;
/** 연령제한: YouTube refuses this video in embedded players; `audio` = the server has its audio file (songs/audio.ts). */
ageRestricted: boolean;
audio: boolean;
genres: string[];
/** Level shown on the reveal card: the 'all' scope level when there is one. */
level: number | null;
@@ -27,9 +30,9 @@ export interface QuizSong {
export function quizSong(db: Database, id: number): QuizSong | null {
const s = db
.query<
{ id: number; title: string; artist: string; year: number | null; duration_sec: number | null; youtube_id: string | null; highlight_sec: number | null; youtube_status: string; deleted_at: number | null; loudness_lufs: number | null; loudness_video: string | null },
{ id: number; title: string; artist: string; year: number | null; duration_sec: number | null; youtube_id: string | null; highlight_sec: number | null; youtube_status: string; deleted_at: number | null; loudness_lufs: number | null; loudness_video: string | null; age_video: string | null; audio_video: string | null },
[number]
>('SELECT id, title, artist, year, duration_sec, youtube_id, highlight_sec, youtube_status, deleted_at, loudness_lufs, loudness_video FROM songs WHERE id = ?')
>('SELECT id, title, artist, year, duration_sec, youtube_id, highlight_sec, youtube_status, deleted_at, loudness_lufs, loudness_video, age_video, audio_video FROM songs WHERE id = ?')
.get(id);
if (!s || s.deleted_at) return null;
const col = <T>(sql: string) => db.query<{ v: T }, [number]>(sql).all(id).map((r) => r.v);
@@ -42,6 +45,8 @@ export function quizSong(db: Database, id: number): QuizSong | null {
videoId: s.youtube_status === 'matched' ? s.youtube_id : null,
loudness: s.youtube_id && s.loudness_video === s.youtube_id ? s.loudness_lufs : null,
highlightSec: s.highlight_sec,
ageRestricted: !!s.youtube_id && s.age_video === s.youtube_id,
audio: !!s.youtube_id && s.age_video === s.youtube_id && s.audio_video === s.youtube_id,
genres: col<string>('SELECT genre_id v FROM song_genres WHERE song_id = ?'),
level: db.query<{ level: number }, [number]>("SELECT level FROM song_levels WHERE song_id = ? ORDER BY scope = 'all' DESC LIMIT 1").get(id)?.level ?? null,
titleAliases: col<string>("SELECT alias v FROM song_aliases WHERE song_id = ? AND part = 'title'"),
@@ -85,12 +90,16 @@ export const YOUTUBE_TARGET_LUFS = -14;
*/
export const TARGET_LUFS = -18;
/** A downloaded file (연령제한 곡) whose loudness is unknown: assume a loud modern master. */
export const FILE_UNMEASURED_LUFS = -9;
/**
* 소리 크기 맞추기: factor (0..1) for the player volume so this song sounds like TARGET_LUFS — on top of YouTube's own
* turn-down. Not measured yet: the gain of a typical song (louder than YouTube's target).
* `file`: our own downloaded audio (연령제한 곡) — no YouTube turn-down, so the whole difference is ours.
*/
export function loudnessGain(lufs: number | null): number {
const heard = Math.min(lufs ?? YOUTUBE_TARGET_LUFS, YOUTUBE_TARGET_LUFS);
export function loudnessGain(lufs: number | null, file = false): number {
const heard = file ? (lufs ?? FILE_UNMEASURED_LUFS) : Math.min(lufs ?? YOUTUBE_TARGET_LUFS, YOUTUBE_TARGET_LUFS);
return Math.round(Math.min(1, 10 ** ((TARGET_LUFS - heard) / 20)) * 1000) / 1000;
}

BIN
apps/web/public/silence.m4a Normal file

Binary file not shown.

View File

@@ -7,6 +7,8 @@
* 0.3s after the start → blocked
* - catchUp: back in a running question (refresh / reconnect): load muted where everyone is, then play along; a fresh
* page first waits for "참가 (소리 켜기)" (the browser blocks sound before a click), the others never stop for it
* 연령제한 곡 (YouTube refuses them in embedded players): the server sends its own file's address (`/api/audio/…`) instead of a
* video id; an <audio> element plays it behind the same interface, so all of the above works the same.
* A fake player (localStorage mq:fakePlayer = "1") lets e2e runs play 30 questions without YouTube.
*/
import { create } from 'zustand';
@@ -140,6 +142,98 @@ function youtubeEngine(l: Listener): Engine {
};
}
// ---------------------------------------------------------------- our own audio file (연령제한 곡)
/** A short silent file the entry click plays, so this <audio> element may play with sound later (Safari unlocks per element). */
const SILENCE = '/silence.m4a';
function fileEngine(l: Listener): Engine {
let a: HTMLAudioElement | null = null;
let current: string | null = null;
let state = -1;
const set = (s: number) => {
if (state === s) return;
state = s;
l.onState?.(s);
};
const el = () => {
if (a) return a;
a = new Audio();
a.preload = 'auto';
a.addEventListener('playing', () => current !== SILENCE && set(1));
a.addEventListener('pause', () => current !== SILENCE && set(a!.ended ? 0 : 2));
a.addEventListener('waiting', () => current !== SILENCE && set(3));
a.addEventListener('ended', () => current !== SILENCE && set(0));
// 4 = the address answers 404 (gone) or the file is broken: like a deleted video (100). Others: network, retried by prepare.
a.addEventListener('error', () => current && current !== SILENCE && l.onError?.(a!.error?.code === 4 ? 100 : 5));
return a;
};
const start = (x: HTMLAudioElement) =>
x.play().catch((e: unknown) => {
if ((e as { name?: string })?.name === 'NotAllowedError' && current !== SILENCE) l.onBlocked?.();
});
return {
init: () => Promise.resolve(void el()),
unlock() {
const x = el();
// Inside the click: play something with sound on (volume 0), like the YouTube player's unlock.
x.volume = 0;
x.muted = false;
if (!current) {
current = SILENCE;
x.src = SILENCE;
}
void start(x);
},
load(src, startSec) {
const x = el();
current = src;
state = -1;
x.src = src;
x.addEventListener('loadedmetadata', () => current === src && (x.currentTime = startSec), { once: true });
void start(x);
},
seek: (sec) => void (el().currentTime = sec),
play: () => void start(el()),
pause: () => el().pause(),
mute: (on) => void (el().muted = on),
isMuted: () => el().muted,
volume: (v) => void (el().volume = Math.max(0, Math.min(1, v / 100))),
time: () => a?.currentTime ?? 0,
state: () => (current === SILENCE ? -1 : state),
videoId: () => (current === SILENCE ? null : current),
};
}
/** The YouTube player for video ids, our <audio> for `/api/audio/…` addresses; one of them is in use at a time. */
function routedEngine(yt: Engine, file: Engine): Engine {
let cur = yt;
return {
init: () => Promise.all([yt.init(), file.init()]).then(() => {}),
unlock() {
// Both, so the next question may use either; the one not in use stops again after the click.
yt.unlock();
file.unlock();
const other = cur === yt ? file : yt;
setTimeout(() => other !== cur && other.pause(), 2000);
},
load(id, startSec) {
const next = id.startsWith('/') ? file : yt;
if (next !== cur) cur.pause();
cur = next;
cur.load(id, startSec);
},
seek: (sec) => cur.seek(sec),
play: () => cur.play(),
pause: () => cur.pause(),
mute: (on) => (yt.mute(on), file.mute(on)),
isMuted: () => cur.isMuted(),
volume: (v) => (yt.volume(v), file.volume(v)),
time: () => cur.time(),
state: () => cur.state(),
videoId: () => cur.videoId(),
};
}
// ---------------------------------------------------------------- fake engine for e2e (no network)
function fakeEngine(l: Listener): Engine {
let state = -1;
@@ -248,7 +342,7 @@ function eng(): Engine {
},
onBlocked: () => blocked(),
};
engine = useFake() ? fakeEngine(l) : youtubeEngine(l);
engine = useFake() ? fakeEngine(l) : routedEngine(youtubeEngine(l), fileEngine(l));
window.mqPlayer = { pos: () => engine!.time(), state: () => engine!.state(), muted: () => engine!.isMuted(), video: () => engine!.videoId() };
}
return engine;

View File

@@ -128,7 +128,7 @@ function onMessage(m: ServerMessage): void {
return;
}
case 'prepare':
if (m.code === useRoom.getState().code) void player.prepare(m.n, m.videoId, m.startSec, m.endSec, m.gain);
if (m.code === useRoom.getState().code) void player.prepare(m.n, m.audio ?? m.videoId!, m.startSec, m.endSec, m.gain);
return;
case 'play':
if (m.code === useRoom.getState().code) player.play(m.n, m.startAt, m.atSec, m.endSec);
@@ -137,7 +137,7 @@ function onMessage(m: ServerMessage): void {
if (m.code === useRoom.getState().code) player.pause();
return;
case 'catchup':
if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.videoId, m.clipStart, m.startAt, m.atSec, m.endSec, m.gain);
if (m.code === useRoom.getState().code) void player.catchUp(m.n, m.audio ?? m.videoId!, m.clipStart, m.startAt, m.atSec, m.endSec, m.gain);
return;
case 'typing':
if (m.code === useRoom.getState().code) useRoom.setState({ typing: m.ids });

View File

@@ -26,6 +26,11 @@ COPY --from=build /app/apps/server ./apps/server
COPY --from=build /app/apps/web/dist ./apps/web/dist
# Song seed for the collector (docs/07): bun apps/server/src/collector/cli.ts import-sta
COPY data/seed ./data/seed
# 연령제한 곡 (docs/08 §2-1): yt-dlp downloads their audio with the adult-verified cookie (bun solves YouTube's JS
# challenge). Pinned with its checksum; when YouTube breaks it, bump both from the release's SHA2-256SUMS.
ARG YTDLP_VERSION=2026.08.19
ADD --chmod=755 --checksum=sha256:58162f9bfdc27458ea47bfcb311cf47028f17d8154a8bf7d689861d46399230a \
https://github.com/yt-dlp/yt-dlp/releases/download/${YTDLP_VERSION}/yt-dlp_linux /usr/local/bin/yt-dlp
RUN mkdir -p /data && chown bun:bun /data
USER bun
VOLUME ["/data"]

View File

@@ -61,6 +61,27 @@
- 실제 브라우저 시험 결과와 남은 수동 검증 목록은 12-decisions.md Q1
- 퍼가기 금지 영상 → 매칭 때 oEmbed(`https://www.youtube.com/oembed?url=https://youtu.be/ID`)가 401이면 제외, 재생 오류 101/150이면 건너뛰고 "재생 불가" 표시
## 2-1. 연령제한 곡: 서버에 받아 둔 음원으로 재생 (2026-10-20)
유튜브는 외부 사이트에 넣은 플레이어(IFrame)에서 연령제한 영상을 막는다. 보는 사람이 로그인했든 성인 인증을 받았든 똑같이 오류 150이다(https://support.google.com/youtube/answer/2802167). 그래서 이런 곡만 서버가 성인 인증된 계정으로 음원을 한 번 받아 두고, 게임은 그 파일을 재생한다.
직접 확인한 사실 (2026-10-20)
| 시험 | 결과 |
|---|---|
| 연령제한 영상 `6kLq3WMV1nU` 영상 페이지, 로그인 없이 | `playabilityStatus` `LOGIN_REQUIRED` "성인 인증하려면 로그인하세요", 소리 크기·heatmap 없음 |
| 같은 페이지, `ytm_cookie.txt` 계정으로 | `OK`(이 계정은 성인 인증됨), 소리 크기 -14.17 LUFS(ffmpeg로 잰 파일 -14.2와 같음) |
| yt-dlp 2026.08.19(`--cookies`, `--js-runtimes bun`, `-f bestaudio[ext=m4a]`) | 약 5초, 4MB m4a. 배포 이미지 안에서도 같음 |
| ffmpeg 없이 받은 DASH m4a를 크롬 `<audio>`로 | 재생·임의 위치 이동 정상, Bun이 Range 요청(206)을 알아서 처리 |
| 매칭된 곡 중 소리 크기를 잰 약 2,180곡 | 연령제한 0곡(못 잰 2곡은 "Music Premium 전용"). 연령제한 곡은 드물다 |
방식
- 찾기: 소리 크기·하이라이트 수집이 로그인 없이 받은 영상 페이지가 성인 인증 화면이면 `songs.age_video`에 표시. 게임 중 오류 100/101/150이 나면 지금처럼 다른 곡으로 바꾸고 "재생 불가"로 표시한 뒤 `age-audio` 작업을 바로 띄운다. 작업이 그 곡을 다시 보고, 연령제한이면 게임으로 되돌린다(음원을 받은 뒤부터 출제)
- 받기: `cli.ts age-audio`(`collector/age-audio.ts`). 로그인한 영상 페이지에서 소리 크기·heatmap 하이라이트를 저장하고, yt-dlp로 소리만 받아 `/data/audio/<영상id>.m4a`. 저장한 영상 id는 `songs.audio_video`. 곡당 3초 간격, 차단 신호면 멈춤, 실패한 곡은 하루 뒤 다시. 쿠키 계정으로도 성인 인증 화면이 나오면(쿠키 만료·성인 인증 안 된 계정) 작업을 멈추고 로그에 남김. 어느 곡도 안 쓰는 파일은 지움
- 출제: 연령제한 곡은 음원을 받은 뒤에만 낸다(`songs/pool.ts`). 영상이 바뀌면 둘 다 다시
- 재생: `prepare`·`catchup`에 `videoId` 대신 `audio`(`/api/audio/<무작위 토큰>`, 6시간 유효, 문제마다 새로 발급)를 보낸다. 브라우저는 그 주소면 숨긴 `<audio>`로 재생하고 나머지(같은 시각 시작, 재생 위치로 정지, 1초마다 위치 맞추기, 버퍼링 멈춤, 새로고침 이어 듣기)는 유튜브와 같다. 입장 클릭 때 `/silence.m4a`를 같이 틀어 이 `<audio>`도 소리를 허용받는다
- 소리 크기: 유튜브가 -14로 줄여 주지 않으므로 보정값 = min(1, 10^((-18 − 곡 크기)/20)), 못 잰 파일은 -9 LUFS로 본다
- 쿠키: `YT_COOKIE_PATH`(지금은 곡 검색과 같은 `ytm_cookie.txt`). 헤더 형식(`a=1; b=2`)과 cookies.txt 둘 다 읽는다
- 한계: 관리자 노래 편집의 미리듣기는 유튜브 플레이어라서 연령제한 곡은 들리지 않는다. yt-dlp는 유튜브가 바뀌면 깨질 수 있다(Dockerfile의 버전·체크섬을 올림). bun을 JS 실행기로 쓰는 것은 yt-dlp에서 "deprecated" 경고가 나온다(지금은 동작)
## 3. 재생 위치 "처음부터 / 하이라이트부터"
하이라이트 = 그 곡에서 가장 유명한 부분(대개 후렴). 곡마다 `highlight_ms`를 저장한다.

View File

@@ -5,7 +5,7 @@
인터넷 → NPMplus (192.168.10.3:443, mquiz.tkrmagid.kr, Let's Encrypt)
→ 192.168.10.9:2974 → 컨테이너 music_quiz-app:3000 (Bun)
└─ /data/app.db (SQLite, WAL)
↘ 음악은 각자 브라우저의 유튜브 플레이어가 재생(서버 음원 캐시 없음)
↘ 음악은 각자 브라우저의 유튜브 플레이어가 재생(연령제한 곡만 서버가 받아 둔 음원 /data/audio, docs/08 §2-1)
↘ Lavalink 192.168.10.6:2333 (유튜브 검색·재생 가능 확인, 읽기 전용 사용)
```
- 위치: .9 `/home/claude/projects/music_quiz` (joke-app과 같은 방식)
@@ -40,13 +40,13 @@
- 주 1회 유튜브 주소 재확인(삭제·지역제한 감지 → "재생 불가"로 표시 후 재매칭)
## 디스크
- .9 남은 공간 약 140GB(72% 사용). 음원 캐시가 없어서 이 프로젝트는 DB 정도만 쓴다
- .9 남은 공간 약 140GB(72% 사용). 음원은 연령제한 곡만 받아 두므로(곡당 3~5MB, 드묾) 이 프로젝트는 DB 정도만 쓴다
- 용량이 부족해지면(남은 공간 50GB 미만) `~/disk-low-cleanup.sh`가 다시 받으면 되는 설치 캐시(pip·uv·npm·bun)만 지운다(도커 빌드 캐시는 `docker-disk-guard.sh`). `~/.tmp`·모델·프로젝트·DB·실행 중인 작업은 건드리지 않는다 (12 문서)
## 실제 배포 상태 (2026-10-18)
- .9 `/home/claude/projects/music_quiz` (gitea main 클론), 컨테이너 `music_quiz-app-1`, 볼륨 `music_quiz_app-data`, `deploy/.env` 권한 600
- NPMplus 프록시 호스트 id 14 → `192.168.10.9:2974`, 고급 설정은 `deploy/npmplus-advanced.conf`, 인증서는 `deploy/cert.sh`(다시 돌려도 안전)
- 백업(매일 `VACUUM INTO`)은 노래 DB가 생기는 3단계에서 붙인다
- 수집 cron(.9 crontab, 컨테이너 안에서 `docker exec`): 02:00 차트, 02:20 멜론 곡 정보 1,000곡, 03:15 하이라이트 400곡(4초 간격), 03:55 난이도, 08:10 유튜브 원곡 찾기 3,000곡(04:00~08:00은 봇이 Claude를 부르지 않는 시간이라 피함), 12:30 소리 크기 재기 3,000곡(3초 간격, 새로 확정된 곡). 로그 `~/music_quiz-collect.log`, `~/music_quiz-match.log`. 관리자 "수집" 화면에서 따로 실행·멈추기 가능
- 수집 cron(.9 crontab, 컨테이너 안에서 `docker exec`): 02:00 차트, 02:20 멜론 곡 정보 1,000곡, 03:15 하이라이트 400곡(4초 간격), 03:55 난이도, 08:10 유튜브 원곡 찾기 3,000곡(04:00~08:00은 봇이 Claude를 부르지 않는 시간이라 피함), 12:30 소리 크기 재기 3,000곡(3초 간격, 새로 확정된 곡), 15:40 연령제한 곡 음원 받기 100곡(`age-audio`, 3초 간격). 로그 `~/music_quiz-collect.log`, `~/music_quiz-match.log`. 관리자 "수집" 화면에서 따로 실행·멈추기 가능
- 9단계 전 DB 백업: `~/backups/music_quiz/app-before-stage9-*.db`

View File

@@ -5,10 +5,13 @@
* everyone, skip / hint / replay votes with Ctrl+K / Ctrl+I / Ctrl+R, a broken video swapped, typing indicator,
* reconnect mid-game, refresh while the music runs (catch up in sync), results with YouTube links, 이어풀기, 다시 하기, phone / dark screens, the last one out closes the room.
* Part B plays real YouTube Topic videos in two browsers: sound on after the entry click, same start, 3s clip stop.
* Its 연령제한 step plays a downloaded age-restricted song (our <audio>) next to a YouTube one, when E2E_AGE_AUDIO points
* to that m4a (yt-dlp with the adult cookie: 6kLq3WMV1nU, docs/08 §2-1).
* Run: bun run build && bun e2e/game.e2e.ts (needs /usr/bin/google-chrome; Part B needs internet, skip with E2E_NO_YT=1)
*/
import { chromium, type Browser, type Page } from 'playwright-core';
import { mkdirSync, mkdtempSync } from 'node:fs';
import { copyFileSync, existsSync, mkdirSync, mkdtempSync } from 'node:fs';
import { audioDir, audioFile } from '../apps/server/src/songs/audio';
import { join } from 'node:path';
import { loadConfig } from '../apps/server/src/config';
import { startServer } from '../apps/server/src/server';
@@ -56,6 +59,23 @@ const db = app.db;
db.query("UPDATE songs SET youtube_status = 'matched', embeddable = 1, youtube_id = ?, duration_sec = ?, loudness_lufs = ?, loudness_video = ? WHERE id = ?").run(vid, dur, lufs, lufs === null ? null : vid, id!);
}
}
// 연령제한: a real age-restricted video whose audio was downloaded, next to a YouTube one (level 8)
const AGE_AUDIO = process.env.E2E_AGE_AUDIO ?? '';
if (AGE_AUDIO && existsSync(AGE_AUDIO)) {
const c = new Catalog(db);
const songs: [string, string, string, number, boolean][] = [
['연령제한노래', '어른가수', '6kLq3WMV1nU', 246, true],
['Love wins all', '아이유', 'Wu5FY8J6P8Q', 272, false],
];
for (const [title, artist, vid, dur, age] of songs) {
const [id] = c.upsert({ title, artists: [{ name: artist }], year: 2000, genres: ['kpop'], source: 'e2e', ref: vid });
c.autoLevel(id!, 'all', 8);
db.query("UPDATE songs SET youtube_status = 'matched', embeddable = 1, youtube_id = ?, duration_sec = ?, loudness_lufs = -14.17, loudness_video = youtube_id WHERE id = ?").run(vid, dur, id!);
if (age) db.query('UPDATE songs SET age_video = youtube_id, audio_video = youtube_id WHERE id = ?').run(id!);
}
mkdirSync(audioDir(config.dbPath), { recursive: true });
copyFileSync(AGE_AUDIO, audioFile(audioDir(config.dbPath), '6kLq3WMV1nU'));
}
const titleOf = (vid: string) => db.query<{ title: string; artist: string }, [string]>('SELECT title, artist FROM songs WHERE youtube_id = ?').get(vid)!;
const browser: Browser = await chromium.launch({
@@ -439,6 +459,52 @@ try {
await x.context().close();
await y.context().close();
});
if (AGE_AUDIO) await step('B: 연령제한 곡 — 받아 둔 음원(<audio>)과 유튜브 곡이 섞여도 같은 순간 재생·클립 정지, 음원 중 새로고침해도 같은 위치', async () => {
const x = await guestPage('어른방장', { fake: false });
const y = await guestPage('어른손님', { fake: false });
const rc = await makeRoom(x, { count: 10, level: 8, listenSec: 10, startAt: 'start' });
await y.goto(`${ORIGIN}/r/${rc}`);
await y.locator('.pool-banner').waitFor();
for (const p of [x, y]) await p.getByRole('button', { name: '참가 (소리 켜기)' }).click();
await x.getByText('조건에 맞는 곡 2곡').waitFor({ timeout: 5000 });
await x.getByRole('button', { name: '게임 시작' }).click();
await x.getByRole('button', { name: '그대로 시작' }).click();
const kinds: string[] = [];
for (const n of [1, 2]) {
await Promise.all([x, y].map((p) => playing(p, n, 40000)));
await x.waitForTimeout(1200);
const src = (await video(x))!;
const file = src.startsWith('/api/audio/');
kinds.push(file ? 'file' : 'youtube');
expect(file || src === 'Wu5FY8J6P8Q', `소스 ${src}`);
if (file) {
// the address says nothing about the video
expect(!src.includes('6kLq3WMV1nU'), '주소에 영상 id');
await y.reload();
await y.getByRole('button', { name: '🔊 참가 (소리 켜기)' }).click({ timeout: 30000 });
await y.locator('.stage .lp.spin').waitFor({ timeout: 15000 });
await y.waitForTimeout(800);
}
const st = await Promise.all([x, y].map((p) => p.evaluate(() => ({ pos: (window as any).mqPlayer.pos(), muted: (window as any).mqPlayer.muted(), state: (window as any).mqPlayer.state(), v: (window as any).mqPlayer.video() }))));
console.log(` ${n}번 ${file ? '받은 음원' : '유튜브'}${file ? '(손님 새로고침 뒤)' : ''} 위치 ${st.map((s) => s.pos.toFixed(3)).join(' / ')}`);
expect(st[0]!.v === st[1]!.v, `다른 곡 ${JSON.stringify(st)}`);
expect(st.every((s) => s.state === 1 && !s.muted), `재생 상태 ${JSON.stringify(st)}`);
expect(Math.abs(st[0]!.pos - st[1]!.pos) < 0.4, `두 사람 위치 차이 ${JSON.stringify(st)}`);
await x.locator('.stage .mode', { hasText: '재생이 끝났어요' }).waitFor({ timeout: 10000 });
const end = await Promise.all([x, y].map((p) => p.evaluate(() => ({ pos: (window as any).mqPlayer.pos(), state: (window as any).mqPlayer.state() }))));
console.log(` 멈춘 위치 ${end.map((s) => s.pos.toFixed(3)).join(' / ')}`);
expect(end.every((s) => s.state === 2 && s.pos >= 9.9 && s.pos < 10.5), `클립 뒤 정지 ${JSON.stringify(end)}`);
const title = file ? '연령제한노래' : 'Love wins all';
await say(x, title);
await x.locator('.reveal .big', { hasText: file ? '어른가수' : '아이유' }).waitFor({ timeout: 5000 });
if (file) await x.screenshot({ path: join(SHOTS, 'age-audio-reveal.png') });
}
expect(kinds.sort().join() === 'file,youtube', `곡 종류 ${kinds}`);
await x.getByRole('heading', { name: '🏁 게임 결과' }).waitFor({ timeout: 10000 });
await x.context().close();
await y.context().close();
});
}
expect(errors.length === 0, `페이지 오류: ${errors.join(' | ')}`);

View File

@@ -152,8 +152,9 @@ export type ServerMessage =
/**
* Load the video muted at `startSec`, buffer, report audio ready (docs/12 Q1).
* `gain` (0..1): 소리 크기 맞추기 — the player plays at the listener's volume × gain, so songs sound equally loud.
* 연령제한 곡 (YouTube refuses them in embedded players): `audio` is our own file's address and `videoId` is null.
*/
| { t: 'prepare'; code: string; n: number; videoId: string; startSec: number; endSec: number | null; gain: number }
| { t: 'prepare'; code: string; n: number; videoId: string | null; audio: string | null; startSec: number; endSec: number | null; gain: number }
/** Everyone unmutes and plays from `atSec` at server time `startAt`; stop at `endSec` by playback position. */
| { t: 'play'; code: string; n: number; startAt: number; atSec: number; endSec: number | null }
/** Stop now (someone is buffering / the host paused / the clip is over). */
@@ -162,7 +163,7 @@ export type ServerMessage =
* Back in a question whose music is running (refresh / reconnect): load `videoId` and play along the running timeline
* (position `atSec` at server time `startAt`); `clipStart` is where the clip began, for the progress bar.
*/
| { t: 'catchup'; code: string; n: number; videoId: string; clipStart: number; startAt: number; atSec: number; endSec: number | null; gain: number }
| { t: 'catchup'; code: string; n: number; videoId: string | null; audio: string | null; clipStart: number; startAt: number; atSec: number; endSec: number | null; gain: number }
| { t: 'typing'; code: string; ids: string[] }
| { t: 'saved'; id: number; name: string }
/** Recent lines for someone who just joined (or came back). */