UNPKG

rsshub

Version:
106 lines (104 loc) 4.43 kB
import { n as init_esm_shims, t as __dirname } from "./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 art } from "./render-BQo6B4tL.mjs"; import { t as got_default } from "./got-KxxWdaxq.mjs"; import { t as timezone } from "./timezone-D8cuwzTY.mjs"; import path from "node:path"; import { load } from "cheerio"; //#region lib/routes/udn/breaking-news.ts init_esm_shims(); const route = { path: "/news/breakingnews/:id", categories: ["traditional-media"], example: "/udn/news/breakingnews/99", parameters: { id: "类别" }, features: { requireConfig: false, requirePuppeteer: false, antiCrawler: false, supportBT: false, supportPodcast: false, supportScihub: false }, radar: [{ source: ["udn.com/news/breaknews/1/:id", "udn.com/"] }], name: "即時新聞", maintainers: ["miles170"], handler, description: `| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 11 | 12 | 13 | 99 | | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ------ | | 精選 | 要聞 | 社會 | 地方 | 兩岸 | 國際 | 財經 | 運動 | 娛樂 | 生活 | 股市 | 文教 | 數位 | 不分類 |` }; async function handler(ctx) { const id = ctx.req.param("id"); const link = `/news/breaknews/1/${id}#breaknews`; const name = await getLinkName(link); const rootUrl = "https://udn.com"; const response = await got_default(`${rootUrl}/api/more?page=1&channelId=1&cate_id=${id}&type=breaknews`); const items = await Promise.all(response.data.lists.map((item) => { let link$1 = item.titleLink.startsWith("http") ? item.titleLink : `${rootUrl}${item.titleLink}`; const linkUrl = new URL(link$1); linkUrl.query = linkUrl.search = ""; link$1 = linkUrl.toString(); return cache_default.tryGet(link$1, async () => { let result = await got_default(link$1); const vip = result.data.match(/<script language=javascript>window\.location\.href="(https?:\/\/[^"]+)"/); if (vip) result = await got_default(vip[1]); const $ = load(result.data); const metadata = $("script[type=\"application/ld+json\"]").eq(0).text().trim().replaceAll(/[\b\t\n]/g, ""); const data = metadata.startsWith("[") ? JSON.parse(metadata)[0] : JSON.parse(metadata); const content = $(".article-content__editor"); const body = $(".article-body__editor"); let description = ""; if (data.image) description += art(path.join(__dirname, "templates/figure-a45dec39.art"), { src: data.image.contentUrl, alt: data.image.name }); if (content.length) description += content.html(); else if (body.length) description += body.html(); if (data.publisher.name.includes("轉角國際 udn Global")) description = $(".story_body_content").html().split(/<!--\d+?-->/g).slice(1, -1).join(""); return { title: item.title, author: [ { name: $(".article-content__author").text().match("中央社")?.at(0) }, { name: data.publisher.name.match("轉角國際 udn Global")?.at(0) }, data.author ].filter((e) => Boolean(e.name)), description, pubDate: timezone(parseDate(item.time.date, "YYYY-MM-DD HH:mm"), 8), category: [ data.articleSection, vip ? $(".article-head li.breadcrumb__item:last > b").text() : $("meta[name='subsection']").attr("content"), ...data.keywords.split(",") ], link: link$1 }; }); })); return { title: `即時${name} - 聯合新聞網`, link: `${rootUrl}${link}`, description: "udn.com 提供即時新聞以及豐富的政治、社會、地方、兩岸、國際、財經、數位、運動、NBA、娛樂、生活、健康、旅遊新聞,以最即時、多元的內容,滿足行動世代的需求", item: items }; } const getLinkName = async (link) => { const url = "https://udn.com/news/breaknews"; const links = await cache_default.tryGet(url, async () => { const $ = load((await got_default(url)).data); const data = $(".cate-list__subheader a").toArray().map((item) => { item = $(item); return [item.attr("href"), item.text().trim()]; }); return Object.fromEntries(data); }); if (link in links) return links[link]; return "列表"; }; //#endregion export { route };