UNPKG

rsshub

Version:
95 lines (93 loc) 3.63 kB
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs"; import "./config-C37vj7VH.mjs"; import "./dist-BInvbO1W.mjs"; import { t as logger_default } from "./logger-Czu8UMNd.mjs"; import "./ofetch-BIyrKU3Y.mjs"; import { t as parseDate } from "./parse-date-BrP7mxXf.mjs"; import { t as cache_default } from "./cache-Bo__VnGm.mjs"; import "./helpers-DxBp0Pty.mjs"; import { t as art } from "./render-BQo6B4tL.mjs"; import "./proxy-Db7uGcYb.mjs"; import { t as got_default } from "./got-KxxWdaxq.mjs"; import { n as puppeteer_default } from "./puppeteer-DGmvuGvT.mjs"; import { n as setCookies } from "./puppeteer-utils-BK3JC9qW.mjs"; import path from "node:path"; import { load } from "cheerio"; import { CookieJar } from "tough-cookie"; //#region lib/routes/pnas/index.ts init_esm_shims(); const route = { path: "/:topicPath{.+}?", radar: [{ source: ["pnas.org/*topicPath"], target: "/:topicPath" }], name: "Unknown", maintainers: [], handler, url: "pnas.org/*topicPath" }; async function handler(ctx) { const baseUrl = "https://www.pnas.org"; const link = `${baseUrl}/${ctx.req.param("topicPath") ?? "latest"}`; let cookieJar = await cache_default.get("pnas:cookieJar"); const cacheMiss = !cookieJar; cookieJar = cacheMiss ? new CookieJar() : CookieJar.fromJSON(cookieJar); const { data: res } = await got_default(link, { cookieJar }); if (cacheMiss) await cache_default.set("pnas:cookieJar", cookieJar.toJSON()); const $ = load(res); const list = $(".card--row-reversed .card-content").toArray().map((item) => { item = $(item); const a = item.find(".article-title a"); return { title: a.text(), link: new URL(a.attr("href"), baseUrl).href, pubDate: parseDate(item.find(".card__meta__date").text()) }; }); const browser = await puppeteer_default(); const out = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => { const page = await browser.newPage(); await setCookies(page, await cookieJar.getCookieString(item.link), ".pnas.org"); await page.setRequestInterception(true); page.on("request", (request) => { request.resourceType() === "document" ? request.continue() : request.abort(); }); logger_default.http(`Requesting ${item.link}`); await page.goto(item.link, { waitUntil: "domcontentloaded", referer: link }); await page.waitForSelector(".core-container"); const res$1 = await page.evaluate(() => document.documentElement.innerHTML); await page.close(); const $$1 = load(res$1); const PNASdataLayer = JSON.parse($$1("script").text().match(/PNASdataLayer =(.*?);/)[1]); $$1(".signup-alert-ad, .citations-truncation button").remove(); const { keywords, topic } = PNASdataLayer.page.attributes; item.category = [...keywords, topic]; item.author = PNASdataLayer.page.pageInfo.author; item.doi = PNASdataLayer.page.pageInfo.DOI; item.description = art(path.join(__dirname, "templates/article-8214e5db.art"), { access: PNASdataLayer.user.access === "yes", abstracts: $$1("#abstracts .core-container").html(), articleBody: $$1("[property=articleBody]").html(), dataAvailability: $$1("#data-availability").html(), acknowledgments: $$1("#acknowledgments").html(), supplementaryMaterials: $$1("#supplementary-materials").html(), bibliography: $$1("#bibliography").html() }); return item; }))); await browser.close(); return { title: `${$(".banner-widget__content h1").text()} - PNAS`, description: $(".banner-widget__content p").text(), image: "https://www.pnas.org/favicon.ico", language: "en-US", link, item: out }; } //#endregion export { route };