UNPKG

rsshub

Version:
60 lines (59 loc) 2.43 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 { load } from "cheerio"; //#region lib/routes/apnews/utils.ts function removeDuplicateByKey(items, key) { return new Map(items.map((x) => [x[key], x])).values().toArray(); } function fetchArticle(item) { return cache_default.tryGet(item.link, async () => { const $ = load(await rofetch(item.link)); if ($("#link-ld-json").length === 0) { const gtmRaw = $("meta[name=\"gtm-dataLayer\"]").attr("content"); if (gtmRaw) { const gtmParsed = JSON.parse(gtmRaw); return { title: gtmParsed.headline, pubDate: parseDate(gtmParsed.publication_date), description: $("div.RichTextStoryBody").html() || $(":is(.VideoLead, .VideoPage-pageSubHeading)").html(), category: gtmParsed.tag_array.split(","), guid: $("meta[name='brightspot.contentId']").attr("content"), author: gtmParsed.author, ...item }; } return item; } const rawLdjson = JSON.parse($("#link-ld-json").text()); let ldjson; if (rawLdjson["@type"] === "NewsArticle" || Array.isArray(rawLdjson) && rawLdjson.some((e) => e["@type"] === "NewsArticle")) { ldjson = Array.isArray(rawLdjson) ? rawLdjson.find((e) => e["@type"] === "NewsArticle") : rawLdjson; $("div.Enhancement").remove(); const section = $("meta[property='article:section']").attr("content"); return { ...item, title: ldjson.headline, pubDate: parseDate(ldjson.datePublished), updated: parseDate(ldjson.dateModified), description: $("div.RichTextStoryBody").html() || $(":is(.VideoLead, .VideoPage-pageSubHeading)").html(), category: [...section ? [section] : [], ...ldjson.keywords ?? []], guid: $("meta[name='brightspot.contentId']").attr("content"), author: ldjson.author }; } ldjson = rawLdjson; const url = new URL(item.link); const description = url.hash ? $(url.hash).parent().find(".LiveBlogPost-body").html() : ldjson.description; const pubDate = url.hash ? parseDate(Number($(url.hash).parent().attr("data-posted-date-timestamp"))) : parseDate(ldjson.coverageStartTime); return { ...item, category: ldjson.keywords, pubDate, description, guid: $("meta[name='brightspot.contentId']").attr("content") }; }); } //#endregion export { removeDuplicateByKey as n, fetchArticle as t };