feat: 新增获取全部接口信息

This commit is contained in:
imsyy 2023-07-03 16:14:40 +08:00
parent ffc9e7cb37
commit e5bd04fde1
19 changed files with 301 additions and 171 deletions

View File

@ -32,16 +32,25 @@
| 腾讯新闻 | 热点榜 | newsqq | 🟢 | | 腾讯新闻 | 热点榜 | newsqq | 🟢 |
| 抖音热榜 | 热点榜 | douyin | 🟢 | | 抖音热榜 | 热点榜 | douyin | 🟢 |
| 英雄联盟 | 更新公告 | lol | 🟢 | | 英雄联盟 | 更新公告 | lol | 🟢 |
| 微信读书 | 飙升榜 | weread | 🟢 |
| 历史上的今天 | 指定日期 | calendar | 🟢 | | 历史上的今天 | 指定日期 | calendar | 🟢 |
### 特殊接口说明 ### 特殊接口说明
#### 获取全部接口信息
获取除了下方特殊接口外的全部接口列表
```http
GET https://{example.com}/all
```
#### 历史上的今天(指定日期) #### 历史上的今天(指定日期)
将指定的月份和日期传入即可得到当天数据,请注意格式 将指定的月份和日期传入即可得到当天数据,请注意格式
```http ```http
GET https://api-hot.imsyy.top/calendar/date?month=06&day=01 GET https://{example.com}/calendar/date?month=06&day=01
``` ```
## 部署 ## 部署

View File

