rsshub
Version:
Make RSS Great Again!
94 lines (92 loc) • 3.29 kB
JavaScript
import "./esm-shims-CzJ_djXG.mjs";
import { t as config } from "./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 { t as rss_parser_default } from "./rss-parser-Dtop7M8f.mjs";
import { load } from "cheerio";
//#region lib/routes/blockworks/index.ts
const route = {
path: "/",
categories: ["finance"],
example: "/blockworks",
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
radar: [{
source: ["blockworks.co/"],
target: "/"
}],
name: "News",
maintainers: ["pseudoyu"],
handler,
description: "Blockworks news with full text support."
};
async function handler(ctx) {
const feed = await rss_parser_default.parseURL("https://blockworks.co/feed");
const limit = ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit"), 10) : 20;
const limitedItems = feed.items.slice(0, limit);
const buildId = await getBuildId();
const items = await Promise.all(limitedItems.map((item) => ({
...item,
link: item.link?.split("?")[0]
})).map((item) => cache_default.tryGet(item.link, async () => {
const content = await extractFullText(item.link.split("/").pop(), buildId);
return {
title: item.title || "Untitled",
pubDate: item.isoDate ? parseDate(item.isoDate) : void 0,
link: item.link,
description: content.description || item.content || item.contentSnippet || item.summary || "",
author: item.author,
category: content.category,
media: content.imageUrl ? { content: { url: content.imageUrl } } : void 0
};
})));
return {
title: feed.title || "Blockworks News",
link: feed.link || "https://blockworks.co",
description: feed.description || "Latest news from Blockworks",
item: items,
language: feed.language || "en"
};
}
async function extractFullText(slug, buildId) {
try {
const article = (await ofetch_default(`https://blockworks.co/_next/data/${buildId}/news/${slug}.json?slug=${slug}`)).pageProps.article;
const $ = load(article.content, null, false);
$("hr").remove();
$("p > em, p > strong").each((_, el) => {
const $el = $(el);
if ($el.text().includes("To read full editions") || $el.text().includes("Get the news in your inbox")) $el.parent().remove();
});
$("ul.wp-block-list > li > a").each((_, el) => {
const $el = $(el);
if ($el.attr("href") === "https://blockworks.co/newsletter/daily") $el.parent().parent().remove();
});
return {
description: $.html(),
imageUrl: article.imageUrl,
category: [...new Set([...article.categories, ...article.tags])]
};
} catch (error) {
logger_default.error("Error extracting full text from Blockworks:", error);
return {
description: "",
imageUrl: "",
category: []
};
}
}
const getBuildId = () => cache_default.tryGet("blockworks:buildId", async () => {
return load(await ofetch_default("https://blockworks.co"))("script#__NEXT_DATA__").text()?.match(/"buildId":"(.*?)",/)?.[1] || "";
}, config.cache.routeExpire, false);
//#endregion
export { route };