// What one gallery-dl run says about Instagram (instagramSignals.ts). Every // line here is invented, in the shape gallery-dl 1.32.9 prints. import { test } from "node:test"; import assert from "node:assert/strict"; import { instagramWallPage, instagramErrorStatus, instagramStopSignal, classifyInstagramError, isInstagramPrivateLine, parseInstagramCursorLine, parseInstagramRequestLine, profilePageStatus, } from "./instagramSignals"; const req = (path: string, status: number, method = "GET") => `[urllib3.connectionpool][debug] https://www.instagram.com:443 "${method} ${path} HTTP/1.1" ${status} ${status === 200 ? "None" : "20445"}`; const H = "example.account"; const UID = "4000000001"; const lines = (...l: string[]) => l.map((x) => parseInstagramRequestLine(x)!).filter(Boolean); test("a request line is read; other lines are not requests", () => { assert.deepEqual(parseInstagramRequestLine(req(`/api/v1/feed/user/${UID}/?count=30`, 404)), { host: "www.instagram.com", method: "GET", path: `/api/v1/feed/user/${UID}/?count=30`, status: 404, }); assert.equal(parseInstagramRequestLine("[urllib3.connectionpool][debug] Starting new HTTPS connection (1): www.instagram.com:443"), null); assert.equal(parseInstagramRequestLine("[instagram][debug] Sleeping 8.83 seconds (request)"), null); assert.equal(parseInstagramRequestLine(req("/api/v1/clips/user/", 200, "POST"))?.method, "POST"); }); test("the cursor line and the private warning", () => { assert.equal(parseInstagramCursorLine("[instagram][debug] Cursor: 3500000000000000001_4000000001"), "3500000000000000001_4000000001"); assert.equal(parseInstagramCursorLine("[instagram][debug] Sleeping 8.83 seconds (request)"), null); assert.ok(isInstagramPrivateLine("[instagram][warning] example.account's posts are private")); }); test("LOGGED OUT: the search's 404 then the feed's 404 is the login, never a gone account", () => { const requests = lines(req(`/web/search/topsearch/?query=${H}`, 404), req(`/${H}`, 200), req(`/api/v1/feed/user/${UID}/?count=30`, 404)); const s = classifyInstagramError( { error: "HttpError", message: `'404 Not Found' for 'https://www.instagram.com/api/v1/feed/user/${UID}/?count=30'` }, { requests, handle: H }, ); assert.equal(s.class, "login"); assert.match(s.line, /^HttpError: '404 Not Found'/); }); test("the account is gone only when its own page answered 404", () => { const row = { error: "NotFoundError", message: "Requested user could not be found" }; const gone = lines(req(`/web/search/topsearch/?query=${H}`, 404), req(`/${H}`, 404)); assert.equal(classifyInstagramError(row, { requests: gone, handle: H }).class, "not-found"); // The handle's case and a trailing slash do not matter. assert.equal(profilePageStatus(lines(req("/Example.Account/", 404)), H), 404); // The lookup failed some other way (a login redirect swallowed by gallery-dl): not proven gone. const walled = lines(req(`/web/search/topsearch/?query=${H}`, 404), req(`/${H}`, 302), req("/accounts/login/?next=%2F", 200)); assert.equal(classifyInstagramError(row, { requests: walled, handle: H }).class, "login"); assert.equal(classifyInstagramError(row, { requests: [], handle: H }).class, "login"); // Another account's page is not this one's. assert.equal(profilePageStatus(lines(req("/someone.else", 404)), H), null); }); test("redirects: the challenge and checkpoint pages, the login and home pages", () => { const ctx = { requests: [], handle: H }; const abort = (page: string, url: string) => ({ error: "AbortExtraction", message: `HTTP redirect to ${page} page (${url})` }); assert.equal(classifyInstagramError(abort("challenge", "https://www.instagram.com/challenge/"), ctx).class, "challenge"); assert.equal(classifyInstagramError(abort("login", "https://www.instagram.com/accounts/login/"), ctx).class, "login"); assert.equal(classifyInstagramError(abort("home", "https://www.instagram.com/"), ctx).class, "login"); assert.equal(classifyInstagramError({ error: "AuthRequired", message: "authenticated cookies needed to access this resource" }, ctx).class, "login"); assert.equal(classifyInstagramError({ error: "ChallengeError", message: "Cloudflare challenge (403 Forbidden) for 'https://www.instagram.com/x'" }, ctx).class, "challenge"); }); test("a 429, a 401 and a 403 are the rate limit; a 5xx and a crash are plain errors", () => { const ctx = { requests: [], handle: H }; const http = (s: string) => ({ error: "HttpError", message: `'${s}' for 'https://www.instagram.com/api/v1/feed/user/${UID}/?count=30'` }); assert.equal(classifyInstagramError(http("429 Too Many Requests"), ctx).class, "rate-limit"); assert.equal(classifyInstagramError(http("401 Unauthorized"), ctx).class, "rate-limit"); assert.equal(classifyInstagramError(http("403 Forbidden"), ctx).class, "rate-limit"); assert.equal(classifyInstagramError(http("500 Internal Server Error"), ctx).class, "error"); assert.equal(classifyInstagramError({ error: "KeyError", message: "'items'" }, ctx).class, "error"); assert.equal(classifyInstagramError({ error: "AbortExtraction", message: "example.account's posts are private" }, ctx).class, "private"); }); test("a request line that ends the run: a 429/401/403, the login page, the challenge page — and never a 404", () => { const sig = (l: string) => instagramStopSignal(parseInstagramRequestLine(l)!); assert.deepEqual(sig(req(`/web/search/topsearch/?query=${H}`, 429)), { class: "rate-limit", status: 429, line: "GET /web/search/topsearch/ answered 429" }); assert.equal(sig(req(`/${H}`, 401))?.class, "rate-limit"); assert.equal(sig(req(`/api/v1/feed/user/${UID}/?count=30`, 403))?.status, 403); assert.equal(sig(req("/accounts/login/?next=%2Fexample.account%2F", 200))?.class, "login"); assert.equal(sig(req("/challenge/?next=/x", 200))?.class, "challenge"); assert.equal(sig(req("/challenge/", 302))?.class, "challenge"); assert.equal(sig(req("/api/v1/checkpoint/required/", 400))?.class, "challenge"); // LOGGED OUT the search answers 404 and the run goes on: not a signal by itself. assert.equal(sig(req(`/web/search/topsearch/?query=${H}`, 404)), null); assert.equal(sig(req(`/api/v1/feed/user/${UID}/?count=30`, 404)), null); assert.equal(sig(req(`/api/v1/feed/user/${UID}/?count=30`, 200)), null); assert.equal(sig(req(`/${H}`, 302)), null); // The query string never reaches a log line (it may name an account). assert.ok(!sig(req(`/web/search/topsearch/?query=${H}`, 429))!.line.includes(H)); // Another host's 403 (a CDN) is not Instagram's refusal. assert.equal(instagramStopSignal({ host: "cdn.example.invalid", method: "GET", path: "/a.jpg", status: 403 }), null); assert.equal(instagramErrorStatus({ message: "'401 Unauthorized' for 'https://www.instagram.com/x'" }), 401); assert.equal(instagramErrorStatus({ message: "Requested user could not be found" }), null); }); // Review 2, N3: the walls are path SEGMENTS. gallery-dl asks for the account's // own page as /, and a handle may begin with "challenge" or contain // "checkpoint" (both handles invented). test("a handle that begins with 'challenge' or contains 'checkpoint' is an account's page, never a wall", () => { const sig = (l: string) => instagramStopSignal(parseInstagramRequestLine(l)!); for (const handle of ["challengeaccepted", "checkpoint.charlie", "challenge", "checkpoint"]) { assert.equal(sig(req(`/${handle}`, 200)), null, handle); assert.equal(instagramWallPage(`/${handle}`), null, handle); // Its 404 is the account being gone, as for any handle. const gone = classifyInstagramError( { error: "NotFoundError", message: "Requested user could not be found" }, { requests: lines(req(`/web/search/topsearch/?query=${handle}`, 404), req(`/${handle}`, 404)), handle }, ); assert.equal(gone.class, "not-found", handle); // An error row that quotes the account's own URL is not a challenge either. const row = classifyInstagramError( { error: "HttpError", message: `'500 Internal Server Error' for 'https://www.instagram.com/${handle}'` }, { requests: [], handle }, ); assert.equal(row.class, "error", handle); // The search for it is not a wall. assert.equal(sig(req(`/web/search/topsearch/?query=${handle}`, 200)), null); } // The walls themselves, by segment. assert.equal(instagramWallPage("/challenge/"), "challenge"); assert.equal(instagramWallPage("/challenge/action/abc/"), "challenge"); assert.equal(instagramWallPage("/api/v1/challenge/"), null, "only the first segment is the challenge page"); assert.equal(instagramWallPage("/accounts/checkpoint/"), "challenge"); assert.equal(instagramWallPage("/checkpoint/"), "challenge"); assert.equal(instagramWallPage("/accounts/login/"), "login"); assert.equal(instagramWallPage("/accounts/login"), "login"); assert.equal(instagramWallPage("/accounts/loginhelp/"), null); assert.equal(instagramWallPage("/challengeaccepted/"), null, "a handle with a trailing slash is still a handle"); assert.equal(instagramWallPage("/"), null); // A row whose URL IS a wall still classifies by it. const ctx = { requests: [], handle: "challengeaccepted" }; assert.equal(classifyInstagramError({ error: "HttpError", message: "'400 Bad Request' for 'https://www.instagram.com/challenge/?next=/'" }, ctx).class, "challenge"); assert.equal(classifyInstagramError({ error: "HttpError", message: "'200 OK' for 'https://www.instagram.com/accounts/login/?next=/'" }, ctx).class, "login"); });