// AN X TRENDING LIBRARY (lane x-trending, slice 1): a PRIVATE posts channel on // platform "twitter", `sourceKind: "social"`, with no `url` — nothing syncs it. // Its records are the posts X's trending pages (x.com/i/trending/) showed, // from any author, read one page at a time by `read-x-trending` // (controller/readXTrending.ts) and grouped into the operator's topics // (lib/xTrending/store.ts). `config.xTrending` marks the channel and may hold // the caps a read uses when the ask names none. // // NEVER ON A SITE, IN NO INDEX BUILD: every site writer refuses it // (lib/site.ts), and buildIndex skips it, so no staging index, shard, // corpus.json, llms.txt or MCP corpus tool carries its posts. The library's // own listing reads its files (store.ts), never the index. // // Client-safe, and a leaf but for ./marker.mjs (itself one): lib/channelConfig.ts // composes the coercion below, and the channel form imports the URL helper. export type XTrendingConfig = { // How many times a read moves to the bottom of the loaded page (each brings // at most one more page of the timeline). Absent: X_TRENDING_DEFAULT_SCROLLS. maxScrolls?: number; // A read stops scrolling once it has seen this many posts. Absent: // X_TRENDING_DEFAULT_POSTS. maxPosts?: number; // A media file larger than this many MB is not fetched for a post nobody // has kept (a watched topic's new posts): it is owed until the post is // kept. Absent: X_TRENDING_DEFAULT_VIDEO_MB. maxVideoMb?: number; // THE WATCH's defaults for this library (lib/xTrending/watch.ts): how often // a watched topic is read (minutes), for how long (hours), and how many // topics may be watched at once. Absent: 30, 48 and 4. watchIntervalMinutes?: number; watchHours?: number; watchMaxTopics?: number; }; // The two job kinds, and X's platform queue they run on: one browser on the // profile at a time, X's gap between jobs. export const READ_X_TRENDING_KIND = "read-x-trending"; export const REPARSE_X_TRENDING_KIND = "reparse-x-trending"; export const X_TRENDING_QUEUE = "platform:twitter"; // The job kinds that open the connected X profile in a browser while they // run on X's queue: a Connect or a Refresh (which open the same profile) is // refused while one of them is running (editor/app/lib/cookieRefreshGuard.ts). export const X_PROFILE_BROWSER_JOB_KINDS: readonly string[] = [READ_X_TRENDING_KIND, "capture-posts"]; // X is cooling down after a rate limit: why `what` ("trending read", "X // capture") does not start, and until when. One sentence for every job that // reads the cooldown. export function xRateLimitHoldSentence(what: string, remainingMs: number, now: number): string { const until = new Date(now + remainingMs).toISOString().slice(11, 16); return ( `X answered a rate limit (429) a short while ago: no ${what} starts before ${until} UTC ` + `(${Math.ceil(remainingMs / 60_000)} min from now). Ask again then; nothing retries by itself.` ); } export const X_TRENDING_DEFAULT_SCROLLS = 20; export const X_TRENDING_MAX_SCROLLS = 40; export const X_TRENDING_DEFAULT_POSTS = 200; export const X_TRENDING_MAX_POSTS = 400; export const X_TRENDING_DEFAULT_VIDEO_MB = 100; export const X_TRENDING_MAX_VIDEO_MB = 2000; // The watch: X's gap between two jobs is 5 minutes and a tick is a read and // then a capture, so nothing shorter than 15 minutes is a real interval. export const X_TRENDING_WATCH_DEFAULT_INTERVAL_MINUTES = 30; export const X_TRENDING_WATCH_MIN_INTERVAL_MINUTES = 15; export const X_TRENDING_WATCH_MAX_INTERVAL_MINUTES = 24 * 60; export const X_TRENDING_WATCH_DEFAULT_HOURS = 48; export const X_TRENDING_WATCH_MAX_HOURS = 7 * 24; export const X_TRENDING_WATCH_DEFAULT_TOPICS = 4; export const X_TRENDING_WATCH_MAX_TOPICS = 12; // A read ends after this many scrolls in a row that brought no new post. export const X_TRENDING_IDLE_SCROLLS = 2; function capOf(v: unknown, max: number): number | undefined { return typeof v === "number" && Number.isInteger(v) && v >= 1 && v <= max ? v : undefined; } // `config.xTrending`: an object (the marker), its caps kept when valid and // omitted when not. Anything that is not an object is no library. export function coerceXTrendingConfig(v: unknown): XTrendingConfig | undefined { if (!v || typeof v !== "object" || Array.isArray(v)) return undefined; const r = v as Record; const out: XTrendingConfig = {}; const scrolls = capOf(r.maxScrolls, X_TRENDING_MAX_SCROLLS); const posts = capOf(r.maxPosts, X_TRENDING_MAX_POSTS); if (scrolls !== undefined) out.maxScrolls = scrolls; if (posts !== undefined) out.maxPosts = posts; const videoMb = capOf(r.maxVideoMb, X_TRENDING_MAX_VIDEO_MB); if (videoMb !== undefined) out.maxVideoMb = videoMb; const interval = capOf(r.watchIntervalMinutes, X_TRENDING_WATCH_MAX_INTERVAL_MINUTES); if (interval !== undefined && interval >= X_TRENDING_WATCH_MIN_INTERVAL_MINUTES) out.watchIntervalMinutes = interval; const hours = capOf(r.watchHours, X_TRENDING_WATCH_MAX_HOURS); if (hours !== undefined) out.watchHours = hours; const topics = capOf(r.watchMaxTopics, X_TRENDING_WATCH_MAX_TOPICS); if (topics !== undefined) out.watchMaxTopics = topics; return out; } // An X trending library: a channel carrying the marker. THE MARKER ALONE // DECIDES (review 1) — ./marker.mjs holds the predicate, plain JavaScript so // umtool's .mjs readers of the corpus ask the very same question. export { isXTrendingChannel } from "./marker.mjs"; export const X_TRENDING_NOT_CITABLE = (slug: string) => `"${slug}" is an X trending library: it is private, and a report cannot cite its posts. ` + `Publish the posts out of it into a posts channel first.`; export const X_TRENDING_NOT_ON_A_SITE = (slug: string) => `${slug} is an X trending library: it is private, and never put on a site.`; export const X_TRENDING_HAS_NO_URL = "An X trending library has no URL: its pages are read one at a time (pnpm ops read-x-trending, or Read a trending page on its channel page)."; export const X_TRENDING_NOT_FETCHED = (slug: string) => `${slug} is an X trending library: it has no timeline to fetch. Read a trending page into it (pnpm ops read-x-trending).`; export const NOT_AN_X_TRENDING_LIBRARY = (slug: string) => `${slug} is not an X trending library (create one with platform "x-trending").`; export const NOT_A_TRENDING_URL = "not a trending page URL: give X's trending page, https://x.com/i/trending/"; const X_HOST_RE = /^(?:www\.|mobile\.)?(?:x|twitter)\.com$/i; export type XTrendingRef = { // X's numeric id of the trending page. trendId: string; // The page as it is asked: always x.com, no query, no fragment. url: string; }; // An X trending page URL (x.com or twitter.com /i/trending/), or null. // The query string and fragment are dropped: the page is its id. export function parseXTrendingUrl(input: unknown): XTrendingRef | null { if (typeof input !== "string" || !input.trim()) return null; let u: URL; try { u = new URL(input.trim()); } catch { return null; } if (u.protocol !== "https:" && u.protocol !== "http:") return null; if (!X_HOST_RE.test(u.hostname)) return null; const m = /^\/i\/trending\/(\d{1,25})\/?$/.exec(u.pathname); if (!m) return null; return { trendId: m[1], url: `https://x.com/i/trending/${m[1]}` }; } export type XTrendingLimits = { scrolls: number; posts: number }; // The caps of one read: the ask's, else the channel's, else the defaults. // A value outside 1–max is refused with a sentence, never clamped. export function xTrendingLimits( config: XTrendingConfig | undefined, ask: { scrolls?: unknown; posts?: unknown } = {}, ): { ok: true; limits: XTrendingLimits } | { ok: false; error: string } { const one = (asked: unknown, stored: number | undefined, dflt: number, max: number, what: string) => { if (asked === undefined || asked === null) return stored ?? dflt; const n = capOf(asked, max); return n === undefined ? `"${what}" is a whole number, 1–${max}` : n; }; const scrolls = one(ask.scrolls, config?.maxScrolls, X_TRENDING_DEFAULT_SCROLLS, X_TRENDING_MAX_SCROLLS, "scrolls"); if (typeof scrolls === "string") return { ok: false, error: scrolls }; const posts = one(ask.posts, config?.maxPosts, X_TRENDING_DEFAULT_POSTS, X_TRENDING_MAX_POSTS, "posts"); if (typeof posts === "string") return { ok: false, error: posts }; return { ok: true, limits: { scrolls, posts } }; } // The per-file cap, in bytes, for media fetched before a post is kept. export function xTrendingMaxVideoBytes(config: XTrendingConfig | undefined): number { return (config?.maxVideoMb ?? X_TRENDING_DEFAULT_VIDEO_MB) * 1024 * 1024; } export type XTrendingWatchTerms = { intervalMinutes: number; hours: number }; // One watch's interval and length: the ask's, else the library's, else the // defaults. A value out of range is refused with a sentence, never clamped. export function xTrendingWatchTerms( config: XTrendingConfig | undefined, ask: { intervalMinutes?: unknown; hours?: unknown } = {}, ): { ok: true; terms: XTrendingWatchTerms } | { ok: false; error: string } { let intervalMinutes = config?.watchIntervalMinutes ?? X_TRENDING_WATCH_DEFAULT_INTERVAL_MINUTES; if (ask.intervalMinutes !== undefined && ask.intervalMinutes !== null) { const n = capOf(ask.intervalMinutes, X_TRENDING_WATCH_MAX_INTERVAL_MINUTES); if (n === undefined || n < X_TRENDING_WATCH_MIN_INTERVAL_MINUTES) { return { ok: false, error: `"intervalMinutes" is a whole number, ${X_TRENDING_WATCH_MIN_INTERVAL_MINUTES}–${X_TRENDING_WATCH_MAX_INTERVAL_MINUTES} ` + `(X's jobs are 5 minutes apart, and a tick is a read and then a capture)`, }; } intervalMinutes = n; } let hours = config?.watchHours ?? X_TRENDING_WATCH_DEFAULT_HOURS; if (ask.hours !== undefined && ask.hours !== null) { const n = capOf(ask.hours, X_TRENDING_WATCH_MAX_HOURS); if (n === undefined) return { ok: false, error: `"hours" is a whole number, 1–${X_TRENDING_WATCH_MAX_HOURS}` }; hours = n; } return { ok: true, terms: { intervalMinutes, hours } }; } export function xTrendingWatchMaxTopics(config: XTrendingConfig | undefined): number { return config?.watchMaxTopics ?? X_TRENDING_WATCH_DEFAULT_TOPICS; }