UNPKG

rsshub

Version:
45 lines (44 loc) 1.53 kB
import { t as rofetch } from "./ofetch-W1aeTHCo.mjs"; import { t as parseDate } from "./parse-date-CjhZE69i.mjs"; import { t as cache_default } from "./cache-CiDoUSLo.mjs"; import { load } from "cheerio"; //#region lib/routes/worldhappiness/analysis.ts const route = { path: "/analysis", categories: ["new-media"], example: "/worldhappiness/analysis", name: "Analysis", maintainers: ["nczitzk"], handler }; async function handler(ctx) { const limit = ctx.req.query("limit"); const rootUrl = "https://www.worldhappiness.report"; const currentUrl = `${rootUrl}/analysis/`; const $ = load(await rofetch(currentUrl)); const list = $("li.chapter").toArray().map((item) => { const $item = $(item); const a = $item.find(".chapter-title a"); return { title: `${$item.find(".chapter-title-meta").text()}: ${a.text()}`, link: new URL(a.attr("href"), rootUrl).href, pubDate: parseDate($item.find(".chapter-title-meta").text().split(" ", 1)[0], "YYYY"), category: $item.attr("data-tags")?.split(", ") }; }).slice(0, limit ? Number(limit) : 20); const items = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => { const content = load(await rofetch(item.link)); return { ...item, author: content(".author").toArray().map((author) => ({ name: content(author).contents().first().text().trim() })), description: content(".chapter-body").html() }; }))); return { title: $("head title").text(), link: currentUrl, item: items }; } //#endregion export { route };