rsshub
Version:
Make RSS Great Again!
116 lines (111 loc) • 4.14 kB
JavaScript
import "./esm-shims-CzJ_djXG.mjs";
import "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import "./logger-Czu8UMNd.mjs";
import { t as ofetch_default } from "./ofetch-BIyrKU3Y.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/bbc/utils.ts
const ProcessFeed = ($) => {
let content = $("#main-content article");
if (content.length === 0) content = $("div.story-body");
if (content.length === 0) content = $("main[role=\"main\"]");
content.find("header, section, [data-testid=\"bbc-logo-wrapper\"]").remove();
content.find("noscript").each((i, e) => {
$(e).parent().html($(e).html());
});
content.find("img").each((i, e) => {
if (!$(e).attr("src") && $(e).attr("srcSet")) {
const lastSrc = $(e).attr("srcSet").split(", ").at(-1);
$(e).attr("src", lastSrc.split(" ")[0]);
}
});
content.find("[data-component=\"media-block\"] figcaption").prepend("<span>View video in browser: </span>");
return content.html();
};
var utils_default = { ProcessFeed };
//#endregion
//#region lib/routes/bbc/index.ts
const route = {
path: "/:site?/:channel?",
name: "News",
maintainers: [
"HenryQW",
"DIYgod",
"pseudoyu"
],
handler,
example: "/bbc/world-asia",
parameters: {
site: "语言,简体或繁体中文",
channel: "channel, default to `top stories`"
},
categories: ["traditional-media"],
description: `Provides a better reading experience (full text articles) over the official ones.
Support major channels, refer to [BBC RSS feeds](https://www.bbc.co.uk/news/10628494). Eg, \`business\` for \`https://feeds.bbci.co.uk/news/business/rss.xml\`.
- Channel contains sub-directories, such as \`https://feeds.bbci.co.uk/news/world/asia/rss.xml\`, replace \`/\` with \`-\`, \`/bbc/world-asia\`.`
};
async function handler(ctx) {
let feed, title, link;
const { site, channel } = ctx.req.param();
if (site) switch (site.toLowerCase()) {
case "chinese":
title = "BBC News 中文网";
feed = await (channel ? rss_parser_default.parseURL(`https://www.bbc.co.uk/zhongwen/simp/${channel}/index.xml`) : rss_parser_default.parseURL("https://www.bbc.co.uk/zhongwen/simp/index.xml"));
break;
case "traditionalchinese":
title = "BBC News 中文網";
feed = await (channel ? rss_parser_default.parseURL(`https://www.bbc.co.uk/zhongwen/trad/${channel}/index.xml`) : rss_parser_default.parseURL("https://www.bbc.co.uk/zhongwen/trad/index.xml"));
link = "https://www.bbc.com/zhongwen/trad";
break;
default:
feed = await rss_parser_default.parseURL(`https://feeds.bbci.co.uk/news/${site.split("-").join("/")}/rss.xml`);
title = `BBC News ${site}`;
link = `https://www.bbc.co.uk/news/${site.split("-").join("/")}`;
break;
}
else {
feed = await rss_parser_default.parseURL("https://feeds.bbci.co.uk/news/rss.xml");
title = "BBC News Top Stories";
link = "https://www.bbc.co.uk/news";
}
const items = await Promise.all(feed.items.filter((item) => item && item.link).map((item) => cache_default.tryGet(item.link, async () => {
try {
const linkURL = new URL(item.link);
if (linkURL.hostname === "www.bbc.com") linkURL.hostname = "www.bbc.co.uk";
const $ = load(await ofetch_default(linkURL.href, { retryStatusCodes: [403] }));
const path = linkURL.pathname;
let description;
switch (true) {
case path.startsWith("/sport"):
description = item.content;
break;
case path.startsWith("/sounds/play"):
description = item.content;
break;
case path.startsWith("/news/live"):
description = item.content;
break;
default: description = utils_default.ProcessFeed($);
}
return {
title: item.title || "",
description: description || "",
pubDate: item.pubDate || (/* @__PURE__ */ new Date()).toUTCString(),
link: item.link
};
} catch {
return {};
}
})));
return {
title,
link,
image: "https://www.bbc.com/favicon.ico",
description: title,
item: items.filter((item) => Object.keys(item).length > 0)
};
}
//#endregion
export { route };