Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 8 additions & 132 deletions apps/api/src/scraper/scrapeURL/engines/x-twitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import { Meta } from "../..";
import { EngineScrapeResult } from "..";
import { EngineError, XTwitterConfigurationError } from "../../error";
import { safeMarkdownToHtml } from "../pdf/markdownToHtml";
import {
isXTwitterUrl,
parseXTwitterUrl,
type XTwitterPostUrl,
type XTwitterProfileUrl,
} from "./url";

export { isXTwitterUrl };

const XAI_RESPONSES_MODEL = "grok-4-1-fast-non-reasoning";

Expand All @@ -21,37 +29,6 @@ function xTwitterTelemetry(functionId: string, meta: Meta) {
};
}

const RESERVED_PROFILE_PATHS = new Set([
"compose",
"explore",
"hashtag",
"home",
"i",
"intent",
"login",
"logout",
"messages",
"notifications",
"search",
"settings",
"share",
]);

type XTwitterProfileUrl = {
kind: "profile";
handle: string;
normalizedUrl: string;
};

type XTwitterPostUrl = {
kind: "post";
handle?: string;
postId: string;
normalizedUrl: string;
};

type XTwitterUrl = XTwitterProfileUrl | XTwitterPostUrl;

type ProfilePost = {
text: string;
url?: string | null;
Expand Down Expand Up @@ -220,99 +197,6 @@ const postSchema = {
additionalProperties: false,
};

function parseXTwitterUrl(url: string): XTwitterUrl | null {
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return null;
}

if (!["http:", "https:"].includes(parsed.protocol)) {
return null;
}

const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
if (
hostname !== "x.com" &&
hostname !== "twitter.com" &&
hostname !== "mobile.twitter.com"
) {
return null;
}

const segments = parsed.pathname
.split("/")
.map(segment => segment.trim())
.filter(Boolean);

if (segments.length === 0) {
return null;
}

if (
segments.length >= 4 &&
segments[0] === "i" &&
segments[1] === "web" &&
segments[2] === "status" &&
isPostId(segments[3])
) {
return {
kind: "post",
postId: segments[3],
normalizedUrl: `https://x.com/i/web/status/${segments[3]}`,
};
}

if (
segments.length >= 3 &&
segments[0] === "i" &&
segments[1] === "status" &&
isPostId(segments[2])
) {
return {
kind: "post",
postId: segments[2],
normalizedUrl: `https://x.com/i/web/status/${segments[2]}`,
};
}

if (
segments.length >= 3 &&
isHandle(segments[0]) &&
["status", "statuses"].includes(segments[1]) &&
isPostId(segments[2])
) {
const handle = segments[0];
const postId = segments[2];
return {
kind: "post",
handle,
postId,
normalizedUrl: `https://x.com/${handle}/status/${postId}`,
};
}

if (
segments.length === 1 &&
isHandle(segments[0]) &&
!RESERVED_PROFILE_PATHS.has(segments[0].toLowerCase())
) {
const handle = segments[0];
return {
kind: "profile",
handle,
normalizedUrl: `https://x.com/${handle}`,
};
}

return null;
}

export function isXTwitterUrl(url: string): boolean {
return parseXTwitterUrl(url) !== null;
}

export async function scrapeURLWithXTwitter(
meta: Meta,
): Promise<EngineScrapeResult> {
Expand Down Expand Up @@ -582,14 +466,6 @@ async function fetchPost(
return output as XTwitterPostData;
}

function isHandle(value: string): boolean {
return /^[A-Za-z0-9_]{1,15}$/.test(value);
}

function isPostId(value: string): boolean {
return /^\d{5,}$/.test(value);
}

function stripAt(value: string | null | undefined): string | undefined {
const trimmed = value?.trim();
if (!trimmed) {
Expand Down
134 changes: 134 additions & 0 deletions apps/api/src/scraper/scrapeURL/engines/x-twitter/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Pure URL recognition for the X/Twitter engine, kept apart from the engine so
// credit estimation can use it without pulling in the scraper and AI SDK.

const RESERVED_PROFILE_PATHS = new Set([
"compose",
"explore",
"hashtag",
"home",
"i",
"intent",
"login",
"logout",
"messages",
"notifications",
"search",
"settings",
"share",
]);

export type XTwitterProfileUrl = {
kind: "profile";
handle: string;
normalizedUrl: string;
};

export type XTwitterPostUrl = {
kind: "post";
handle?: string;
postId: string;
normalizedUrl: string;
};

export type XTwitterUrl = XTwitterProfileUrl | XTwitterPostUrl;

export function parseXTwitterUrl(url: string): XTwitterUrl | null {
let parsed: URL;
try {
parsed = new URL(url);
} catch {
return null;
}

if (!["http:", "https:"].includes(parsed.protocol)) {
return null;
}

const hostname = parsed.hostname.toLowerCase().replace(/^www\./, "");
if (
hostname !== "x.com" &&
hostname !== "twitter.com" &&
hostname !== "mobile.twitter.com"
) {
return null;
}

const segments = parsed.pathname
.split("/")
.map(segment => segment.trim())
.filter(Boolean);

if (segments.length === 0) {
return null;
}

if (
segments.length >= 4 &&
segments[0] === "i" &&
segments[1] === "web" &&
segments[2] === "status" &&
isPostId(segments[3])
) {
return {
kind: "post",
postId: segments[3],
normalizedUrl: `https://x.com/i/web/status/${segments[3]}`,
};
}

if (
segments.length >= 3 &&
segments[0] === "i" &&
segments[1] === "status" &&
isPostId(segments[2])
) {
return {
kind: "post",
postId: segments[2],
normalizedUrl: `https://x.com/i/web/status/${segments[2]}`,
};
}

if (
segments.length >= 3 &&
isHandle(segments[0]) &&
["status", "statuses"].includes(segments[1]) &&
isPostId(segments[2])
) {
const handle = segments[0];
const postId = segments[2];
return {
kind: "post",
handle,
postId,
normalizedUrl: `https://x.com/${handle}/status/${postId}`,
};
}

if (
segments.length === 1 &&
isHandle(segments[0]) &&
!RESERVED_PROFILE_PATHS.has(segments[0].toLowerCase())
) {
const handle = segments[0];
return {
kind: "profile",
handle,
normalizedUrl: `https://x.com/${handle}`,
};
}

return null;
}

export function isXTwitterUrl(url: string): boolean {
return parseXTwitterUrl(url) !== null;
}

function isHandle(value: string): boolean {
return /^[A-Za-z0-9_]{1,15}$/.test(value);
}

function isPostId(value: string): boolean {
return /^\d{5,}$/.test(value);
}
Loading
Loading