UNPKG

rsshub

Version:
89 lines (87 loc) 4.04 kB
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.mjs"; import { t as config } from "./config-C37vj7VH.mjs"; import { t as parseDate } from "./parse-date-BrP7mxXf.mjs"; import { t as art } from "./render-BQo6B4tL.mjs"; import { t as got_default } from "./got-KxxWdaxq.mjs"; import path from "node:path"; import { load } from "cheerio"; //#region lib/routes/yahoo/news/utils.ts init_esm_shims(); const getArchive = async (region, limit, tag, providerId) => { const { data: response } = await got_default(`https://${region}.news.yahoo.com/_td-news/api/resource/NCPListService;api=archive;ncpParams=${encodeURIComponent(JSON.stringify({ query: { count: limit, start: 0, providerid: providerId, tag } }))}`); return response; }; const getList = async (region, listId) => { const { data: response } = await got_default(`https://${region}.news.yahoo.com/_td-news/api/resource/StreamService;category=LISTID%3A${listId};useNCP=true`); return response; }; const getCategories = (region, tryGet) => tryGet(`yahoo:${region}:categoryMap`, async () => { const { PageStore } = await getStores(region, tryGet); const { Col1: col1 } = PageStore.pagesConfigRaw.base.section.regions; const { categoryMap } = col1.find((c) => c.name === "ArchiveFilterBar").props; for (const [key, value] of Object.entries(categoryMap)) categoryMap[key] = { name: value, yctMap: col1.find((c) => c.name === "StreamContainerArchive").props.yctMap[key] }; return categoryMap; }); const getProviderList = (region, tryGet) => tryGet(`yahoo:${region}:providerList`, async () => { const { ProviderListStore } = await getStores(region, tryGet); return ProviderListStore.providerList.flatMap((list) => list.providers.map((provider) => ({ title: `${list.title} - ${provider.title}`, key: provider.key, link: new URL(provider.url, `https://${region}.news.yahoo.com`).href }))); }); const getStores = (region, tryGet) => tryGet(`yahoo:${region}:stores`, async () => { const { data: response } = await got_default(`https://${region}.news.yahoo.com/archive`); const $ = load(response); return JSON.parse($("script:contains(\"root.App.main\")").text().match(/root.App.main\s+=\s+({.+});/)?.[1]).context.dispatcher.stores; }); const parseList = (region, response) => response.map((item) => ({ title: item.title, link: item.url.startsWith("http") ? item.url : new URL(item.url, `https://${region}.news.yahoo.com`).href, description: item.summary, pubDate: parseDate(item.published_at, "X") })); const parseItem = (item, tryGet) => tryGet(item.link, async () => { const { data: response } = await got_default(item.link, { headers: { "User-Agent": config.trueUA } }); const $ = load(response); const ldJson = JSON.parse($("script[type=\"application/ld+json\"]").toArray().find((ele) => $(ele).text().includes("\"@type\":\"NewsArticle\""))?.children[0].data); const author = ldJson.author.name; const body = $(".atoms"); body.find("noscript, .text-gandalf, [id^=\"sda-inbody-\"]").remove(); body.find(".caas-figure-with-pb, .caas-img-container").each((_, ele) => { $(ele).removeAttr("style"); }); body.find("img").each((_, ele) => { const $ele = $(ele); let dataSrc = $ele.data("src"); if (dataSrc) { const match = dataSrc.match(/.*--\/.*--\/(.*)/); if (match?.[1]) dataSrc = match?.[1]; $ele.attr("src", dataSrc); $ele.removeAttr("data-src"); } }); body.find(".caas-iframe").each((_, ele) => { const $ele = $(ele); if ($ele.data("type") === "youtube") { const blockquoteSrc = $ele.find("blockquote").data("src"); $ele.replaceWith(art(path.join(__dirname, "templates/youtube-c5469910.art"), { id: blockquoteSrc.split("/").pop()?.split("?")?.[0] })); } }); item.description = body.html(); item.author = author; item.category = ldJson.keywords; item.pubDate = parseDate(ldJson.datePublished); item.updated = parseDate(ldJson.dateModified); return item; }); //#endregion export { parseItem as a, getProviderList as i, getCategories as n, parseList as o, getList as r, getArchive as t };