// WHAT ONE gallery-dl RUN SAYS ABOUT INSTAGRAM (lane instagram). Pure and // client-safe. // // gallery-dl's `--dump-json` run ends 0 whatever happened: an exception is // the LAST ROW of what it prints, `[-1, {"error": , "message": }]`, // and with `--verbose` each request is a line on stderr, // `[urllib3.connectionpool][debug] https://www.instagram.com:443 "GET /path HTTP/1.1" 404 20445`. // Both are read here, because the error row alone lies about two things // (one logged-out read, 18 Nov 2026): // // - LOGGED OUT, Instagram answers the feed endpoint — and the account // search — with a 404 "Page Not Found". That is "needs a login", never // "the account is gone". // - gallery-dl turns EVERY failure of its account lookup into // `NotFoundError: Requested user could not be found`, a login redirect // included. The account is gone only when the PROFILE PAGE's own request // (`GET /`) answered 404. // // The classes, and what each does (instagramGalleryDlFetcher.ts): // "challenge" a redirect to Instagram's challenge or checkpoint page: the // alert goes up and every Instagram job is refused until a // person's Recheck. // "login" a redirect to its login (or home) page, an auth error, the // feed's or the search's 404: the same alert, as a login failure. // "rate-limit" a 429, or a 401/403 (Instagram's "please wait a few minutes" // is a 401): the run ends and Instagram cools down for an hour. // "private" the account is private and the login does not follow it. // "not-found" the profile page itself answered 404. // "error" anything else that failed (a 5xx, a crash): no alert, no cooldown. export type InstagramSignalClass = "challenge" | "login" | "rate-limit" | "private" | "not-found" | "error"; export type InstagramRequestLine = { method: string; host: string; path: string; status: number }; const REQUEST_LINE = /^\[urllib3\.connectionpool\]\[debug\]\s+https?:\/\/([^\s:/]+)(?::\d+)?\s+"([A-Z]+)\s+(\S+)\s+HTTP\/[\d.]+"\s+(\d{3})\b/; // One stderr line as a request gallery-dl made, or null. export function parseInstagramRequestLine(line: string): InstagramRequestLine | null { const m = REQUEST_LINE.exec(line.trim()); if (!m) return null; return { host: m[1]!.toLowerCase(), method: m[2]!, path: m[3]!, status: Number(m[4]) }; } export function isInstagramApiHost(host: string): boolean { return host === "instagram.com" || host.endsWith(".instagram.com"); } const CURSOR_LINE = /\]\[debug\]\s+Cursor:\s+(\S+)\s*$/; // The resume point gallery-dl logs after each page it finished, or null. export function parseInstagramCursorLine(line: string): string | null { return CURSOR_LINE.exec(line)?.[1] ?? null; } const PRIVATE_LINE = /posts are private/i; export function isInstagramPrivateLine(line: string): boolean { return PRIVATE_LINE.test(line); } // What a request for the account's own page (`GET /`) answered, or // null when the run made none. export function profilePageStatus(requests: readonly InstagramRequestLine[], handle: string): number | null { const want = `/${handle}`.toLowerCase(); for (const r of requests) { if (!isInstagramApiHost(r.host) || r.method !== "GET") continue; const p = r.path.split("?")[0]!.replace(/\/+$/, "").toLowerCase(); if (p === want) return r.status; } return null; } // WHICH WALL A PATH IS, by its SEGMENTS — never by a substring: gallery-dl // asks for the account's own page as `/`, and a handle may begin with // "challenge" or contain "checkpoint". A wall is // /challenge/… the first segment, with something under it (or a // trailing slash): never the bare `/` form // /…/checkpoint/… a segment that is exactly "checkpoint", in a path // of more than one segment (or with a trailing slash) // /accounts/login… the first two segments export function instagramWallPage(pathname: string): "challenge" | "login" | null { const path = pathname.split("?")[0]!.toLowerCase(); const segs = path.split("/").filter(Boolean); if (segs.length === 0) return null; // The bare one-segment form with no trailing slash is an account's page. const bareHandle = segs.length === 1 && !path.endsWith("/"); if (segs[0] === "accounts" && segs[1] === "login") return "login"; if (bareHandle) return null; if (segs[0] === "challenge" || segs.includes("checkpoint")) return "challenge"; return null; } // The wall an error row's text names: gallery-dl's own words for a redirect, // or the path of the URL it quotes — never a word inside a handle. function wallInText(text: string): "challenge" | "login" | null { if (/redirect to challenge page|ChallengeError|challenge_required|checkpoint_required/i.test(text)) return "challenge"; if (/redirect to (?:login|home) page/i.test(text)) return "login"; for (const m of text.matchAll(/https?:\/\/[^\s'")]+/g)) { try { const u = new URL(m[0]); if (!isInstagramApiHost(u.hostname.toLowerCase())) continue; const page = instagramWallPage(u.pathname); if (page) return page; } catch { /* not a URL */ } } return null; } // WHAT ONE REQUEST LINE SAYS, AS IT ARRIVES. gallery-dl swallows every // exception of its account lookup (the search, then the profile page) and // goes on to the next request, so a refusal there never reaches the error // row: the run would carry on into the feed with no hold and no alert. The // request lines are therefore read live, and the FIRST one that is a // refusal ends the run and decides its class, whatever the error row says: // "rate-limit" status 429, 401 or 403 from Instagram // "login" a request under /accounts/login // "challenge" a request under /challenge/, or a path naming a checkpoint // A 404 is NOT one: logged out, the search answers 404 and the run goes on to // the profile page, which is how the account's id is then found. export function instagramStopSignal(r: InstagramRequestLine): { class: "rate-limit" | "login" | "challenge"; status: number; line: string } | null { if (!isInstagramApiHost(r.host)) return null; const path = r.path.split("?")[0]!.toLowerCase(); const line = `${r.method} ${r.path.split("?")[0]} answered ${r.status}`.slice(0, 300); const page = instagramWallPage(path); if (page === "challenge") return { class: "challenge", status: r.status, line: `${r.method} ${path} (a challenge or checkpoint page)` }; if (page === "login") return { class: "login", status: r.status, line: `${r.method} ${path} (the login page)` }; if (r.status === 429 || r.status === 401 || r.status === 403) return { class: "rate-limit", status: r.status, line }; return null; } // The status a refusal named in an error row, or null. export function instagramErrorStatus(row: { message: string }): number | null { const m = STATUS_IN_MESSAGE.exec(row.message); return m ? Number(m[1]) : null; } const STATUS_IN_MESSAGE = /'(\d{3}) [^']*' for '([^']*)'/; export type InstagramSignal = { class: InstagramSignalClass; line: string }; // The error row of a run (`error` its class, `message` its text), read with // the requests the run made and the account it asked for. export function classifyInstagramError( row: { error: string; message: string }, ctx: { requests: readonly InstagramRequestLine[]; handle: string }, ): InstagramSignal { const line = `${row.error}: ${row.message}`.trim().slice(0, 300); const text = `${row.error} ${row.message}`; const wall = wallInText(text); if (wall === "challenge") return { class: "challenge", line }; if (wall === "login") return { class: "login", line }; if (/AuthRequired|AuthenticationError|AuthorizationError|authenticated cookies|login required|credentials/i.test(text)) { return { class: "login", line }; } if (PRIVATE_LINE.test(text)) return { class: "private", line }; if (/NotFoundError/.test(row.error) || /could not be found/i.test(row.message)) { return { class: profilePageStatus(ctx.requests, ctx.handle) === 404 ? "not-found" : "login", line }; } const http = STATUS_IN_MESSAGE.exec(row.message); if (http) { const status = Number(http[1]); if (status === 429 || status === 401 || status === 403) return { class: "rate-limit", line }; // The feed's, the search's or any API path's "Page Not Found": the login. if (status === 404) return { class: "login", line }; return { class: "error", line }; } if (/\b429\b|too many requests|wait a few minutes/i.test(text)) return { class: "rate-limit", line }; return { class: "error", line }; }