// The X / Twitter extractor over SYNTHETIC captures (__fixtures__/x-*): the // 2012 and 2015 desktop profile layouts, a 2016 status page with its // conversation, the legacy mobile site, the 2022 JavaScript shell and a // suspended account's notice. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/waybackPages/extractors/x.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { extractX, parseXUrl, snowflakeIso, statusUrl, xExtractor } from "./x"; import { captureOf, fixture } from "./__fixtures__/capture"; const run = (file: string, url: string, ts: string) => extractX({ html: fixture(file), capture: captureOf(url, ts) }); test("URLs: profile, status and the i/ forms; X's own pages are not handles", () => { assert.deepEqual(parseXUrl(new URL("https://twitter.com/TeapotFan")), { handle: "TeapotFan" }); assert.deepEqual(parseXUrl(new URL("https://twitter.com/TeapotFan/with_replies")), { handle: "TeapotFan" }); assert.deepEqual(parseXUrl(new URL("https://mobile.twitter.com/TeapotFan/status/123456789012345678")), { handle: "TeapotFan", statusId: "123456789012345678" }); assert.deepEqual(parseXUrl(new URL("https://twitter.com/TeapotFan/statuses/123")), { handle: "TeapotFan", statusId: "123" }); assert.deepEqual(parseXUrl(new URL("https://x.com/i/web/status/123")), { statusId: "123" }); assert.deepEqual(parseXUrl(new URL("https://twitter.com/search?q=tea")), {}); assert.deepEqual(parseXUrl(new URL("https://twitter.com/")), {}); assert.equal(statusUrl("a", "1"), "https://twitter.com/a/status/1"); assert.equal(statusUrl(undefined, "1"), "https://twitter.com/i/status/1"); }); test("a status id's own time (snowflake); none before snowflakes", () => { assert.equal(snowflakeIso("606838887674810369"), "2015-06-05T15:04:00.000Z"); assert.equal(snowflakeIso("1038715922543546369"), "2018-09-09T09:09:09.000Z"); assert.equal(snowflakeIso("20"), undefined); assert.equal(snowflakeIso("12345678901234"), undefined); }); test("a 2015 profile: the header, then one tweet per tweet — pinned repeat once, reply, retweet, quote", () => { const drafts = run("x-2015-profile.html", "https://twitter.com/TeapotFan", "20150606000000"); assert.deepEqual(drafts.map((d) => d.itemId), [ "x-profile-teapotfan-20150606000000", "tweet-606838887674810369", "tweet-606384896209850369", "tweet-606163436958650369", "tweet-604605924111290369", ]); const [profile, plain, reply, retweet, quote] = drafts; assert.equal(profile.kind, "x-profile"); assert.equal(profile.url, "https://twitter.com/TeapotFan"); assert.equal(profile.author, "TeapotFan"); assert.equal(profile.authorName, "Teapot Fan"); assert.equal(profile.publishedAt, undefined); assert.equal( profile.text, [ "Teapot Fan (@TeapotFan)", "I collect teapots. Kettles too, ask @kettlefriend. More at https://teapots.example", "Location: Leeds", "Website: https://teapots.example", "Joined 2009-06-05", "Tweets: 1,234 · Following: 56 · Followers: 7,890 · Likes: 12", ].join("\n"), ); assert.deepEqual(profile.links, ["https://teapots.example"]); assert.equal(plain.kind, "tweet"); assert.equal(plain.url, "https://twitter.com/TeapotFan/status/606838887674810369"); assert.equal(plain.author, "TeapotFan"); assert.equal(plain.authorName, "Teapot Fan"); assert.equal(plain.publishedAt, "2015-06-05T15:04:00.000Z"); // The emoji as its character, a mention and a hashtag as text, the t.co // link expanded, the pic.twitter.com tail dropped. assert.equal(plain.text, "Brewing the first pot of the day ☕ with @kettlefriend #tea https://example.com/brew"); assert.deepEqual(plain.links, ["https://example.com/brew"]); assert.deepEqual(plain.media, [{ kind: "image", url: "https://pbs.twimg.com/media/CGteapotA.jpg" }]); assert.equal(plain.isReply, false); assert.equal(plain.isRepost, false); assert.equal(reply.isReply, true); assert.deepEqual(reply.replyTo, { platform: "twitter", id: "186422658462650369", url: "https://twitter.com/i/status/186422658462650369" }); assert.equal(reply.text, "Agreed, a warm pot matters.\nAlways warm it first."); // A retweet is the retweet's own status, by the timeline's owner, at the // retweet's time, pointing at the original. assert.equal(retweet.author, "TeapotFan"); assert.equal(retweet.url, "https://twitter.com/TeapotFan/status/606163436958650369"); assert.equal(retweet.publishedAt, "2015-06-03T18:20:00.000Z"); assert.equal(retweet.isRepost, true); assert.equal(retweet.isReply, false); assert.deepEqual(retweet.repostOf, { platform: "twitter", id: "605267533624250369", url: "https://twitter.com/kettlefriend/status/605267533624250369", author: "kettlefriend", }); assert.equal(retweet.text, "Descaling day. Vinegar works."); assert.deepEqual(quote.quoted, { platform: "twitter", id: "186732198097850369", url: "https://twitter.com/kettlefriend/status/186732198097850369", author: "kettlefriend", }); assert.equal(quote.text, "Still true three years later"); assert.deepEqual(quote.links, []); }); test("a 2012 profile: the old header and timeline", () => { const drafts = run("x-2012-profile.html", "https://twitter.com/kettlefriend", "20120403000000"); assert.deepEqual( drafts.map((d) => [d.kind, d.itemId, d.publishedAt ?? null]), [ ["x-profile", "x-profile-kettlefriend-20120403000000", null], ["tweet", "tweet-186732198097850369", "2012-04-02T08:30:00.000Z"], ["tweet", "tweet-186422658462650369", "2012-04-01T12:00:00.000Z"], ], ); assert.equal( drafts[0].text, "Kettle Friend (@kettlefriend)\nBoiling water since 2009.\nLocation: Kettlewell\nWebsite: http://kettle.example\nTweets: 2,001 · Following: 150 · Followers: 3,456", ); assert.equal(drafts[1].text, "Water boils at 100 degrees. Mostly. http://physics.example/boiling"); assert.deepEqual(drafts[1].links, ["http://physics.example/boiling"]); assert.equal(drafts[2].text, "No jokes today, only #tea."); assert.equal(drafts[2].authorName, "Kettle Friend"); }); test("a status page: the tweet and its conversation, each answering the one it replies to", () => { const drafts = run("x-2016-status.html", "https://twitter.com/TeapotFan/status/697374449464250369", "20160211000000"); assert.ok(drafts.every((d) => d.kind === "tweet")); assert.deepEqual( drafts.map((d) => [d.itemId, d.author, d.replyTo?.id ?? null, d.isReply]), [ ["tweet-697359349969850369", "kettlefriend", null, false], ["tweet-697360608261050369", "TeapotFan", "697359349969850369", true], ["tweet-697364383134650369", "kettlefriend", "697360608261050369", true], ["tweet-697374449464250369", "TeapotFan", "697364383134650369", true], // A threaded group: the first answers the page's tweet, the next the first. ["tweet-697381999211450369", "kettlefriend", "697374449464250369", true], ["tweet-697389548958650369", "TeapotFan", "697381999211450369", true], ["tweet-697381999211450370", "mugperson", "697374449464250369", true], ], ); const main = drafts[3]; // The permalink tweet's time is its printed line (no _timestamp on it). assert.equal(main.publishedAt, "2016-02-10T11:00:00.000Z"); assert.equal(main.text, "Brown hides the tea stains."); assert.equal(main.authorName, "Teapot Fan"); assert.deepEqual(main.replyTo, { platform: "twitter", id: "697364383134650369", url: "https://twitter.com/kettlefriend/status/697364383134650369", author: "kettlefriend", }); }); test("the legacy mobile layout: header, tweets timed by their ids, links from data-url", () => { const drafts = run("x-mobile-profile.html", "https://mobile.twitter.com/TeapotFan", "20180910000000"); assert.deepEqual(drafts.map((d) => d.itemId), [ "x-profile-teapotfan-20180910000000", "tweet-1038715922543546369", "tweet-1038517326443450369", ]); assert.equal( drafts[0].text, "Teapot Fan (@TeapotFan)\nI collect teapots.\nLocation: Leeds\nWebsite: https://teapots.example\nTweets: 1,500 · Following: 60 · Followers: 8,001", ); const [, shelf, reply] = drafts; assert.equal(shelf.publishedAt, "2018-09-09T09:09:09.000Z"); assert.equal(shelf.text, "New shelf for the collection https://shelves.example/oak"); assert.deepEqual(shelf.links, ["https://shelves.example/oak"]); assert.deepEqual(shelf.media, [{ kind: "image", url: "https://pbs.twimg.com/media/DmShelf.jpg" }]); assert.equal(shelf.url, "https://twitter.com/TeapotFan/status/1038715922543546369"); assert.equal(reply.text, "@kettlefriend Oak, not pine."); assert.equal(reply.isReply, true); assert.equal(reply.publishedAt, "2018-09-08T20:00:00.000Z"); }); test("the JavaScript app's shell: one noContent draft, no item id, profile or status by the URL", () => { const profile = run("x-2022-shell.html", "https://twitter.com/TeapotFan", "20221101000000"); assert.deepEqual(profile, [ { kind: "x-profile", text: "This capture holds no posts: it is X's JavaScript app, and the capture did not keep any posts.", links: [], noContent: true, author: "TeapotFan", }, ]); const status = run("x-2022-shell.html", "https://twitter.com/TeapotFan/status/1038715922543546369", "20221101000000"); assert.equal(status.length, 1); assert.equal(status[0].kind, "tweet"); assert.equal(status[0].itemId, undefined); assert.equal(status[0].noContent, true); assert.equal(status[0].text, "This capture holds no posts: it is X's JavaScript app, and the capture did not keep this tweet."); }); test("a notice page: no posts, the notice quoted", () => { const drafts = run("x-suspended.html", "https://twitter.com/GoneAccount", "20170101000000"); assert.equal(drafts.length, 1); assert.equal(drafts[0].kind, "x-profile"); assert.equal(drafts[0].noContent, true); assert.equal(drafts[0].text, 'This capture holds no posts. The page says: "Account suspended".'); }); test("matches: twitter.com, mobile.twitter.com and x.com only", () => { const m = (u: string) => xExtractor.matches(new URL(u), ""); assert.equal(m("https://twitter.com/TeapotFan"), true); assert.equal(m("http://mobile.twitter.com/TeapotFan"), true); assert.equal(m("https://x.com/TeapotFan"), true); assert.equal(m("https://nottwitter.com/TeapotFan"), false); assert.equal(xExtractor.name, "x"); assert.equal(xExtractor.version, 1); });