diff --git a/src/router.types.d.ts b/src/router.types.d.ts index 4f727f6..bed6b6b 100644 --- a/src/router.types.d.ts +++ b/src/router.types.d.ts @@ -431,4 +431,14 @@ export type RouterType = { name: string; }; }; + gameres: { + id: string; + title: string; + hot: number | undefined; + desc: string; + cover: string; + timestamp: number | undefined; + url: string; + mobileUrl: string; + }; }; diff --git a/src/routes/gameres.ts b/src/routes/gameres.ts new file mode 100644 index 0000000..cf6b561 --- /dev/null +++ b/src/routes/gameres.ts @@ -0,0 +1,66 @@ +import type { RouterData } from "../types.js"; +import { load } from "cheerio"; +import { get } from "../utils/getData.js"; +import { getTime } from "../utils/getTime.js"; +import { RouterType } from "../router.types.js"; + +export const handleRoute = async (_: undefined, noCache: boolean) => { + const listData = await getList(noCache); + + const routeData: RouterData = { + name: "gameres", + title: "GameRes 游资网", + type: "最新资讯", + description: + "面向游戏从业者的游戏开发资讯,旨在为游戏制作人提供游戏研发类的程序技术、策划设计、艺术设计、原创设计等资讯内容。", + link: "https://www.gameres.com", + total: listData.data?.length || 0, + ...listData, + }; + + return routeData; +}; + +const getList = async (noCache: boolean) => { + const url = `https://www.gameres.com`; + const result = await get({ url, noCache }); + const $ = load(result.data); + + const container = $('div[data-news-pane-id="100000"]'); + const listDom = container.find("article.feed-item"); + + const listData = Array.from(listDom).map((el) => { + const dom = $(el); + + const titleEl = dom.find(".feed-item-title-a").first(); + const title = titleEl.text().trim(); + + const href = titleEl.attr("href"); + const url = href?.startsWith("http") ? href : `https://www.gameres.com${href ?? ""}`; + + const cover = dom.find(".thumb").attr("data-original") || ""; + const desc = dom.find(".feed-item-right > p").first().text().trim(); + + const dateTime = dom.find(".mark-info").contents().first().text().trim(); + const timestamp = getTime(dateTime); + + // 热度(列表暂无评论数) + const hot = undefined; + + return { + title, + desc, + cover, + timestamp, + hot, + url, + id: url, + mobileUrl: url, + } as RouterType["gameres"]; + }); + + return { + ...result, + data: listData, + }; +}; diff --git a/src/utils/getTime.ts b/src/utils/getTime.ts index d6ff25f..012b6bc 100644 --- a/src/utils/getTime.ts +++ b/src/utils/getTime.ts @@ -90,6 +90,12 @@ export const getTime = (timeInput: string | number): number | undefined => { .valueOf(); } + // 处理 `N 小时前` 的时间格式 + if (/小时前/.test(timeInput)) { + const hoursAgo = parseInt(timeInput.replace("小时前", "")); + return dayjs().subtract(hoursAgo, "hour").valueOf(); + } + if (/分钟前/.test(timeInput)) { const minutesAgo = parseInt(timeInput.replace("分钟前", "")); return dayjs().subtract(minutesAgo, "minute").valueOf();