UNPKG

rsshub

Version:
140 lines (138 loc) 4.53 kB
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs"; import "./config-C37vj7VH.mjs"; import { t as ViewType } from "./types-D84BRIt4.mjs"; import "./dist-BInvbO1W.mjs"; import "./logger-Czu8UMNd.mjs"; import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs"; import { t as parseDate } from "./parse-date-BrP7mxXf.mjs"; import { t as cache_default } from "./cache-Bo__VnGm.mjs"; import { t as art } from "./render-BQo6B4tL.mjs"; import path from "node:path"; import { load } from "cheerio"; //#region lib/routes/ornl/all-news.ts init_esm_shims(); const handler = async (ctx) => { const limit = Number.parseInt(ctx.req.query("limit") ?? "10", 10); const baseUrl = "https://www.ornl.gov"; const targetUrl = new URL("all-news", baseUrl).href; const $ = load(await ofetch_default(targetUrl)); const language = $("html").attr("lang") ?? "en"; let items = []; items = $("div.view-rows-main div.list-item-wrapper").slice(0, limit).toArray().map((el) => { const $el = $(el); const $aEl = $el.find("div.list-item-title h2 a"); const $imgEl = $el.find("div.list-item-thumbnail-wrapper img"); const title = $aEl.text(); const image = $imgEl.attr("src"); const description = art(path.join(__dirname, "templates/description-33c9a08c.art"), { images: image ? [{ src: image, alt: $imgEl.attr("alt") || title, width: Number($imgEl.attr("width")) || void 0, height: Number($imgEl.attr("height")) || void 0 }] : void 0, intro: $el.find("div.list-item-desc p").text() }); const pubDateStr = $el.find("div.list-item-date").attr("datetime"); const linkUrl = $aEl.attr("href"); const upDatedStr = pubDateStr; return { title, description, pubDate: pubDateStr ? parseDate(pubDateStr) : void 0, link: linkUrl ? new URL(linkUrl, baseUrl).href : void 0, content: { html: description, text: description }, image, banner: image, updated: upDatedStr ? parseDate(upDatedStr) : void 0, language }; }); items = await Promise.all(items.map((item) => { if (!item.link) return item; return cache_default.tryGet(item.link, async () => { const $$ = load(await ofetch_default(item.link)); const $$imgEl = $$("div.image-landscape img"); const title = $$("h1.page-title").text(); const image = $$imgEl.attr("src"); const description = art(path.join(__dirname, "templates/description-33c9a08c.art"), { images: image ? [{ src: image, alt: $$imgEl.attr("alt") || title, width: Number($$imgEl.attr("width")) || void 0, height: Number($$imgEl.attr("height")) || void 0 }] : void 0, description: $$("div.image-description").html() }); const pubDateStr = $$("div.publish-date time").attr("datetime"); const authors = $$("div.related-researcher-container").toArray().map((authorEl) => { const $$authorEl = $$(authorEl).find("div.related-researcher-name a"); return { name: $$authorEl.text(), url: $$authorEl.attr("href") ? new URL($$authorEl.attr("href"), baseUrl).href : void 0, avatar: $$authorEl.find("div.related-researcher-photo img").attr("src") ? new URL($$authorEl.find("div.related-researcher-photo img").attr("src"), baseUrl).href : void 0 }; }); const upDatedStr = pubDateStr; const processedItem = { title, description, pubDate: pubDateStr ? parseDate(pubDateStr) : item.pubDate, author: authors, content: { html: description, text: description }, image, banner: image, updated: upDatedStr ? parseDate(upDatedStr) : item.updated, language }; return { ...item, ...processedItem }; }); })); return { title: $("title").text(), description: $("meta[name=\"description\"]").attr("content"), link: targetUrl, item: items, allowEmpty: true, image: $("meta[name=\"twitter:image\"]").attr("content"), author: $("meta[property=\"og:site_name\"]").attr("content"), language, id: $("meta[property=\"og:url\"]").attr("content") }; }; const route = { path: "/all-news", name: "All News", url: "www.ornl.gov", maintainers: ["nczitzk"], handler, example: "/ornl/all-news", parameters: void 0, description: void 0, categories: ["government"], features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportRadar: true, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["www.ornl.gov/all-news"], target: "/all-news" }], view: ViewType.Articles }; //#endregion export { handler, route };