UNPKG

rsshub

Version:
51 lines (49 loc) 1.82 kB
import { t as rofetch } from "./ofetch-W1aeTHCo.mjs"; import "./types-DNj6Etbg.mjs"; import { t as parseDate } from "./parse-date-CjhZE69i.mjs"; import { a as threadUrl, n as extractThreadItems, r as parseRouteOptions, t as buildContent } from "./utils-Csqmm28X.mjs"; import { load } from "cheerio"; //#region lib/routes/threads/search.ts const route = { path: "/search/:keyword/:routeParams?", categories: ["social-media"], view: 1, example: "/threads/search/RSS", parameters: { keyword: "Search keyword", routeParams: { description: `Extra parameters, in the format of query string. Accepts the same options as User timeline, plus: | Key | Description | Accepts | Defaults to | | ----------- | ----------- | -------------------------- | ----------- | | \`serpType\` | Search type | \`tags\`/\`default\`/\`recent\` | \`tags\` |` } }, name: "Search", maintainers: ["TonyRL"], handler }; async function handler(ctx) { const { keyword, routeParams } = ctx.req.param(); const params = new URLSearchParams(routeParams); const options = parseRouteOptions(params); const serpType = params.get("serpType") ?? "tags"; const link = `https://www.threads.com/search?q=${encodeURIComponent(keyword)}&serp_type=${serpType}`; const threadsData = extractThreadItems(load(await rofetch(link))); if (!threadsData.length) throw new Error("Failed to fetch thread data"); ctx.set("json", threadsData); const items = threadsData.map((item) => { const { title, description } = buildContent(item, options); return { author: item.post.user?.username, title, description, pubDate: parseDate(item.post.taken_at, "X"), link: threadUrl(item.post.code) }; }); return { title: `${keyword} - Search on Threads`, link, item: items }; } //#endregion export { route };