rsshub
Version:
Make RSS Great Again!
73 lines (71 loc) • 3.03 kB
JavaScript
import { n as init_esm_shims, t as __dirname } from "./esm-shims-CzJ_djXG.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/jiemian/common.ts
init_esm_shims();
const handler = async (ctx) => {
const { category = "" } = ctx.req.param();
const limit = ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit"), 10) : 50;
const rootUrl = "https://www.jiemian.com";
const currentUrl = new URL(category ? `${category}.html` : "", rootUrl).href;
const $ = load(await ofetch_default(currentUrl));
let items = {};
const links = $("a").toArray();
for (const el of links) {
const item = $(el);
const href = item.prop("href");
const link = href ? href.startsWith("/") ? new URL(href, rootUrl).href : href : void 0;
if (link && /\/(article|video)\/\w+\.html/.test(link)) items[link] = {
title: item.text(),
link
};
}
items = await Promise.all(Object.values(items).slice(0, limit).map((item) => cache_default.tryGet(item.link, async () => {
const content = load(await ofetch_default(item.link));
const image$1 = content("div.article-img img").first();
const video = content("#video-player").first();
item.title = content("div.article-header h1").eq(0).text();
item.description = art(path.join(__dirname, "templates/description-55895928.art"), {
image: image$1 ? {
src: image$1.prop("src"),
alt: image$1.next("p").text() || item.title
} : void 0,
video: video ? {
src: video.prop("data-url"),
poster: video.prop("data-poster"),
width: video.prop("width"),
height: video.prop("height")
} : void 0,
intro: content("div.article-header p").text(),
description: content("div.article-content").html()
});
item.author = content("span.author").first().find("a").toArray().map((a) => content(a).text()).join("/");
item.category = content("meta.meta-container a").toArray().map((c) => content(c).text());
item.pubDate = parseDate(content("div.article-info span[data-article-publish-time]").prop("data-article-publish-time"), "X");
item.upvotes = content("span.opt-praise__count").text() ? Number.parseInt(content("span.opt-praise__count").text(), 10) : 0;
item.comments = content("span.opt-comment__count").text() ? Number.parseInt(content("span.opt-comment__count").text(), 10) : 0;
return item;
})));
const title = $("title").text();
const titleSplits = title.split(/_/);
const image = $("div.logo img").prop("src");
const icon = new URL($("link[rel=\"icon\"]").prop("href"), rootUrl).href;
return {
item: items,
title,
link: currentUrl,
description: $("meta[name=\"description\"]").prop("content"),
language: $("html").prop("lang"),
image,
icon,
logo: icon,
subtitle: titleSplits[0],
author: titleSplits.pop()
};
};
//#endregion
export { handler as t };