// INSTAGRAM IDS AND URLS (lane instagram). Pure and client-safe. // // THE ARCHIVE KEY IS THE NUMERIC MEDIA PK, never the shortcode: a story item // has no shortcode at all, a shortcode is case-sensitive and may begin with // "-" (a hazard as a directory name under `posts-media/`), and the two convert // to each other with no request — a shortcode is the pk written in base 64 // with the alphabet below. So a post's URL (which carries the shortcode) still // resolves to its archive key offline. // // A pk is beyond 2^53 for every post made since about 2012: it is a STRING of // digits here, converted with BigInt, never a Number. const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; const INDEX = new Map([...ALPHABET].map((c, i) => [c, BigInt(i)])); const PK_RE = /^\d{1,25}$/; const SHORTCODE_RE = /^[A-Za-z0-9_-]{1,64}$/; // A private account's post has 28 more characters after the public shortcode. const PRIVATE_TAIL = 28; export function isInstagramPk(v: unknown): v is string { return typeof v === "string" && PK_RE.test(v); } export function isInstagramShortcode(v: unknown): v is string { return typeof v === "string" && SHORTCODE_RE.test(v); } // "64" -> "BA". Null for anything that is not a run of digits. export function instagramShortcodeFromPk(pk: string): string | null { if (!isInstagramPk(pk)) return null; let n = BigInt(pk); if (n === 0n) return ALPHABET[0]!; let out = ""; while (n > 0n) { out = ALPHABET[Number(n % 64n)]! + out; n /= 64n; } return out; } // "BA" -> "64". A private post's long shortcode is cut to its public part. // Null when what it names is not a pk (longer than any pk is). export function instagramPkFromShortcode(shortcode: string): string | null { if (!isInstagramShortcode(shortcode)) return null; const code = shortcode.length > PRIVATE_TAIL ? shortcode.slice(0, -PRIVATE_TAIL) : shortcode; let n = 0n; for (const c of code) { const d = INDEX.get(c); if (d === undefined) return null; n = n * 64n + d; } const pk = n.toString(); return isInstagramPk(pk) ? pk : null; } export const INSTAGRAM_ORIGIN = "https://www.instagram.com"; export type InstagramPostKind = "post" | "reel" | "story" | "highlight"; export const INSTAGRAM_POST_KINDS: ReadonlyArray = ["post", "reel", "story", "highlight"]; // The permalink of a post or a reel, from its shortcode. export function instagramPostUrl(kind: "post" | "reel", shortcode: string): string { return `${INSTAGRAM_ORIGIN}/${kind === "reel" ? "reel" : "p"}/${shortcode}/`; } export function instagramAccountUrl(handle: string): string { return `${INSTAGRAM_ORIGIN}/${handle}/`; } function instagramPath(input: string): string[] | null { let u: URL; try { u = new URL(input.trim()); } catch { return null; } if (u.protocol !== "https:" && u.protocol !== "http:") return null; const host = u.hostname.toLowerCase().replace(/^www\./, ""); if (host !== "instagram.com") return null; try { return u.pathname.split("/").filter(Boolean).map((s) => decodeURIComponent(s)); } catch { return null; } } export function isInstagramUrl(input: string | undefined | null): boolean { return Boolean(input && instagramPath(input)); } const HANDLE_RE = /^[A-Za-z0-9._]{1,30}$/; // First path segments that are not accounts. const RESERVED = new Set([ "p", "tv", "reel", "reels", "stories", "s", "explore", "accounts", "direct", "share", "about", "api", "graphql", "web", "developer", "legal", "challenge", ]); // The account an account URL names (instagram.com//…), or null: a // post, a story, a tag page and the login pages are not accounts. export function instagramHandleFromUrl(input: string): string | null { const seg = instagramPath(input); if (!seg || seg.length === 0) return null; const first = seg[0]!.replace(/^@/, ""); if (RESERVED.has(first.toLowerCase())) return null; return HANDLE_RE.test(first) ? first : null; } // Words in a shortcode's place that are not one. const NOT_SHORTCODES = new Set(["audio", "explore", "videos", "create", "trending"]); export type InstagramPostUrlRef = { // The archive key (the numeric pk), from the shortcode. id: string; shortcode: string; kind: "post" | "reel"; handle?: string; url: string; }; // A post or reel URL: /p//, /reel//, /reels//, /tv//, // each also under //. A story URL is not one (its item has no // shortcode, and reading it needs a login). export function parseInstagramPostUrl(input: string): InstagramPostUrlRef | null { const seg = instagramPath(input); if (!seg) return null; const k = seg.findIndex((s) => s === "p" || s === "tv" || s === "reel" || s === "reels"); if (k < 0 || k > 1) return null; const shortcode = seg[k + 1]; if (!shortcode || !isInstagramShortcode(shortcode)) return null; // /reels/audio// is a sound's page, /p// nothing we know: a // post's path ends at its shortcode (or one "embed" / "c/" below it // is still that post, but is not offered here). if (NOT_SHORTCODES.has(shortcode.toLowerCase()) || seg.length > k + 2) return null; const id = instagramPkFromShortcode(shortcode); if (!id) return null; const handle = k === 1 && HANDLE_RE.test(seg[0]!) && !RESERVED.has(seg[0]!.toLowerCase()) ? seg[0]! : undefined; if (k === 1 && !handle) return null; const kind = seg[k] === "p" ? "post" : "reel"; return { id, shortcode, kind, ...(handle ? { handle } : {}), url: instagramPostUrl(kind, shortcode) }; }