rsshub
Version:
Make RSS Great Again!
82 lines (80 loc) • 2.99 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 path from "node:path";
import { load } from "cheerio";
//#region lib/routes/kantarworldpanel/index.ts
init_esm_shims();
const route = {
path: "/:region?/:category{.+}?",
name: "Unknown",
maintainers: [],
handler
};
async function handler(ctx) {
const { region = "cn-en", category = "news" } = ctx.req.param();
const limit = ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit"), 10) : 30;
const rootUrl = "https://www.kantarworldpanel.com/";
const currentUrl = new URL(`${region}/${category}`, rootUrl).href;
const { data: response } = await got_default(currentUrl);
const $ = load(response);
let items = $("ul.slide, #newssource").find("li").slice(0, limit).toArray().map((item) => {
item = $(item);
const a = item.find("a");
const image = item.find("img");
const title = item.find("h3.mediumFont").text().trim();
let link = a.prop("href");
link = link === "#" ? currentUrl : link;
return {
title,
link,
description: art(path.join(__dirname, "templates/description-742410ab.art"), {
description: item.find("p.gowhite").text(),
image: image.prop("src") ? {
src: image.prop("src"),
alt: image.prop("alt")
} : void 0
}),
guid: link.startsWith(rootUrl) ? `${link}#${title}` : link,
pubDate: parseDate(item.find("p.medGrey").text(), "DD/MM/YYYY")
};
});
items = await Promise.all(items.map((item) => cache_default.tryGet(item.link, async () => {
if (item.link === currentUrl || !item.link.startsWith(rootUrl)) return item;
else if (/dwl\.php/.test(item.link)) {
item.enclosure_url = item.link;
item.enclosure_type = "application/pdf";
return item;
}
const detailResponse = await got_default(item.link);
if (!detailResponse.url.startsWith(rootUrl)) return item;
const content = load(detailResponse.data);
item.title = content("h1.newshead").text();
item.description = content("div.center-content div.left-layout-col").html();
item.category = content("meta[name=\"keywords\"]").prop("content")?.split(/,\s?/) ?? [];
item.pubDate = parseDate(content("p.newsdateshare").text(), "DD/MM/YYYY");
return item;
})));
const icon = new URL("favicon.ico", rootUrl).href;
return {
item: items,
title: $("title").text(),
link: currentUrl,
description: $("meta[name=\"description\"]").prop("content"),
language: $("html").prop("lang"),
image: $("#logoprint img").prop("src"),
icon,
logo: icon,
subtitle: $("meta[name=\"keywords\"]").prop("content"),
allowEmpty: true
};
}
//#endregion
export { route };