import { readXTrendingAction } from "../../../channels/[slug]/xTrendingActions"; import { jobResponse, ops, optBool, optPositiveInt, optString, reqSlug } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug, url, topic?, scrolls?, posts?, tabs?, probe?, headed? } -> { ok: true, jobId } // POST { slug, reparse: "", topic? } -> { ok: true, jobId } // // ONE X trending page (https://x.com/i/trending/) read into `slug`, // an X trending library (a private posts channel made with platform // "x-trending"), as ONE `read-x-trending` job on X's queue: the page is // opened in the connected X profile's browser, every GraphQL answer the page // itself fetched is saved raw with a screenshot // (channels//trending/reads//), and the posts are parsed from // the saved file and written as the library's posts under a topic. A read // lists posts; it archives no media and no thread. // // topic the topic's id or name, or a new one's name. Absent: the topic // this page was read into before, else a new one named for it. // scrolls at most this many moves to the bottom of a tab (1–40; the // channel's cap, else 20). posts: stop once this many are seen on // a tab (1–400; else 200). A tab's read also stops after two // scrolls that brought nothing new, and the whole read at once on // a 429 or a login wall. // tabs the page's tabs to read: ["top"], ["latest"] or both (the // default). Both are read in THIS one job: Top to its stop, then // the page's Latest tab is pressed. A Latest that cannot be // opened ends the read "Top read; Latest not read: ", ok. // probe save the capture and its summary; write no post. // headed open a window on the editor's desktop instead of a headless one. // reparse a saved read's id: its capture parsed again, OFFLINE (a // `reparse-x-trending` job; X is asked nothing, so no fresh jar). // // The log ends with `@@read-x-trending {ok, slug, mode, url, trendId, read, // outcome, stop?, scrolls?, responses, operations, posts: {onPage, added}, // topic?, sentence, error?}` — operation NAMES and SIZES, never a body. A // capture with no post the parser knows ends ok, "read saved, nothing // recognised". // // Every refusal is the action's own sentence: no such channel, not a // library, not a trending page URL, a cap out of range, no connected X // profile, a Connect or Refresh of X in flight, a stale cookie jar or a // raised X alert (the network guard), a saved read that is not there. export async function POST(request: Request) { return ops( request, ["slug", "url", "reparse", "topic", "scrolls", "posts", "tabs", "probe", "headed"], async (body) => { const url = optString(body, "url"); const reparse = optString(body, "reparse"); const topic = optString(body, "topic"); const scrolls = optPositiveInt(body, "scrolls"); const posts = optPositiveInt(body, "posts"); const probe = optBool(body, "probe"); const headed = optBool(body, "headed"); return jobResponse( await readXTrendingAction({ slug: reqSlug(body, "slug"), ...(url !== undefined ? { url } : {}), ...(reparse !== undefined ? { reparse } : {}), ...(topic !== undefined ? { topic } : {}), ...(scrolls !== undefined ? { scrolls } : {}), ...(posts !== undefined ? { posts } : {}), ...(body.tabs !== undefined ? { tabs: body.tabs } : {}), ...(probe ? { probe: true } : {}), ...(headed ? { headed: true } : {}), }), ); }, ); }