mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
🦄 refactor: Refactoring using hono
This commit is contained in:
57
src/routes/jianshu.ts
Normal file
57
src/routes/jianshu.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import { load } from "cheerio";
|
||||
import { get } from "../utils/getData.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const { fromCache, data, updateTime } = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "jianshu",
|
||||
title: "简书",
|
||||
type: "热门推荐",
|
||||
description: "一个优质的创作社区",
|
||||
link: "https://www.jianshu.com/",
|
||||
total: data?.length || 0,
|
||||
updateTime,
|
||||
fromCache,
|
||||
data,
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
// 获取 ID
|
||||
const getID = (url: string) => {
|
||||
if (!url) return "undefined";
|
||||
const match = url.match(/([^/]+)$/);
|
||||
return match ? match[1] : "undefined";
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://www.jianshu.com/`;
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
headers: {
|
||||
Referer: "https://www.jianshu.com",
|
||||
},
|
||||
});
|
||||
const $ = load(result.data);
|
||||
const listDom = $("ul.note-list li");
|
||||
const listData = listDom.toArray().map((item) => {
|
||||
const dom = $(item);
|
||||
const href = dom.find("a").attr("href") || "";
|
||||
return {
|
||||
id: getID(href),
|
||||
title: dom.find("a.title").text()?.trim(),
|
||||
cover: dom.find("img").attr("src"),
|
||||
desc: dom.find("p.abstract").text()?.trim(),
|
||||
author: dom.find("a.nickname").text()?.trim(),
|
||||
url: `https://www.jianshu.com${href}`,
|
||||
mobileUrl: `https://www.jianshu.com${href}`,
|
||||
};
|
||||
});
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
data: listData,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user