rsshub
Version:
Make RSS Great Again!
81 lines (79 loc) • 3.54 kB
JavaScript
import "./esm-shims-CzJ_djXG.mjs";
import "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import { t as logger_default } from "./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 "./proxy-Db7uGcYb.mjs";
import { t as timezone } from "./timezone-D8cuwzTY.mjs";
import { n as puppeteer_default } from "./puppeteer-DGmvuGvT.mjs";
import { load } from "cheerio";
//#region lib/routes/chinatimes/index.ts
const route = {
path: "/:category?",
categories: ["traditional-media"],
example: "/chinatimes/realtimenews",
parameters: { category: "分類,見下表,留空為 `realtimenews`" },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{ source: ["www.chinatimes.com/:category/", "www.chinatimes.com/"] }],
name: "分類",
maintainers: ["KingJem"],
handler,
url: "www.chinatimes.com/",
description: `| 即時 | 熱門 | 政治 | 生活 | 娛樂 | 財經 | 國際 | 言論 | 兩岸 | 軍事 | 社會 | 健康 | 體育 | 科技 | 運勢 | 有影 | 寶島 |
| :----------: | :-----: | :-----: | :--: | :--: | :---: | :---: | :-----: | :-----: | :------: | :-----: | :----: | :----: | :------------: | :-----: | :--: | :----: |
| realtimenews | hotnews | politic | life | star | money | world | opinion | chinese | armament | society | health | sports | technologynews | fortune | tube | taiwan |`
};
async function handler(ctx) {
const baseUrl = "https://www.chinatimes.com";
const { category = "realtimenews" } = ctx.req.param("category");
const link = `${baseUrl}/${category}/?chdtv`;
const $ = load(await ofetch_default(link));
const browser = await puppeteer_default();
const list = $(".articlebox-compact").toArray().slice(0, ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit"), 10) : 20).map((item) => {
const $item = $(item);
const a = $item.find(".title a");
return {
title: a.text().trim(),
link: `${baseUrl}${a.attr("href")}?chdtv`,
guid: `${baseUrl}${a.attr("href")}`,
pubDate: timezone(parseDate($item.find("time").attr("datetime")), 8),
category: $item.find(".category a").toArray().map((i) => $(i).text().trim())
};
});
const items = await Promise.all(list.map((item) => cache_default.tryGet(item.link, async () => {
const page = await browser.newPage();
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" });
const response = await page.content();
await page.close();
const $$1 = load(response);
item.category = [...new Set([...item.category, ...$$1(".article-hash-tag a").toArray().map((i) => $$1(i).text().trim())])];
$$1(".ad, #donate-form-container, .promote-word, .google-news-promote, .article-hash-tag").remove();
item.description = $$1(".main-figure").html() + $$1(".article-body").html();
return item;
})));
await browser.close();
return {
title: $("head title").text(),
description: $("head meta[name=\"description\"]").attr("content"),
link,
language: $("html").attr("lang"),
image: `${baseUrl}/images/2020/apple-touch-icon.png`,
item: items
};
}
//#endregion
export { route };