Skip to content

Commit 755d4e2

Browse files
authored
Merge pull request #114 from Zeno2019/master
feat: 添加游资网(gameres)路由,并支持处理“X小时前”的时间格式
2 parents 259dd2c + 72d6d97 commit 755d4e2

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/router.types.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,4 +431,14 @@ export type RouterType = {
431431
name: string;
432432
};
433433
};
434+
gameres: {
435+
id: string;
436+
title: string;
437+
hot: number | undefined;
438+
desc: string;
439+
cover: string;
440+
timestamp: number | undefined;
441+
url: string;
442+
mobileUrl: string;
443+
};
434444
};

src/routes/gameres.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import type { RouterData } from "../types.js";
2+
import { load } from "cheerio";
3+
import { get } from "../utils/getData.js";
4+
import { getTime } from "../utils/getTime.js";
5+
import { RouterType } from "../router.types.js";
6+
7+
export const handleRoute = async (_: undefined, noCache: boolean) => {
8+
const listData = await getList(noCache);
9+
10+
const routeData: RouterData = {
11+
name: "gameres",
12+
title: "GameRes 游资网",
13+
type: "最新资讯",
14+
description:
15+
"面向游戏从业者的游戏开发资讯,旨在为游戏制作人提供游戏研发类的程序技术、策划设计、艺术设计、原创设计等资讯内容。",
16+
link: "https://www.gameres.com",
17+
total: listData.data?.length || 0,
18+
...listData,
19+
};
20+
21+
return routeData;
22+
};
23+
24+
const getList = async (noCache: boolean) => {
25+
const url = `https://www.gameres.com`;
26+
const result = await get({ url, noCache });
27+
const $ = load(result.data);
28+
29+
const container = $('div[data-news-pane-id="100000"]');
30+
const listDom = container.find("article.feed-item");
31+
32+
const listData = Array.from(listDom).map((el) => {
33+
const dom = $(el);
34+
35+
const titleEl = dom.find(".feed-item-title-a").first();
36+
const title = titleEl.text().trim();
37+
38+
const href = titleEl.attr("href");
39+
const url = href?.startsWith("http") ? href : `https://www.gameres.com${href ?? ""}`;
40+
41+
const cover = dom.find(".thumb").attr("data-original") || "";
42+
const desc = dom.find(".feed-item-right > p").first().text().trim();
43+
44+
const dateTime = dom.find(".mark-info").contents().first().text().trim();
45+
const timestamp = getTime(dateTime);
46+
47+
// 热度(列表暂无评论数)
48+
const hot = undefined;
49+
50+
return {
51+
title,
52+
desc,
53+
cover,
54+
timestamp,
55+
hot,
56+
url,
57+
id: url,
58+
mobileUrl: url,
59+
} as RouterType["gameres"];
60+
});
61+
62+
return {
63+
...result,
64+
data: listData,
65+
};
66+
};

src/utils/getTime.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ export const getTime = (timeInput: string | number): number | undefined => {
9090
.valueOf();
9191
}
9292

93+
// 处理 `N 小时前` 的时间格式
94+
if (//.test(timeInput)) {
95+
const hoursAgo = parseInt(timeInput.replace("小时前", ""));
96+
return dayjs().subtract(hoursAgo, "hour").valueOf();
97+
}
98+
9399
if (//.test(timeInput)) {
94100
const minutesAgo = parseInt(timeInput.replace("分钟前", ""));
95101
return dayjs().subtract(minutesAgo, "minute").valueOf();

0 commit comments

Comments
 (0)