rsshub
Version:
Make RSS Great Again!
58 lines (57 loc) • 2.1 kB
JavaScript
import { t as rofetch } from "./ofetch-W1aeTHCo.mjs";
import { t as logger } from "./logger-BPV-Cx6k.mjs";
import { t as parseDate } from "./parse-date-CjhZE69i.mjs";
import { t as cache_default } from "./cache-CiDoUSLo.mjs";
import { t as timezone } from "./timezone-DE--ze1V.mjs";
import { n as outPlaywright } from "./playwright-Dd_brOFk.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 () => {
item.description = getDescription(load(await rofetch(item.link, { headers: { cookie } }))) ?? void 0;
return item;
});
})) : list;
return {
title: $("title").text(),
link,
item: items
};
};
//#endregion
export { getFeed as t };