// INVENTED Instagram runs, in the shape gallery-dl 1.32.9 prints with // `--dump-json` (extractor/instagram.py `_parse_post_rest`, job.py DataJob): // one array, `[2, post]` per post, `[3, url, file]` per media file, and // `[-1, {error, message}]` for the exception that ended the run. No account, // id, shortcode or address here is a real one. import { instagramShortcodeFromPk } from "../../lib/instagram/ids"; export const IG_HANDLE = "example.account"; export const IG_UID = "4000000001"; export const IG_CDN = "https://cdn.example.invalid"; // Beyond 2^53, like every real pk. export const igPk = (n: number): string => (3500000000000000000n + BigInt(n)).toString(); export type FakeIgPost = { n: number; date: string; // "YYYY-MM-DD HH:MM:SS" text?: string; type?: "post" | "reel" | "story"; // Media of the post: "image" | "video" each. media?: ("image" | "video")[]; pinned?: boolean; owner?: string; ownerName?: string; coauthors?: { username: string; full_name?: string; id?: string }[]; likes?: number; // Print the pk as a bare JSON number, as gallery-dl may. bareNumber?: boolean; }; export function igRows(posts: FakeIgPost[], opts: { handle?: string } = {}): unknown[] { const rows: unknown[] = []; for (const p of posts) { const pk = igPk(p.n); const code = instagramShortcodeFromPk(pk)!; const type = p.type ?? (p.media?.length === 1 && p.media[0] === "video" ? "reel" : "post"); const media = p.media ?? ["image"]; const post = { category: "instagram", subcategory: "posts", post_id: pk, post_shortcode: code, post_url: `https://www.instagram.com/${type === "reel" ? "reel" : "p"}/${code}/`, type, username: p.owner ?? opts.handle ?? IG_HANDLE, fullname: p.ownerName ?? "Example Account", owner_id: IG_UID, description: p.text ?? "", post_date: p.date, date: p.date, likes: p.likes ?? 0, liked: false, pinned: p.pinned ? [Number(IG_UID)] : [], count: media.length, ...(p.coauthors ? { coauthors: p.coauthors } : {}), ...(media.length > 1 ? { sidecar_media_id: pk, sidecar_shortcode: code } : {}), }; rows.push([2, post]); media.forEach((kind, i) => { const mediaId = igPk(p.n * 100 + i + 1); const ext = kind === "video" ? "mp4" : "jpg"; const url = `${IG_CDN}/v/t51/${mediaId}.${ext}?sig=invented&oe=0`; rows.push([ 3, url, { ...post, num: i + 1, media_id: mediaId, shortcode: instagramShortcodeFromPk(mediaId), display_url: `${IG_CDN}/v/t51/${mediaId}.jpg?sig=invented&oe=0`, video_url: kind === "video" ? url : null, extension: ext, filename: mediaId, width: 1080, height: 1350, }, ]); }); } return rows; } // What gallery-dl prints: the array, with the named pks as bare numbers. export function igStdout(rows: unknown[], bare: string[] = []): string { let text = JSON.stringify(rows, null, 2); for (const pk of bare) text = text.replaceAll(`"post_id": "${pk}"`, `"post_id": ${pk}`); return text; } export const igError = (error: string, message: string): unknown => [-1, { error, message }]; // A request line as `--verbose` prints it. export const igRequestLine = (path: string, status = 200, method = "GET"): string => `[urllib3.connectionpool][debug] https://www.instagram.com:443 "${method} ${path} HTTP/1.1" ${status} ${status === 200 ? "None" : "20445"}`; export const igCursorLine = (cursor: string): string => `[instagram][debug] Cursor: ${cursor}`; export const igFeedPath = (uid = IG_UID): string => `/api/v1/feed/user/${uid}/?count=30`; export const igSearchPath = (handle = IG_HANDLE): string => `/web/search/topsearch/?query=${handle}`;