Initial commit: read social posts back without the app
antisocial is the other half of a StopTheMadness redirect rule. Links to X, Threads, Instagram, TikTok and Bluesky get rewritten to /<prefix>/<original path>, and this resolves the post and shows the media and the words, with a badge saying where it came from and a button to copy the original URL. Every request drives a real headless Chromium, logged out, from a residential IP. One code path, and it survives markup changes better than parsing from the outside would. Extraction is layered, most structured first: the platform's own API response caught in flight, then an inline payload, then the rendered DOM, then Open Graph tags. Media is never linked straight at a CDN. Instagram and TikTok reject requests without a matching Referer and cookies, and proxying keeps the viewer's browser from talking to the platform at all. Range is forwarded so the native video scrubber can seek. HLS is the exception, since proxying it would mean rewriting playlists. TikTok sometimes answers with a slider puzzle. Rather than reporting that as a failure, the page is parked and the viewer is handed the puzzle: screenshots stream out, pointer events are replayed back. Solving it leaves the cookie in the shared browser context, so the retry is an ordinary request. A failed resolve is never a blank error page. The card carries the platform, the original URL and the copy button, so a broken adapter still leaves the link one tap away. Verified end to end against real shared links on all five platforms, in the container, including multi-image carousels, reels, TikTok short links and photo posts. 49 tests run the adapters against captured payloads with no network. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BGkRmLfiWuJHx6tQ12EELY
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import type { Media } from '../types.ts';
|
||||
|
||||
/**
|
||||
* Instagram and Threads are the same product underneath and serve the same
|
||||
* media schema, so both adapters normalize through here.
|
||||
*/
|
||||
|
||||
type Candidate = { url?: string; width?: number; height?: number };
|
||||
|
||||
export type MetaMediaNode = {
|
||||
image_versions2?: { candidates?: Candidate[] };
|
||||
video_versions?: Candidate[];
|
||||
carousel_media?: MetaMediaNode[];
|
||||
accessibility_caption?: string;
|
||||
original_width?: number;
|
||||
original_height?: number;
|
||||
video_duration?: number;
|
||||
};
|
||||
|
||||
/** These CDNs 403 anything that arrives without a matching referrer. */
|
||||
export function metaHeaders(referer: string): Record<string, string> {
|
||||
return { Referer: referer, Origin: new URL(referer).origin };
|
||||
}
|
||||
|
||||
function largest(candidates: Candidate[] | undefined): Candidate | undefined {
|
||||
return (candidates ?? [])
|
||||
.filter((c) => c.url)
|
||||
.sort((a, b) => (b.width ?? 0) - (a.width ?? 0))[0];
|
||||
}
|
||||
|
||||
export function mediaFromMetaNode(node: MetaMediaNode, referer: string): Media[] {
|
||||
if (node.carousel_media?.length) {
|
||||
return node.carousel_media.flatMap((child) => mediaFromMetaNode(child, referer));
|
||||
}
|
||||
|
||||
const fetchHeaders = metaHeaders(referer);
|
||||
const poster = largest(node.image_versions2?.candidates);
|
||||
const size = {
|
||||
...(node.original_width ? { width: node.original_width } : {}),
|
||||
...(node.original_height ? { height: node.original_height } : {}),
|
||||
};
|
||||
|
||||
const video = largest(node.video_versions);
|
||||
if (video?.url) {
|
||||
return [{
|
||||
kind: 'video',
|
||||
url: video.url,
|
||||
fetchHeaders,
|
||||
...(poster?.url ? { poster: { url: poster.url, fetchHeaders } } : {}),
|
||||
...(node.video_duration ? { durationSec: Math.round(node.video_duration) } : {}),
|
||||
...size,
|
||||
}];
|
||||
}
|
||||
|
||||
if (poster?.url) {
|
||||
return [{
|
||||
kind: 'image',
|
||||
url: poster.url,
|
||||
fetchHeaders,
|
||||
...(node.accessibility_caption ? { alt: node.accessibility_caption } : {}),
|
||||
...(poster.width ? { width: poster.width } : size),
|
||||
...(poster.height ? { height: poster.height } : {}),
|
||||
}];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
function nonEmptyArray(value: unknown): boolean {
|
||||
return Array.isArray(value) && value.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Carrying the key is not the same as carrying media: these payloads are
|
||||
* full of stub nodes whose `image_versions2.candidates` is an empty array.
|
||||
* Only a node with something in it counts.
|
||||
*/
|
||||
function looksLikeMedia(value: Record<string, unknown>): boolean {
|
||||
if (nonEmptyArray(value['carousel_media'])) return true;
|
||||
if (nonEmptyArray(value['video_versions'])) return true;
|
||||
const images = value['image_versions2'] as { candidates?: unknown } | undefined;
|
||||
return nonEmptyArray(images?.candidates);
|
||||
}
|
||||
|
||||
/**
|
||||
* Threads buries the post inside a pile of Relay payloads whose shape moves
|
||||
* around. Rather than chase a path, walk the tree for the first node that
|
||||
* carries media and, separately, the first caption.
|
||||
*/
|
||||
export function findMetaMedia(root: unknown): MetaMediaNode | undefined {
|
||||
return walk(root, (record) => (looksLikeMedia(record) ? (record as MetaMediaNode) : undefined));
|
||||
}
|
||||
|
||||
/** Same walk, looking for `caption.text`. */
|
||||
export function findCaption(root: unknown): string | undefined {
|
||||
return walk(root, (record) => {
|
||||
const caption = record['caption'];
|
||||
if (!caption || typeof caption !== 'object') return undefined;
|
||||
const text = (caption as Record<string, unknown>)['text'];
|
||||
return typeof text === 'string' && text.length > 0 ? text : undefined;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Breadth-first so the node nearest the root — the post itself — wins over
|
||||
* anything quoted or recommended below it.
|
||||
*/
|
||||
function walk<T>(root: unknown, visit: (record: Record<string, unknown>) => T | undefined): T | undefined {
|
||||
const seen = new Set<unknown>();
|
||||
const queue: unknown[] = [root];
|
||||
|
||||
for (let i = 0; i < queue.length; i += 1) {
|
||||
const value = queue[i];
|
||||
if (value === null || typeof value !== 'object' || seen.has(value)) continue;
|
||||
seen.add(value);
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
queue.push(...value);
|
||||
continue;
|
||||
}
|
||||
|
||||
const record = value as Record<string, unknown>;
|
||||
const hit = visit(record);
|
||||
if (hit !== undefined) return hit;
|
||||
queue.push(...Object.values(record));
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
Reference in New Issue
Block a user