UNPKG

rsshub

Version:
59 lines (58 loc) 2.13 kB
import { t as rofetch } from "./ofetch-U0CdpE2g.mjs"; import { t as logger } from "./logger-DlhSwgkg.mjs"; import { t as parseDate } from "./parse-date-3kcy2Eau.mjs"; import { t as cache_default } from "./cache-C1Y-9qDV.mjs"; import { t as timezone } from "./timezone-UiMFOuvL.mjs"; import { n as outPlaywright } from "./playwright-CFVkq6JF.mjs"; import { t as getCookies } from "./playwright-utils-Bzle4SGT.mjs"; import { load } from "cheerio"; //#region lib/routes/gov/caict/utils.ts const baseUrl = "https://www.caict.ac.cn"; const fetchChallenged = async (link, waitSelector) => { const context = await outPlaywright(); try { const page = await context.newPage(); await page.route("**/*", (route) => { const resourceType = route.request().resourceType(); return resourceType === "document" || resourceType === "script" ? route.continue() : route.abort(); }); logger.http(`Requesting ${link}`); await page.goto(link, { waitUntil: "domcontentloaded" }); await page.waitForSelector(waitSelector, { timeout: 1e4 }); return { html: await page.content(), cookie: await getCookies(page, "www.caict.ac.cn") }; } finally { await context.close(); } }; const getFeed = async (path, itemSelector, getDescription) => { const link = `${baseUrl}/${path}/`; const { html, cookie } = await fetchChallenged(link, itemSelector); const $ = load(html); const list = $(itemSelector).toArray().map((item) => { const $item = $(item); const a = $item.find("a"); return { title: a.text().trim().replace(/^-\s*/, ""), link: new URL(a.attr("href"), link).href, pubDate: timezone(parseDate($item.next().find("span.kxyj_text").text()), 8) }; }); const items = getDescription ? await Promise.all(list.map((item) => { if (!item.link.startsWith(`${baseUrl}/`)) return item; return cache_default.tryGet(item.link, async () => { const response = await rofetch(item.link, { headers: { cookie } }); item.description = getDescription(load(response)) ?? void 0; return item; }); })) : list; return { title: $("title").text(), link, item: items }; }; //#endregion export { getFeed as t };