feat: 新增 虎嗅 & 爱范儿

This commit is contained in:
imsyy
2024-05-16 18:10:34 +08:00
parent cdf2479044
commit 679fdab87e
5 changed files with 110 additions and 3 deletions

51
src/routes/huxiu.ts Normal file
View File

@@ -0,0 +1,51 @@
import type { RouterData } from "../types.js";
import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
export const handleRoute = async (_: undefined, noCache: boolean) => {
const { fromCache, data, updateTime } = await getList(noCache);
const routeData: RouterData = {
name: "huxiu",
title: "虎嗅",
type: "24小时",
link: "https://www.huxiu.com/moment/",
total: data?.length || 0,
updateTime,
fromCache,
data,
};
return routeData;
};
// 标题处理
const titleProcessing = (text: string) => {
const paragraphs = text.split("<br><br>");
const title = paragraphs.shift().replace(/。$/, "");
const intro = paragraphs.join("<br><br>");
return { title, intro };
};
const getList = async (noCache: boolean) => {
const url = `https://www.huxiu.com/moment/`;
const result = await get({
url,
noCache,
});
// 正则查找
const pattern =
/<script>[\s\S]*?window\.__INITIAL_STATE__\s*=\s*(\{[\s\S]*?\});[\s\S]*?<\/script>/;
const matchResult = result.data.match(pattern);
const jsonObject = JSON.parse(matchResult[1]).moment.momentList.moment_list.datalist;
return {
fromCache: result.fromCache,
updateTime: result.updateTime,
data: jsonObject.map((v: RouterType["huxiu"]) => ({
id: v.object_id,
title: titleProcessing(v.content).title,
desc: titleProcessing(v.content).intro,
author: v.user_info.username,
url: v.url || "https://www.huxiu.com/moment/",
mobileUrl: v.url || "https://m.huxiu.com/moment/",
})),
};
};