rsshub
Version:
Make RSS Great Again!
81 lines (78 loc) • 2.81 kB
JavaScript
import "./esm-shims-CzJ_djXG.mjs";
import "./config-C37vj7VH.mjs";
import "./dist-BInvbO1W.mjs";
import { t as logger_default } from "./logger-Czu8UMNd.mjs";
import { t as ofetch_default } from "./ofetch-BIyrKU3Y.mjs";
import { t as parseDate } from "./parse-date-BrP7mxXf.mjs";
import { t as md5 } from "./md5-C8GRvctM.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import "./helpers-DxBp0Pty.mjs";
import { t as got_default } from "./got-KxxWdaxq.mjs";
import { load } from "cheerio";
//#region lib/routes/51cto/utils.ts
const getToken = () => cache_default.tryGet("51cto:token", async () => {
return (await ofetch_default("https://api-media.51cto.com/api/token-get")).data.data.token;
}, 3600, false);
const sign = (requestPath, payload = {}, timestamp, token) => {
payload.timestamp = timestamp;
payload.token = token;
const sortedParams = Object.keys(payload).toSorted();
return md5(md5(requestPath) + md5(sortedParams + md5(token) + timestamp));
};
//#endregion
//#region lib/routes/51cto/recommend.ts
const route = {
path: "/index/recommend",
categories: ["programming"],
example: "/51cto/index/recommend",
radar: [{ source: ["51cto.com/"] }],
name: "推荐",
maintainers: ["cnkmmk", "ovo-tim"],
handler,
url: "51cto.com/"
};
const pattern = /'(WTKkN|bOYDu|wyeCN)':\s*(\d+)/g;
async function getFullcontent(item, cookie = "") {
let fullContent = null;
const articleResponse = await ofetch_default(item.url, { headers: { cookie } });
const $ = load(articleResponse);
fullContent = new URL(item.url).host === "ost.51cto.com" ? $(".posts-content").html() : $("article").html();
if (!fullContent && cookie === "") try {
const [v1, v2, v3] = articleResponse.match(pattern).slice(0, 3).map((str) => Number(str.split(":")[1]));
return await getFullcontent(item, "__tst_status=" + (v1 + v2 + v3) + "#;");
} catch (error) {
logger_default.error(error);
}
return {
title: item.title,
link: item.url,
pubDate: parseDate(item.pubdate, 8),
description: fullContent || item.abstract
};
}
async function handler(ctx) {
const url = "https://api-media.51cto.com";
const requestPath = "index/index/recommend";
const token = await getToken();
const timestamp = Date.now();
const params = {
page: 1,
page_size: ctx.req.query("limit") ? Number.parseInt(ctx.req.query("limit"), 10) : 50,
limit_time: 0,
name_en: ""
};
const list = (await got_default(`${url}/${requestPath}`, { searchParams: {
...params,
timestamp,
token,
sign: sign(requestPath, params, timestamp, token)
} })).data.data.data.list;
return {
title: "51CTO",
link: "https://www.51cto.com/",
description: "51cto - 推荐",
item: await Promise.all(list.map((item) => cache_default.tryGet(item.url, async () => await getFullcontent(item))))
};
}
//#endregion
export { route };