UNPKG

rsshub

Version:
47 lines (46 loc) 1.6 kB
import { t as rofetch } from "./ofetch-U0CdpE2g.mjs"; import { t as parseDate } from "./parse-date-3kcy2Eau.mjs"; import { t as cache_default } from "./cache-C1Y-9qDV.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 response = await rofetch(currentUrl); const $ = load(response); 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 detailResponse = await rofetch(item.link); const content = load(detailResponse); 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 };