rsshub
Version:
Make RSS Great Again!
75 lines (73 loc) • 3.2 kB
JavaScript
import "./dist-Bog6z_OM.mjs";
import "./config-MQ-7ebJ_.mjs";
import "./logger-C4avouvV.mjs";
import { t as ofetch_default } from "./ofetch-DKl_Ma9g.mjs";
import "./parse-date-r5WDWHno.mjs";
import "./is-worker-DESPicPc.mjs";
import { t as cache_default } from "./cache-SV6W5EPy.mjs";
import { r as getSubPath } from "./common-utils-Cz5bFBus.mjs";
import { Fragment, jsx, jsxs } from "hono/jsx/jsx-runtime";
import { load } from "cheerio";
import { renderToString } from "hono/jsx/dom/server";
import { raw } from "hono/html";
//#region lib/routes/aljazeera/index.tsx
const languages = {
arabic: {
rootUrl: "https://www.aljazeera.net",
rssUrl: "rss"
},
chinese: {
rootUrl: "https://chinese.aljazeera.net",
rssUrl: void 0
},
english: {
rootUrl: "https://www.aljazeera.com",
rssUrl: "xml/rss/all.xml"
}
};
const renderDescription = (image, description) => renderToString(/* @__PURE__ */ jsxs(Fragment, { children: [image ? /* @__PURE__ */ jsx("figure", { children: /* @__PURE__ */ jsx(Fragment, { children: raw(image) }) }) : null, description ? /* @__PURE__ */ jsx(Fragment, { children: raw(description) }) : null] }));
const route = {
path: "*",
name: "Unknown",
maintainers: ["nczitzk"],
handler
};
async function handler(ctx) {
const params = getSubPath(ctx) === "/" ? ["arabic"] : getSubPath(ctx).replace(/^\//, "").split("/");
if (!Object.hasOwn(languages, params[0])) params.unshift("arabic");
const language = params.shift();
const isRSS = params.length === 1 && params.at(-1) === "rss" && languages[language].rssUrl;
const rootUrl = languages[language].rootUrl;
const currentUrl = `${rootUrl}/${isRSS ? languages[language].rssUrl : params.join("/")}`;
const response = await ofetch_default(currentUrl);
const $ = load(response);
let items = isRSS ? response.data.match(new RegExp("<link>" + rootUrl + "/(.*?)</link>", "g")).map((item) => ({ link: item.match(/<link>(.*?)<\/link>/)[1] })) : $(".u-clickable-card__link").toArray().map((item) => {
item = $(item);
return { link: `${rootUrl}${item.attr("href")}` };
});
items = await Promise.all(items.slice(0, ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit")) : 50).map((item) => cache_default.tryGet(item.link, async () => {
const detailResponse = await ofetch_default(item.link);
const content = load(detailResponse);
content(".more-on").parent().remove();
content(".responsive-image img").removeAttr("srcset");
let pubDate;
const datePublished = detailResponse.match(/"datePublished": ?"(.*?)",/);
if (datePublished && datePublished.length > 1) pubDate = detailResponse.match(/"datePublished": ?"(.*?)",/)[1];
else {
const uploadDate = detailResponse.match(/"uploadDate": ?"(.*?)",/)[1];
pubDate = uploadDate && uploadDate.length > 1 ? uploadDate : content("div.date-simple > span:nth-child(2)").text();
}
item.title = content("h1").first().text();
item.author = content(".author").text();
item.pubDate = pubDate;
item.description = renderDescription(content(".article-featured-image").html(), content(".wysiwyg").html());
return item;
})));
return {
title: $("title").first().text(),
link: currentUrl,
item: items
};
}
//#endregion
export { route };