rsshub
Version:
Make RSS Great Again!
74 lines (72 loc) • 2.96 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 "./ofetch-BIyrKU3Y.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import "./helpers-DxBp0Pty.mjs";
import { t as art } from "./render-BQo6B4tL.mjs";
import { t as got_default } from "./got-KxxWdaxq.mjs";
import { t as timezone } from "./timezone-D8cuwzTY.mjs";
import { t as invalid_parameter_default } from "./invalid-parameter-rr4AgGpp.mjs";
import { t as isValidHost } from "./valid-host-C-u5eW3j.mjs";
import path from "node:path";
import { load } from "cheerio";
//#region lib/routes/m4/index.ts
init_esm_shims();
const route = {
path: "/:id?/:category{.+}?",
name: "Unknown",
maintainers: [],
handler
};
async function handler(ctx) {
const { id = "news", category = "china" } = ctx.req.param();
if (!isValidHost(id)) throw new invalid_parameter_default("Invalid id");
const limit = ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit"), 10) : 30;
const rootUrl = `http://${id}.m4.cn`;
const currentUrl = new URL(category ? `/${category.replace(/\/$/, "")}/` : "/", rootUrl).href;
const { data: response } = await got_default(currentUrl);
const $ = load(response);
let items = $("div.articleitem0 div.aheader0").slice(0, limit).toArray().map((item) => {
item = $(item);
const a = item.find("a").first();
return {
title: a.text(),
link: a.prop("href"),
description: art(path.join(__dirname, "templates/description-881baa33.art"), { images: [{
src: item.parent().find("div.aimg0 a img").prop("src"),
alt: a.text()
}] }),
category: item.find("a.aclass").toArray().map((c) => $(c).text().replaceAll("[]", "").trim()),
pubDate: timezone(parseDate(item.find("span.atime").text()), 8)
};
});
items = await Promise.all(items.map((item) => cache_default.tryGet(item.link, async () => {
const { data: detailResponse } = await got_default(item.link);
const content = load(detailResponse);
item.title = content("h1").first().text();
item.description = art(path.join(__dirname, "templates/description-881baa33.art"), {
intro: content("div.aintro1, p.cont-summary").text(),
description: content("div.content0, div.cont-detail").html()
});
item.category = content("span.dd0 a, a[rel=\"category\"]").toArray().map((c) => content(c).text()).slice(1);
item.pubDate = timezone(parseDate(content("span.atime1, span.post-time").text()), 8);
return item;
})));
const image = $("a.logo0_b img").prop("src");
return {
item: items,
title: $("title").text(),
link: currentUrl,
description: $("meta[name=\"description\"]").prop("content"),
language: "zh",
image,
subtitle: $("meta[name=\"keywords\"]").prop("content"),
author: $("meta[name=\"author\"]").prop("content"),
allowEmpty: true
};
}
//#endregion
export { route };