mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
feat: 支持处理“X小时前”的时间格式并添加gameres路由
This commit is contained in:
67
src/routes/gameres.ts
Normal file
67
src/routes/gameres.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import { load } from "cheerio";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.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 hotText = dom.find(".comment").text().trim();
|
||||
// const hot = Number(hotText.replace(/\D/g, "")) || 0;
|
||||
const hot = undefined;
|
||||
|
||||
return {
|
||||
title,
|
||||
desc,
|
||||
cover,
|
||||
timestamp,
|
||||
hot,
|
||||
url,
|
||||
id: url,
|
||||
mobileUrl: url,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
data: listData,
|
||||
};
|
||||
};
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user