// WHAT ONE gallery-dl STDERR LINE SAYS ABOUT X (plans/pacing-guard.md, // plans/cookie-refresh-alerts.md). Pure and client-safe: the X fetcher // (xGalleryDlFetcher.ts) asks it of every stderr line, and the answer decides // whether X's cookie alert goes up — which refuses EVERY X job until it is // cleared (jobs/networkGuard.ts). So the classes are drawn carefully: // // "challenge" Cloudflare's (or DDoS-Guard's) check or a captcha. One is // enough: the next request after it is the one that gets the // account locked. // "auth" the login itself is gone or refused: gallery-dl's // AuthRequired ("authenticated cookies needed to access this // timeline" — what X answers a logged-out timeline with), // AuthenticationError, "Could not authenticate you", a 401, a // 403 from X's API, an account X has temporarily locked. The // COOKIES are the fix (counted only on a warning or error line). // "rate-limit" a 429, "Too Many Requests", "rate limit" — PACING, never // cookies: gallery-dl waits it out itself, and fresh cookies // would not help. // "account" about ONE account or post, not the login: a protected // account, one that blocked ours, a suspended or missing one, // an NSFW or protected tweet, a media file's 403/404 on // twimg.com. Not a cookie signal — raising X's alert for it // would stop every other X job for nothing. // null anything else (progress, debug, an unrelated error). // // The order matters: a ChallengeError line carries "403" too // ("Cloudflare challenge (403 Forbidden) for '…'"), and a 429 line may carry // a URL with digits in it, so challenge is asked first, then the rate limit, // then the per-account phrases, and only then the login. export type XStderrClass = "challenge" | "auth" | "rate-limit" | "account"; const DEBUG_LINE = /\]\[debug\]/i; // "[twitter][warning] …", "[twitter][error] …", "[download][error] …". const LEVEL_LINE = /\]\[(warning|error)\]/i; const CHALLENGE = /cloudflare|\bchallenge\b|just a moment|captcha|ddos-guard/i; const RATE_LIMIT = /\b429\b|too many requests|rate[- ]?limit/i; // One account's or one post's refusal: the login is fine. const ACCOUNT_SCOPED = new RegExp( [ "tweets are protected", "blocked your account", "protected tweet", "nsfw tweet", "age-restricted", "account (?:is )?suspended", "user (?:is )?suspended", "user not found", "could not be found", "notfounderror", "does not exist", // A media file on X's CDN: a removed video or picture, not the login. "twimg\\.com", ].join("|"), "i", ); const AUTH = new RegExp( [ "authrequired", "authenticationerror", "authorizationerror", "authenticated cookies needed", "account credentials required", "invalid login credentials", "could not authenticate you", "login required", "requires authentication", "redirect to (?:the )?login", "no username", "bad credentials", "could not log in", "unauthorized", "account temporarily locked", "\\b401\\b", "\\b403\\b", ].join("|"), "i", ); // `anyLevel`: count a login phrase on a line with no [warning]/[error] tag // too — for the tail of a run that already EXITED non-zero, where the failure // is not in doubt and only its cause is asked. export function classifyXStderrLine(line: string, opts: { anyLevel?: boolean } = {}): XStderrClass | null { const l = line.trim(); if (!l || DEBUG_LINE.test(l)) return null; if (CHALLENGE.test(l)) return "challenge"; if (RATE_LIMIT.test(l)) return "rate-limit"; const levelled = opts.anyLevel || LEVEL_LINE.test(l); if (ACCOUNT_SCOPED.test(l)) return levelled ? "account" : null; if (AUTH.test(l)) return levelled ? "auth" : null; return null; } // The lines that stop an X run and raise its cookie alert: a challenge on any // line, a login failure on a warning or error line. export function xBlockLine(line: string): boolean { const c = classifyXStderrLine(line); return c === "challenge" || c === "auth"; } // The alert's kind for a blocking line. export function xBlockKind(line: string): "challenge" | "auth-failure" { return classifyXStderrLine(line) === "challenge" ? "challenge" : "auth-failure"; } // A failed run's stderr tail: does it say the LOGIN failed (the Needs-cookies // bucket), as opposed to a rate limit, one protected account, or a crash? export function looksLikeXAuthFailure(text: string): boolean { return text.split("\n").some((l) => { const c = classifyXStderrLine(l, { anyLevel: true }); return c === "auth" || c === "challenge"; }); }