UNPKG

rsshub

Version:
76 lines (73 loc) 2.75 kB
import "./esm-shims-CzJ_djXG.mjs"; import "./config-C37vj7VH.mjs"; import "./dist-BInvbO1W.mjs"; import "./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 got_default } from "./got-KxxWdaxq.mjs"; import { load } from "cheerio"; //#region lib/routes/newsmarket/index.ts const route = { path: "/:category?", categories: ["new-media"], example: "/newsmarket", parameters: { category: "分类,见下表,默认为首页" }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["newsmarket.com.tw/blog/category/:category", "newsmarket.com.tw/"] }], name: "分類", maintainers: ["nczitzk"], handler, description: `| 時事。政策 | 食安 | 新知 | 愛地方 | 種好田 | 好吃。好玩 | | ----------- | ----------- | --------- | ------------ | ------------ | ------------- | | news-policy | food-safety | knowledge | country-life | good-farming | good-food-fun | | 食農教育 | 人物 | 漁業。畜牧 | 綠生活。國際 | 評論 | | -------------- | ------------------ | -------------------- | ------------------- | ------- | | food-education | people-and-history | raising-and-breeding | living-green-travel | opinion |` }; async function handler(ctx) { const category = ctx.req.param("category") ?? ""; const currentUrl = `https://www.newsmarket.com.tw${category ? `/blog/category/${category}` : ""}`; const $ = load((await got_default({ method: "get", url: currentUrl })).data); const list = $(".title a").slice(0, ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit")) : 20).toArray().map((item) => { item = $(item); return { title: item.text(), link: item.attr("href"), pubDate: parseDate() }; }); const items = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => { const detailResponse = await got_default({ method: "get", url: item.link }); const content = load(detailResponse.data); content("figure img").each(function() { content(this).parent().html(`<img src="${content(this).attr("data-src")}">`); }); content(".inline-post").remove(); item.author = content(".author-name").text(); item.description = content(".entry-content").html(); item.pubDate = parseDate(detailResponse.data.match(/"datePublished":"(.*)","dateModified"/)[1]); return item; }))); return { title: $("title").text(), link: currentUrl, item: items }; } //#endregion export { route };