From c1b21f196432ecb71e001c69f91b8f2236aee1e5 Mon Sep 17 00:00:00 2001 From: acanyo <30819792@qq.com> Date: Thu, 9 Jan 2025 11:19:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20=E6=96=B0=E5=A2=9E=20LinuxD?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- package.json | 1 + src/router.types.d.ts | 8 ++++++ src/routes/linuxdo.ts | 57 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/routes/linuxdo.ts diff --git a/.gitignore b/.gitignore index 74de06a..f1c0a74 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ dist *.ntvs* *.njsproj *.sln -*.sw? \ No newline at end of file +*.sw? +.nvmrc diff --git a/package.json b/package.json index 06f84f1..07d3242 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ }, "devDependencies": { "@eslint/js": "^9.16.0", + "@types/cheerio": "^0.22.35", "@types/md5": "^2.3.5", "@types/node": "^22.10.1", "@types/user-agents": "^1.0.4", diff --git a/src/router.types.d.ts b/src/router.types.d.ts index a00e232..720daab 100644 --- a/src/router.types.d.ts +++ b/src/router.types.d.ts @@ -377,4 +377,12 @@ export type RouterType = { }[]; }; }; + linuxdo: { + id: string; + title: string; + url: string; + author: string; + desc: string; + timestamp: string; + }; }; diff --git a/src/routes/linuxdo.ts b/src/routes/linuxdo.ts new file mode 100644 index 0000000..cab1a87 --- /dev/null +++ b/src/routes/linuxdo.ts @@ -0,0 +1,57 @@ +import type { RouterData } from "../types.js"; +import { get } from "../utils/getData.js"; +import { getTime } from "../utils/getTime.js"; + +interface Topic { + id: number; + title: string; + excerpt: string; + last_poster_username: string; + created_at: string; + views: number; + like_count: number; +} + +export const handleRoute = async (_: undefined, noCache: boolean) => { + const listData = await getList(noCache); + const routeData: RouterData = { + name: "linuxdo", + title: "Linux.do", + type: "热门文章", + description: "Linux 技术社区热搜", + link: "https://linux.do/hot", + total: listData.data?.length || 0, + ...listData, + }; + return routeData; +}; + +const getList = async (noCache: boolean) => { + const url = "https://linux.do/top/weekly.json"; + const result = await get({ + url, + noCache, + headers: { + "Accept": "application/json", + } + }); + + const topics = result.data.topic_list.topics as Topic[]; + const list = topics.map((topic) => { + return { + id: topic.id, + title: topic.title, + desc: topic.excerpt, + author: topic.last_poster_username, + timestamp: getTime(topic.created_at), + url: `https://linux.do/t/${topic.id}`, + mobileUrl: `https://linux.do/t/${topic.id}`, + hot: topic.views || topic.like_count + }; + }); + + return { + ...result, + data: list + }; +}; \ No newline at end of file