유튜브 뮤직 쿠키 끊김 줄이기: 429가 한 번이라도 나오면 쿠키를 쓰는 모든 작업(자동 갱신·로그인 확인·원곡 찾기 검색·조회수 짝 찾기·연령제한 음원 받기)이 3시간 쉼 — <쿠키 파일>.cooldown에 끝 시각을 적어 서버와 수집 CLI가 함께 봄(RateLimited). 원곡 찾기는 429에 1분 뒤 다시 묻지 않고 바로 멈춤(stopped), 갱신은 10분 뒤 재시도 대신 쉼이 끝날 때까지 기다림(새 쿠키 파일은 바로 확인). 검색 간격 2초 → 5초, 조회수 곡 간격 0.5초 → 2초. 다른 사이트 요청에서 복사해 __Secure-3P*만 있는 쿠키도 받음: SAPISID를 같은 값인 __Secure-3PAPISID로 읽을 때 채우고, 로그인 값은 __Secure-1PSID/3PSID 중 하나면 됨. 11/03 08:10 원곡 찾기 567곡 직후 갱신 429 → 401로 세션이 끝난 것. 회귀 테스트(채우기·갱신·검색·키퍼·원곡 찾기·연령제한·조회수), docs 08·09·12
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { DEFAULT_ROOM_CONFIG } from '@mq/shared';
|
||||
import { openDb } from '../db';
|
||||
@@ -9,6 +10,7 @@ import { loudnessGain, quizSong } from '../songs/quiz';
|
||||
import { tempDbPath } from '../test-utils';
|
||||
import { cookieForms, collectAgeAudio } from './age-audio';
|
||||
import { Catalog } from './catalog';
|
||||
import { cooldownUntil } from './cookie-jar';
|
||||
import { Blocked } from './charts';
|
||||
import { collectLoudness, isAgeGated } from './loudness';
|
||||
|
||||
@@ -120,4 +122,20 @@ describe('연령제한 곡', () => {
|
||||
expect(ids.every((id) => !quizSong(db, id)!.audio)).toBe(true);
|
||||
expect(existsSync(dirname(audioFile(dir, 'x')))).toBe(true);
|
||||
});
|
||||
|
||||
test('a block while downloading starts the 429 pause; during it the downloads wait (nothing is tried)', async () => {
|
||||
const { db, dir } = setup(1);
|
||||
db.query('UPDATE songs SET age_video = youtube_id').run();
|
||||
const cookiePath = join(mkdtempSync(join(tmpdir(), 'mq-age-')), 'ytm_cookie.txt');
|
||||
writeFileSync(cookiePath, 'SAPISID=a; __Secure-1PSID=b');
|
||||
let downloads = 0;
|
||||
const blocked = { delayMs: 0, cookiePath, fetchSignedIn: async () => ok(-9), download: async () => (downloads++, Promise.reject(new Blocked('HTTP Error 429'))) };
|
||||
await collectAgeAudio(db, dir, blocked);
|
||||
expect(downloads).toBe(1);
|
||||
expect(cooldownUntil(cookiePath)).toBeGreaterThan(Date.now() + 2.9 * 3600_000);
|
||||
db.query('DELETE FROM signals').run();
|
||||
await collectAgeAudio(db, dir, blocked);
|
||||
expect(downloads).toBe(1);
|
||||
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('429') });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,7 +15,7 @@ import { join } from 'node:path';
|
||||
import { audioFile } from '../songs/audio';
|
||||
import { Catalog, Job, sleep } from './catalog';
|
||||
import { Blocked } from './charts';
|
||||
import { cookieForms, jarChanges, mergeCookies, writeCookieFile } from './cookie-jar';
|
||||
import { cookieForms, cooldownUntil, jarChanges, mergeCookies, RateLimited, startCooldown, writeCookieFile } from './cookie-jar';
|
||||
import { parseHeatmap, pickHighlight } from './heatmap';
|
||||
import { fetchWatchPage, isAgeGated, parseLoudness, saveLoudness } from './loudness';
|
||||
|
||||
@@ -124,14 +124,16 @@ export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioO
|
||||
await sleep(opts.delayMs ?? 3000);
|
||||
}
|
||||
|
||||
// 2. download with the adult-verified account
|
||||
// 2. download with the adult-verified account (not during a 429 pause; a block here starts one — cookie-jar.ts)
|
||||
// (tests that inject both without a cookie path touch no cookie file)
|
||||
const path = opts.fetchSignedIn && opts.download ? opts.cookiePath : (opts.cookiePath ?? process.env.YT_COOKIE_PATH ?? '/home/claude/.config/music_quiz/ytm_cookie.txt');
|
||||
if (path && cooldownUntil(path) > Date.now()) throw new RateLimited(cooldownUntil(path));
|
||||
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}`);
|
||||
if (!path || !existsSync(path)) throw new CookieProblem(`쿠키 파일이 없어요: ${path}`);
|
||||
tmp = mkdtempSync(join(tmpdir(), 'mq-cookie-'));
|
||||
const cookieFile = join(tmp, 'cookies.txt');
|
||||
// the file is read for every song: the server keeps rotating its values (cookie-keeper.ts)
|
||||
@@ -152,6 +154,7 @@ export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioO
|
||||
stats.downloaded++;
|
||||
job.log(`${s.youtube_id} downloaded`);
|
||||
} catch (e) {
|
||||
if (e instanceof Blocked && path) startCooldown(path);
|
||||
if (e instanceof Blocked || e instanceof CookieProblem) throw e;
|
||||
stats.failed++;
|
||||
cat.signal(s.id, 'yt_audio_tried', Date.now());
|
||||
@@ -173,7 +176,7 @@ export async function collectAgeAudio(db: Database, dir: string, opts: AgeAudioO
|
||||
} catch (e) {
|
||||
job.log(`stopped: ${(e as Error).message}`);
|
||||
job.finish('failed');
|
||||
if (!(e instanceof Blocked || e instanceof CookieProblem)) throw e;
|
||||
if (!(e instanceof Blocked || e instanceof CookieProblem || e instanceof RateLimited)) throw e;
|
||||
} finally {
|
||||
if (tmp) rmSync(tmp, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* Song catalog collector (docs/07 처리 순서). Runs next to the app on the same DB (WAL):
|
||||
* bun apps/server/src/collector/cli.ts import-norabose [--limit N]
|
||||
* bun apps/server/src/collector/cli.ts import-sta
|
||||
* bun apps/server/src/collector/cli.ts match-youtube [--limit N] [--delay ms] [--deadline epochMs] [--require-cookie] (default pace: 2s apart; cron caps a day at 3000; most popular level first)
|
||||
* bun apps/server/src/collector/cli.ts match-youtube [--limit N] [--delay ms] [--deadline epochMs] [--require-cookie] (default pace: 5s apart, stops at the first 429; cron caps a day at 300; most popular level first)
|
||||
* bun apps/server/src/collector/cli.ts reconfirm-youtube [--dry-run] [--delay ms] (후보 곡을 이미 저장된 검색 결과로 현재 기준에 다시 비교해 확정)
|
||||
* bun apps/server/src/collector/cli.ts research-youtube [--dry-run] [--limit N] [--delay ms] [--deadline epochMs] (가수가 다른 문자로 적혀 결과에 그 가수가 없던 후보 곡을 영어/한글 이름으로 다시 검색; 쿠키가 풀렸으면 일반 유튜브 검색)
|
||||
* bun apps/server/src/collector/cli.ts charts [--from YYYY] [--to YYYY] [--sources melon-ye,genie-ye,tj-ye,melon-rt,bugs-rt] [--refetch]
|
||||
|
||||
@@ -9,7 +9,7 @@ import { matchYoutube, reconfirmCandidates, researchYoutube } from './match-yout
|
||||
import { isConfident, notConfidentReasons, scoreResult } from './score';
|
||||
import { soundsLike } from './sound';
|
||||
import { learnArtistAliases } from './artist-aliases';
|
||||
import { parseDuration, parseSearch, type YtmSong } from './ytmusic';
|
||||
import { parseDuration, parseSearch, RateLimited, type YtmSong } from './ytmusic';
|
||||
import { catalogStats, listSongs } from '../songs/store';
|
||||
import { tempDbPath } from '../test-utils';
|
||||
|
||||
@@ -315,3 +315,20 @@ test('YouTube matching asks the most popular level first and stops at the deadli
|
||||
expect(asked.length).toBe(2);
|
||||
expect(db.query<{ status: string }, []>("SELECT status FROM collect_jobs WHERE kind = 'match-youtube' ORDER BY id DESC LIMIT 1").get()!.status).toBe('done');
|
||||
});
|
||||
|
||||
test('YouTube matching stops at the first 429 (no retry a minute later) and leaves the job stopped', async () => {
|
||||
const db = freshDb();
|
||||
const c = new Catalog(db);
|
||||
c.upsert({ title: '곡1', artists: [{ name: '가' }], source: 't', ref: '1' });
|
||||
c.upsert({ title: '곡2', artists: [{ name: '나' }], source: 't', ref: '2' });
|
||||
let n = 0;
|
||||
const searchSongs = async () => {
|
||||
n++;
|
||||
throw new RateLimited(Date.now() + 3 * 3600_000);
|
||||
};
|
||||
const started = Date.now();
|
||||
await expect(matchYoutube(db, { searchSongs }, { delayMs: 0 })).rejects.toBeInstanceOf(RateLimited);
|
||||
expect(n).toBe(1);
|
||||
expect(Date.now() - started).toBeLessThan(5000);
|
||||
expect(db.query<{ status: string; log: string }, []>("SELECT status, log FROM collect_jobs WHERE kind = 'match-youtube' ORDER BY id DESC LIMIT 1").get()).toMatchObject({ status: 'stopped', log: expect.stringContaining('429') });
|
||||
});
|
||||
|
||||
@@ -3,21 +3,73 @@
|
||||
* Google keeps changing some of its values (__Secure-*PSIDTS, *SIDCC); the server writes them back here (cookie-keeper.ts),
|
||||
* so every reader takes the file fresh instead of keeping a copy.
|
||||
*/
|
||||
import { writeFileSync } from 'node:fs';
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
|
||||
/** Returns both forms of the file: the header for fetch, the cookies.txt for yt-dlp. */
|
||||
export function cookieForms(raw: string): { header: string; netscape: string } {
|
||||
const text = raw.trim();
|
||||
if (text.includes('\t')) {
|
||||
const rows = netscapeRows(text).filter((p) => /youtube\.com$/.test(p[0]!));
|
||||
return { header: rows.map((p) => `${p[5]}=${p[6]!.trim()}`).join('; '), netscape: `${text}\n` };
|
||||
return { header: fillSignIn(rows.map((p) => [p[5]!, p[6]!.trim()] as [string, string])).map(([k, v]) => `${k}=${v}`).join('; '), netscape: `${text}\n` };
|
||||
}
|
||||
const exp = Math.floor(Date.now() / 1000) + 365 * 86400;
|
||||
const pairs = headerPairs(text);
|
||||
const pairs = fillSignIn(headerPairs(text));
|
||||
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') };
|
||||
}
|
||||
|
||||
/**
|
||||
* A cookie copied from a request another site sent to YouTube (an embedded player) has only the cross-site copies
|
||||
* (__Secure-3P*) of the login values. SAPISID — the one the request signature (SAPISIDHASH) is made from — has the same
|
||||
* value as __Secure-3PAPISID / __Secure-1PAPISID, so a missing one is filled from them. The file itself is left as given.
|
||||
* (__Secure-1PSID and SID differ from __Secure-3PSID and cannot be filled.)
|
||||
*/
|
||||
function fillSignIn(pairs: [string, string][]): [string, string][] {
|
||||
const has = (k: string) => pairs.some(([n]) => n === k);
|
||||
const apisid = pairs.find(([k]) => k === '__Secure-3PAPISID' || k === '__Secure-1PAPISID')?.[1];
|
||||
return !has('SAPISID') && apisid ? [...pairs, ['SAPISID', apisid]] : pairs;
|
||||
}
|
||||
|
||||
/** Signed-in values are there: SAPISID (or a copy to fill it from) and a session id of either kind. */
|
||||
export function hasSignIn(header: string): boolean {
|
||||
return /(?:^|; )SAPISID=/.test(header) && /(?:^|; )__Secure-[13]PSID=/.test(header);
|
||||
}
|
||||
|
||||
/**
|
||||
* YouTube said 429 (too many requests) to this cookie's account / our address: everything that uses the cookie pauses
|
||||
* until the time in `<cookie file>.cooldown` (the server's keeper and the collector CLI are separate processes).
|
||||
*/
|
||||
export const COOLDOWN_MS = 3 * 3600_000;
|
||||
const cooldownFile = (path: string) => `${path}.cooldown`;
|
||||
|
||||
export function cooldownUntil(path: string): number {
|
||||
try {
|
||||
return Number(readFileSync(cooldownFile(path), 'utf8').trim()) || 0;
|
||||
} catch {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function startCooldown(path: string, now = Date.now()): number {
|
||||
const until = Math.max(cooldownUntil(path), now + COOLDOWN_MS);
|
||||
try {
|
||||
writeFileSync(cooldownFile(path), String(until));
|
||||
} catch {
|
||||
// read-only place: the caller still stops on its own
|
||||
}
|
||||
return until;
|
||||
}
|
||||
|
||||
/** "14:05" (Korea time) for messages. */
|
||||
export const clock = (t: number) => new Date(t).toLocaleTimeString('ko-KR', { timeZone: 'Asia/Seoul', hour: '2-digit', minute: '2-digit', hour12: false });
|
||||
|
||||
/** Thrown while the 429 pause lasts (and by the request that got the 429). */
|
||||
export class RateLimited extends Error {
|
||||
constructor(public until: number) {
|
||||
super(`유튜브가 요청이 너무 많다고 해서(429) ${clock(until)}까지 쉬어요`);
|
||||
}
|
||||
}
|
||||
|
||||
function headerPairs(text: string): [string, string][] {
|
||||
return text
|
||||
.split(/;\s*/)
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, test } from 'bun:test';
|
||||
import { mkdtempSync, readFileSync, statSync, utimesSync, writeFileSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { cookieForms, jarChanges, mergeCookies, parseSetCookies } from './cookie-jar';
|
||||
import { cookieForms, cooldownUntil, hasSignIn, jarChanges, mergeCookies, parseSetCookies, RateLimited, startCooldown } from './cookie-jar';
|
||||
import { downloadWithCookie } from './age-audio';
|
||||
import { CookieKeeper } from './cookie-keeper';
|
||||
import { CookieError, YtMusic } from './ytmusic';
|
||||
@@ -143,6 +143,98 @@ describe('CookieKeeper', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('쿠키 일부만 복사한 경우 (다른 사이트가 보낸 요청의 쿠키: __Secure-3P* 만 있음)', () => {
|
||||
const CROSS = '__Secure-3PSID=p3; __Secure-3PAPISID=ap; __Secure-3PSIDTS=ts3; __Secure-3PSIDCC=cc3; YSC=y\n';
|
||||
|
||||
test('SAPISID is filled from __Secure-3PAPISID (same value) for the header and the cookies.txt; the file is left as given', () => {
|
||||
const { header, netscape } = cookieForms(CROSS);
|
||||
expect(header).toBe('__Secure-3PSID=p3; __Secure-3PAPISID=ap; __Secure-3PSIDTS=ts3; __Secure-3PSIDCC=cc3; YSC=y; SAPISID=ap');
|
||||
expect(netscape).toContain('\tSAPISID\tap');
|
||||
expect(hasSignIn(header)).toBe(true);
|
||||
expect(cookieForms(HEADER).header).toBe(HEADER.trim()); // a full cookie stays as it is
|
||||
expect(hasSignIn(cookieForms('YSC=a; __Secure-3PAPISID=ap').header)).toBe(false); // no session id
|
||||
const rotated = mergeCookies(CROSS, parseSetCookies(ROTATED, NOW));
|
||||
expect(rotated).not.toContain('SAPISID=ap;');
|
||||
expect(rotated).toContain('__Secure-3PSIDTS=new3');
|
||||
});
|
||||
|
||||
test('the keeper rotates such a cookie and YtMusic signs its requests with the filled SAPISID', async () => {
|
||||
const path = tempFile(CROSS);
|
||||
const sent: string[] = [];
|
||||
const k = new CookieKeeper(path, {
|
||||
fetch: (async (_u: string, init: RequestInit) => (sent.push((init.headers as Record<string, string>).cookie!), rotateRes(200, ROTATED))) as unknown as typeof fetch,
|
||||
checkLogin: async () => {},
|
||||
});
|
||||
await k.run();
|
||||
expect(k.status.state).toBe('ok');
|
||||
expect(sent[0]).toContain('SAPISID=ap');
|
||||
let auth = '';
|
||||
const ytm = new YtMusic('', (async (_u: string, init: RequestInit) => {
|
||||
auth = (init.headers as Record<string, string>).authorization!;
|
||||
return Response.json({ responseContext: { serviceTrackingParams: [{ params: [{ key: 'logged_in', value: '1' }] }] } });
|
||||
}) as unknown as typeof fetch, path);
|
||||
await ytm.searchSongs('a');
|
||||
expect(auth).toStartWith('SAPISIDHASH ');
|
||||
});
|
||||
});
|
||||
|
||||
describe('429: 쿠키를 쓰는 모든 작업이 3시간 쉼', () => {
|
||||
test('a search that gets 429 pauses every later request with the cookie (no request goes out)', async () => {
|
||||
const path = tempFile(HEADER);
|
||||
let n = 0;
|
||||
const ytm = new YtMusic('', (async () => (n++, new Response('', { status: 429 }))) as unknown as typeof fetch, path);
|
||||
await expect(ytm.searchSongs('a')).rejects.toBeInstanceOf(RateLimited);
|
||||
expect(cooldownUntil(path)).toBeGreaterThan(Date.now() + 2.9 * 3600_000);
|
||||
await expect(ytm.searchSongs('b')).rejects.toThrow('429');
|
||||
const other = new YtMusic('', (async () => (n++, Response.json({}))) as unknown as typeof fetch, path);
|
||||
await expect(other.searchSongs('c')).rejects.toBeInstanceOf(RateLimited);
|
||||
expect(n).toBe(1);
|
||||
});
|
||||
|
||||
test('the keeper: a 429 on rotation (or on its check) waits out the pause instead of asking again in 10 minutes', async () => {
|
||||
const path = tempFile(HEADER);
|
||||
let status = 429;
|
||||
let n = 0;
|
||||
const k = new CookieKeeper(path, { fetch: (async () => (n++, rotateRes(status, ROTATED))) as unknown as typeof fetch, checkLogin: async () => {}, everyMs: 10 * 60_000 });
|
||||
await k.tick();
|
||||
expect(k.status.state).toBe('error');
|
||||
expect(k.status.message).toContain('429');
|
||||
const until = cooldownUntil(path);
|
||||
expect(until).toBeGreaterThan(Date.now() + 2.9 * 3600_000);
|
||||
await k.tick(Date.now() + 11 * 60_000);
|
||||
expect(n).toBe(1);
|
||||
status = 200;
|
||||
await k.tick(until + 1);
|
||||
expect(n).toBe(2);
|
||||
expect(k.status.state).toBe('ok');
|
||||
|
||||
const k2 = new CookieKeeper(tempFile(HEADER), {
|
||||
fetch: (async () => rotateRes(200, ROTATED)) as unknown as typeof fetch,
|
||||
checkLogin: async () => {
|
||||
throw new RateLimited(Date.now());
|
||||
},
|
||||
});
|
||||
await k2.run();
|
||||
expect(k2.status.state).toBe('error');
|
||||
});
|
||||
|
||||
test('a pause started by a collector also holds the keeper; a new cookie file is still checked', async () => {
|
||||
const path = tempFile(HEADER);
|
||||
let n = 0;
|
||||
const k = new CookieKeeper(path, { fetch: (async () => (n++, rotateRes(200, ROTATED))) as unknown as typeof fetch, checkLogin: async () => {}, everyMs: 10 * 60_000 });
|
||||
await k.tick();
|
||||
expect(n).toBe(1);
|
||||
startCooldown(path);
|
||||
await k.tick(Date.now() + 11 * 60_000);
|
||||
expect(n).toBe(1);
|
||||
writeFileSync(path, HEADER.replace('p1', 'p2'));
|
||||
const t = new Date(Date.now() + 5000);
|
||||
utimesSync(path, t, t);
|
||||
await k.tick(Date.now() + 12 * 60_000);
|
||||
expect(n).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('YtMusic reads the cookie file again when it changes', () => {
|
||||
test('a long 원곡 찾기 run uses the rotated values', async () => {
|
||||
const path = tempFile(HEADER);
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
import { existsSync, readFileSync, statSync } from 'node:fs';
|
||||
import type { LogFn } from '../server';
|
||||
import { cookieForms, mergeCookies, parseSetCookies, writeCookieFile } from './cookie-jar';
|
||||
import { cookieForms, cooldownUntil, hasSignIn, mergeCookies, parseSetCookies, RateLimited, startCooldown, writeCookieFile } from './cookie-jar';
|
||||
import { CookieError, YtMusic } from './ytmusic';
|
||||
|
||||
export interface CookieStatus {
|
||||
@@ -70,10 +70,13 @@ export class CookieKeeper {
|
||||
}
|
||||
}
|
||||
|
||||
/** Runs when the file changed, or every `everyMs` while the session is alive (an ended session waits for a new file). */
|
||||
/**
|
||||
* Runs when the file changed, or every `everyMs` while the session is alive (an ended session waits for a new file).
|
||||
* After a 429 (here or in a collector) it waits out the pause instead of asking again in 10 minutes.
|
||||
*/
|
||||
async tick(now = Date.now()): Promise<void> {
|
||||
const changed = this.mtime() !== this.seenMtime;
|
||||
const due = now - this.lastRun >= this.everyMs && this.status.state !== 'expired' && this.status.state !== 'missing';
|
||||
const due = now - this.lastRun >= this.everyMs && now >= cooldownUntil(this.path) && this.status.state !== 'expired' && this.status.state !== 'missing';
|
||||
if (changed || due) await this.run();
|
||||
}
|
||||
|
||||
@@ -87,6 +90,11 @@ export class CookieKeeper {
|
||||
this.status = { ...this.status, state, message, checkedAt: Date.now() };
|
||||
}
|
||||
|
||||
/** 429: rotation and every search with the cookie wait (tick skips until the pause is over). */
|
||||
private pause(): void {
|
||||
this.set('error', `${new RateLimited(startCooldown(this.path)).message} — 그동안 갱신·검색을 멈춰요`);
|
||||
}
|
||||
|
||||
private async rotate(): Promise<void> {
|
||||
this.lastRun = Date.now();
|
||||
this.seenMtime = this.mtime();
|
||||
@@ -94,7 +102,7 @@ export class CookieKeeper {
|
||||
try {
|
||||
const raw = readFileSync(this.path, 'utf8');
|
||||
const { header } = cookieForms(raw);
|
||||
if (!/(?:^|; )SAPISID=/.test(header) || !/(?:^|; )__Secure-1PSID=/.test(header)) return this.set('expired', '쿠키에 로그인 값(SAPISID·__Secure-1PSID)이 없어요');
|
||||
if (!hasSignIn(header)) return this.set('expired', '쿠키에 로그인 값(SAPISID·__Secure-1PSID/3PSID)이 없어요');
|
||||
const res = await this.fetch(ROTATE_URL, {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json', origin: 'https://www.youtube.com', cookie: header, 'user-agent': UA },
|
||||
@@ -102,6 +110,7 @@ export class CookieKeeper {
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
if (res.status === 401 || res.status === 403) return this.set('expired', `구글이 이 로그인 세션을 끝냈어요(갱신 ${res.status})`);
|
||||
if (res.status === 429) return this.pause();
|
||||
if (!res.ok) return this.set('error', `쿠키 갱신 요청 실패(${res.status}) — 다음에 다시 시도해요`);
|
||||
const updates = parseSetCookies(res.headers.getSetCookie());
|
||||
let current = header;
|
||||
@@ -119,6 +128,7 @@ export class CookieKeeper {
|
||||
this.set('ok', null);
|
||||
} catch (e) {
|
||||
if (e instanceof CookieError) return this.set('expired', e.message);
|
||||
if (e instanceof RateLimited) return this.pause();
|
||||
this.set('error', `확인하지 못했어요: ${(e as Error).message} — 다음에 다시 시도해요`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
/**
|
||||
* Finds the official audio (Topic / Art Track) for each song via YouTube Music song search (docs/08 §1),
|
||||
* checks it can be embedded (IFrame playback, docs/12 Q1) and records candidates for the admin.
|
||||
* Resumable: songs tried in the last `retryDays` are skipped. Stops on cookie problems.
|
||||
* Resumable: songs tried in the last `retryDays` are skipped. Stops on cookie problems and on the first 429.
|
||||
* Paced 5s apart (2s ran into a 429 and then an ended session on 11/03); the cron caps a day at 300 songs.
|
||||
*/
|
||||
import type { Database } from 'bun:sqlite';
|
||||
import { Job, sleep } from './catalog';
|
||||
@@ -9,7 +10,10 @@ import { checkEmbed } from './oembed';
|
||||
import { addsJunk, addsVideo, confirmedDespiteLength, isConfident, namesOtherCut, scoreResult, type Scored } from './score';
|
||||
import { learnArtistAliases } from './artist-aliases';
|
||||
import { otherScriptNames } from './other-names';
|
||||
import { CookieError, type YtMusic, type YtmSong } from './ytmusic';
|
||||
import { CookieError, RateLimited, type YtMusic, type YtmSong } from './ytmusic';
|
||||
|
||||
/** Between YouTube Music searches. */
|
||||
export const SEARCH_DELAY_MS = 5000;
|
||||
|
||||
interface Row {
|
||||
id: number;
|
||||
@@ -55,7 +59,8 @@ async function searchRetry(ytm: Pick<YtMusic, 'searchSongs'>, query: string, job
|
||||
try {
|
||||
return await ytm.searchSongs(query);
|
||||
} catch (e) {
|
||||
if (e instanceof CookieError) throw e;
|
||||
// 429: no retry — another try a minute later is what gets a session ended
|
||||
if (e instanceof CookieError || e instanceof RateLimited) throw e;
|
||||
job?.log(`search failed (${(e as Error).message}), waiting 60s`);
|
||||
await sleep(60_000);
|
||||
return ytm.searchSongs(query);
|
||||
@@ -135,7 +140,7 @@ export async function matchYoutube(db: Database, ytm: Pick<YtMusic, 'searchSongs
|
||||
// Melon wrote the artist in the other script ("디제이디오씨" for DJ DOC) and no result is by them: search again by that name
|
||||
if (!scored.some(isConfident) && !scored.some((c) => c.artistOk || c.artistSound)) {
|
||||
for (const name of otherNames(s.artist)) {
|
||||
await sleep(opts.delayMs ?? 2000);
|
||||
await sleep(opts.delayMs ?? SEARCH_DELAY_MS);
|
||||
// only that artist's songs count from this search: the same title by someone else is not the song
|
||||
const byArtist = (await searchRetry(ytm, `${name} ${s.title}`, job)).slice(0, 8).map((r, rank) => scoreResult(song, r, rank, aliases)).filter((c) => c.artistOk || c.artistSound);
|
||||
scored = mergeScored(scored, byArtist);
|
||||
@@ -168,13 +173,13 @@ export async function matchYoutube(db: Database, ytm: Pick<YtMusic, 'searchSongs
|
||||
tried = i + 1;
|
||||
job.progress(tried);
|
||||
if ((i + 1) % 50 === 0) job.log(`youtube ${i + 1}/${rows.length} matched ${stats.matched} candidate ${stats.candidate} none ${stats.none}`);
|
||||
await sleep(opts.delayMs ?? 2000);
|
||||
await sleep(opts.delayMs ?? SEARCH_DELAY_MS);
|
||||
}
|
||||
job.log(`youtube done ${tried}/${rows.length}: matched ${stats.matched} candidate ${stats.candidate} none ${stats.none}`);
|
||||
job.finish('done');
|
||||
} catch (e) {
|
||||
job.log(`stopped: ${(e as Error).message}`);
|
||||
job.finish(e instanceof CookieError ? 'stopped' : 'failed');
|
||||
job.finish(e instanceof CookieError || e instanceof RateLimited ? 'stopped' : 'failed');
|
||||
throw e;
|
||||
}
|
||||
return stats;
|
||||
@@ -286,7 +291,7 @@ export async function researchYoutube(
|
||||
const found = (await searchRetry(ytm, `${name} ${s.title}`, job)).slice(0, 8).map((r, rank) => scoreResult(song, r, rank, aliases)).filter((c) => c.artistOk || c.artistSound);
|
||||
stats.searches++;
|
||||
scored = mergeScored(scored, found);
|
||||
await sleep(opts.delayMs ?? 2000);
|
||||
await sleep(opts.delayMs ?? SEARCH_DELAY_MS);
|
||||
if (scored.some(isConfident)) break;
|
||||
}
|
||||
scored.sort((a, b) => b.score - a.score);
|
||||
@@ -303,7 +308,7 @@ export async function researchYoutube(
|
||||
job.finish('done');
|
||||
} catch (e) {
|
||||
job.log(`stopped: ${(e as Error).message}`);
|
||||
job.finish(e instanceof CookieError ? 'stopped' : 'failed');
|
||||
job.finish(e instanceof CookieError || e instanceof RateLimited ? 'stopped' : 'failed');
|
||||
throw e;
|
||||
}
|
||||
return stats;
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
* Same approach as music_bot_v2: WEB_REMIX client + SAPISIDHASH.
|
||||
*/
|
||||
import { statSync } from 'node:fs';
|
||||
import { cookieForms } from './cookie-jar';
|
||||
import { COOLDOWN_MS, cookieForms, cooldownUntil, RateLimited, startCooldown } from './cookie-jar';
|
||||
|
||||
export { RateLimited } from './cookie-jar';
|
||||
|
||||
export interface YtmSong {
|
||||
videoId: string;
|
||||
@@ -79,6 +81,7 @@ export class YtMusic {
|
||||
}
|
||||
|
||||
private async post(endpoint: 'search' | 'next', body: Record<string, unknown>): Promise<any> {
|
||||
if (this.path && cooldownUntil(this.path) > Date.now()) throw new RateLimited(cooldownUntil(this.path));
|
||||
await this.reload();
|
||||
const sapisid = /(?:^|; )SAPISID=([^;]+)/.exec(this.cookie)![1]!;
|
||||
const ts = Math.floor(Date.now() / 1000);
|
||||
@@ -98,7 +101,8 @@ export class YtMusic {
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
if (res.status === 401 || res.status === 403) throw new CookieError(`유튜브 뮤직 인증 실패(${res.status}) — 쿠키 갱신 필요`);
|
||||
if (res.status === 429) throw new Error('rate limited (429)');
|
||||
// one 429 and nothing with this cookie asks again for a while (the 11/03 burst: 429, then the session ended)
|
||||
if (res.status === 429) throw new RateLimited(this.path ? startCooldown(this.path) : Date.now() + COOLDOWN_MS);
|
||||
if (!res.ok) throw new Error(`ytmusic ${endpoint} ${res.status}`);
|
||||
return res.json();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/** 이지선다 조회수 자동: 토픽 ↔ MV pair from a YouTube Music watch response, view counts, and the upload → fetch → editor flow. */
|
||||
import { afterEach, expect, test } from 'bun:test';
|
||||
import { duelViewsNote, type DuelViews, type QuizItem } from '@mq/shared';
|
||||
import { CookieError, parseSongPair, type SongPair } from './collector/ytmusic';
|
||||
import { CookieError, parseSongPair, RateLimited, type SongPair } from './collector/ytmusic';
|
||||
import { fetchDuelViews, youtubeViewCount, type ViewsSource } from './quizzes/duel-views';
|
||||
import { TEST_SUPERADMINS, api, boot, discordLogin } from './test-utils';
|
||||
|
||||
@@ -213,3 +213,20 @@ test('signed out (or a song that cannot be read): songs are marked failed with t
|
||||
expect((await call('POST', `/quizzes/${normal}/views`, {})).res.status).toBe(400);
|
||||
expect((await call('GET', `/quizzes/${normal}`)).body.quiz.items[0]).toMatchObject({ views: null, viewsAuto: null });
|
||||
});
|
||||
|
||||
test('429 from YouTube Music: every waiting song stops with the pause in its note (no retry)', async () => {
|
||||
let n = 0;
|
||||
const src = fakeSource({}, {});
|
||||
const { call, settled } = await setup(async () => ({
|
||||
...src,
|
||||
songPair: async () => {
|
||||
n++;
|
||||
throw new RateLimited(Date.parse('2026-11-03T15:00:00Z'));
|
||||
},
|
||||
}));
|
||||
const id = (await call('POST', '/quizzes/file', { name: '429', kind: 'duel', items: [row('aaaaaaaaaa1', '하나'), row('aaaaaaaaaa2', '둘')] })).body.id as number;
|
||||
const items = await settled(id);
|
||||
expect(n).toBe(1);
|
||||
expect(items.map((i) => i.viewsAuto?.state)).toEqual(['failed', 'failed']);
|
||||
expect(duelViewsNote(items[1]!)).toContain('00:00까지 쉬어요');
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
*/
|
||||
import type { Database } from 'bun:sqlite';
|
||||
import { PENDING_VIEWS, type DuelViews } from '@mq/shared';
|
||||
import { CookieError, YtMusic, type SongPair } from '../collector/ytmusic';
|
||||
import { CookieError, RateLimited, YtMusic, type SongPair } from '../collector/ytmusic';
|
||||
import type { LogFn } from '../server';
|
||||
|
||||
export interface ViewsSource {
|
||||
@@ -123,7 +123,7 @@ export class DuelViewsJob {
|
||||
try {
|
||||
src ??= await this.source();
|
||||
result = await fetchDuelViews(row.video_id, src).catch(async (e) => {
|
||||
if (e instanceof CookieError) throw e;
|
||||
if (e instanceof CookieError || e instanceof RateLimited) throw e;
|
||||
await sleep(this.opts.retryMs ?? 3000);
|
||||
return fetchDuelViews(row!.video_id, src!);
|
||||
});
|
||||
@@ -135,13 +135,20 @@ export class DuelViewsJob {
|
||||
this.opts.log?.('warn', 'duel views: cookie', { message: e.message });
|
||||
break;
|
||||
}
|
||||
if (e instanceof RateLimited) {
|
||||
// the same for every waiting song: they wait for 다시 불러오기 once the pause is over
|
||||
const failed = JSON.stringify(failure(`${e.message} — 그 뒤에 다시 불러와 주세요`));
|
||||
this.db.query(`UPDATE custom_quiz_items SET views_auto = ? WHERE ${PENDING}`).run(failed);
|
||||
this.opts.log?.('warn', 'duel views: rate limited', { message: e.message });
|
||||
break;
|
||||
}
|
||||
result = failure(`조회수를 불러오지 못했어요: ${(e as Error).message}`.slice(0, 300));
|
||||
}
|
||||
const views = result.state === 'done' ? totalViews(result) : null;
|
||||
if (views !== null) write.run(views, JSON.stringify(result), row.id, row.video_id);
|
||||
else this.db.query(`UPDATE custom_quiz_items SET views_auto = ? WHERE id = ? AND video_id = ? AND ${PENDING}`).run(JSON.stringify(result), row.id, row.video_id);
|
||||
changed = true;
|
||||
await sleep(this.opts.delayMs ?? 500);
|
||||
await sleep(this.opts.delayMs ?? 2000);
|
||||
}
|
||||
if (changed) this.opts.onChange?.();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user