mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 05:04:56 +08:00
🐞 fix: 部分类型错误
- 新增 米游社 综合接口
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
.vscode
|
||||
docker-compose.yml
|
||||
dist
|
||||
logs
|
||||
!/.github
|
||||
@@ -1,14 +0,0 @@
|
||||
{
|
||||
"env": {
|
||||
"browser": true,
|
||||
"es2021": true
|
||||
},
|
||||
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
|
||||
"parser": "@typescript-eslint/parser",
|
||||
"parserOptions": {
|
||||
"ecmaVersion": "latest",
|
||||
"sourceType": "module"
|
||||
},
|
||||
"plugins": ["@typescript-eslint"],
|
||||
"rules": {}
|
||||
}
|
||||
14
eslint.config.js
Normal file
14
eslint.config.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import globals from "globals";
|
||||
import pluginJs from "@eslint/js";
|
||||
import tseslint from "typescript-eslint";
|
||||
|
||||
/** @type {import('eslint').Linter.Config[]} */
|
||||
export default [
|
||||
{
|
||||
ignores: ["**/node_modules", "**/dist", "**/.gitignore", "**/logs", "**/docker-compose.yml"],
|
||||
},
|
||||
{ files: ["**/*.{js,mjs,cjs,ts}"] },
|
||||
{ languageOptions: { globals: globals.node } },
|
||||
pluginJs.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
];
|
||||
35
package.json
35
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dailyhot-api",
|
||||
"version": "2.0.6",
|
||||
"version": "2.0.7",
|
||||
"description": "An Api on Today's Hot list",
|
||||
"keywords": [
|
||||
"API",
|
||||
@@ -28,7 +28,7 @@
|
||||
],
|
||||
"scripts": {
|
||||
"format": "prettier --write .",
|
||||
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts,.vue --fix",
|
||||
"lint": "eslint .",
|
||||
"dev": "cross-env NODE_ENV=development tsx watch --no-cache src/index.ts",
|
||||
"dev:cache": "cross-env NODE_ENV=development tsx watch src/index.ts",
|
||||
"build": "tsc --project tsconfig.json",
|
||||
@@ -36,30 +36,35 @@
|
||||
},
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@hono/node-server": "^1.13.5",
|
||||
"axios": "^1.7.7",
|
||||
"@hono/node-server": "^1.13.7",
|
||||
"axios": "^1.7.8",
|
||||
"chalk": "^5.3.0",
|
||||
"cheerio": "1.0.0-rc.12",
|
||||
"cheerio": "^1.0.0",
|
||||
"dayjs": "^1.11.13",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv": "^16.4.6",
|
||||
"feed": "^4.2.2",
|
||||
"hono": "^4.6.9",
|
||||
"hono": "^4.6.12",
|
||||
"md5": "^2.3.0",
|
||||
"node-cache": "^5.1.2",
|
||||
"puppeteer-cluster": "^0.24.0",
|
||||
"rss-parser": "^3.13.0",
|
||||
"winston": "^3.16.0",
|
||||
"winston": "^3.17.0",
|
||||
"xml2js": "^0.6.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^20.17.6",
|
||||
"@eslint/js": "^9.16.0",
|
||||
"@types/md5": "^2.3.5",
|
||||
"@types/node": "^22.10.1",
|
||||
"@types/xml2js": "^0.4.14",
|
||||
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
||||
"@typescript-eslint/parser": "^7.18.0",
|
||||
"@typescript-eslint/eslint-plugin": "^8.17.0",
|
||||
"@typescript-eslint/parser": "^8.17.0",
|
||||
"cross-env": "^7.0.3",
|
||||
"eslint": "^8.57.1",
|
||||
"prettier": "^3.3.3",
|
||||
"tsx": "^3.14.0",
|
||||
"typescript": "^5.6.3"
|
||||
"eslint": "^9.16.0",
|
||||
"globals": "^15.13.0",
|
||||
"prettier": "^3.4.1",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.7.2",
|
||||
"typescript-eslint": "^8.17.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
|
||||
1696
pnpm-lock.yaml
generated
1696
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -18,7 +18,7 @@ export type Config = {
|
||||
const getEnvVariable = (key: string): string | undefined => {
|
||||
const value = process.env[key];
|
||||
if (value === undefined) {
|
||||
return null;
|
||||
return undefined;
|
||||
}
|
||||
return value;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { serve } from "@hono/node-server";
|
||||
import { config } from "./config.js";
|
||||
import packageJson from "../package.json";
|
||||
import logger from "./utils/logger.js";
|
||||
import app from "./app.js";
|
||||
|
||||
@@ -10,6 +11,7 @@ const serveHotApi: (port?: number) => void = (port: number = config.PORT) => {
|
||||
fetch: app.fetch,
|
||||
port,
|
||||
});
|
||||
logger.info(`📦 Version: ${packageJson.version}`);
|
||||
logger.info(`🔥 DailyHot API 成功在端口 ${port} 上运行`);
|
||||
logger.info(`🔗 Local: 👉 http://localhost:${port}`);
|
||||
return apiServer;
|
||||
|
||||
@@ -15,7 +15,7 @@ let allRoutePath: Array<string> = [];
|
||||
const routersDirName: string = "routes";
|
||||
|
||||
// 排除路由
|
||||
const excludeRoutes: Array<string> = ["52pojie", "hostloc"];
|
||||
const excludeRoutes: Array<string> = [];
|
||||
|
||||
// 建立完整目录路径
|
||||
const routersDirPath = path.join(__dirname, routersDirName);
|
||||
|
||||
12
src/router.types.d.ts
vendored
12
src/router.types.d.ts
vendored
@@ -310,9 +310,19 @@ export type RouterType = {
|
||||
id: number;
|
||||
ttitle: string;
|
||||
shareUrl: string;
|
||||
username:string;
|
||||
username: string;
|
||||
tpic: string;
|
||||
message: string;
|
||||
replynum: number;
|
||||
};
|
||||
nodeseek: {
|
||||
guid: {
|
||||
_: string;
|
||||
}[];
|
||||
title: string;
|
||||
description: string | string[];
|
||||
"dc:creator": string;
|
||||
pubDate: string[];
|
||||
link: string[];
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,24 +1,26 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterData, ListContext, Options, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { post } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
const typeMap: Record<string, string> = {
|
||||
hot: "人气榜",
|
||||
video: "视频榜",
|
||||
comment: "热议榜",
|
||||
collect: "收藏榜",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "hot";
|
||||
const { fromCache, data, updateTime } = await getList({ type }, noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "36kr",
|
||||
title: "36氪",
|
||||
type: "热榜",
|
||||
type: typeMap[type],
|
||||
params: {
|
||||
type: {
|
||||
name: "热榜分类",
|
||||
type: {
|
||||
hot: "人气榜",
|
||||
video: "视频榜",
|
||||
comment: "热议榜",
|
||||
collect: "收藏榜",
|
||||
},
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: "https://m.36kr.com/hot-list-m",
|
||||
@@ -30,7 +32,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const getList = async (options: Options, noCache: boolean): Promise<RouterResType> => {
|
||||
const { type } = options;
|
||||
const url = `https://gateway.36kr.com/api/mis/nav/home/nav/rank/${type}`;
|
||||
const result = await post({
|
||||
@@ -67,7 +69,7 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
cover: item.widgetImage,
|
||||
author: item.authorName,
|
||||
timestamp: getTime(v.publishTime),
|
||||
hot: item.statCollect,
|
||||
hot: item.statCollect || undefined,
|
||||
url: `https://www.36kr.com/p/${v.itemId}`,
|
||||
mobileUrl: `https://m.36kr.com/p/${v.itemId}`,
|
||||
};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { RouterData, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { getToken, sign } from "../utils/getToken/51cto.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
@@ -19,7 +19,7 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const getList = async (noCache: boolean): Promise<RouterResType> => {
|
||||
const url = `https://api-media.51cto.com/index/index/recommend`;
|
||||
const params = {
|
||||
page: 1,
|
||||
@@ -49,6 +49,7 @@ const getList = async (noCache: boolean) => {
|
||||
cover: v.cover,
|
||||
desc: v.abstract,
|
||||
timestamp: getTime(v.pubdate),
|
||||
hot: undefined,
|
||||
url: v.url,
|
||||
mobileUrl: v.url,
|
||||
})),
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import type { RouterData, ListContext, Options, RouterResType } from "../types.js";
|
||||
import { web } from "../utils/getData.js";
|
||||
import { extractRss, parseRSS } from "../utils/parseRSS.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
@@ -31,7 +30,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const getList = async (options: Options, noCache: boolean): Promise<RouterResType> => {
|
||||
const { type } = options;
|
||||
const url = `https://www.52pojie.cn/forum.php?mod=guide&view=${type}&rss=1`;
|
||||
const result = await web({
|
||||
@@ -41,26 +40,24 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36",
|
||||
});
|
||||
const parseData = async () => {
|
||||
if (typeof result?.data === "string") {
|
||||
const rssContent = extractRss(result.data);
|
||||
return await parseRSS(rssContent);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
if (typeof result?.data !== "string") return [];
|
||||
const rssContent = extractRss(result.data);
|
||||
if (!rssContent) return [];
|
||||
return await parseRSS(rssContent);
|
||||
};
|
||||
const list = await parseData();
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
data: list.map((v: RouterType["discuz"]) => ({
|
||||
id: v.guid,
|
||||
title: v.title,
|
||||
desc: v.content,
|
||||
author: v.author,
|
||||
timestamp: getTime(v.pubDate),
|
||||
hot: null,
|
||||
url: v.link,
|
||||
mobileUrl: v.link,
|
||||
data: list.map((v, i) => ({
|
||||
id: v.guid || i,
|
||||
title: v.title || "",
|
||||
desc: v.content || "",
|
||||
author: v.author || "",
|
||||
timestamp: getTime(v.pubDate || 0),
|
||||
hot: 0,
|
||||
url: v.link || "",
|
||||
mobileUrl: v.link || "",
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterData, ListContext, Options, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
const typeMap: Record<string, string> = {
|
||||
"-1": "综合",
|
||||
"155": "番剧",
|
||||
"1": "动画",
|
||||
"60": "娱乐",
|
||||
"201": "生活",
|
||||
"58": "音乐",
|
||||
"123": "舞蹈·偶像",
|
||||
"59": "游戏",
|
||||
"70": "科技",
|
||||
"68": "影视",
|
||||
"69": "体育",
|
||||
"125": "鱼塘",
|
||||
};
|
||||
|
||||
const rangeMap: Record<string, string> = {
|
||||
DAY: "今日",
|
||||
THREE_DAYS: "三日",
|
||||
WEEK: "本周",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "-1";
|
||||
const range = c.req.query("range") || "DAY";
|
||||
@@ -10,33 +31,16 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const routeData: RouterData = {
|
||||
name: "acfun",
|
||||
title: "AcFun",
|
||||
type: "排行榜",
|
||||
type: `排行榜 · ${typeMap[type]}`,
|
||||
description: "AcFun是一家弹幕视频网站,致力于为每一个人带来欢乐。",
|
||||
params: {
|
||||
type: {
|
||||
name: "频道",
|
||||
type: {
|
||||
"-1": "全站综合",
|
||||
"155": "番剧",
|
||||
"1": "动画",
|
||||
"60": "娱乐",
|
||||
"201": "生活",
|
||||
"58": "音乐",
|
||||
"123": "舞蹈·偶像",
|
||||
"59": "游戏",
|
||||
"70": "科技",
|
||||
"68": "影视",
|
||||
"69": "体育",
|
||||
"125": "鱼塘",
|
||||
},
|
||||
type: typeMap,
|
||||
},
|
||||
range: {
|
||||
name: "时间",
|
||||
type: {
|
||||
DAY: "今日",
|
||||
THREE_DAYS: "三日",
|
||||
WEEK: "本周",
|
||||
},
|
||||
type: rangeMap,
|
||||
},
|
||||
},
|
||||
link: "https://www.acfun.cn/rank/list/",
|
||||
@@ -48,7 +52,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const getList = async (options: Options, noCache: boolean): Promise<RouterResType> => {
|
||||
const { type, range } = options;
|
||||
const url = `https://www.acfun.cn/rest/pc-direct/rank/channel?channelId=${type === "-1" ? "" : type}&rankLimit=30&rankPeriod=${range}`;
|
||||
const result = await get({
|
||||
|
||||
@@ -1,25 +1,27 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterData, ListContext, Options, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
|
||||
const typeMap: Record<string, string> = {
|
||||
realtime: "热搜",
|
||||
novel: "小说",
|
||||
movie: "电影",
|
||||
teleplay: "电视剧",
|
||||
car: "汽车",
|
||||
game: "游戏",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "realtime";
|
||||
const { fromCache, data, updateTime } = await getList({ type }, noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "baidu",
|
||||
title: "百度",
|
||||
type: "热搜榜",
|
||||
type: typeMap[type],
|
||||
params: {
|
||||
type: {
|
||||
name: "热搜类别",
|
||||
type: {
|
||||
realtime: "热搜",
|
||||
novel: "小说",
|
||||
movie: "电影",
|
||||
teleplay: "电视剧",
|
||||
car: "汽车",
|
||||
game: "游戏",
|
||||
},
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: "https://top.baidu.com/board",
|
||||
@@ -31,7 +33,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const getList = async (options: Options, noCache: boolean): Promise<RouterResType> => {
|
||||
const { type } = options;
|
||||
const url = `https://top.baidu.com/board?tab=${type}`;
|
||||
const result = await get({
|
||||
@@ -55,8 +57,8 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
desc: v.desc,
|
||||
cover: v.img,
|
||||
author: v.show?.length ? v.show : "",
|
||||
timestamp: null,
|
||||
hot: Number(v.hotScore),
|
||||
timestamp: 0,
|
||||
hot: Number(v.hotScore || 0),
|
||||
url: `https://www.baidu.com/s?wd=${encodeURIComponent(v.query)}`,
|
||||
mobileUrl: v.rawUrl,
|
||||
})),
|
||||
|
||||
@@ -1,35 +1,37 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterData, ListContext, Options, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import getBiliWbi from "../utils/getToken/bilibili.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
const typeMap: Record<string, string> = {
|
||||
"0": "全站",
|
||||
"1": "动画",
|
||||
"3": "音乐",
|
||||
"4": "游戏",
|
||||
"5": "娱乐",
|
||||
"36": "科技",
|
||||
"119": "鬼畜",
|
||||
"129": "舞蹈",
|
||||
"155": "时尚",
|
||||
"160": "生活",
|
||||
"168": "国创相关",
|
||||
"188": "数码",
|
||||
"181": "影视",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "0";
|
||||
const { fromCache, data, updateTime } = await getList({ type }, noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "bilibili",
|
||||
title: "哔哩哔哩",
|
||||
type: "热门榜",
|
||||
type: `热榜 · ${typeMap[type]}`,
|
||||
description: "你所热爱的,就是你的生活",
|
||||
params: {
|
||||
type: {
|
||||
name: "排行榜分区",
|
||||
type: {
|
||||
0: "全站",
|
||||
1: "动画",
|
||||
3: "音乐",
|
||||
4: "游戏",
|
||||
5: "娱乐",
|
||||
36: "科技",
|
||||
119: "鬼畜",
|
||||
129: "舞蹈",
|
||||
155: "时尚",
|
||||
160: "生活",
|
||||
168: "国创相关",
|
||||
188: "数码",
|
||||
181: "影视",
|
||||
},
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: "https://www.bilibili.com/v/popular/rank/all",
|
||||
@@ -41,7 +43,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const getList = async (options: Options, noCache: boolean): Promise<RouterResType> => {
|
||||
const { type } = options;
|
||||
const wbiData = await getBiliWbi();
|
||||
const url = `https://api.bilibili.com/x/web-interface/ranking/v2?tid=${type}&type=all&${wbiData}`;
|
||||
@@ -64,10 +66,10 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
id: v.bvid,
|
||||
title: v.title,
|
||||
desc: v.desc || "该视频暂无简介",
|
||||
cover: v.pic.replace(/http:/, "https:"),
|
||||
author: v.owner.name,
|
||||
cover: v.pic?.replace(/http:/, "https:"),
|
||||
author: v.owner?.name,
|
||||
timestamp: getTime(v.pubdate),
|
||||
hot: v.stat.view,
|
||||
hot: v.stat?.view || 0,
|
||||
url: v.short_link_v2 || `https://www.bilibili.com/video/${v.bvid}`,
|
||||
mobileUrl: `https://m.bilibili.com/video/${v.bvid}`,
|
||||
})),
|
||||
@@ -93,7 +95,7 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
id: v.bvid,
|
||||
title: v.title,
|
||||
desc: v.desc || "该视频暂无简介",
|
||||
cover: v.pic.replace(/http:/, "https:"),
|
||||
cover: v.pic?.replace(/http:/, "https:"),
|
||||
author: v.author,
|
||||
timestamp: null,
|
||||
hot: v.video_review,
|
||||
|
||||
@@ -35,8 +35,8 @@ const getList = async (noCache: boolean) => {
|
||||
cover: v.tpic,
|
||||
author: v.username,
|
||||
desc: v.ttitle,
|
||||
timestamp: null,
|
||||
hot: null,
|
||||
timestamp: undefined,
|
||||
hot: undefined,
|
||||
url: v.shareUrl,
|
||||
mobileUrl: v.shareUrl,
|
||||
})),
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { RouterData, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
@@ -19,7 +19,7 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const getList = async (noCache: boolean): Promise<RouterResType> => {
|
||||
const url = "https://blog.csdn.net/phoenix/web/blog/hot-rank?page=0&pageSize=30";
|
||||
const result = await get({ url, noCache });
|
||||
const list = result.data.data;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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 { fromCache, data, updateTime } = await getList(noCache);
|
||||
@@ -42,9 +43,9 @@ const getList = async (noCache: boolean) => {
|
||||
title: dom.find("h3 a").text().trim(),
|
||||
cover: dom.find(".pic-wrap img").attr("src"),
|
||||
desc: dom.find(".block p").text().trim(),
|
||||
timestamp: dom.find("span.pubtime").text().trim(),
|
||||
hot: null,
|
||||
url,
|
||||
timestamp: getTime(dom.find("span.pubtime").text().trim()),
|
||||
hot: 0,
|
||||
url: url || `https://www.douban.com/group/topic/${getNumbers(url)}`,
|
||||
mobileUrl: `https://m.douban.com/group/topic/${getNumbers(url)}/`,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const routeData: RouterData = {
|
||||
name: "douban-movie",
|
||||
title: "豆瓣电影",
|
||||
type: "新片排行榜",
|
||||
type: "新片榜",
|
||||
link: "https://movie.douban.com/chart",
|
||||
total: data?.length || 0,
|
||||
updateTime,
|
||||
@@ -51,9 +51,9 @@ const getList = async (noCache: boolean) => {
|
||||
title: `【${score}】${dom.find("a").attr("title")}`,
|
||||
cover: dom.find("img").attr("src"),
|
||||
desc: dom.find("p.pl").text(),
|
||||
timestamp: null,
|
||||
timestamp: undefined,
|
||||
hot: getNumbers(dom.find("span.pl").text()),
|
||||
url,
|
||||
url: url || `https://movie.douban.com/subject/${getNumbers(url)}/`,
|
||||
mobileUrl: `https://m.douban.com/movie/subject/${getNumbers(url)}/`,
|
||||
};
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
const mappings = {
|
||||
const mappings: Record<string, string> = {
|
||||
O_TIME: "发震时刻(UTC+8)",
|
||||
LOCATION_C: "参考位置",
|
||||
M: "震级(M)",
|
||||
@@ -13,34 +13,12 @@ const mappings = {
|
||||
SAVE_TIME: "录入时间",
|
||||
};
|
||||
|
||||
const typeMappings = {
|
||||
1: "最近24小时地震信息",
|
||||
2: "最近48小时地震信息",
|
||||
3: "最近7天地震信息",
|
||||
4: "最近30天地震信息",
|
||||
5: "最近一年3.0级以上地震信息",
|
||||
6: "最近一年地震信息",
|
||||
7: "最近一年3.0级以下地震",
|
||||
8: "最近一年4.0级以上地震信息",
|
||||
9: "最近一年5.0级以上地震信息",
|
||||
0: "最近一年6.0级以上地震信息",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "5";
|
||||
const { fromCache, data, updateTime } = await getList({ type }, noCache);
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const { fromCache, data, updateTime } = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "earthquake",
|
||||
title: "中国地震台",
|
||||
type: "地震速报",
|
||||
params: {
|
||||
type: {
|
||||
name: "速报分类",
|
||||
type: {
|
||||
...typeMappings,
|
||||
},
|
||||
},
|
||||
},
|
||||
link: "https://news.ceic.ac.cn/",
|
||||
total: data?.length || 0,
|
||||
updateTime,
|
||||
@@ -50,12 +28,12 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const { type } = options;
|
||||
const url = `http://www.ceic.ac.cn/ajax/speedsearch?num=${type}`;
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://news.ceic.ac.cn/speedsearch.html`;
|
||||
const result = await get({ url, noCache });
|
||||
const data = result.data.replace(/,"page":"(.*?)","num":/, ',"num":');
|
||||
const list = JSON.parse(data.substring(1, data.length - 1)).shuju;
|
||||
const regex = /const newdata = (\[.*?\]);/s;
|
||||
const match = result.data.match(regex);
|
||||
const list = match && match[1] ? JSON.parse(match[1]) : [];
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
@@ -63,14 +41,16 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
const contentBuilder = [];
|
||||
const { NEW_DID, LOCATION_C, M } = v;
|
||||
for (const mappingsKey in mappings) {
|
||||
contentBuilder.push(`${mappings[mappingsKey]}:${v[mappingsKey]}`);
|
||||
contentBuilder.push(
|
||||
`${mappings[mappingsKey as keyof typeof mappings]}:${v[mappingsKey as keyof typeof v]}`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
id: NEW_DID,
|
||||
title: `${LOCATION_C}发生${M}级地震`,
|
||||
desc: contentBuilder.join("\n"),
|
||||
timestamp: getTime(v["O_TIME"]),
|
||||
hot: null,
|
||||
timestamp: getTime(v["O_TIME" as keyof typeof v]),
|
||||
hot: undefined,
|
||||
url: `https://news.ceic.ac.cn/${NEW_DID}.html`,
|
||||
mobileUrl: `https://news.ceic.ac.cn/${NEW_DID}.html`,
|
||||
};
|
||||
|
||||
@@ -27,8 +27,8 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
};
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const { month, day } = options;
|
||||
const monthStr = month.toString().padStart(2, "0");
|
||||
const dayStr = day.toString().padStart(2, "0");
|
||||
const monthStr = month?.toString().padStart(2, "0");
|
||||
const dayStr = day?.toString().padStart(2, "0");
|
||||
const url = `https://baike.baidu.com/cms/home/eventsOnHistory/${monthStr}.json`;
|
||||
const result = await get({
|
||||
url,
|
||||
@@ -37,7 +37,7 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
_: new Date().getTime(),
|
||||
},
|
||||
});
|
||||
const list = result.data[monthStr][monthStr + dayStr];
|
||||
const list = monthStr ? result.data[monthStr][monthStr + dayStr] : [];
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
|
||||
@@ -1,25 +1,26 @@
|
||||
import type { RouterData, ListContext, Options } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { web } from "../utils/getData.js";
|
||||
import { extractRss, parseRSS } from "../utils/parseRSS.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
const typeMap: Record<string, string> = {
|
||||
hot: "最新热门",
|
||||
digest: "最新精华",
|
||||
new: "最新回复",
|
||||
newthread: "最新发表",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "hot";
|
||||
const { fromCache, data, updateTime } = await getList({ type }, noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "hostloc",
|
||||
title: "全球主机交流",
|
||||
type: "榜单",
|
||||
type: typeMap[type],
|
||||
params: {
|
||||
type: {
|
||||
name: "榜单分类",
|
||||
type: {
|
||||
hot: "最新热门",
|
||||
digest: "最新精华",
|
||||
new: "最新回复",
|
||||
newthread: "最新发表",
|
||||
},
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: "https://hostloc.com/",
|
||||
@@ -41,26 +42,24 @@ const getList = async (options: Options, noCache: boolean) => {
|
||||
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36",
|
||||
});
|
||||
const parseData = async () => {
|
||||
if (typeof result?.data === "string") {
|
||||
const rssContent = extractRss(result.data);
|
||||
return await parseRSS(rssContent);
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
if (typeof result?.data !== "string") return [];
|
||||
const rssContent = extractRss(result.data);
|
||||
if (!rssContent) return [];
|
||||
return await parseRSS(rssContent);
|
||||
};
|
||||
const list = await parseData();
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
data: list.map((v: RouterType["discuz"]) => ({
|
||||
id: v.guid,
|
||||
title: v.title,
|
||||
desc: v.content,
|
||||
author: v.author,
|
||||
timestamp: getTime(v.pubDate),
|
||||
hot: null,
|
||||
url: v.link,
|
||||
mobileUrl: v.link,
|
||||
data: list.map((v, i) => ({
|
||||
id: v.guid || i,
|
||||
title: v.title || "",
|
||||
desc: v.content || "",
|
||||
author: v.author || "",
|
||||
timestamp: getTime(v.pubDate || 0),
|
||||
hot: undefined,
|
||||
url: v.link || "",
|
||||
mobileUrl: v.link || "",
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -21,7 +21,7 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
// 标题处理
|
||||
const titleProcessing = (text: string) => {
|
||||
const paragraphs = text.split("<br><br>");
|
||||
const title = paragraphs.shift().replace(/。$/, "");
|
||||
const title = paragraphs.shift()?.replace(/。$/, "");
|
||||
const intro = paragraphs.join("<br><br>");
|
||||
return { title, intro };
|
||||
};
|
||||
@@ -47,8 +47,8 @@ const getList = async (noCache: boolean) => {
|
||||
author: v.user_info.username,
|
||||
timestamp: getTime(v.publish_time),
|
||||
hot: null,
|
||||
url: v.url || "https://www.huxiu.com/moment/",
|
||||
mobileUrl: v.url || "https://m.huxiu.com/moment/",
|
||||
url: v.url || `https://www.huxiu.com/moment/${v.object_id}.html`,
|
||||
mobileUrl: v.url || `https://m.huxiu.com/moment/${v.object_id}.html`,
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -32,8 +32,8 @@ const getList = async (noCache: boolean) => {
|
||||
desc: v.post_content,
|
||||
timestamp: getTime(v.created_at),
|
||||
hot: v.like_count || v.comment_count,
|
||||
url: `https://www.ifanr.com/${v.id}` || v.buzz_original_url,
|
||||
mobileUrl: `https://www.ifanr.com/digest/${v.id}` || v.buzz_original_url,
|
||||
url: v.buzz_original_url || `https://www.ifanr.com/${v.id}`,
|
||||
mobileUrl: v.buzz_original_url || `https://www.ifanr.com/digest/${v.id}`,
|
||||
})),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -44,10 +44,10 @@ const getList = async (noCache: boolean) => {
|
||||
title: dom.find(".newsbody h2").text().trim(),
|
||||
desc: dom.find(".newsbody p").text().trim(),
|
||||
cover: dom.find("img").attr("data-original"),
|
||||
timestamp: getTime(dateTime),
|
||||
timestamp: getTime(dateTime || 0),
|
||||
hot: Number(dom.find(".comment").text().replace(/\D/g, "")),
|
||||
url: href || undefined,
|
||||
mobileUrl: href ? replaceLink(href) : undefined,
|
||||
url: href || "",
|
||||
mobileUrl: href ? replaceLink(href) : "",
|
||||
};
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
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 { fromCache, data, updateTime } = await getList(noCache);
|
||||
@@ -43,10 +44,10 @@ const getList = async (noCache: boolean) => {
|
||||
id: href ? Number(replaceLink(href, true)) : 100000,
|
||||
title: dom.find(".plc-title").text().trim(),
|
||||
cover: dom.find("img").attr("data-original"),
|
||||
timestamp: dom.find("span.post-time").text().trim(),
|
||||
timestamp: getTime(dom.find("span.post-time").text().trim()),
|
||||
hot: Number(dom.find(".review-num").text().replace(/\D/g, "")),
|
||||
url: href ? replaceLink(href) : undefined,
|
||||
mobileUrl: href || undefined,
|
||||
url: href ? replaceLink(href) : "",
|
||||
mobileUrl: href ? replaceLink(href) : "",
|
||||
};
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -45,8 +45,8 @@ const getList = async (noCache: boolean) => {
|
||||
cover: dom.find("img").attr("src"),
|
||||
desc: dom.find("p.abstract").text()?.trim(),
|
||||
author: dom.find("a.nickname").text()?.trim(),
|
||||
hot: null,
|
||||
timestamp: null,
|
||||
hot: undefined,
|
||||
timestamp: undefined,
|
||||
url: `https://www.jianshu.com${href}`,
|
||||
mobileUrl: `https://www.jianshu.com${href}`,
|
||||
};
|
||||
|
||||
@@ -29,7 +29,7 @@ const getList = async (noCache: boolean) => {
|
||||
title: v.content.title,
|
||||
author: v.author.name,
|
||||
hot: v.content_counter.hot_rank,
|
||||
timestamp: null,
|
||||
timestamp: undefined,
|
||||
url: `https://juejin.cn/post/${v.content.content_id}`,
|
||||
mobileUrl: `https://juejin.cn/post/${v.content.content_id}`,
|
||||
})),
|
||||
|
||||
75
src/routes/miyoushe.ts
Normal file
75
src/routes/miyoushe.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import type { RouterData, ListContext, Options, RouterResType } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
// 游戏分类
|
||||
const gameMap: Record<string, string> = {
|
||||
"1": "崩坏3",
|
||||
"2": "原神",
|
||||
"3": "崩坏学园2",
|
||||
"4": "未定事件簿",
|
||||
"5": "大别野",
|
||||
"6": "崩坏:星穹铁道",
|
||||
"7": "暂无",
|
||||
"8": "绝区零",
|
||||
};
|
||||
|
||||
// 榜单分类
|
||||
const typeMap: Record<string, string> = {
|
||||
"1": "公告",
|
||||
"2": "活动",
|
||||
"3": "资讯",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const game = c.req.query("game") || "1";
|
||||
const type = c.req.query("type") || "1";
|
||||
const { fromCache, data, updateTime } = await getList({ game, type }, noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "miyoushe",
|
||||
title: `米游社 · ${gameMap[game]}`,
|
||||
type: `最新${typeMap[type]}`,
|
||||
params: {
|
||||
game: {
|
||||
name: "游戏分类",
|
||||
type: gameMap,
|
||||
},
|
||||
type: {
|
||||
name: "榜单分类",
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: "https://www.miyoushe.com/",
|
||||
total: data?.length || 0,
|
||||
updateTime,
|
||||
fromCache,
|
||||
data,
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (options: Options, noCache: boolean): Promise<RouterResType> => {
|
||||
const { game, type } = options;
|
||||
const url = `https://bbs-api-static.miyoushe.com/painter/wapi/getNewsList?client_type=4&gids=${game}&last_id=&page_size=30&type=${type}`;
|
||||
const result = await get({ url, noCache });
|
||||
const list = result.data.data.list;
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
data: list.map((v: RouterType["miyoushe"]) => {
|
||||
const data = v.post;
|
||||
return {
|
||||
id: data.post_id,
|
||||
title: data.subject,
|
||||
desc: data.content,
|
||||
cover: data.cover || data?.images?.[0],
|
||||
author: v.user?.nickname || null,
|
||||
timestamp: getTime(data.created_at),
|
||||
hot: data.view_status || 0,
|
||||
url: `https://www.miyoushe.com/ys/article/${data.post_id}`,
|
||||
mobileUrl: `https://m.miyoushe.com/ys/#/article/${data.post_id}`,
|
||||
};
|
||||
}),
|
||||
};
|
||||
};
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { parseStringPromise } from "xml2js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const { fromCache, data, updateTime } = await getList(noCache);
|
||||
@@ -33,13 +35,13 @@ const getList = async (noCache: boolean) => {
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
updateTime: result.updateTime,
|
||||
data: list.map((v) => ({
|
||||
data: list.map((v: RouterType["nodeseek"]) => ({
|
||||
id: v.guid[0]._,
|
||||
title: v.title[0],
|
||||
desc: v.description ? v.description[0] : "",
|
||||
author: v["dc:creator"] ? v["dc:creator"][0] : "unknown",
|
||||
timestamp: new Date(v.pubDate[0]).getTime(),
|
||||
hot: null, // NodeSeek RSS 中没有类似于hot的字段
|
||||
timestamp: getTime(v.pubDate[0]),
|
||||
hot: undefined,
|
||||
url: v.link[0],
|
||||
mobileUrl: v.link[0],
|
||||
})),
|
||||
|
||||
@@ -68,7 +68,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const routeData: RouterData = {
|
||||
name: "sina-news",
|
||||
title: "新浪新闻",
|
||||
type: listType[type].name,
|
||||
type: listType[type as keyof typeof listType].name,
|
||||
params: {
|
||||
type: {
|
||||
name: "榜单分类",
|
||||
@@ -106,7 +106,7 @@ const parseData = (data: string) => {
|
||||
const jsonData = JSON.parse(jsonString);
|
||||
return jsonData;
|
||||
} catch (error) {
|
||||
throw new Error("Failed to parse JSON: " + error.message);
|
||||
throw new Error("Failed to parse JSON: " + error);
|
||||
}
|
||||
} else {
|
||||
throw new Error("Invalid JSON format");
|
||||
@@ -116,7 +116,7 @@ const parseData = (data: string) => {
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const { type } = options;
|
||||
// 必要数据
|
||||
const { params, www } = listType[type];
|
||||
const { params, www } = listType[type as keyof typeof listType];
|
||||
const { year, month, day } = getCurrentDateTime(true);
|
||||
const url = `https://top.${www}.sina.com.cn/ws/GetTopDataList.php?top_type=day&top_cat=${params}&top_time=${year + month + day}&top_show_num=50`;
|
||||
const result = await get({ url, noCache });
|
||||
|
||||
@@ -27,7 +27,7 @@ export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
|
||||
const getList = async (options: Options, noCache: boolean) => {
|
||||
const { province } = options;
|
||||
const url = `http://www.nmc.cn/rest/findAlarm?pageNo=1&pageSize=20&signaltype=&signallevel=&province=${encodeURIComponent(province)}`;
|
||||
const url = `http://www.nmc.cn/rest/findAlarm?pageNo=1&pageSize=20&signaltype=&signallevel=&province=${encodeURIComponent(province || "")}`;
|
||||
const result = await get({ url, noCache });
|
||||
const list = result.data.data.page.list;
|
||||
return {
|
||||
|
||||
42
src/types.d.ts
vendored
42
src/types.d.ts
vendored
@@ -4,20 +4,27 @@ import type { Context } from "hono";
|
||||
export type ListContext = Context;
|
||||
|
||||
// 榜单数据
|
||||
export type ListItem = {
|
||||
export interface ListItem {
|
||||
id: number | string;
|
||||
title: string;
|
||||
cover?: string;
|
||||
author?: string;
|
||||
desc?: string;
|
||||
hot: number | null;
|
||||
timestamp: number | string | null;
|
||||
url: string | undefined;
|
||||
mobileUrl: string | undefined;
|
||||
};
|
||||
hot: number | undefined;
|
||||
timestamp: number | undefined;
|
||||
url: string;
|
||||
mobileUrl: string;
|
||||
}
|
||||
|
||||
// 路由接口数据
|
||||
export interface RouterResType {
|
||||
updateTime: string;
|
||||
fromCache: boolean;
|
||||
data: ListItem[];
|
||||
}
|
||||
|
||||
// 路由数据
|
||||
export type RouterData = {
|
||||
export interface RouterData extends RouterResType {
|
||||
name: string;
|
||||
title: string;
|
||||
type: string;
|
||||
@@ -25,13 +32,10 @@ export type RouterData = {
|
||||
params?: Record<string, string | object>;
|
||||
total: number;
|
||||
link?: string;
|
||||
updateTime: string;
|
||||
fromCache: boolean;
|
||||
data: ListItem[];
|
||||
};
|
||||
}
|
||||
|
||||
// 请求类型
|
||||
export type Get = {
|
||||
export interface Get {
|
||||
url: string;
|
||||
headers?: Record<string, string | string[]>;
|
||||
params?: Record<string, string | number>;
|
||||
@@ -39,9 +43,9 @@ export type Get = {
|
||||
noCache?: boolean;
|
||||
ttl?: number;
|
||||
originaInfo?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export type Post = {
|
||||
export interface Post {
|
||||
url: string;
|
||||
headers?: Record<string, string | string[]>;
|
||||
body?: string | object | Buffer | undefined;
|
||||
@@ -49,17 +53,17 @@ export type Post = {
|
||||
noCache?: boolean;
|
||||
ttl?: number;
|
||||
originaInfo?: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export type Web = {
|
||||
export interface Web {
|
||||
url: string;
|
||||
timeout?: number;
|
||||
noCache?: boolean;
|
||||
ttl?: number;
|
||||
userAgent?: string;
|
||||
};
|
||||
}
|
||||
|
||||
// 参数类型
|
||||
export type Options = {
|
||||
export interface Options {
|
||||
[key: string]: string | number | undefined;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Get, Post, Web } from "../types.ts";
|
||||
import { config } from "../config.js";
|
||||
import { getCache, setCache, delCache } from "./cache.js";
|
||||
// import { Cluster } from "puppeteer-cluster";
|
||||
import { Cluster } from "puppeteer-cluster";
|
||||
import logger from "./logger.js";
|
||||
import axios from "axios";
|
||||
|
||||
@@ -13,26 +13,41 @@ const request = axios.create({
|
||||
});
|
||||
|
||||
// puppeteer-cluster
|
||||
// export const createCluster = async () => {
|
||||
// return await Cluster.launch({
|
||||
// concurrency: Cluster.CONCURRENCY_BROWSER,
|
||||
// maxConcurrency: 5,
|
||||
// });
|
||||
// };
|
||||
export const createCluster = async () => {
|
||||
return await Cluster.launch({
|
||||
concurrency: Cluster.CONCURRENCY_BROWSER,
|
||||
maxConcurrency: 5,
|
||||
// puppeteer
|
||||
puppeteerOptions: {
|
||||
headless: true,
|
||||
args: ["--no-sandbox", "--disable-setuid-sandbox"],
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// Cluster
|
||||
// const cluster = await createCluster();
|
||||
const cluster = null;
|
||||
const cluster = await createCluster();
|
||||
|
||||
// Cluster configuration
|
||||
// cluster.task(async ({ page, data: { url, userAgent } }) => {
|
||||
// if (userAgent) {
|
||||
// await page.setUserAgent(userAgent);
|
||||
// }
|
||||
// await page.goto(url, { waitUntil: "networkidle0" });
|
||||
// const pageContent = await page.content();
|
||||
// return pageContent;
|
||||
// });
|
||||
cluster.task(async ({ page, data: { url, userAgent } }) => {
|
||||
// 用户代理
|
||||
if (userAgent) await page.setUserAgent(userAgent);
|
||||
// 请求拦截
|
||||
await page.setRequestInterception(true);
|
||||
// 拦截非必要资源
|
||||
page.on("request", (request) => {
|
||||
const type = request.resourceType();
|
||||
if (type === "document" || type === "script") {
|
||||
request.continue();
|
||||
} else {
|
||||
request.abort();
|
||||
}
|
||||
});
|
||||
// 加载页面
|
||||
await page.goto(url, { waitUntil: "networkidle0", timeout: config.REQUEST_TIMEOUT });
|
||||
const pageContent = await page.content();
|
||||
return pageContent;
|
||||
});
|
||||
|
||||
// 请求拦截
|
||||
request.interceptors.request.use(
|
||||
@@ -127,6 +142,7 @@ export const web = async (options: Web) => {
|
||||
const { url, noCache, ttl = config.CACHE_TTL, userAgent } = options;
|
||||
logger.info("使用 Puppeteer 发起页面请求", options);
|
||||
try {
|
||||
if (!cluster) throw new Error("Cluster is not initialized");
|
||||
// 检查缓存
|
||||
if (noCache) {
|
||||
delCache(url);
|
||||
|
||||
@@ -8,7 +8,13 @@ interface CurrentDateTime {
|
||||
minute: string;
|
||||
second: string;
|
||||
}
|
||||
export const getTime = (timeInput: string | number): number | null => {
|
||||
|
||||
/**
|
||||
* 将时间字符串或数字转换为时间戳
|
||||
* @param timeInput 时间字符串或数字
|
||||
* @returns 时间戳
|
||||
*/
|
||||
export const getTime = (timeInput: string | number): number | undefined => {
|
||||
try {
|
||||
let num: number;
|
||||
|
||||
@@ -18,7 +24,78 @@ export const getTime = (timeInput: string | number): number | null => {
|
||||
num = Number(timeInput);
|
||||
|
||||
if (isNaN(num)) {
|
||||
// 将各种分隔符替换为标准格式
|
||||
const now = dayjs();
|
||||
|
||||
// 处理 "00:00"
|
||||
if (/^\d{2}:\d{2}$/.test(timeInput)) {
|
||||
const [hour, minute] = timeInput.split(":").map(Number);
|
||||
return now.set("hour", hour).set("minute", minute).set("second", 0).valueOf();
|
||||
}
|
||||
|
||||
// 处理 昨天的时间
|
||||
if (/^昨日\s+\d{2}:\d{2}$/.test(timeInput)) {
|
||||
const timeStr = timeInput.replace("昨日", "").trim();
|
||||
const [hour, minute] = timeStr.split(":").map(Number);
|
||||
return now
|
||||
.subtract(1, "day")
|
||||
.set("hour", hour)
|
||||
.set("minute", minute)
|
||||
.set("second", 0)
|
||||
.valueOf();
|
||||
}
|
||||
|
||||
// 处理 今年的日期
|
||||
if (/^\d{1,2}月\d{1,2}日$/.test(timeInput)) {
|
||||
const [month, day] = timeInput
|
||||
.replace("月", "-")
|
||||
.replace("日", "")
|
||||
.split("-")
|
||||
.map(Number);
|
||||
return now
|
||||
.set("month", month - 1)
|
||||
.set("date", day)
|
||||
.startOf("day")
|
||||
.valueOf();
|
||||
}
|
||||
|
||||
// 处理 今年的日期+时间
|
||||
if (/^\d{1,2}月\d{1,2}日\s+\d{2}:\d{2}$/.test(timeInput)) {
|
||||
const [datePart, timePart] = timeInput.split(" ");
|
||||
const [month, day] = datePart.replace("月", "-").replace("日", "").split("-").map(Number);
|
||||
const [hour, minute] = timePart.split(":").map(Number);
|
||||
return now
|
||||
.set("month", month - 1)
|
||||
.set("date", day)
|
||||
.set("hour", hour)
|
||||
.set("minute", minute)
|
||||
.set("second", 0)
|
||||
.valueOf();
|
||||
}
|
||||
|
||||
// 处理相对时间
|
||||
if (/今天/.test(timeInput)) {
|
||||
const timeStr = timeInput.replace("今天", "").trim();
|
||||
return dayjs()
|
||||
.set("hour", parseInt(timeStr.split(":")[0]))
|
||||
.set("minute", parseInt(timeStr.split(":")[1]))
|
||||
.valueOf();
|
||||
}
|
||||
|
||||
if (/昨天/.test(timeInput)) {
|
||||
const timeStr = timeInput.replace("昨天", "").trim();
|
||||
return dayjs()
|
||||
.subtract(1, "day")
|
||||
.set("hour", parseInt(timeStr.split(":")[0]))
|
||||
.set("minute", parseInt(timeStr.split(":")[1]))
|
||||
.valueOf();
|
||||
}
|
||||
|
||||
if (/分钟前/.test(timeInput)) {
|
||||
const minutesAgo = parseInt(timeInput.replace("分钟前", ""));
|
||||
return dayjs().subtract(minutesAgo, "minute").valueOf();
|
||||
}
|
||||
|
||||
// 处理为标准格式
|
||||
let standardizedInput = timeInput
|
||||
.replace(/(\d{4})-(\d{2})-(\d{2})-(\d{2})/, "$1-$2-$3 $4") // "YYYY-MM-DD-HH" -> "YYYY-MM-DD HH"
|
||||
.replace(/(\d{4})-(\d{2})-(\d{2})[T\s](\d{2}):?(\d{2})?:?(\d{2})?/, "$1-$2-$3 $4:$5:$6") // "YYYY-MM-DDTHH:mm:ss" -> "YYYY-MM-DD HH:mm:ss"
|
||||
@@ -46,7 +123,7 @@ export const getTime = (timeInput: string | number): number | null => {
|
||||
if (parsedDate && parsedDate.isValid()) {
|
||||
return parsedDate.valueOf();
|
||||
} else {
|
||||
return null;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -61,10 +138,15 @@ export const getTime = (timeInput: string | number): number | null => {
|
||||
return num * 1000;
|
||||
}
|
||||
} catch (error) {
|
||||
return null;
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取当前日期时间
|
||||
* @param padZero 是否补零
|
||||
* @returns 当前日期时间
|
||||
*/
|
||||
export const getCurrentDateTime = (padZero: boolean = false): CurrentDateTime => {
|
||||
const now = dayjs();
|
||||
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
import RSSParser from "rss-parser";
|
||||
import logger from "./logger.js";
|
||||
|
||||
/**
|
||||
* 提取 RSS 内容
|
||||
* @param content HTML 内容
|
||||
* @returns RSS 内容
|
||||
*/
|
||||
export const extractRss = (content: string): string | null => {
|
||||
// 匹配 <rss> 标签及内容
|
||||
const rssRegex = /(<rss[\s\S]*?<\/rss>)/i;
|
||||
const matches = content.match(rssRegex);
|
||||
return matches ? matches[0] : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* 解析 RSS 内容
|
||||
* @param rssContent RSS 内容
|
||||
* @returns 解析后的 RSS 内容
|
||||
*/
|
||||
export const parseRSS = async (rssContent: string) => {
|
||||
const parser = new RSSParser();
|
||||
// 是否为网址
|
||||
@@ -14,7 +25,8 @@ export const parseRSS = async (rssContent: string) => {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch (_) {
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -36,6 +48,6 @@ export const parseRSS = async (rssContent: string) => {
|
||||
return items;
|
||||
} catch (error) {
|
||||
logger.error("解析 RSS 内容时出错:", error);
|
||||
return null;
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"strict": false,
|
||||
"types": ["node"],
|
||||
"jsx": "react-jsx",
|
||||
"jsxImportSource": "hono/jsx",
|
||||
|
||||
Reference in New Issue
Block a user