Merge branch 'master' of github.com:imsyy/DailyHotApi

This commit is contained in:
imsyy
2025-05-11 00:01:43 +08:00
15 changed files with 1158 additions and 421 deletions

5
.gitignore vendored
View File

@@ -22,3 +22,8 @@ dist
*.njsproj
*.sln
*.sw?
package-lock.json
# Sentry Config File
.env.sentry-build-plugin
.nvmrc

View File

@@ -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
View 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
View 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
}
}]
}

View File

@@ -32,44 +32,51 @@
"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",
"@hono/node-server": "^1.14.0",
"axios": "^1.8.4",
"chalk": "^5.4.1",
"cheerio": "^1.0.0",
"dayjs": "^1.11.13",
"dotenv": "^16.4.6",
"dotenv": "^16.4.7",
"feed": "^4.2.2",
"flatted": "^3.3.2",
"hono": "^4.6.12",
"flatted": "^3.3.3",
"hono": "^4.7.5",
"iconv-lite": "^0.6.3",
"ioredis": "^5.4.1",
"ioredis": "^5.6.0",
"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.496",
"winston": "^3.17.0"
},
"devDependencies": {
"@eslint/js": "^9.16.0",
"@eslint/js": "^9.23.0",
"@types/cheerio": "^0.22.35",
"@types/md5": "^2.3.5",
"@types/node": "^22.10.1",
"@types/node": "^22.13.17",
"@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.29.0",
"@typescript-eslint/parser": "^8.29.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.23.0",
"globals": "^15.15.0",
"prettier": "^3.5.3",
"tsx": "^4.19.3",
"typescript": "^5.8.2",
"typescript-eslint": "^8.29.0"
},
"engines": {
"node": ">=20"
},
"pnpm": {
"onlyBuiltDependencies": [
"esbuild"
]
}
}

934
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

34
src/router.types.d.ts vendored
View File

@@ -95,6 +95,7 @@ export type RouterType = {
title: string;
excerpt: string;
created: number;
url: string;
};
children: [
{
@@ -377,6 +378,39 @@ 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;

View File

@@ -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: {

206
src/routes/github.ts Normal file
View 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
View 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}`);
}
};

View File

@@ -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,

57
src/routes/linuxdo.ts Normal file
View File

@@ -0,0 +1,57 @@
import type { RouterData } from "../types.js";
import { get } from "../utils/getData.js";
import { getTime } from "../utils/getTime.js";
interface Topic {
id: number;
title: string;
excerpt: string;
last_poster_username: string;
created_at: string;
views: number;
like_count: number;
}
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/hot",
total: listData.data?.length || 0,
...listData,
};
return routeData;
};
const getList = async (noCache: boolean) => {
const url = "https://linux.do/top/weekly.json";
const result = await get({
url,
noCache,
headers: {
"Accept": "application/json",
}
});
const topics = result.data.topic_list.topics as Topic[];
const list = topics.map((topic) => {
return {
id: topic.id,
title: topic.title,
desc: topic.excerpt,
author: topic.last_poster_username,
timestamp: getTime(topic.created_at),
url: `https://linux.do/t/${topic.id}`,
mobileUrl: `https://linux.do/t/${topic.id}`,
hot: topic.views || topic.like_count
};
});
return {
...result,
data: list
};
};

60
src/routes/producthunt.ts Normal file
View 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}`);
}
};

View File

@@ -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,

View File

@@ -24,6 +24,7 @@ const getList = async (noCache: boolean) => {
...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 +32,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}`,
};
}),
};