연령제한 곡 재생: 유튜브가 퍼가기 플레이어에서 막는(오류 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;
}