@ -1,6 +1,6 @@
{ {
"name": "dailyhot_api", "name": "dailyhot_api",
"version": "0.0.2", "version": "1.0.0",
"description": "一个今日热榜", "description": "一个今日热榜",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

View File

@ -3,6 +3,12 @@ const krRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "36氪",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "krData"; const cacheKey = "krData";
@ -56,8 +62,7 @@ krRouter.get("/36kr", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "36氪", ...routerInfo,
subtitle: "热榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -67,8 +72,7 @@ krRouter.get("/36kr", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "36氪", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -95,8 +99,7 @@ krRouter.get("/36kr/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "36氪", ...routerInfo,
subtitle: "热榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -114,8 +117,7 @@ krRouter.get("/36kr/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "36氪", ...routerInfo,
subtitle: "热榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -124,12 +126,12 @@ krRouter.get("/36kr/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "36氪", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
krRouter.info = routerInfo;
module.exports = krRouter; module.exports = krRouter;

View File

@ -3,6 +3,9 @@ const baiduRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = { title: "百度", subtitle: "热搜榜" };
// 缓存键名 // 缓存键名
const cacheKey = "baiduData"; const cacheKey = "baiduData";
@ -54,8 +57,7 @@ baiduRouter.get("/baidu", async (ctx) => {
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "百度", ...routerInfo,
subtitle: "热搜榜",
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -66,8 +68,7 @@ baiduRouter.get("/baidu", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "百度", ...routerInfo,
subtitle: "热搜榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -96,8 +97,7 @@ baiduRouter.get("/baidu/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "百度", ...routerInfo,
subtitle: "热搜榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -115,8 +115,7 @@ baiduRouter.get("/baidu/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "百度", ...routerInfo,
subtitle: "热搜榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -125,12 +124,12 @@ baiduRouter.get("/baidu/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "百度", ...routerInfo,
subtitle: "热搜榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
baiduRouter.info = routerInfo;
module.exports = baiduRouter; module.exports = baiduRouter;

View File

@ -3,6 +3,12 @@ const bilibiliRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "哔哩哔哩",
subtitle: "热门榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "bilibiliData"; const cacheKey = "bilibiliData";
@ -50,8 +56,7 @@ bilibiliRouter.get("/bilibili", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "哔哩哔哩", ...routerInfo,
subtitle: "热门榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -61,9 +66,8 @@ bilibiliRouter.get("/bilibili", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "哔哩哔哩", ...routerInfo,
subtitle: "热门榜", message: "获取失败",
message: "哔哩哔哩热门榜获取失败",
}; };
} }
}); });
@ -82,8 +86,7 @@ bilibiliRouter.get("/bilibili/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "哔哩哔哩", ...routerInfo,
subtitle: "热门榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -101,8 +104,7 @@ bilibiliRouter.get("/bilibili/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "哔哩哔哩", ...routerInfo,
subtitle: "热门榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -111,12 +113,12 @@ bilibiliRouter.get("/bilibili/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "哔哩哔哩", ...routerInfo,
subtitle: "热门榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
bilibiliRouter.info = routerInfo;
module.exports = bilibiliRouter; module.exports = bilibiliRouter;

View File

@ -3,6 +3,12 @@ const douyinRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "抖音",
subtitle: "热点榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "douyinHotData"; const cacheKey = "douyinHotData";
const cacheCookieKey = "douyinCookieData"; const cacheCookieKey = "douyinCookieData";
@ -95,8 +101,7 @@ douyinRouter.get("/douyin", async (ctx) => {
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "抖音热点榜", ...routerInfo,
subtitle: "热点榜",
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -107,8 +112,7 @@ douyinRouter.get("/douyin", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "抖音热点榜", ...routerInfo,
subtitle: "热点榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -118,6 +122,7 @@ douyinRouter.get("/douyin", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
...routerInfo,
message: "获取失败", message: "获取失败",
}; };
} }
@ -142,8 +147,7 @@ douyinRouter.get("/douyin/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "抖音热点榜", ...routerInfo,
subtitle: "热点榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -161,8 +165,7 @@ douyinRouter.get("/douyin/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "抖音热点榜", ...routerInfo,
subtitle: "热点榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -171,12 +174,12 @@ douyinRouter.get("/douyin/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "抖音热点榜", ...routerInfo,
subtitle: "热点榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
douyinRouter.info = routerInfo;
module.exports = douyinRouter; module.exports = douyinRouter;

View File

@ -4,6 +4,14 @@ const Router = require("koa-router");
const router = new Router(); const router = new Router();
// 全部路由数据
const allRouterInfo = {
name: "全部接口",
subtitle: "除了特殊接口外的全部接口列表",
total: 0,
data: [],
};
// 根目录 // 根目录
router.get("/", async (ctx) => { router.get("/", async (ctx) => {
await ctx.render("index"); await ctx.render("index");
@ -17,10 +25,43 @@ fs.readdirSync(__dirname)
const routerModule = require(routerPath); const routerModule = require(routerPath);
// 自动注册路由 // 自动注册路由
if (routerModule instanceof Router) { if (routerModule instanceof Router) {
// 写入路由数据
if (routerModule?.info) {
allRouterInfo.total++;
allRouterInfo.data.push({
...routerModule.info,
stack: routerModule.stack,
});
}
// 引用路由
router.use(routerModule.routes()); router.use(routerModule.routes());
} }
}); });
// 全部接口路由
router.get("/all", async (ctx) => {
console.log("获取全部接口路由");
if (allRouterInfo.total > 0) {
ctx.body = {
code: 200,
message: "获取成功",
...allRouterInfo,
};
} else if (allRouterInfo.total === 0) {
ctx.body = {
code: 200,
message: "暂无接口,请添加",
...allRouterInfo,
};
} else {
ctx.body = {
code: 500,
message: "获取失败",
...allRouterInfo,
};
}
});
// 404 路由 // 404 路由
router.use(async (ctx) => { router.use(async (ctx) => {
await ctx.render("404"); await ctx.render("404");

View File

@ -4,6 +4,12 @@ const axios = require("axios");
const cheerio = require("cheerio"); const cheerio = require("cheerio");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "IT之家",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "itHomeData"; const cacheKey = "itHomeData";
@ -78,8 +84,7 @@ itHomeRouter.get("/ithome", async (ctx) => {
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "IT之家", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -90,8 +95,7 @@ itHomeRouter.get("/ithome", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "IT之家", ...routerInfo,
subtitle: "热榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -101,8 +105,7 @@ itHomeRouter.get("/ithome", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "IT之家", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -122,8 +125,7 @@ itHomeRouter.get("/ithome/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "IT之家", ...routerInfo,
subtitle: "热榜",
updateTime, updateTime,
total: data.length, total: data.length,
data: newData, data: newData,
@ -141,8 +143,7 @@ itHomeRouter.get("/ithome/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "IT之家", ...routerInfo,
subtitle: "热榜",
total: data.length, total: data.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -151,12 +152,12 @@ itHomeRouter.get("/ithome/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "IT之家", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
itHomeRouter.info = routerInfo;
module.exports = itHomeRouter; module.exports = itHomeRouter;

View File

@ -3,6 +3,12 @@ const juejinRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "稀土掘金",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "juejinData"; const cacheKey = "juejinData";
@ -47,8 +53,7 @@ juejinRouter.get("/juejin", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "稀土掘金", ...routerInfo,
subtitle: "热榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -58,8 +63,7 @@ juejinRouter.get("/juejin", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "稀土掘金", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -79,8 +83,7 @@ juejinRouter.get("/juejin/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "稀土掘金", ...routerInfo,
subtitle: "热榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -98,8 +101,7 @@ juejinRouter.get("/juejin/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "稀土掘金", ...routerInfo,
subtitle: "热榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -108,12 +110,12 @@ juejinRouter.get("/juejin/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "稀土掘金", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
juejinRouter.info = routerInfo;
module.exports = juejinRouter; module.exports = juejinRouter;

View File

@ -3,6 +3,12 @@ const lolRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "英雄联盟",
subtitle: "更新公告",
};
// 缓存键名 // 缓存键名
const cacheKey = "lolData"; const cacheKey = "lolData";
@ -10,24 +16,29 @@ const cacheKey = "lolData";
let updateTime = new Date().toISOString(); let updateTime = new Date().toISOString();
// 调用路径 // 调用路径
const url = "https://apps.game.qq.com/cmc/zmMcnTargetContentList?r0=jsonp&page=1&num=16&target=24&source=web_pc&r1=jQuery191002324053053181463_1687855508930&_=1687855508933"; const url =
"https://apps.game.qq.com/cmc/zmMcnTargetContentList?r0=jsonp&page=1&num=16&target=24&source=web_pc&r1=jQuery191002324053053181463_1687855508930&_=1687855508933";
// 数据处理 // 数据处理
const getData = (data) => { const getData = (data) => {
if (!data) return []; if (!data) return [];
const dataList = []; const dataList = [];
try { try {
const pattern = /jQuery191002324053053181463_1687855508930\((.*?)\)/s; const pattern = /jQuery191002324053053181463_1687855508930\((.*?)\)/s;
const matchResult = data.match(pattern); const matchResult = data.match(pattern);
const jsonObject = JSON.parse(matchResult[1])['data'].result; const jsonObject = JSON.parse(matchResult[1])["data"].result;
jsonObject.forEach((v) => { jsonObject.forEach((v) => {
dataList.push({ dataList.push({
title: v.sTitle, title: v.sTitle,
desc: v.sAuthor, desc: v.sAuthor,
pic: `https:${v.sIMG}`, pic: `https:${v.sIMG}`,
hot: Number(v.iTotalPlay), hot: Number(v.iTotalPlay),
url: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent(v.iDocID)}`, url: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent(
mobileUrl: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent(v.iDocID)}`, v.iDocID
)}`,
mobileUrl: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent(
v.iDocID
)}`,
}); });
}); });
return dataList; return dataList;
@ -54,8 +65,7 @@ lolRouter.get("/lol", async (ctx) => {
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "英雄联盟", ...routerInfo,
subtitle: "更新公告",
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -66,8 +76,7 @@ lolRouter.get("/lol", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "英雄联盟", ...routerInfo,
subtitle: "更新公告",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -96,8 +105,7 @@ lolRouter.get("/lol/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "英雄联盟", ...routerInfo,
subtitle: "更新公告",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -115,8 +123,7 @@ lolRouter.get("/lol/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "英雄联盟", ...routerInfo,
subtitle: "更新公告",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -125,12 +132,12 @@ lolRouter.get("/lol/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "英雄联盟", ...routerInfo,
subtitle: "更新公告",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
lolRouter.info = routerInfo;
module.exports = lolRouter; module.exports = lolRouter;

View File

@ -3,6 +3,12 @@ const newsqqRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "腾讯新闻",
subtitle: "热点榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "newsqqData"; const cacheKey = "newsqqData";
@ -49,8 +55,7 @@ newsqqRouter.get("/newsqq", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "腾讯新闻", ...routerInfo,
subtitle: "热点榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -60,8 +65,7 @@ newsqqRouter.get("/newsqq", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "腾讯新闻", ...routerInfo,
subtitle: "热点榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -81,8 +85,7 @@ newsqqRouter.get("/newsqq/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "腾讯新闻", ...routerInfo,
subtitle: "热点榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -100,8 +103,7 @@ newsqqRouter.get("/newsqq/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "腾讯新闻", ...routerInfo,
subtitle: "热点榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -110,12 +112,12 @@ newsqqRouter.get("/newsqq/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "腾讯新闻", ...routerInfo,
subtitle: "热点榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
newsqqRouter.info = routerInfo;
module.exports = newsqqRouter; module.exports = newsqqRouter;

View File

@ -3,6 +3,12 @@ const sspaiRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "少数派",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "sspaiData"; const cacheKey = "sspaiData";
@ -49,8 +55,7 @@ sspaiRouter.get("/sspai", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "少数派", ...routerInfo,
subtitle: "最热",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -60,8 +65,7 @@ sspaiRouter.get("/sspai", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "少数派", ...routerInfo,
subtitle: "最热",
message: "获取失败", message: "获取失败",
}; };
} }
@ -81,8 +85,7 @@ sspaiRouter.get("/sspai/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "少数派", ...routerInfo,
subtitle: "最热",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -100,8 +103,7 @@ sspaiRouter.get("/sspai/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "少数派", ...routerInfo,
subtitle: "最热",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -110,12 +112,12 @@ sspaiRouter.get("/sspai/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "少数派", ...routerInfo,
subtitle: "最热",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
sspaiRouter.info = routerInfo;
module.exports = sspaiRouter; module.exports = sspaiRouter;

View File

@ -3,6 +3,12 @@ const thepaperRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "澎湃新闻",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "thepaperData"; const cacheKey = "thepaperData";
@ -48,8 +54,7 @@ thepaperRouter.get("/thepaper", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "澎湃新闻", ...routerInfo,
subtitle: "热榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -59,8 +64,7 @@ thepaperRouter.get("/thepaper", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "澎湃新闻", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -80,8 +84,7 @@ thepaperRouter.get("/thepaper/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "澎湃新闻", ...routerInfo,
subtitle: "热榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -99,8 +102,7 @@ thepaperRouter.get("/thepaper/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "澎湃新闻", ...routerInfo,
subtitle: "热榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -109,12 +111,12 @@ thepaperRouter.get("/thepaper/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "澎湃新闻", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
thepaperRouter.info = routerInfo;
module.exports = thepaperRouter; module.exports = thepaperRouter;

View File

@ -3,6 +3,12 @@ const tiebaRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "百度贴吧",
subtitle: "热议榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "tiebaData"; const cacheKey = "tiebaData";
@ -48,8 +54,7 @@ tiebaRouter.get("/tieba", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "百度贴吧", ...routerInfo,
subtitle: "热议榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -59,8 +64,7 @@ tiebaRouter.get("/tieba", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "百度贴吧", ...routerInfo,
subtitle: "热议榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -80,8 +84,7 @@ tiebaRouter.get("/tieba/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "百度贴吧", ...routerInfo,
subtitle: "热议榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -99,8 +102,7 @@ tiebaRouter.get("/tieba/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "百度贴吧", ...routerInfo,
subtitle: "热议榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -109,12 +111,12 @@ tiebaRouter.get("/tieba/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "百度贴吧", ...routerInfo,
subtitle: "热议榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
tiebaRouter.info = routerInfo;
module.exports = tiebaRouter; module.exports = tiebaRouter;

View File

@ -3,6 +3,12 @@ const toutiaoRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "今日头条",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "toutiaoData"; const cacheKey = "toutiaoData";
@ -47,8 +53,7 @@ toutiaoRouter.get("/toutiao", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "今日头条", ...routerInfo,
subtitle: "热榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -58,8 +63,7 @@ toutiaoRouter.get("/toutiao", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "今日头条", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -79,8 +83,7 @@ toutiaoRouter.get("/toutiao/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "今日头条", ...routerInfo,
subtitle: "热榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -98,8 +101,7 @@ toutiaoRouter.get("/toutiao/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "今日头条", ...routerInfo,
subtitle: "热榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -108,12 +110,12 @@ toutiaoRouter.get("/toutiao/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "今日头条", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
toutiaoRouter.info = routerInfo;
module.exports = toutiaoRouter; module.exports = toutiaoRouter;

View File

@ -4,6 +4,12 @@ const axios = require("axios");
// const cheerio = require("cheerio"); // const cheerio = require("cheerio");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "微博",
subtitle: "热搜榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "weiboData"; const cacheKey = "weiboData";
@ -50,6 +56,7 @@ weiboRouter.get("/weibo", async (ctx) => {
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
...routerInfo,
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -60,8 +67,7 @@ weiboRouter.get("/weibo", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "微博", ...routerInfo,
subtitle: "热搜榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -71,8 +77,7 @@ weiboRouter.get("/weibo", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "微博", ...routerInfo,
subtitle: "热搜榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -92,8 +97,7 @@ weiboRouter.get("/weibo/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "微博", ...routerInfo,
subtitle: "热搜榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -111,8 +115,7 @@ weiboRouter.get("/weibo/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "微博", ...routerInfo,
subtitle: "热搜榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -121,12 +124,12 @@ weiboRouter.get("/weibo/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "微博", ...routerInfo,
subtitle: "热搜榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
weiboRouter.info = routerInfo;
module.exports = weiboRouter; module.exports = weiboRouter;

View File

@ -3,6 +3,12 @@ const wereadRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
// 接口信息
const routerInfo = {
title: "微信读书",
subtitle: "飙升榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "wereadData"; const cacheKey = "wereadData";
@ -10,22 +16,23 @@ const cacheKey = "wereadData";
let updateTime = new Date().toISOString(); let updateTime = new Date().toISOString();
// 调用路径 // 调用路径
const url = const url = "https://weread.qq.com/web/bookListInCategory/rising?rank=1";
"https://weread.qq.com/web/bookListInCategory/rising?maxIndex=0&rank=1";
// 数据处理 // 数据处理
const getData = (data) => { const getData = (data) => {
if (!data) return []; if (!data) return [];
return data.map((v) => { return data.map((v) => {
const book = v.bookInfo; const book = v.bookInfo;
console.log(book);
return { return {
id: book.topic_id, id: book.bookId,
title: v.topic_name, title: book.title,
desc: v.topic_desc, desc: book.intro,
pic: v.topic_pic, pic: book.cover.replace("s_", "t9_"),
hot: v.discuss_num, hot: v.readingCount,
url: v.topic_url, author: book.author,
mobileUrl: v.topic_url, url: "https://weread.qq.com/web/category/rising",
mobileUrl: "https://weread.qq.com/web/category/rising",
}; };
}); });
}; };
@ -41,14 +48,18 @@ wereadRouter.get("/weread", async (ctx) => {
// 如果缓存中不存在数据 // 如果缓存中不存在数据
console.log("从服务端重新获取微信读书"); console.log("从服务端重新获取微信读书");
// 从服务器拉取数据 // 从服务器拉取数据
const response = await axios.get(url); const response = await axios.get(url, {
Headers: {
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67",
},
});
data = getData(response.data.books); data = getData(response.data.books);
updateTime = new Date().toISOString(); updateTime = new Date().toISOString();
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "微信读书", ...routerInfo,
subtitle: "飙升榜",
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -59,8 +70,7 @@ wereadRouter.get("/weread", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "微信读书", ...routerInfo,
subtitle: "飙升榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -81,7 +91,7 @@ wereadRouter.get("/weread/new", async (ctx) => {
try { try {
// 从服务器拉取最新数据 // 从服务器拉取最新数据
const response = await axios.get(url); const response = await axios.get(url);
const newData = getData(response.data); const newData = getData(response.data.books);
updateTime = new Date().toISOString(); updateTime = new Date().toISOString();
console.log("从服务端重新获取微信读书"); console.log("从服务端重新获取微信读书");
@ -89,8 +99,7 @@ wereadRouter.get("/weread/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "微信读书", ...routerInfo,
subtitle: "飙升榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -108,8 +117,7 @@ wereadRouter.get("/weread/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "微信读书", ...routerInfo,
subtitle: "飙升榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -118,12 +126,12 @@ wereadRouter.get("/weread/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "微信读书", ...routerInfo,
subtitle: "飙升榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
wereadRouter.info = routerInfo;
module.exports = wereadRouter; module.exports = wereadRouter;

View File

@ -2,6 +2,13 @@ const Router = require("koa-router");
const zhihuRouter = new Router(); const zhihuRouter = new Router();
const axios = require("axios"); const axios = require("axios");
const { get, set, del } = require("../utils/cacheData"); const { get, set, del } = require("../utils/cacheData");
const router = require(".");
// 接口信息
const routerInfo = {
title: "知乎",
subtitle: "热榜",
};
// 缓存键名 // 缓存键名
const cacheKey = "zhihuData"; const cacheKey = "zhihuData";
@ -59,6 +66,7 @@ zhihuRouter.get("/zhihu", async (ctx) => {
if (!data) { if (!data) {
ctx.body = { ctx.body = {
code: 500, code: 500,
...routerInfo,
message: "获取失败", message: "获取失败",
}; };
return false; return false;
@ -69,8 +77,7 @@ zhihuRouter.get("/zhihu", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "知乎", ...routerInfo,
subtitle: "热榜",
from, from,
total: data.length, total: data.length,
updateTime, updateTime,
@ -80,8 +87,7 @@ zhihuRouter.get("/zhihu", async (ctx) => {
console.error(error); console.error(error);
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "知乎", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
@ -101,8 +107,7 @@ zhihuRouter.get("/zhihu/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "知乎", ...routerInfo,
subtitle: "热榜",
total: newData.length, total: newData.length,
updateTime, updateTime,
data: newData, data: newData,
@ -120,8 +125,7 @@ zhihuRouter.get("/zhihu/new", async (ctx) => {
ctx.body = { ctx.body = {
code: 200, code: 200,
message: "获取成功", message: "获取成功",
title: "知乎", ...routerInfo,
subtitle: "热榜",
total: cachedData.length, total: cachedData.length,
updateTime, updateTime,
data: cachedData, data: cachedData,
@ -130,12 +134,12 @@ zhihuRouter.get("/zhihu/new", async (ctx) => {
// 如果缓存中也没有数据,则返回错误信息 // 如果缓存中也没有数据,则返回错误信息
ctx.body = { ctx.body = {
code: 500, code: 500,
title: "知乎", ...routerInfo,
subtitle: "热榜",
message: "获取失败", message: "获取失败",
}; };
} }
} }
}); });
zhihuRouter.info = routerInfo;
module.exports = zhihuRouter; module.exports = zhihuRouter;

39
utils/test.json Normal file
View File

@ -0,0 +1,39 @@
{
"opts": {},
"methods": ["HEAD", "OPTIONS", "GET", "PUT", "PATCH", "POST", "DELETE"],
"exclusive": false,
"params": {},
"stack": [
{
"opts": {
"end": true,
"name": null,
"sensitive": false,
"strict": false,
"prefix": ""
},
"name": null,
"methods": ["HEAD", "GET"],
"paramNames": [],
"stack": [null],
"path": "/zhihu",
"regexp": {}
},
{
"opts": {
"end": true,
"name": null,
"sensitive": false,
"strict": false,
"prefix": ""
},
"name": null,
"methods": ["HEAD", "GET"],
"paramNames": [],
"stack": [null],
"path": "/zhihu/new",
"regexp": {}
}
],
"info": { "title": "知乎", "subtitle": "热榜" }
}