mirror of
https://github.com/imsyy/DailyHotApi.git
synced 2026-01-12 13:14:55 +08:00
Compare commits
43 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d18d97f72d | ||
|
|
61f89ed13d | ||
|
|
7dc310de43 | ||
|
|
7270d9cf16 | ||
|
|
17d1658818 | ||
|
|
82f983793c | ||
|
|
772f421157 | ||
|
|
c190efd005 | ||
|
|
9801c4c7ff | ||
|
|
3298e734a0 | ||
|
|
0a3869f1a2 | ||
|
|
10db165fc4 | ||
|
|
faa6f0225d | ||
|
|
755d4e22c1 | ||
|
|
72d6d970e1 | ||
|
|
150a3d9c1e | ||
|
|
259dd2c270 | ||
|
|
dad7e442da | ||
|
|
d5581815f4 | ||
|
|
c6a8c13772 | ||
|
|
46cb5ccec2 | ||
|
|
41aeba290a | ||
|
|
20a5b537f9 | ||
|
|
2ed851bdc2 | ||
|
|
dddcb27487 | ||
|
|
410d88860d | ||
|
|
22a14dd64e | ||
|
|
26f10d0c6a | ||
|
|
2a6f4db768 | ||
|
|
94ba2478d1 | ||
|
|
ec190229cc | ||
|
|
6ce0f7d8b0 | ||
|
|
2c9526b7c9 | ||
|
|
212b58ada9 | ||
|
|
16b9ad25d3 | ||
|
|
be9db7c625 | ||
|
|
e2bee69738 | ||
|
|
b033e7574c | ||
|
|
484b73c083 | ||
|
|
0fa3108b92 | ||
|
|
c1b21f1964 | ||
|
|
b31d4f59a2 | ||
|
|
38f3f38403 |
@@ -15,6 +15,7 @@ DISALLOW_ROBOT=true
|
||||
REDIS_HOST="127.0.0.1"
|
||||
REDIS_PORT=6379
|
||||
REDIS_PASSWORD=""
|
||||
REDIS_DB=0
|
||||
|
||||
# 缓存时长( 秒 )
|
||||
CACHE_TTL=3600
|
||||
@@ -26,4 +27,7 @@ REQUEST_TIMEOUT=6000
|
||||
USE_LOG_FILE=true
|
||||
|
||||
# RSS Mode
|
||||
RSS_MODE=false
|
||||
RSS_MODE=false
|
||||
|
||||
# Weibo
|
||||
FILTER_WEIBO_ADVERTISEMENT=false
|
||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -21,4 +21,9 @@ dist
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
||||
*.sw?
|
||||
|
||||
package-lock.json
|
||||
# Sentry Config File
|
||||
.env.sentry-build-plugin
|
||||
.nvmrc
|
||||
|
||||
@@ -167,6 +167,13 @@ npm run build
|
||||
npm run start
|
||||
```
|
||||
|
||||
### pm2 部署
|
||||
|
||||
```bash
|
||||
npm i pm2 -g
|
||||
sh ./deploy.sh
|
||||
```
|
||||
|
||||
成功启动后程序会在控制台输出可访问的地址
|
||||
|
||||
### Vercel 部署
|
||||
|
||||
35
deploy.sh
Normal file
35
deploy.sh
Normal file
@@ -0,0 +1,35 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 日志文件
|
||||
LOG_FILE="deploy.log"
|
||||
|
||||
# 输出时间戳的日志函数
|
||||
log() {
|
||||
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a $LOG_FILE
|
||||
}
|
||||
|
||||
# 错误处理函数
|
||||
handle_error() {
|
||||
log "错误: $1"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 开始拉去代码
|
||||
log "开始拉取代码..."
|
||||
git pull
|
||||
# 开始部署
|
||||
log "开始部署..."
|
||||
|
||||
# 安装依赖
|
||||
log "正在安装依赖..."
|
||||
npm install || handle_error "npm install 失败"
|
||||
|
||||
# 构建项目
|
||||
log "正在构建项目..."
|
||||
npm run build || handle_error "构建失败"
|
||||
|
||||
# 使用 pm2 重启或启动项目
|
||||
log "正在启动/重启服务..."
|
||||
pm2 restart daily-news || pm2 start ecosystem.config.cjs || handle_error "PM2 启动失败"
|
||||
|
||||
log "部署完成!"
|
||||
15
ecosystem.config.cjs
Normal file
15
ecosystem.config.cjs
Normal file
@@ -0,0 +1,15 @@
|
||||
module.exports = {
|
||||
apps: [{
|
||||
name: 'daily-news',
|
||||
script: 'npm',
|
||||
args: 'start',
|
||||
instances: 1,
|
||||
autorestart: true,
|
||||
watch: false,
|
||||
max_memory_restart: '1G',
|
||||
env: {
|
||||
NODE_ENV: 'development',
|
||||
PORT: 6688
|
||||
}
|
||||
}]
|
||||
}
|
||||
50
package.json
50
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dailyhot-api",
|
||||
"version": "2.0.7",
|
||||
"version": "2.0.8",
|
||||
"description": "An Api on Today's Hot list",
|
||||
"keywords": [
|
||||
"API",
|
||||
@@ -32,44 +32,50 @@
|
||||
"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",
|
||||
"start": "cross-env NODE_ENV=development tsx dist/index.js"
|
||||
"start": "cross-env NODE_ENV=development node dist/index.js"
|
||||
},
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@hono/node-server": "^1.13.7",
|
||||
"axios": "^1.7.8",
|
||||
"chalk": "^5.3.0",
|
||||
"cheerio": "^1.0.0",
|
||||
"@hono/node-server": "^1.17.1",
|
||||
"axios": "^1.11.0",
|
||||
"chalk": "^5.4.1",
|
||||
"cheerio": "^1.1.2",
|
||||
"dayjs": "^1.11.13",
|
||||
"dotenv": "^16.4.6",
|
||||
"feed": "^4.2.2",
|
||||
"flatted": "^3.3.2",
|
||||
"hono": "^4.6.12",
|
||||
"dotenv": "^17.2.1",
|
||||
"feed": "^5.1.0",
|
||||
"flatted": "^3.3.3",
|
||||
"hono": "^4.8.9",
|
||||
"iconv-lite": "^0.6.3",
|
||||
"ioredis": "^5.4.1",
|
||||
"ioredis": "^5.6.1",
|
||||
"md5": "^2.3.0",
|
||||
"node-cache": "^5.1.2",
|
||||
"node-fetch": "^3.3.2",
|
||||
"rss-parser": "^3.13.0",
|
||||
"user-agents": "^1.1.379",
|
||||
"user-agents": "^1.1.614",
|
||||
"winston": "^3.17.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.16.0",
|
||||
"@eslint/js": "^9.32.0",
|
||||
"@types/md5": "^2.3.5",
|
||||
"@types/node": "^22.10.1",
|
||||
"@types/node": "^22.16.5",
|
||||
"@types/user-agents": "^1.0.4",
|
||||
"@types/xml2js": "^0.4.14",
|
||||
"@typescript-eslint/eslint-plugin": "^8.17.0",
|
||||
"@typescript-eslint/parser": "^8.17.0",
|
||||
"@typescript-eslint/eslint-plugin": "^8.38.0",
|
||||
"@typescript-eslint/parser": "^8.38.0",
|
||||
"cross-env": "^7.0.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"
|
||||
"eslint": "^9.32.0",
|
||||
"globals": "^16.3.0",
|
||||
"prettier": "^3.6.2",
|
||||
"tsx": "^4.20.3",
|
||||
"typescript": "^5.8.3",
|
||||
"typescript-eslint": "^8.38.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=20"
|
||||
},
|
||||
"pnpm": {
|
||||
"onlyBuiltDependencies": [
|
||||
"esbuild"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
1109
pnpm-lock.yaml
generated
1109
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,9 @@ export type Config = {
|
||||
REDIS_HOST: string;
|
||||
REDIS_PORT: number;
|
||||
REDIS_PASSWORD: string;
|
||||
REDIS_DB: number;
|
||||
ZHIHU_COOKIE: string;
|
||||
FILTER_WEIBO_ADVERTISEMENT: boolean;
|
||||
};
|
||||
|
||||
// 验证并提取环境变量
|
||||
@@ -51,4 +54,7 @@ export const config: Config = {
|
||||
REDIS_HOST: getEnvVariable("REDIS_HOST") || "127.0.0.1",
|
||||
REDIS_PORT: getNumericEnvVariable("REDIS_PORT", 6379),
|
||||
REDIS_PASSWORD: getEnvVariable("REDIS_PASSWORD") || "",
|
||||
REDIS_DB: getNumericEnvVariable("REDIS_DB", 0),
|
||||
ZHIHU_COOKIE: getEnvVariable("ZHIHU_COOKIE") || "",
|
||||
FILTER_WEIBO_ADVERTISEMENT: getBooleanEnvVariable("FILTER_WEIBO_ADVERTISEMENT", false),
|
||||
};
|
||||
|
||||
68
src/router.types.d.ts
vendored
68
src/router.types.d.ts
vendored
@@ -82,12 +82,17 @@ export type RouterType = {
|
||||
};
|
||||
weibo: {
|
||||
mid: string;
|
||||
itemid: string;
|
||||
desc: string;
|
||||
scheme: string;
|
||||
word: string;
|
||||
word_scheme: string;
|
||||
note: string;
|
||||
flag_desc: string;
|
||||
num: number;
|
||||
// num: number;
|
||||
desc_extr: number;
|
||||
onboard_time: number;
|
||||
pic: string;
|
||||
};
|
||||
zhihu: {
|
||||
target: {
|
||||
@@ -95,6 +100,7 @@ export type RouterType = {
|
||||
title: string;
|
||||
excerpt: string;
|
||||
created: number;
|
||||
url: string;
|
||||
};
|
||||
children: [
|
||||
{
|
||||
@@ -377,4 +383,64 @@ export type RouterType = {
|
||||
}[];
|
||||
};
|
||||
};
|
||||
linuxdo: {
|
||||
id: string;
|
||||
title: string;
|
||||
url: string;
|
||||
author: string;
|
||||
desc: string;
|
||||
timestamp: string;
|
||||
};
|
||||
hackernews: {
|
||||
id: string;
|
||||
title: string;
|
||||
hot: number | undefined;
|
||||
timestamp: number | undefined;
|
||||
url: string;
|
||||
mobileUrl: string;
|
||||
};
|
||||
github: {
|
||||
id: string;
|
||||
title: string;
|
||||
desc?: string;
|
||||
hot: number | undefined;
|
||||
timestamp: number | undefined;
|
||||
url: string;
|
||||
mobileUrl: string;
|
||||
};
|
||||
producthunt: {
|
||||
id: string;
|
||||
title: string;
|
||||
hot: number | undefined;
|
||||
timestamp: number | undefined;
|
||||
url: string;
|
||||
mobileUrl: string;
|
||||
};
|
||||
newsmth: {
|
||||
firstArticleId: string;
|
||||
subject: string;
|
||||
article: {
|
||||
topicId: string;
|
||||
postTime: number;
|
||||
subject: string;
|
||||
body: string;
|
||||
account: {
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
board: {
|
||||
title: string;
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
gameres: {
|
||||
id: string;
|
||||
title: string;
|
||||
hot: number | undefined;
|
||||
desc: string;
|
||||
cover: string;
|
||||
timestamp: number | undefined;
|
||||
url: string;
|
||||
mobileUrl: string;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -3,20 +3,19 @@ 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";
|
||||
|
||||
import logger from "../utils/logger.js";
|
||||
const typeMap: Record<string, string> = {
|
||||
"0": "全站",
|
||||
"1": "动画",
|
||||
"3": "音乐",
|
||||
"4": "游戏",
|
||||
"5": "娱乐",
|
||||
"36": "科技",
|
||||
"188": "科技",
|
||||
"119": "鬼畜",
|
||||
"129": "舞蹈",
|
||||
"155": "时尚",
|
||||
"160": "生活",
|
||||
"168": "国创相关",
|
||||
"188": "数码",
|
||||
"181": "影视",
|
||||
};
|
||||
|
||||
@@ -44,18 +43,30 @@ export const handleRoute = async (c: ListContext, 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}`;
|
||||
const url = `https://api.bilibili.com/x/web-interface/ranking/v2?rid=${type}&type=all&${wbiData}`;
|
||||
const result = await get({
|
||||
url,
|
||||
headers: {
|
||||
Referer: `https://www.bilibili.com/ranking/all`,
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
|
||||
'Referer': 'https://www.bilibili.com/ranking/all',
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
|
||||
'Sec-Ch-Ua-Mobile': '?0',
|
||||
'Sec-Ch-Ua-Platform': '"Windows"',
|
||||
'Sec-Fetch-Dest': 'document',
|
||||
'Sec-Fetch-Mode': 'navigate',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'Sec-Fetch-User': '?1',
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
},
|
||||
noCache,
|
||||
noCache: false,
|
||||
});
|
||||
|
||||
// 是否触发风控
|
||||
if (result.data?.data?.list?.length > 0) {
|
||||
logger.info('bilibili 新接口')
|
||||
const list = result.data.data.list;
|
||||
return {
|
||||
fromCache: result.fromCache,
|
||||
@@ -75,7 +86,8 @@ const getList = async (options: Options, noCache: boolean): Promise<RouterResTyp
|
||||
}
|
||||
// 采用备用接口
|
||||
else {
|
||||
const url = `https://api.bilibili.com/x/web-interface/ranking?jsonp=jsonp?rid=${type}&type=1&callback=__jp0`;
|
||||
logger.info('bilibili 备用接口')
|
||||
const url = `https://api.bilibili.com/x/web-interface/ranking?jsonp=jsonp?rid=${type}&type=all&callback=__jp0`;
|
||||
const result = await get({
|
||||
url,
|
||||
headers: {
|
||||
|
||||
66
src/routes/gameres.ts
Normal file
66
src/routes/gameres.ts
Normal file
@@ -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,
|
||||
};
|
||||
};
|
||||
@@ -27,7 +27,6 @@ const getList = async (noCache: boolean) => {
|
||||
const post = v.post;
|
||||
return {
|
||||
id: post.id,
|
||||
|
||||
title: post.title,
|
||||
desc: post.abstract,
|
||||
cover: post.cover_url,
|
||||
|
||||
206
src/routes/github.ts
Normal file
206
src/routes/github.ts
Normal file
@@ -0,0 +1,206 @@
|
||||
// getTrending.ts
|
||||
import fetch from "node-fetch";
|
||||
import * as cheerio from "cheerio";
|
||||
import { ListContext } from "../types";
|
||||
import logger from "../utils/logger.js";
|
||||
import { getCache, setCache } from "../utils/cache.js";
|
||||
|
||||
/**
|
||||
* 定义 Trending 仓库信息的类型
|
||||
*/
|
||||
type RepoInfo = {
|
||||
owner: string; // 仓库所属者
|
||||
repo: string; // 仓库名称
|
||||
url: string; // 仓库链接
|
||||
description: string; // 仓库描述
|
||||
language: string; // 编程语言
|
||||
stars: string; // Stars (由于可能包含逗号或者其他符号,这里先用 string 存;实际可自行转 number)
|
||||
forks: string; // Forks
|
||||
todayStars?: string | number; // 今日 Star
|
||||
};
|
||||
|
||||
type TrendingRepoInfo = {
|
||||
data: RepoInfo[];
|
||||
updateTime: string;
|
||||
fromCache: boolean;
|
||||
};
|
||||
|
||||
type TrendingType = "daily" | "weekly" | "monthly";
|
||||
|
||||
const typeMap: Record<TrendingType, string> = {
|
||||
daily: "日榜",
|
||||
weekly: "周榜",
|
||||
monthly: "月榜",
|
||||
};
|
||||
|
||||
function isTrendingType(value: string): value is TrendingType {
|
||||
return ["daily", "weekly", "monthly"].includes(value as TrendingType);
|
||||
}
|
||||
|
||||
export const handleRoute = async (c: ListContext) => {
|
||||
const typeParam = c.req.query("type") || "daily";
|
||||
const type = isTrendingType(typeParam) ? typeParam : "daily";
|
||||
|
||||
const listData = await getTrendingRepos(type);
|
||||
|
||||
const routeData = {
|
||||
name: "github",
|
||||
title: "github 趋势",
|
||||
type: typeMap[type],
|
||||
params: {
|
||||
type: {
|
||||
name: '排行榜分区',
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: `https://github.com/trending?since=${type}`,
|
||||
total: listData?.data?.length || 0,
|
||||
...{
|
||||
...listData,
|
||||
data: listData?.data?.map((v, index)=>{
|
||||
return {
|
||||
id:index,
|
||||
title: v.repo,
|
||||
desc: v.description,
|
||||
hot: v.stars,
|
||||
...v
|
||||
}
|
||||
})
|
||||
}
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
/**
|
||||
* 爬取 GitHub Trending 列表
|
||||
* @param since 可选参数: 'daily' | 'weekly' | 'monthly',默认值为 'daily'
|
||||
* @returns Promise<RepoInfo[]> 返回包含热门项目信息的数组
|
||||
*/
|
||||
export async function getTrendingRepos(
|
||||
type: TrendingType | string = "daily",
|
||||
ttl = 60 * 60 * 24,
|
||||
): Promise<TrendingRepoInfo> {
|
||||
const url = `https://github.com/trending?since=${type}`;
|
||||
// 先从缓存中取
|
||||
const cachedData = await getCache(url);
|
||||
if (cachedData) {
|
||||
logger.info("💾 [CHCHE] The request is cached");
|
||||
return {
|
||||
fromCache: true,
|
||||
updateTime: cachedData.updateTime,
|
||||
data: (cachedData?.data as RepoInfo[]) || [],
|
||||
};
|
||||
}
|
||||
logger.info(`🌐 [GET] ${url}`);
|
||||
|
||||
// 更新请求头
|
||||
const headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
|
||||
'Accept-Language': 'en-US,en;q=0.5',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Connection': 'keep-alive',
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
'Sec-Fetch-Dest': 'document',
|
||||
'Sec-Fetch-Mode': 'navigate',
|
||||
'Sec-Fetch-Site': 'none',
|
||||
'Sec-Fetch-User': '?1',
|
||||
'Cache-Control': 'max-age=0',
|
||||
};
|
||||
|
||||
// 添加重试逻辑
|
||||
const maxRetries = 3;
|
||||
let lastError;
|
||||
|
||||
for (let i = 0; i < maxRetries; i++) {
|
||||
try {
|
||||
// 设置超时时间为 20 秒
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 20000);
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers,
|
||||
signal: controller.signal
|
||||
});
|
||||
clearTimeout(timeout);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
const html = await response.text();
|
||||
// 1. 加载 HTML
|
||||
const $ = cheerio.load(html);
|
||||
// 2. 存储结果的数组
|
||||
const results: RepoInfo[] = [];
|
||||
// 3. 遍历每个 article.Box-row
|
||||
$("article.Box-row").each((_, el) => {
|
||||
const $el = $(el);
|
||||
// 仓库标题和链接 (在 <h2> > <a> 里)
|
||||
const $repoAnchor = $el.find("h2 a");
|
||||
// 可能出现 "owner / repo" 这种文本
|
||||
// eg: "owner / repo"
|
||||
const fullNameText = $repoAnchor
|
||||
.text()
|
||||
.trim()
|
||||
// 可能有多余空格,可以再做一次 split
|
||||
// "owner / repo" => ["owner", "repo"]
|
||||
.replace(/\r?\n/g, "") // 去掉换行
|
||||
.replace(/\s+/g, " ") // 多空格处理
|
||||
.split("/")
|
||||
.map((s) => s.trim());
|
||||
|
||||
const owner = fullNameText[0] || "";
|
||||
const repoName = fullNameText[1] || "";
|
||||
|
||||
// href 即仓库链接
|
||||
const repoUrl = "https://github.com" + $repoAnchor.attr("href");
|
||||
|
||||
// 仓库描述 (<p class="col-9 color-fg-muted ...">)
|
||||
const description = $el.find("p.col-9.color-fg-muted").text().trim();
|
||||
|
||||
// 语言 (<span itemprop="programmingLanguage">)
|
||||
const language = $el.find('[itemprop="programmingLanguage"]').text().trim();
|
||||
|
||||
const starsText = $el.find('a[href$="/stargazers"]').text().trim();
|
||||
|
||||
const forksText = $el.find(`a[href$="/forks"]`).text().trim();
|
||||
|
||||
// 整合
|
||||
results.push({
|
||||
owner,
|
||||
repo: repoName,
|
||||
url: repoUrl || "",
|
||||
description,
|
||||
language,
|
||||
stars: starsText,
|
||||
forks: forksText,
|
||||
});
|
||||
});
|
||||
|
||||
const updateTime = new Date().toISOString();
|
||||
const data = results;
|
||||
|
||||
await setCache(url, { data, updateTime }, ttl);
|
||||
// 返回数据
|
||||
logger.info(`✅ [${response?.status}] 请求成功!`);
|
||||
return { fromCache: false, updateTime, data };
|
||||
} catch (error: Error | unknown) {
|
||||
lastError = error;
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
||||
logger.error(`❌ [ERROR] 第 ${i + 1} 请求失败: ${errorMessage}`);
|
||||
|
||||
// 如果是最后一次重试,则抛出错误
|
||||
if (i === maxRetries - 1) {
|
||||
logger.error("❌ [ERROR] 所有尝试请求失败!");
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
// 等待一段时间后重试 (1秒、2秒、4秒...)
|
||||
await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error("请求失败!");
|
||||
}
|
||||
63
src/routes/hackernews.ts
Normal file
63
src/routes/hackernews.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { load } from "cheerio";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "hackernews",
|
||||
title: "Hacker News",
|
||||
type: "Popular",
|
||||
description: "News about hacking and startups",
|
||||
link: "https://news.ycombinator.com/",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const baseUrl = "https://news.ycombinator.com";
|
||||
const result = await get({
|
||||
url: baseUrl,
|
||||
noCache,
|
||||
headers: {
|
||||
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const $ = load(result.data);
|
||||
const stories: RouterType["hackernews"][] = [];
|
||||
|
||||
$(".athing").each((_, el) => {
|
||||
const item = $(el);
|
||||
const id = item.attr("id") || "";
|
||||
const title = item.find(".titleline a").first().text().trim();
|
||||
const url = item.find(".titleline a").first().attr("href");
|
||||
|
||||
// 获取分数并转换为数字
|
||||
const scoreText = $(`#score_${id}`).text().match(/\d+/)?.[0];
|
||||
const hot = scoreText ? parseInt(scoreText, 10) : undefined;
|
||||
|
||||
if (id && title) {
|
||||
stories.push({
|
||||
id,
|
||||
title,
|
||||
hot,
|
||||
timestamp: undefined,
|
||||
url: url || `${baseUrl}/item?id=${id}`,
|
||||
mobileUrl: url || `${baseUrl}/item?id=${id}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
data: stories,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to parse HackerNews HTML: ${error}`);
|
||||
}
|
||||
};
|
||||
61
src/routes/huxiu.ts
Normal file → Executable file
61
src/routes/huxiu.ts
Normal file → Executable file
@@ -1,7 +1,7 @@
|
||||
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";
|
||||
import axios from "axios";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
@@ -16,36 +16,39 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
// 标题处理
|
||||
const titleProcessing = (text: string) => {
|
||||
const paragraphs = text.split("<br><br>");
|
||||
const title = paragraphs.shift()?.replace(/。$/, "");
|
||||
const intro = paragraphs.join("<br><br>");
|
||||
return { title, intro };
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://www.huxiu.com/moment/`;
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
// PC 端接口
|
||||
const url = `https://moment-api.huxiu.com/web-v3/moment/feed?platform=www`;
|
||||
const res = await axios.get(url, {
|
||||
headers: {
|
||||
"User-Agent": "Mozilla/5.0",
|
||||
Referer: "https://www.huxiu.com/moment/",
|
||||
},
|
||||
timeout: 10000,
|
||||
});
|
||||
// 正则查找
|
||||
const pattern =
|
||||
/<script>[\s\S]*?window\.__INITIAL_STATE__\s*=\s*(\{[\s\S]*?\});[\s\S]*?<\/script>/;
|
||||
const matchResult = result.data.match(pattern);
|
||||
const jsonObject = JSON.parse(matchResult[1]).moment.momentList.moment_list.datalist;
|
||||
const list: RouterType["huxiu"][] = res.data?.data?.moment_list?.datalist || [];
|
||||
return {
|
||||
...result,
|
||||
data: jsonObject.map((v: RouterType["huxiu"]) => ({
|
||||
id: v.object_id,
|
||||
title: titleProcessing(v.content).title,
|
||||
desc: titleProcessing(v.content).intro,
|
||||
author: v.user_info.username,
|
||||
timestamp: getTime(v.publish_time),
|
||||
hot: undefined,
|
||||
url: v.url || `https://www.huxiu.com/moment/${v.object_id}.html`,
|
||||
mobileUrl: v.url || `https://m.huxiu.com/moment/${v.object_id}.html`,
|
||||
})),
|
||||
fromCache: false,
|
||||
updateTime: new Date().toISOString(),
|
||||
data: list.map((v: RouterType["huxiu"]) => {
|
||||
const content = (v.content || "").replace(/<br\s*\/?>/gi, "\n");
|
||||
const [titleLine, ...rest] = content
|
||||
.split("\n")
|
||||
.map((s) => s.trim())
|
||||
.filter(Boolean);
|
||||
const title = titleLine?.replace(/。$/, "") || "";
|
||||
const intro = rest.join("\n");
|
||||
const momentId = v.object_id;
|
||||
return {
|
||||
id: momentId,
|
||||
title,
|
||||
desc: intro,
|
||||
author: v.user_info?.username || "",
|
||||
timestamp: getTime(v.publish_time),
|
||||
hot: v.count_info?.agree_num,
|
||||
url: `https://www.huxiu.com/moment/${momentId}.html`,
|
||||
mobileUrl: `https://m.huxiu.com/moment/${momentId}.html`,
|
||||
};
|
||||
}),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,13 +1,55 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { ListContext, RouterData } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
|
||||
const headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
|
||||
'Accept-Encoding': 'gzip, deflate, br',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
'Sec-Ch-Ua': '"Google Chrome";v="123", "Not:A-Brand";v="8", "Chromium";v="123"',
|
||||
'Sec-Ch-Ua-Mobile': '?0',
|
||||
'Sec-Ch-Ua-Platform': '"Windows"',
|
||||
'Sec-Fetch-Dest': 'document',
|
||||
'Sec-Fetch-Mode': 'navigate',
|
||||
'Sec-Fetch-Site': 'same-origin',
|
||||
'Sec-Fetch-User': '?1',
|
||||
'Upgrade-Insecure-Requests': '1',
|
||||
}
|
||||
|
||||
const category_url = 'https://api.juejin.cn/tag_api/v1/query_category_briefs'
|
||||
const getCategory = async()=>{
|
||||
const res = await get({
|
||||
url: category_url,
|
||||
headers
|
||||
})
|
||||
const data = res?.data?.data || []
|
||||
const typeObj: Record<string, string> = {}
|
||||
typeObj['1'] = '综合'
|
||||
data.forEach((c: { category_id: string; category_name: string }) => {
|
||||
typeObj[c.category_id] = c.category_name
|
||||
})
|
||||
|
||||
return typeObj
|
||||
}
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || 1;
|
||||
const listData = await getList(noCache, type);
|
||||
const typeMaps = await getCategory()
|
||||
const routeData: RouterData = {
|
||||
name: "juejin",
|
||||
title: "稀土掘金",
|
||||
type: "文章榜",
|
||||
params: {
|
||||
type: {
|
||||
name: "排行榜分区",
|
||||
type: typeMaps,
|
||||
},
|
||||
},
|
||||
link: "https://juejin.cn/hot/articles",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
@@ -15,9 +57,9 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://api.juejin.cn/content_api/v1/content/article_rank?category_id=1&type=hot`;
|
||||
const result = await get({ url, noCache });
|
||||
const getList = async (noCache: boolean, type: number | string = 1) => {
|
||||
const url = `https://api.juejin.cn/content_api/v1/content/article_rank?category_id=${type}&type=hot`;
|
||||
const result = await get({ url, noCache, headers });
|
||||
const list = result.data.data;
|
||||
return {
|
||||
...result,
|
||||
|
||||
47
src/routes/kuaishou.ts
Normal file → Executable file
47
src/routes/kuaishou.ts
Normal file → Executable file
@@ -4,6 +4,8 @@ import { get } from "../utils/getData.js";
|
||||
import { parseChineseNumber } from "../utils/getNum.js";
|
||||
import UserAgent from "user-agents";
|
||||
|
||||
const APOLLO_STATE_PREFIX = "window.__APOLLO_STATE__=";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
@@ -32,21 +34,52 @@ const getList = async (noCache: boolean) => {
|
||||
});
|
||||
const listData: ListItem[] = [];
|
||||
// 获取主要内容
|
||||
const pattern = /window.__APOLLO_STATE__=(.*);\(function\(\)/s;
|
||||
const matchResult = result.data?.match(pattern);
|
||||
const jsonObject = JSON.parse(matchResult[1])["defaultClient"];
|
||||
const html = result.data || "";
|
||||
const start = html.indexOf(APOLLO_STATE_PREFIX);
|
||||
if (start === -1) {
|
||||
throw new Error("快手页面结构变更,未找到 APOLLO_STATE");
|
||||
}
|
||||
const scriptSlice = html.slice(start + APOLLO_STATE_PREFIX.length);
|
||||
const sentinelA = scriptSlice.indexOf(";(function(");
|
||||
const sentinelB = scriptSlice.indexOf("</script>");
|
||||
const cutIndex =
|
||||
sentinelA !== -1 && sentinelB !== -1 ? Math.min(sentinelA, sentinelB) : Math.max(sentinelA, sentinelB);
|
||||
if (cutIndex === -1) {
|
||||
throw new Error("快手页面结构变更,未找到 APOLLO_STATE 结束标记");
|
||||
}
|
||||
const raw = scriptSlice.slice(0, cutIndex).trim().replace(/;$/, "");
|
||||
let jsonObject;
|
||||
try {
|
||||
// 快手返回的 JSON 末尾常带 undefined/null,需要截断到最后一个 '}' 出现
|
||||
const lastBrace = raw.lastIndexOf("}");
|
||||
const cleanRaw = lastBrace !== -1 ? raw.slice(0, lastBrace + 1) : raw;
|
||||
jsonObject = JSON.parse(cleanRaw)["defaultClient"];
|
||||
} catch (err) {
|
||||
const msg =
|
||||
err instanceof Error
|
||||
? `${err.message} | snippet=${raw.slice(0, 200)}...`
|
||||
: "未知错误";
|
||||
throw new Error(`快手数据解析失败: ${msg}`);
|
||||
}
|
||||
// 获取所有分类
|
||||
const allItems = jsonObject['$ROOT_QUERY.visionHotRank({"page":"home"})']["items"];
|
||||
const allItems =
|
||||
jsonObject['$ROOT_QUERY.visionHotRank({"page":"home"})']?.items ||
|
||||
jsonObject['$ROOT_QUERY.visionHotRank({"page":"home","platform":"web"})']
|
||||
?.items ||
|
||||
[];
|
||||
// 获取全部热榜
|
||||
allItems?.forEach((item: { id: string }) => {
|
||||
allItems.forEach((item: { id: string }) => {
|
||||
// 基础数据
|
||||
const hotItem: RouterType["kuaishou"] = jsonObject[item.id];
|
||||
if (!hotItem) return;
|
||||
const id = hotItem.photoIds?.json?.[0];
|
||||
const hotValue = hotItem.hotValue ?? "";
|
||||
const poster = hotItem.poster ? decodeURIComponent(hotItem.poster) : undefined;
|
||||
listData.push({
|
||||
id: hotItem.id,
|
||||
title: hotItem.name,
|
||||
cover: decodeURIComponent(hotItem.poster),
|
||||
hot: parseChineseNumber(hotItem.hotValue),
|
||||
cover: poster,
|
||||
hot: parseChineseNumber(String(hotValue)),
|
||||
timestamp: undefined,
|
||||
url: `https://www.kuaishou.com/short-video/${id}`,
|
||||
mobileUrl: `https://www.kuaishou.com/short-video/${id}`,
|
||||
|
||||
51
src/routes/linuxdo.ts
Normal file
51
src/routes/linuxdo.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
import { parseRSS } from "../utils/parseRSS.js";
|
||||
|
||||
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/top/weekly",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = "https://linux.do/top.rss?period=weekly";
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
headers: {
|
||||
"Accept": "application/rss+xml, application/xml;q=0.9, */*;q=0.8",
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
|
||||
},
|
||||
});
|
||||
|
||||
const items = await parseRSS(result.data);
|
||||
const list = items.map((item, index) => {
|
||||
const link = item.link || "";
|
||||
return {
|
||||
id: item.guid || link || index,
|
||||
title: item.title || "",
|
||||
desc: item.contentSnippet?.trim() || item.content?.trim() || "",
|
||||
author: item.author,
|
||||
timestamp: getTime(item.pubDate || 0),
|
||||
url: link,
|
||||
mobileUrl: link,
|
||||
hot: undefined,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
data: list,
|
||||
};
|
||||
};
|
||||
42
src/routes/newsmth.ts
Normal file
42
src/routes/newsmth.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
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";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "newsmth",
|
||||
title: "水木社区",
|
||||
type: "热门话题",
|
||||
description: "水木社区是一个源于清华的高知社群。",
|
||||
link: "https://www.newsmth.net/",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://wap.newsmth.net/wap/api/hot/global`;
|
||||
const result = await get({ url, noCache });
|
||||
const list = result.data?.data?.topics;
|
||||
return {
|
||||
...result,
|
||||
data: list.map((v: RouterType["newsmth"]) => {
|
||||
const post = v.article;
|
||||
const url = `https://wap.newsmth.net/article/${post.topicId}?title=${v.board?.title}&from=home`;
|
||||
return {
|
||||
id: v.firstArticleId,
|
||||
title: post.subject,
|
||||
desc: post.body,
|
||||
cover: undefined,
|
||||
author: post?.account?.name,
|
||||
hot: undefined,
|
||||
timestamp: getTime(post.postTime),
|
||||
url,
|
||||
mobileUrl: url,
|
||||
};
|
||||
}),
|
||||
};
|
||||
};
|
||||
60
src/routes/producthunt.ts
Normal file
60
src/routes/producthunt.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import { load } from "cheerio";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
const routeData: RouterData = {
|
||||
name: "producthunt",
|
||||
title: "Product Hunt",
|
||||
type: "Today",
|
||||
description: "The best new products, every day",
|
||||
link: "https://www.producthunt.com/",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
};
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const baseUrl = "https://www.producthunt.com";
|
||||
const result = await get({
|
||||
url: baseUrl,
|
||||
noCache,
|
||||
headers: {
|
||||
userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
const $ = load(result.data);
|
||||
const stories: RouterType["producthunt"][] = [];
|
||||
|
||||
$("[data-test=homepage-section-0] [data-test^=post-item]").each((_, el) => {
|
||||
const a = $(el).find("a").first();
|
||||
const path = a.attr("href");
|
||||
const title = $(el).find("a[data-test^=post-name]").text().trim();
|
||||
const id = $(el).attr("data-test")?.replace("post-item-", "");
|
||||
const vote = $(el).find("[data-test=vote-button]").text().trim();
|
||||
|
||||
if (path && id && title) {
|
||||
stories.push({
|
||||
id,
|
||||
title,
|
||||
hot: parseInt(vote) || undefined,
|
||||
timestamp: undefined,
|
||||
url: `${baseUrl}${path}`,
|
||||
mobileUrl: `${baseUrl}${path}`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
...result,
|
||||
data: stories,
|
||||
};
|
||||
} catch (error) {
|
||||
throw new Error(`Failed to parse Product Hunt HTML: ${error}`);
|
||||
}
|
||||
};
|
||||
@@ -2,6 +2,7 @@ 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";
|
||||
import { config } from "../config";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
@@ -18,24 +19,35 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://weibo.com/ajax/side/hotSearch`;
|
||||
const result = await get({ url, noCache, ttl: 60 });
|
||||
const url = "https://weibo.com/ajax/side/hotSearch";
|
||||
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
ttl: 60,
|
||||
headers: {
|
||||
Referer: "https://weibo.com/",
|
||||
"User-Agent":
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
||||
},
|
||||
});
|
||||
|
||||
if (!result.data?.data?.realtime) {
|
||||
return { ...result, data: [] };
|
||||
}
|
||||
|
||||
const list = result.data.data.realtime;
|
||||
return {
|
||||
...result,
|
||||
data: list.map((v: RouterType["weibo"]) => {
|
||||
const key = v.word_scheme ? v.word_scheme : `#${v.word}`;
|
||||
data: list.map((v: any, index: number) => {
|
||||
const title = v.word || v.word_scheme || `热搜${index + 1}`;
|
||||
return {
|
||||
id: v.mid,
|
||||
title: v.word,
|
||||
desc: v.note || key,
|
||||
author: v.flag_desc,
|
||||
timestamp: getTime(v.onboard_time),
|
||||
hot: v.num,
|
||||
url: `https://s.weibo.com/weibo?q=${encodeURIComponent(key)}&t=31&band_rank=1&Refer=top`,
|
||||
mobileUrl: `https://s.weibo.com/weibo?q=${encodeURIComponent(
|
||||
key,
|
||||
)}&t=31&band_rank=1&Refer=top`,
|
||||
id: v.mid || v.word_scheme || `weibo-${index}`,
|
||||
title: title,
|
||||
desc: v.word_scheme || `#${title}#`,
|
||||
timestamp: getTime(v.onboard_time || Date.now()),
|
||||
url: `https://s.weibo.com/weibo?q=${encodeURIComponent(title)}`,
|
||||
mobileUrl: `https://s.weibo.com/weibo?q=${encodeURIComponent(title)}`,
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
import type { RouterData } from "../types.js";
|
||||
import type { ListContext, RouterData } from "../types.js";
|
||||
import type { RouterType } from "../router.types.js";
|
||||
import { get } from "../utils/getData.js";
|
||||
import getWereadID from "../utils/getToken/weread.js";
|
||||
import { getTime } from "../utils/getTime.js";
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
const typeMap: Record<string, string> = {
|
||||
rising: "飙升榜",
|
||||
hot_search: "热搜榜",
|
||||
newbook: "新书榜",
|
||||
general_novel_rising: "小说榜",
|
||||
all: "总榜",
|
||||
};
|
||||
|
||||
export const handleRoute = async (c: ListContext, noCache: boolean) => {
|
||||
const type = c.req.query("type") || "rising";
|
||||
const listData = await getList(noCache, type);
|
||||
const routeData: RouterData = {
|
||||
name: "weread",
|
||||
title: "微信读书",
|
||||
type: "飙升榜",
|
||||
type: `${typeMap[type]}`,
|
||||
params: {
|
||||
type: {
|
||||
name: "排行榜分区",
|
||||
type: typeMap,
|
||||
},
|
||||
},
|
||||
link: "https://weread.qq.com/",
|
||||
total: listData.data?.length || 0,
|
||||
...listData,
|
||||
@@ -17,8 +32,8 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
return routeData;
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://weread.qq.com/web/bookListInCategory/rising?rank=1`;
|
||||
const getList = async (noCache: boolean, type='rising') => {
|
||||
const url = `https://weread.qq.com/web/bookListInCategory/${type}?rank=1`;
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
|
||||
@@ -2,6 +2,7 @@ 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";
|
||||
import { config } from "../config.js"
|
||||
|
||||
export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
const listData = await getList(noCache);
|
||||
@@ -17,13 +18,22 @@ export const handleRoute = async (_: undefined, noCache: boolean) => {
|
||||
};
|
||||
|
||||
const getList = async (noCache: boolean) => {
|
||||
const url = `https://www.zhihu.com/api/v3/feed/topstory/hot-lists/total?limit=50&desktop=true`;
|
||||
const result = await get({ url, noCache });
|
||||
const url = `https://api.zhihu.com/topstory/hot-lists/total?limit=50`;
|
||||
const result = await get({
|
||||
url,
|
||||
noCache,
|
||||
...(config.ZHIHU_COOKIE && {
|
||||
headers: {
|
||||
Cookie: config.ZHIHU_COOKIE
|
||||
}
|
||||
})
|
||||
});
|
||||
const list = result.data.data;
|
||||
return {
|
||||
...result,
|
||||
data: list.map((v: RouterType["zhihu"]) => {
|
||||
const data = v.target;
|
||||
const questionId = data.url.split("/").pop();
|
||||
return {
|
||||
id: data.id,
|
||||
title: data.title,
|
||||
@@ -31,8 +41,8 @@ const getList = async (noCache: boolean) => {
|
||||
cover: v.children[0].thumbnail,
|
||||
timestamp: getTime(data.created),
|
||||
hot: parseFloat(v.detail_text.split(" ")[0]) * 10000,
|
||||
url: `https://www.zhihu.com/question/${data.id}`,
|
||||
mobileUrl: `https://www.zhihu.com/question/${data.id}`,
|
||||
url: `https://www.zhihu.com/question/${questionId}`,
|
||||
mobileUrl: `https://www.zhihu.com/question/${questionId}`,
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
@@ -26,6 +26,7 @@ const redis = new Redis({
|
||||
host: config.REDIS_HOST,
|
||||
port: config.REDIS_PORT,
|
||||
password: config.REDIS_PASSWORD,
|
||||
db: config.REDIS_DB,
|
||||
maxRetriesPerRequest: 5,
|
||||
// 重试策略:最小延迟 50ms,最大延迟 2s
|
||||
retryStrategy: (times) => Math.min(times * 50, 2000),
|
||||
|
||||
@@ -30,6 +30,25 @@ const getRSS = (data: RouterData) => {
|
||||
name: item.author,
|
||||
},
|
||||
],
|
||||
extensions: [
|
||||
{
|
||||
name: "media:content",
|
||||
objects: {
|
||||
_attributes: {
|
||||
"xmlns:media": "http://search.yahoo.com/mrss/",
|
||||
url: item.cover,
|
||||
},
|
||||
"media:thumbnail": {
|
||||
_attributes: {
|
||||
url: item.cover,
|
||||
},
|
||||
},
|
||||
"media:description": item.desc ? {
|
||||
_cdata: item.desc
|
||||
} : "",
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
const rssData = feed.rss2();
|
||||
|
||||
@@ -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