Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit b4658a0846d9b8726f7214f8ea6b138750d26a27
parent 327b25624e8e328dafef12a77ac7df07df41ac79
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Wed, 18 Nov 2026 13:38:49 -0500

posts: instagram is a posts platform -- its id is the numeric pk, and nothing answers for it with X's

A post's archive key is the media pk; the shortcode its URL carries converts
both ways with BigInt (lib/instagram/ids.ts). postPermalink no longer falls
through to x.com for a platform it does not know, a post URL and an account
URL on instagram.com parse as Instagram's, the MCP's platform filter and the
report conversion name it, and its jobs are spaced ten minutes apart.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mcommon/controller/publishXTrending.ts | 1+
Mcommon/jobs/queueGap.ts | 1+
Mcommon/lib/detectPlatform.mjs | 1+
Acommon/lib/instagram/ids.test.ts | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acommon/lib/instagram/ids.ts | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acommon/lib/instagram/platform.test.ts | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcommon/lib/jobGap.ts | 4+++-
Mcommon/lib/platform.ts | 12++++++++++--
Mcommon/lib/posts.ts | 88++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mcommon/lib/report/convert-server.ts | 4+++-
Mcommon/lib/report/convertManifest.ts | 7++++++-
Mcommon/social/fetchers.ts | 13++++++++++++-
Mcommon/social/importPost.ts | 9+++++++--
Mmcp/src/server.ts | 6+++---
Mumtool/lib/research/case-leads.ts | 2+-
Mumtool/lib/research/case-verify.ts | 2+-
16 files changed, 470 insertions(+), 16 deletions(-)

