UNPKG

rsshub

Version:
91 lines (90 loc) 3.47 kB
import { t as rofetch } from "./ofetch-C3ts-Hud.mjs"; import { t as parseDate } from "./parse-date-Cr0wQjV_.mjs"; import { t as cache_default } from "./cache-BkqOokyU.mjs"; import { t as timezone } from "./timezone-BBvDwS_3.mjs"; import { load } from "cheerio"; //#region lib/routes/ctinews/topic.ts const route = { path: "/topic/:topic?", categories: ["new-media"], example: "/ctinews/topic/KDdek5vgXx", parameters: { topic: "話題 ID,可在 URL 中獲取,留空為 `KDdek5vgXx`" }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["ctinews.com/news/topics/:topic", "ctinews.com"] }], name: "話題", maintainers: ["TonyRL"], handler, url: "ctinews.com" }; async function handler(ctx) { const { topic = "KDdek5vgXx" } = ctx.req.param(); const baseUrl = "https://www.ctinews.com"; const link = `${baseUrl}/news/topics/${topic}`; const $ = load(await rofetch(link)); const list = [ ...$(".hero-news__layer .news-link").toArray().map((item) => { const $item = $(item); return { title: $item.attr("title"), link: $item.attr("href")?.startsWith("http") ? $item.attr("href") : baseUrl + $item.attr("href") }; }), ...$(".second-section .news-link").toArray().map((item) => { const $item = $(item); return { title: $item.attr("title"), link: $item.attr("href")?.startsWith("http") ? $item.attr("href") : baseUrl + $item.attr("href") }; }), ...$(".news-section .news-link.absolute").toArray().map((item) => { const $item = $(item); return { title: $item.attr("title")?.replace("點擊觀看", ""), link: $item.attr("href")?.startsWith("http") ? $item.attr("href") : baseUrl + $item.attr("href") }; }) ]; const seen = /* @__PURE__ */ new Set(); const dedupedList = []; for (const item of list) { const link = item.link || ""; if (seen.has(link)) continue; seen.add(link); dedupedList.push(item); } const items = await Promise.all(dedupedList.map((item) => cache_default.tryGet(item.link, async () => { const $ = load(await rofetch(item.link)); if (item.link?.includes("/videos/")) { const ldJson = JSON.parse($("script[type=\"application/ld+json\"]:contains(\"VideoObject\")").text()); item.description = `<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube-nocookie.com/embed/${ldJson.embedUrl.match(/embed\/([\w-]+)/)?.[1]}" frameborder="0" allowfullscreen referrerpolicy="strict-origin-when-cross-origin"></iframe><br>` + ldJson.description.replaceAll("\n", "<br>"); item.pubDate = timezone(parseDate(ldJson.uploadDate), 8); item.image = ldJson.thumbnailUrl[0]; return item; } const ldJson = JSON.parse($("script[type=\"application/ld+json\"]:contains(\"NewsArticle\")").text()); const description = $(".rendered-content"); description.find(".show-in-md, .article-promote-items, [data-ad-part]").remove(); item.description = description.html(); item.pubDate = parseDate(ldJson.datePublished); item.category = [.../* @__PURE__ */ new Set([ldJson.articleSection, ...ldJson.keywords])]; item.author = ldJson.author?.name ?? ldJson.publisher?.name; return item; }))); return { title: $("title").text(), description: $("meta[name=\"description\"]").attr("content"), link, image: `${baseUrl}/favicon.ico`, language: "zh-TW", item: items }; } //#endregion export { route };