feat: 新增一些接口

This commit is contained in:
imsyy
2024-04-16 15:06:02 +08:00
parent 067190b5a2
commit cdf2479044
14 changed files with 340 additions and 44 deletions

View File

@@ -1,17 +1,17 @@
<div align="center">
<img alt="logo" height="120" src="./public/favicon.png" width="120"/>
<p size="20">今日热榜</p>
<h2>今日热榜</h2>
<p>一个聚合热门数据的 API 接口</p>
</div>
## 特性
## 🚩 特性
- 极快响应,便于开发
- 支持 RSS 模式和 JSON 模式
- 支持多种部署方式
- 简明的路由目录,便于新增
## 示例
## 👀 示例
> 这里是使用该 API 的示例站点
> 示例站点可能由于访问量或者长久未维护而访问异常
@@ -19,12 +19,12 @@
- [今日热榜 - https://hot.imsyy.top/](https://hot.imsyy.top/)
## 总览
## 🎉 总览
> 🟢 状态正常 / 🟠 可能失效 / ❌ 无法使用
> 🟢 状态正常 / 🟠 可能失效 / ❌ 无法使用 / ⚠️ 需要科学上网
| **站点** | **类别** | **调用名称** | **状态** |
| -------------- | -------- | ------------ | -------- |
| ---------------- | ------------ | -------------- | -------- |
| 哔哩哔哩 | 热门榜 | bilibili | 🟢 |
| AcFun | 排行榜 | acfun | 🟢 |
| 微博 | 热搜榜 | weibo | 🟢 |
@@ -36,11 +36,13 @@
| 豆瓣讨论小组 | 讨论精选 | douban-group | 🟢 |
| 百度贴吧 | 热议榜 | tieba | 🟢 |
| 少数派 | 热榜 | sspai | 🟢 |
| IT 之家 | 热榜 | ithome | 🟠 |
| IT之家 | 热榜 | ithome | 🟠 |
| IT之家「喜加一」 | 最新动态 | ithome-xijiayi | 🟠 |
| 简书 | 热门推荐 | jianshu | 🟠 |
| 澎湃新闻 | 热榜 | thepaper | 🟢 |
| 今日头条 | 热榜 | toutiao | 🟢 |
| 36 氪 | 热榜 | 36kr | 🟢 |
| 51CTO | 推荐榜 | 51cto | 🟢 |
| 稀土掘金 | 热榜 | juejin | 🟢 |
| 腾讯新闻 | 热点榜 | qq-news | 🟢 |
| 网易新闻 | 热点榜 | netease-news | 🟢 |
@@ -50,9 +52,12 @@
| 崩坏:星穹铁道 | 最新动态 | starrail | 🟢 |
| 微信读书 | 飙升榜 | weread | 🟢 |
| NGA | 热帖 | ngabbs | 🟢 |
| V2EX | 主题榜 | v2ex | ⚠️ |
| HelloGitHub | Trending | hellogithub | 🟢 |
| 中央气象台 | 全国气象预警 | weatheralarm | 🟢 |
| 中国地震台 | 地震速报 | earthquake | 🟢 |
## 部署
## ⚙️ 部署
具体使用说明可参考 [我的博客](https://blog.imsyy.top/posts/2024/0408),下方仅讲解基础操作:
@@ -128,14 +133,24 @@ npm run start
若您目前仅能通过 `Vercel` 进行部署,那么请暂时不要使用最新版本
## 其他
## ⚠️ 须知
- 本项目为了避免频繁请求官方数据,默认对数据做了缓存处理,默认为 `60` 分钟,如需更改,请自行修改配置
- 本项目部分接口使用了 **页面爬虫**,若违反对应页面的相关规则,请 **及时通知我去除该接口**
## 免责声明
## 📢 免责声明
- 本项目提供的 `API` 仅供开发者进行技术研究和开发测试使用。使用该 `API` 获取的信息仅供参考,不代表本项目对信息的准确性、可靠性、合法性、完整性作出任何承诺或保证。本项目不对任何因使用该 `API` 获取信息而导致的任何直接或间接损失负责。本项目保留随时更改 `API` 接口地址、接口协议、接口参数及其他相关内容的权利。本项目对使用者使用 `API` 的行为不承担任何直接或间接的法律责任
- 本项目并未与相关信息提供方建立任何关联或合作关系,获取的信息均来自公开渠道,如因使用该 `API` 获取信息而产生的任何法律责任,由使用者自行承担
- 本项目对使用 `API` 获取的信息进行了最大限度的筛选和整理,但不保证信息的准确性和完整性。使用 `API` 获取信息时,请务必自行核实信息的真实性和可靠性,谨慎处理相关事项
- 本项目保留对 `API` 的随时更改、停用、限制使用等措施的权利。任何因使用本 `API` 产生的损失,本项目不负担任何赔偿和责任
## 😘 鸣谢
特此感谢为本项目提供支持与灵感的项目
- [RSSHub](https://github.com/DIYgod/RSSHub)
## ⭐ Star History
[![Star History Chart](https://api.star-history.com/svg?repos=imsyy/DailyHotApi&type=Date)](https://star-history.com/#imsyy/DailyHotApi&Date)

View File

@@ -1,6 +1,6 @@
{
"name": "dailyhot_api",
"version": "2.0.0",
"version": "2.0.0-rc.2",
"description": "An Api on Today's Hot list",
"keywords": [
"API",

View File

@@ -32,6 +32,13 @@ export type RouterType = {
url: string;
type: number;
};
"51cto": {
title: string;
url: string;
cover: string;
abstract: string;
source_id: number;
};
bilibili: {
bvid: string;
title: string;
@@ -185,4 +192,26 @@ export type RouterType = {
summary: string;
clicks_total: number;
};
v2ex: {
title: string;
url: string;
content: string;
id: number;
replies: number;
member: {
username: string;
};
};
earthquake: {
NEW_DID: string;
LOCATION_C: string;
M: string;
};
weatheralarm: {
alertid: string;
issuetime: string;
title: string;
url: string;
pic: string;
};
};

54
src/routes/51cto.ts Normal file
View File

@@ -0,0 +1,54 @@
import type { RouterData } from "../types.js";
import type { RouterType } from "../router.types.js";
import { getToken, sign } from "../utils/getToken/51cto.js";
import { get } from "../utils/getData.js";
export const handleRoute = async (_: undefined, noCache: boolean) => {
const { fromCache, data, updateTime } = await getList(noCache);
const routeData: RouterData = {
name: "51cto",
title: "51CTO",
type: "推荐榜",
link: "https://www.51cto.com/",
total: data?.length || 0,
updateTime,
fromCache,
data,
};
return routeData;
};
const getList = async (noCache: boolean) => {
const url = `https://api-media.51cto.com/index/index/recommend`;
const params = {
page: 1,
page_size: 50,
limit_time: 0,
name_en: "",
};
const timestamp = Date.now();
const token = (await getToken()) as string;
const result = await get({
url,
params: {
...params,
timestamp,
token,
sign: sign("index/index/recommend", params, timestamp, token),
},
noCache,
});
const list = result.data.data.data.list;
return {
fromCache: result.fromCache,
updateTime: result.updateTime,
data: list.map((v: RouterType["51cto"]) => ({
id: v.source_id,
title: v.title,
cover: v.cover,
desc: v.abstract,
url: v.url,
mobileUrl: v.url,
})),
};
};

View File

@@ -1,7 +1,7 @@
import type { RouterData, ListContext, Options } from "../types.js";
import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
import getBiliWbi from "../utils/getBiliWbi.js";
import getBiliWbi from "../utils/getToken/bilibili.js";
export const handleRoute = async (c: ListContext, noCache: boolean) => {
const type = c.req.query("type") || "0";

76
src/routes/earthquake.ts Normal file
View File

@@ -0,0 +1,76 @@
import type { RouterData, ListContext, Options } from "../types.js";
import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
const mappings = {
O_TIME: "发震时刻(UTC+8)",
LOCATION_C: "参考位置",
M: "震级(M)",
EPI_LAT: "纬度(°)",
EPI_LON: "经度(°)",
EPI_DEPTH: "深度(千米)",
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);
const routeData: RouterData = {
name: "earthquake",
title: "中国地震台",
type: "地震速报",
parameData: {
type: {
name: "速报分类",
type: {
...typeMappings,
},
},
},
link: "https://news.ceic.ac.cn/",
total: data?.length || 0,
updateTime,
fromCache,
data,
};
return routeData;
};
const getList = async (options: Options, noCache: boolean) => {
const { type } = options;
const url = `http://www.ceic.ac.cn/ajax/speedsearch?num=${type}`;
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;
return {
fromCache: result.fromCache,
updateTime: result.updateTime,
data: list.map((v: RouterType["earthquake"]) => {
const contentBuilder = [];
const { NEW_DID, LOCATION_C, M } = v;
for (const mappingsKey in mappings) {
contentBuilder.push(`${mappings[mappingsKey]}${v[mappingsKey]}`);
}
return {
id: NEW_DID,
title: `${LOCATION_C}发生${M}级地震`,
desc: contentBuilder.join("\n"),
url: `https://news.ceic.ac.cn/${NEW_DID}.html`,
mobileUrl: `https://news.ceic.ac.cn/${NEW_DID}.html`,
};
}),
};
};

View File

@@ -5,7 +5,7 @@ import { get } from "../utils/getData.js";
export const handleRoute = async (_: undefined, noCache: boolean) => {
const { fromCache, data, updateTime } = await getList(noCache);
const routeData: RouterData = {
name: "ithome",
name: "ithome-xijiayi",
title: "IT之家「喜加一」",
type: "最新动态",
description: "最新最全的「喜加一」游戏动态尽在这里!",

48
src/routes/v2ex.ts Normal file
View File

@@ -0,0 +1,48 @@
import type { RouterData, ListContext, Options } from "../types.js";
import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
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: "v2ex",
title: "V2EX",
type: "主题榜",
parameData: {
type: {
name: "榜单分类",
type: {
hot: "最热主题",
latest: "最新主题",
},
},
},
link: "https://www.v2ex.com/",
total: data?.length || 0,
updateTime,
fromCache,
data,
};
return routeData;
};
const getList = async (options: Options, noCache: boolean) => {
const { type } = options;
const url = `https://www.v2ex.com/api/topics/${type}.json`;
const result = await get({ url, noCache });
const list = result.data.data.list;
return {
fromCache: result.fromCache,
updateTime: result.updateTime,
data: list.map((v: RouterType["v2ex"]) => ({
id: v.id,
title: v.title,
desc: v.content,
author: v.member.username,
hot: v.replies,
url: v.url,
mobileUrl: v.url,
})),
};
};

View File

@@ -0,0 +1,45 @@
import type { RouterData, ListContext, Options } from "../types.js";
import type { RouterType } from "../router.types.js";
import { get } from "../utils/getData.js";
export const handleRoute = async (c: ListContext, noCache: boolean) => {
const province = c.req.query("province") || "";
const { fromCache, data, type, updateTime } = await getList({ province }, noCache);
const routeData: RouterData = {
name: "weatheralarm",
title: "中央气象台",
type: type || "全国气象预警",
parameData: {
province: {
name: "预警区域",
value: "省份名称( 例如:广东省 ",
},
},
link: "http://nmc.cn/publish/alarm.html",
total: data?.length || 0,
updateTime,
fromCache,
data,
};
return routeData;
};
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 result = await get({ url, noCache });
const list = result.data.data.page.list;
return {
fromCache: result.fromCache,
updateTime: result.updateTime,
type: province + "气象预警",
data: list.map((v: RouterType["weatheralarm"]) => ({
id: v.alertid,
title: v.title,
desc: v.issuetime + " " + v.title,
cover: v.pic,
url: `http://nmc.cn${v.url}`,
mobileUrl: `http://nmc.cn${v.url}`,
})),
};
};

View 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 getWereadID from "../utils/getWereadID.js";
import getWereadID from "../utils/getToken/weread.js";
export const handleRoute = async (_: undefined, noCache: boolean) => {
const { fromCache, data, updateTime } = await getList(noCache);

View File

@@ -37,7 +37,7 @@ request.interceptors.response.use(
// GET
export const get = async (options: Get) => {
const { url, headers, noCache, ttl = config.CACHE_TTL, originaInfo = false } = options;
const { url, headers, params, noCache, ttl = config.CACHE_TTL, originaInfo = false } = options;
logger.info("发起 GET 请求", options);
try {
// 检查缓存
@@ -51,7 +51,7 @@ export const get = async (options: Get) => {
}
// 缓存不存在时请求接口
logger.info("请求接口", { url });
const response = await request.get(url, { headers });
const response = await request.get(url, { headers, params });
const responseData = response?.data || response;
// 存储新获取的数据到缓存
const updateTime = new Date().toISOString();

View File

@@ -0,0 +1,29 @@
import { getCache, setCache } from "../cache.js";
import { get } from "../getData.js";
import md5 from "md5";
export const getToken = async () => {
const cachedData = getCache("51cto-token");
if (cachedData && typeof cachedData === "object" && "token" in cachedData) {
const { token } = cachedData as { token: string };
return token;
}
const result = await get({
url: "https://api-media.51cto.com/api/token-get",
});
const token = result.data.data.data.token;
setCache("51cto-token", { token });
return token;
};
export const sign = (
requestPath: string,
payload: Record<string, unknown> = {},
timestamp: number,
token: string,
) => {
payload.timestamp = timestamp;
payload.token = token;
const sortedParams = Object.keys(payload).sort();
return md5(md5(requestPath) + md5(sortedParams + md5(token) + timestamp));
};

View File

@@ -1,6 +1,6 @@
// 获取 Bilibili Web 端 WBI 签名鉴权
import { getCache, setCache } from "./cache.js";
import { get } from "./getData.js";
import { getCache, setCache } from "../cache.js";
import { get } from "../getData.js";
import md5 from "md5";
type EncodedKeys = {