diff --git a/common/controller/publishXTrending.ts b/common/controller/publishXTrending.ts @@ -131,6 +131,7 @@ const POST_FIELDS = { // Not an X post's. forum: false, page: false, + instagram: false, // Derived at index time from the availability sidecar, never stored. isDeleted: false, availability: false, diff --git a/common/jobs/queueGap.ts b/common/jobs/queueGap.ts @@ -71,6 +71,7 @@ export function queueGapFor( const PLATFORM_NAMES: Record<string, string> = { twitter: "X", bluesky: "Bluesky", + instagram: "Instagram", xenforo: "Forum", youtube: "YouTube", rumble: "Rumble", diff --git a/common/lib/detectPlatform.mjs b/common/lib/detectPlatform.mjs @@ -51,6 +51,7 @@ export function detectPlatform(url) { if (host === "x.com" || host.endsWith(".x.com")) return "twitter"; if (host.endsWith("twitter.com")) return "twitter"; if (host === "bsky.app" || host.endsWith(".bsky.app")) return "bluesky"; + if (host === "instagram.com" || host === "www.instagram.com") return "instagram"; // archive.org ITEMS only. Not web.archive.org (the Wayback Machine's page // captures are a different kind of record) — see lib/archiveOrgId.ts. if (host === "archive.org" || host === "www.archive.org") return "archiveorg"; diff --git a/common/lib/instagram/ids.test.ts b/common/lib/instagram/ids.test.ts @@ -0,0 +1,92 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { + instagramPkFromShortcode, + instagramShortcodeFromPk, + isInstagramPk, + isInstagramShortcode, + parseInstagramPostUrl, + instagramPostUrl, + instagramAccountUrl, + instagramHandleFromUrl, +} from "./ids"; + +test("a pk and its shortcode convert both ways", () => { + // 0 -> "A", 63 -> "_", 64 -> "BA": the base-64 alphabet A-Z a-z 0-9 - _ + assert.equal(instagramShortcodeFromPk("0"), "A"); + assert.equal(instagramShortcodeFromPk("63"), "_"); + assert.equal(instagramShortcodeFromPk("64"), "BA"); + assert.equal(instagramPkFromShortcode("BA"), "64"); + assert.equal(instagramPkFromShortcode("_"), "63"); +}); + +test("a pk beyond 2^53 survives the round trip as a string", () => { + for (const pk of ["9007199254740993", "3141592653589793238", "18446744073709551615"]) { + const code = instagramShortcodeFromPk(pk); + assert.ok(code && isInstagramShortcode(code), pk); + assert.equal(instagramPkFromShortcode(code!), pk); + } + // 2^53 + 1 is not a Number: the conversion must not have gone through one. + assert.notEqual(String(Number("9007199254740993")), "9007199254740993"); +}); + +test("a shortcode that starts with - or _ converts", () => { + for (const code of ["-AbCdEfGhIj", "_zzzzzzzzzz", "-", "--_-"]) { + const pk = instagramPkFromShortcode(code); + assert.ok(pk && isInstagramPk(pk), code); + } + assert.equal(instagramShortcodeFromPk(instagramPkFromShortcode("-AbCdEfGhIj")!), "-AbCdEfGhIj"); + // A leading "A" is a leading zero: it does not round-trip, and never names a post. + assert.equal(instagramShortcodeFromPk(instagramPkFromShortcode("AB")!), "B"); +}); + +test("a private post's long shortcode is cut to its public part", () => { + const code = "CabcDEF1234"; + const long = code + "x".repeat(28); + assert.equal(instagramPkFromShortcode(long), instagramPkFromShortcode(code)); +}); + +test("what is not a pk or a shortcode is refused", () => { + assert.equal(instagramShortcodeFromPk("12a"), null); + assert.equal(instagramShortcodeFromPk(""), null); + assert.equal(instagramShortcodeFromPk("-5"), null); + assert.equal(instagramPkFromShortcode("ab/cd"), null); + assert.equal(instagramPkFromShortcode(""), null); + assert.equal(isInstagramPk("123_456"), false); + assert.equal(isInstagramPk("0123"), true); +}); + +test("a post's URL names its pk", () => { + const code = "CabcDEF1234"; + const pk = instagramPkFromShortcode(code)!; + for (const u of [ + `https://www.instagram.com/p/${code}/`, + `https://instagram.com/reel/${code}`, + `https://www.instagram.com/reels/${code}/?igsh=abc`, + `https://www.instagram.com/example.account/p/${code}/`, + `https://www.instagram.com/tv/${code}/`, + ]) { + const ref = parseInstagramPostUrl(u); + assert.deepEqual(ref && { id: ref.id, shortcode: ref.shortcode }, { id: pk, shortcode: code }, u); + } + assert.equal(parseInstagramPostUrl(`https://www.instagram.com/example.account/p/${code}/`)?.handle, "example.account"); + assert.equal(parseInstagramPostUrl("https://www.instagram.com/example.account/"), null); + assert.equal(parseInstagramPostUrl("https://example.com/p/abc/"), null); + assert.equal(parseInstagramPostUrl("https://www.instagram.com/stories/example.account/123/"), null); +}); + +test("the URLs the archive writes", () => { + assert.equal(instagramPostUrl("post", "Cabc"), "https://www.instagram.com/p/Cabc/"); + assert.equal(instagramPostUrl("reel", "Cabc"), "https://www.instagram.com/reel/Cabc/"); + assert.equal(instagramAccountUrl("example.account"), "https://www.instagram.com/example.account/"); +}); + +test("an account URL's handle, and the paths that are not accounts", () => { + assert.equal(instagramHandleFromUrl("https://www.instagram.com/example.account/"), "example.account"); + assert.equal(instagramHandleFromUrl("https://instagram.com/Example_Account?hl=en"), "Example_Account"); + for (const p of ["p/abc", "reel/abc", "reels/abc", "stories/x", "explore/tags/x", "accounts/login", "tv/abc", "direct/inbox", "share/abc"]) { + assert.equal(instagramHandleFromUrl(`https://www.instagram.com/${p}/`), null, p); + } + assert.equal(instagramHandleFromUrl("https://example.com/someone/"), null); + assert.equal(instagramHandleFromUrl("https://www.instagram.com/"), null); +}); diff --git a/common/lib/instagram/ids.ts b/common/lib/instagram/ids.ts @@ -0,0 +1,148 @@ +// 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. +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; + } + return n.toString(); +} + +export const INSTAGRAM_ORIGIN = "https://www.instagram.com"; + +export type InstagramPostKind = "post" | "reel" | "story" | "highlight"; + +export const INSTAGRAM_POST_KINDS: ReadonlyArray<InstagramPostKind> = ["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/<handle>/…), 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; +} + +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/<code>/, /reel/<code>/, /reels/<code>/, /tv/<code>/, +// each also under /<handle>/. 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; + 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) }; +} diff --git a/common/lib/instagram/platform.test.ts b/common/lib/instagram/platform.test.ts @@ -0,0 +1,96 @@ +// The places a posts platform is told apart, asked about Instagram: none of +// them may answer with X's. +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { isPostPlatform, parsePost, postPermalink, postPlatformLabel, POST_PLATFORM_VALUES } from "../posts"; +import { defaultWebpageUrl, detectPlatform, isSocialPlatform, platformQueueKey, queueKeyForUrl, PLATFORM_VALUES, SOCIAL_PLATFORM_VALUES } from "../platform"; +import { jobGapSecondsFor } from "../jobGap"; +import { handleFromAccountUrl, parsePostUrl } from "../../social/fetchers"; +import { instagramPkFromShortcode } from "./ids"; + +const CODE = "CabcDEF1234"; +const PK = instagramPkFromShortcode(CODE)!; + +test("instagram is a posts platform, a social platform and a detected host", () => { + assert.ok(isPostPlatform("instagram")); + assert.ok(POST_PLATFORM_VALUES.includes("instagram")); + assert.ok(PLATFORM_VALUES.includes("instagram")); + assert.ok(SOCIAL_PLATFORM_VALUES.includes("instagram")); + assert.ok(isSocialPlatform("instagram")); + assert.equal(postPlatformLabel("instagram"), "Instagram"); + assert.equal(detectPlatform("https://www.instagram.com/example.account/"), "instagram"); + assert.equal(detectPlatform("https://instagram.com/p/abc/"), "instagram"); + assert.equal(detectPlatform("https://notinstagram.com/x/"), null); + assert.equal(queueKeyForUrl("https://www.instagram.com/example.account/"), "platform:instagram"); + assert.equal(platformQueueKey("instagram"), "platform:instagram"); +}); + +test("instagram jobs are spaced ten minutes apart; X's five are unchanged", () => { + assert.deepEqual(jobGapSecondsFor(undefined, "instagram"), { seconds: 600, key: "instagram" }); + assert.deepEqual(jobGapSecondsFor(undefined, "twitter"), { seconds: 300, key: "twitter" }); + assert.deepEqual(jobGapSecondsFor({ jobGapSeconds: { instagram: 900 } }, "instagram"), { seconds: 900, key: "instagram" }); +}); + +test("an instagram permalink is never an x.com one", () => { + assert.equal(postPermalink("instagram", "example.account", PK), `https://www.instagram.com/p/${CODE}/`); + assert.equal(postPermalink("instagram", "example.account", PK, "reel"), `https://www.instagram.com/reel/${CODE}/`); + assert.equal(postPermalink("instagram", "example.account", "not-a-pk"), "https://www.instagram.com/example.account/"); + assert.equal(postPermalink("instagram", "", "not-a-pk"), ""); + assert.equal(defaultWebpageUrl("instagram", PK), ""); + // X's is what it was. + assert.equal(postPermalink("twitter", "someone", "123"), "https://x.com/someone/status/123"); + assert.equal(postPermalink("twitter", "", "123"), "https://x.com/i/status/123"); +}); + +test("a stored instagram post reads back with its facts, and a url rebuilt from its pk", () => { + const post = parsePost({ + id: PK, + channelSlug: "example-ig", + author: "example.account", + createdAt: "2026-01-02T03:04:05.000Z", + text: "hello", + platform: "instagram", + mediaCount: 3, + instagram: { + kind: "reel", + shortcode: CODE, + pinned: true, + coauthors: [{ handle: "second.account", name: "Second", id: "42" }, { nope: 1 }], + junk: "dropped", + }, + }); + assert.ok(post); + assert.equal(post!.url, `https://www.instagram.com/reel/${CODE}/`); + assert.deepEqual(post!.instagram, { + kind: "reel", + shortcode: CODE, + pinned: true, + coauthors: [{ handle: "second.account", name: "Second", id: "42" }], + }); + // A malformed block is dropped, never the post. + const bare = parsePost({ id: PK, channelSlug: "c", createdAt: "2026-01-02T03:04:05.000Z", text: "", platform: "instagram", instagram: { kind: "tweet" } }); + assert.ok(bare && bare.instagram === undefined); + assert.equal(bare!.url, `https://www.instagram.com/p/${CODE}/`); +}); + +test("an instagram account URL gives its handle; a post URL gives none", () => { + assert.equal(handleFromAccountUrl("https://www.instagram.com/example.account/"), "example.account"); + assert.equal(handleFromAccountUrl(`https://www.instagram.com/p/${CODE}/`), null); + assert.equal(handleFromAccountUrl("https://www.instagram.com/stories/example.account/"), null); + assert.equal(handleFromAccountUrl("https://www.instagram.com/explore/tags/x/"), null); + // X's reserved paths are what they were. + assert.equal(handleFromAccountUrl("https://x.com/home"), null); + assert.equal(handleFromAccountUrl("https://x.com/someone"), "someone"); +}); + +test("a post URL names its platform: instagram's is not X's", () => { + assert.deepEqual(parsePostUrl(`https://www.instagram.com/example.account/reel/${CODE}/?x=1`), { + platform: "instagram", + id: PK, + shortcode: CODE, + handle: "example.account", + url: `https://www.instagram.com/reel/${CODE}/`, + }); + assert.equal(parsePostUrl("https://www.instagram.com/example.account/"), null); + assert.equal(parsePostUrl("https://x.com/someone/status/123")?.platform, "twitter"); +}); diff --git a/common/lib/jobGap.ts b/common/lib/jobGap.ts @@ -4,12 +4,14 @@ // /settings form shows the gaps in force. // The built-in gaps: every platform queue waits `default` seconds after the -// previous network job on it ended; X five minutes, a forum and Bluesky one. +// previous network job on it ended; X five minutes, a forum and Bluesky one, +// Instagram ten (it checkpoints the logged-in account that reads too often). export const JOB_GAP_DEFAULT_SECONDS: Readonly<Record<string, number>> = Object.freeze({ default: 20, twitter: 300, xenforo: 60, bluesky: 60, + instagram: 600, }); export const JOB_GAP_MAX_SECONDS = 24 * 60 * 60; diff --git a/common/lib/platform.ts b/common/lib/platform.ts @@ -21,7 +21,9 @@ export type Platform = // lib/waybackPages/): a posts channel whose records are page records. // Never detected from a URL — a web.archive.org URL is a capture of // something else, which import-video or import-page decides. - | "webpage"; + | "webpage" + // An Instagram account's posts and reels (lane instagram). + | "instagram"; export const PLATFORM_VALUES: ReadonlyArray<Platform> = [ "local", @@ -36,6 +38,7 @@ export const PLATFORM_VALUES: ReadonlyArray<Platform> = [ "bluesky", "xenforo", "webpage", + "instagram", ]; // The subset that carries social posts rather than videos. A channel on one of @@ -46,6 +49,7 @@ export const SOCIAL_PLATFORM_VALUES: ReadonlyArray<Platform> = [ "bluesky", "xenforo", "webpage", + "instagram", ]; export function isSocialPlatform(platform: Platform | null | undefined): boolean { @@ -53,7 +57,8 @@ export function isSocialPlatform(platform: Platform | null | undefined): boolean platform === "twitter" || platform === "bluesky" || platform === "xenforo" || - platform === "webpage" + platform === "webpage" || + platform === "instagram" ); } @@ -95,6 +100,9 @@ export function defaultWebpageUrl(platform: Platform, id: string): string { // A page record's URL is its capture's original, which every record // carries; an id (a timestamp and a hash) names none. if (platform === "webpage") return /^https?:\/\//.test(id) ? id : ""; + // An Instagram post's URL carries its shortcode; every archived post has its + // own `url` (postPermalink builds one from the id). + if (platform === "instagram") return /^https?:\/\//.test(id) ? id : ""; return `https://www.youtube.com/watch?v=${id}`; } diff --git a/common/lib/posts.ts b/common/lib/posts.ts @@ -11,6 +11,14 @@ import { pageFileName } from "./manifest"; import type { PagePostInfo } from "./waybackPages/types"; import { parsePagePostInfo } from "./waybackPages/parse"; +import { + INSTAGRAM_POST_KINDS, + instagramAccountUrl, + instagramPostUrl, + instagramShortcodeFromPk, + isInstagramShortcode, + type InstagramPostKind, +} from "./instagram/ids"; // "xenforo" is a forum THREAD read as a posts source: the channel is one // thread, each forum post a Post. It is named for the forum software, not a @@ -18,17 +26,20 @@ import { parsePagePostInfo } from "./waybackPages/parse"; // "webpage" (release 37) is a Wayback Machine capture of a page that is not a // video: one captured state of one page, or one item a capture holds // (`Post.page`; lib/waybackPages/). -export type PostPlatform = "twitter" | "bluesky" | "xenforo" | "webpage"; +// "instagram" (lane instagram) is an Instagram account's posts and reels +// (`Post.instagram`; lib/instagram/). +export type PostPlatform = "twitter" | "bluesky" | "xenforo" | "webpage" | "instagram"; export const POST_PLATFORM_VALUES: ReadonlyArray<PostPlatform> = [ "twitter", "bluesky", "xenforo", "webpage", + "instagram", ]; export function isPostPlatform(v: unknown): v is PostPlatform { - return v === "twitter" || v === "bluesky" || v === "xenforo" || v === "webpage"; + return typeof v === "string" && (POST_PLATFORM_VALUES as ReadonlyArray<string>).includes(v); } // The platform's name as a reader sees it. @@ -37,6 +48,7 @@ export function postPlatformLabel(platform: PostPlatform | string | undefined): if (platform === "bluesky") return "Bluesky"; if (platform === "xenforo") return "Forum"; if (platform === "webpage") return "Web page"; + if (platform === "instagram") return "Instagram"; return "Post"; } @@ -82,6 +94,26 @@ export type ForumPostInfo = { quotes?: { postId?: string; author?: string; authorId?: string }[]; }; +// What an Instagram post carries beyond the common shape (platform +// "instagram"). The post's `id` is its numeric media pk (lib/instagram/ids.ts +// says why); the shortcode its URL carries is kept here. +export type InstagramPostInfo = { + // "post" (a picture, or a carousel of several media), "reel" (one video), + // and, from slice 2, "story" and "highlight". + kind: InstagramPostKind; + shortcode?: string; + // The post is pinned to the top of the account's grid. + pinned?: boolean; + // A collaboration: the accounts it is shared with, the owner aside. `author` + // is the OWNER, who may not be the channel's account; the channel's account + // is then among these. + coauthors?: { handle: string; name?: string; id?: string }[]; + // A story's end (ISO-8601) and when the archive read it; a highlight's title. + expiresAt?: string; + capturedAt?: string; + highlightTitle?: string; +}; + // A pointer to another post, which may or may not itself be archived. Used for // reply/quote/repost edges so thread structure survives even when the // referenced post is outside the archived account. @@ -136,6 +168,8 @@ export type Post = { // The capture a page record came from (platform "webpage"): its original // URL, capture time, replay and raw URLs, kind and extractor. page?: PagePostInfo; + // Instagram-specific facts (platform "instagram"). + instagram?: InstagramPostInfo; engagement?: PostEngagement; // Merged in at index time from the channel's availability sidecar (the same // shape videos use: the stored record is the source of truth, the flag on @@ -299,6 +333,16 @@ export function postPermalink( // A page record's permalink is the page it is a capture of (`base`, its // `page.originalUrl`); an id alone names none. if (platform === "webpage") return base ?? ""; + // An Instagram post's permalink carries its shortcode, which is its id (the + // numeric pk) in base 64. `base` "reel" names a reel. An id that is not a pk + // (never written by the fetcher) names the account instead of a wrong post. + if (platform === "instagram") { + const code = instagramShortcodeFromPk(id); + if (code) return instagramPostUrl(base === "reel" ? "reel" : "post", code); + return author ? instagramAccountUrl(author) : ""; + } + // Only X is left. Anything else has no permalink this function can build. + if (platform !== "twitter") return ""; return `https://x.com/${author || "i"}/status/${id}`; } @@ -340,7 +384,9 @@ export function parsePost(raw: unknown): Post | null { ? forumBaseOf(r.forum) : r.platform === "webpage" ? pageOriginalOf(r.page) - : undefined, + : r.platform === "instagram" + ? instagramKindOf(r.instagram) + : undefined, ), platform: r.platform, isReply: r.isReply === true, @@ -376,9 +422,45 @@ export function parsePost(raw: unknown): Post | null { // A malformed `page` is dropped, never the post. const page = parsePagePostInfo(r.page); if (page) post.page = page; + const instagram = parseInstagramInfo(r.instagram); + if (instagram) post.instagram = instagram; return post; } +function instagramKindOf(raw: unknown): string | undefined { + const kind = (raw as { kind?: unknown } | null)?.kind; + return kind === "reel" ? "reel" : undefined; +} + +// A malformed `instagram` is dropped, never the post. +function parseInstagramInfo(raw: unknown): InstagramPostInfo | null { + if (!raw || typeof raw !== "object") return null; + const r = raw as Record<string, unknown>; + if (!(INSTAGRAM_POST_KINDS as ReadonlyArray<unknown>).includes(r.kind)) return null; + const out: InstagramPostInfo = { kind: r.kind as InstagramPostKind }; + if (isInstagramShortcode(r.shortcode)) out.shortcode = r.shortcode; + if (r.pinned === true) out.pinned = true; + if (Array.isArray(r.coauthors)) { + const list: NonNullable<InstagramPostInfo["coauthors"]> = []; + for (const item of r.coauthors) { + if (!item || typeof item !== "object") continue; + const c = item as Record<string, unknown>; + if (typeof c.handle !== "string" || !c.handle) continue; + list.push({ + handle: c.handle, + ...(typeof c.name === "string" && c.name ? { name: c.name } : {}), + ...(typeof c.id === "string" && c.id ? { id: c.id } : {}), + }); + } + if (list.length > 0) out.coauthors = list; + } + for (const key of ["expiresAt", "capturedAt", "highlightTitle"] as const) { + const v = r[key]; + if (typeof v === "string" && v) out[key] = v; + } + return out; +} + function pageOriginalOf(raw: unknown): string | undefined { const url = (raw as { originalUrl?: unknown } | null)?.originalUrl; return typeof url === "string" && url ? url : undefined; diff --git a/common/lib/report/convert-server.ts b/common/lib/report/convert-server.ts @@ -128,7 +128,9 @@ export function diskPostOf(channelsDir: string): PostOf { ? "forum" : post.platform === "webpage" ? "webpage" - : "x", + : post.platform === "instagram" + ? "instagram" + : "x", url: post.url, createdAt: post.createdAt, author: post.author, diff --git a/common/lib/report/convertManifest.ts b/common/lib/report/convertManifest.ts @@ -365,7 +365,8 @@ export type PostRecord = { // "forum": a forum-thread post (platform "xenforo"), and "webpage" a page // record (a Wayback capture, release 37), which a video manifest does not // carry yet — each is left out with a warning, never relabelled. - platform: "x" | "bluesky" | "forum" | "webpage"; + // "instagram": an Instagram post, left out the same way. + platform: "x" | "bluesky" | "forum" | "webpage" | "instagram"; url: string; createdAt?: string; author?: string; @@ -510,6 +511,10 @@ export async function reportToManifest(raw: unknown, opts: ManifestOptions = {}) warnings.push(`${cid}: post ${c.channel}/${c.id} is a web page capture, which a video manifest does not carry — left out of posts`); return; } + if (rec?.platform === "instagram") { + warnings.push(`${cid}: post ${c.channel}/${c.id} is an Instagram post, which a video manifest does not carry yet — left out of posts`); + return; + } const platform = rec?.platform ?? (/^\d+$/.test(c.id) ? "x" : null); const url = rec?.url ?? (platform === "x" ? `https://x.com/i/status/${c.id}` : null); const date = c.date ?? rec?.createdAt; diff --git a/common/social/fetchers.ts b/common/social/fetchers.ts @@ -21,6 +21,7 @@ import type { PostCaptureState } from "./postCapture"; import type { XAvatarRunSummary } from "./xAvatarCapture"; import type { XContextRunSummary } from "./xPostContext"; import type { ChromiumLike } from "./playwrightRuntime"; +import { instagramHandleFromUrl, isInstagramUrl, parseInstagramPostUrl } from "../lib/instagram/ids"; export type PostFetchInput = { // The account's canonical URL as configured on the channel. @@ -507,6 +508,9 @@ export function handleFromAccountUrl(input: string): string | null { } catch { return null; } + // instagram.com/<handle>: its own reserved paths (p, reel, stories, …) and + // its own handle alphabet (lib/instagram/ids.ts). + if (isInstagramUrl(trimmed)) return instagramHandleFromUrl(trimmed); const segments = url.pathname.split("/").filter(Boolean); if (segments.length === 0) return null; // A forum thread: its "<slug>.<id>" key is the channel's handle. @@ -543,7 +547,10 @@ export function handleFromAccountUrl(input: string): string | null { // (`bsky.app/profile/<handle|did>/post/<rkey>`, or its `at://` URI). export type PostUrlRef = | { platform: "twitter"; id: string; handle?: string; url: string } - | { platform: "bluesky"; id: string; actor: string; url: string }; + | { platform: "bluesky"; id: string; actor: string; url: string } + // An Instagram post or reel (`instagram.com/p/<shortcode>/`, `/reel/…`): its + // id is the numeric pk the shortcode is (lib/instagram/ids.ts). + | { platform: "instagram"; id: string; shortcode: string; handle?: string; url: string }; export function parsePostUrl(input: string): PostUrlRef | null { const raw = input.trim(); @@ -558,6 +565,10 @@ export function parsePostUrl(input: string): PostUrlRef | null { return null; } if (u.protocol !== "https:" && u.protocol !== "http:") return null; + const ig = parseInstagramPostUrl(raw); + if (ig) { + return { platform: "instagram", id: ig.id, shortcode: ig.shortcode, ...(ig.handle ? { handle: ig.handle } : {}), url: ig.url }; + } const host = u.hostname.toLowerCase().replace(/^(www|mobile)\./, ""); const seg = u.pathname.split("/").filter(Boolean).map((x) => decodeURIComponent(x)); if (host === "x.com" || host === "twitter.com") { diff --git a/common/social/importPost.ts b/common/social/importPost.ts @@ -48,7 +48,7 @@ export const NOT_A_POST_URL = "not a post URL: give an X status (https://x.com/<handle>/status/<id>) or a Bluesky post " + "(https://bsky.app/profile/<handle>/post/<id>, or its at:// URI)"; -const PLATFORM_LABEL: Record<string, string> = { twitter: "X", bluesky: "Bluesky", xenforo: "forum" }; +const PLATFORM_LABEL: Record<string, string> = { twitter: "X", bluesky: "Bluesky", xenforo: "forum", instagram: "Instagram" }; export type ImportPostPlan = { config: ChannelConfig; @@ -90,7 +90,12 @@ export async function planImportPost( if (!fetcher.fetchOne) return { ok: false, error: `${slug}'s fetcher (${fetcher.label}) cannot import one post` }; const handle = config.socialHandle ?? handleFromAccountUrl(config.url ?? "") ?? ""; if (!handle) return { ok: false, error: `Could not determine an account handle for ${slug}` }; - const named = ref.platform === "twitter" ? ref.handle : ref.actor.startsWith("did:") ? undefined : ref.actor; + const named = + ref.platform === "twitter" || ref.platform === "instagram" + ? ref.handle + : ref.actor.startsWith("did:") + ? undefined + : ref.actor; if (named && !sameAccount(named, handle)) { return { ok: false, error: `the post is @${named}'s; ${slug} archives @${handle}'s posts` }; } diff --git a/mcp/src/server.ts b/mcp/src/server.ts @@ -517,11 +517,11 @@ export const TOOLS: Tool[] = [ }, platforms: { type: "array", - items: { type: "string", enum: ["twitter", "bluesky", "xenforo", "webpage"] }, + items: { type: "string", enum: ["twitter", "bluesky", "xenforo", "webpage", "instagram"] }, description: "Keep only POST hits on these platforms: 'twitter', 'bluesky', " + "'xenforo' (forum threads), 'webpage' (Wayback Machine page " + - "captures). Absent = every platform. Video hits are not narrowed — " + + "captures), 'instagram'. Absent = every platform. Video hits are not narrowed — " + "add content_types:['post'] for posts only.", }, link_style: { @@ -2249,7 +2249,7 @@ async function handleGetTranscripts( } // search_transcripts' `platforms`: the post platforms named, or undefined. -const POST_PLATFORM_ARGS = new Set(["twitter", "bluesky", "xenforo", "webpage"]); +const POST_PLATFORM_ARGS = new Set(["twitter", "bluesky", "xenforo", "webpage", "instagram"]); function postPlatformsArg(raw: unknown): string[] | undefined { if (!Array.isArray(raw)) return undefined; const out = raw.filter((p): p is string => typeof p === "string" && POST_PLATFORM_ARGS.has(p)); diff --git a/umtool/lib/research/case-leads.ts b/umtool/lib/research/case-leads.ts @@ -108,7 +108,7 @@ const nameHash = (c: Pick<LeadCandidate, "name">) => privateNameHash(c.name.repl export type PostsChannel = { slug: string; name: string; platform: string; inScope: boolean }; -const SOCIAL_PLATFORMS = new Set(["xenforo", "twitter", "bluesky", "webpage", "x-search"]); +const SOCIAL_PLATFORMS = new Set(["xenforo", "twitter", "bluesky", "webpage", "instagram", "x-search"]); async function configOf(slug: string): Promise<Record<string, unknown> | null> { if (!/^[A-Za-z0-9_.@-]+$/.test(slug)) return null; diff --git a/umtool/lib/research/case-verify.ts b/umtool/lib/research/case-verify.ts @@ -187,7 +187,7 @@ async function sourceOf(caseId: string): Promise<string> { async function postTrack(f: FindingRow, hashes: string[]): Promise<CaptionTrack | null> { const platform = await channelPlatform(f.slug); - if (!platform || !["xenforo", "twitter", "bluesky", "webpage"].includes(platform)) return null; + if (!platform || !["xenforo", "twitter", "bluesky", "webpage", "instagram"].includes(platform)) return null; const posts = await readAllPosts(path.join(/* turbopackIgnore: true */ CHANNELS_DIR, f.slug)).catch(() => []); const p = posts.find((x) => x.id === f.id); if (!p) return null;