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
71 lines
2.6 KiB
TypeScript
71 lines
2.6 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { findCaption, findMetaMedia, mediaFromMetaNode } from '../src/platforms/meta-media.ts';
|
|
import { fixture } from './helpers.ts';
|
|
|
|
const REFERER = 'https://www.threads.com/';
|
|
|
|
test('a Threads image post normalizes to one image with referrer headers', () => {
|
|
const node = fixture<Parameters<typeof mediaFromMetaNode>[0]>('threads/image.json');
|
|
const media = mediaFromMetaNode(node, REFERER);
|
|
assert.equal(media.length, 1);
|
|
assert.equal(media[0]?.kind, 'image');
|
|
assert.equal(media[0]?.fetchHeaders?.['Referer'], REFERER);
|
|
});
|
|
|
|
test('a second real Threads post normalizes the same way', () => {
|
|
// Two captures, so the walk is not quietly fitted to one payload.
|
|
const media = mediaFromMetaNode(fixture('threads/second.json'), REFERER);
|
|
assert.equal(media.length, 1);
|
|
assert.equal(media[0]?.kind, 'image');
|
|
});
|
|
|
|
test('the largest candidate wins', () => {
|
|
const media = mediaFromMetaNode(
|
|
{
|
|
image_versions2: {
|
|
candidates: [
|
|
{ url: 'https://cdn/small.jpg', width: 320, height: 320 },
|
|
{ url: 'https://cdn/large.jpg', width: 1440, height: 1440 },
|
|
],
|
|
},
|
|
},
|
|
REFERER,
|
|
);
|
|
assert.equal(media[0]?.url, 'https://cdn/large.jpg');
|
|
});
|
|
|
|
test('a carousel flattens to its children', () => {
|
|
const media = mediaFromMetaNode(
|
|
{
|
|
carousel_media: [
|
|
{ image_versions2: { candidates: [{ url: 'https://cdn/1.jpg', width: 100 }] } },
|
|
{
|
|
image_versions2: { candidates: [{ url: 'https://cdn/poster.jpg', width: 100 }] },
|
|
video_versions: [{ url: 'https://cdn/2.mp4', width: 720 }],
|
|
},
|
|
],
|
|
},
|
|
REFERER,
|
|
);
|
|
assert.deepEqual(media.map((m) => m.kind), ['image', 'video']);
|
|
});
|
|
|
|
test('the media node is found however deeply it is buried', () => {
|
|
const real = { image_versions2: { candidates: [{ url: 'https://cdn/i.jpg', width: 100 }] }, tag: 'me' };
|
|
const found = findMetaMedia({ a: [{ b: { c: real } }] });
|
|
assert.equal((found as { tag?: string })?.tag, 'me');
|
|
});
|
|
|
|
test('a stub node with no candidates is not mistaken for the post', () => {
|
|
// These payloads are full of empty placeholders; picking one used to make
|
|
// a real post look like it had no media at all.
|
|
assert.equal(findMetaMedia({ image_versions2: { candidates: [] }, carousel_media: null }), undefined);
|
|
});
|
|
|
|
test('the caption is found the same way, and an empty one is not a caption', () => {
|
|
assert.equal(findCaption({ x: { caption: { text: 'hello' } } }), 'hello');
|
|
assert.equal(findCaption({ x: { caption: { text: '' } } }), undefined);
|
|
assert.equal(findCaption({ x: { caption: null } }), undefined);
|
|
});
|