rsshub
Version:
Make RSS Great Again!
86 lines (84 loc) • 2.8 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./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 parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import path from "node:path";
import { load } from "cheerio";
//#region lib/routes/google/news.ts
init_esm_shims();
const baseUrl = "https://news.google.com";
const route = {
path: "/news/:category/:locale",
categories: ["new-media"],
example: "/google/news/Top stories/hl=en-US&gl=US&ceid=US:en",
parameters: {
category: "Category Title",
locale: "locales, could be found behind `?`, including `hl`, `gl`, and `ceid` as parameters"
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
name: "News",
maintainers: ["zoenglinghou", "pseudoyu"],
handler
};
async function handler(ctx) {
const category = ctx.req.param("category");
const locale = ctx.req.param("locale");
const categoryUrl = (await cache_default.tryGet(`google:news:${locale}`, async () => {
const $$1 = load(await ofetch_default(`${baseUrl}/?${locale}`));
return [...$$1("a.brSCsc").toArray().slice(3).map((item) => {
item = $$1(item);
return {
category: item.text(),
url: new URL(item.attr("href"), baseUrl).href
};
}), ...$$1("a.aqvwYd").toArray().map((item) => {
item = $$1(item);
return {
category: item.text(),
url: new URL(item.attr("href"), baseUrl).href
};
})];
})).find((item) => item.category === category).url;
const $ = load(await ofetch_default(categoryUrl));
const items = [
...$(".UwIKyb"),
...$(".IBr9hb"),
...$(".IFHyqb")
].map((item) => {
item = $(item);
const title = item.find(".gPFEn").text();
const authorText = item.find(".bInasb span").text();
const authors = authorText ? authorText.replace(/^By\s+/i, "").replaceAll(/\s+\([^)]*\)/g, "").split(/,|\s+&\s+|\s+and\s+/).map((author) => author.trim()).filter((author) => {
if (!author) return false;
return !["et al", "et al."].some((suffix) => author.toLowerCase().endsWith(suffix));
}).map((author) => ({ name: author })) : [];
return {
title,
description: art(path.join(__dirname, "templates/news-c158a6b5.art"), {
img: item.find("img.Quavad").attr("src"),
brief: title
}),
pubDate: parseDate(item.find("time").attr("datetime")),
author: authors,
link: new URL(item.find("a.WwrzSb").first().attr("href"), baseUrl).href
};
});
return {
title: $("title").text(),
link: categoryUrl,
item: items
};
}
//#endregion
export { route };