diff --git a/README.md b/README.md index a83fb7c..d4c1fee 100644 --- a/README.md +++ b/README.md @@ -32,16 +32,25 @@ | 腾讯新闻 | 热点榜 | newsqq | 🟢 | | 抖音热榜 | 热点榜 | douyin | 🟢 | | 英雄联盟 | 更新公告 | lol | 🟢 | +| 微信读书 | 飙升榜 | weread | 🟢 | | 历史上的今天 | 指定日期 | calendar | 🟢 | ### 特殊接口说明 +#### 获取全部接口信息 + +获取除了下方特殊接口外的全部接口列表 + +```http +GET https://{example.com}/all +``` + #### 历史上的今天(指定日期) 将指定的月份和日期传入即可得到当天数据,请注意格式 ```http -GET https://api-hot.imsyy.top/calendar/date?month=06&day=01 +GET https://{example.com}/calendar/date?month=06&day=01 ``` ## 部署 diff --git a/package.json b/package.json index 4a8f0ef..df75c4e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dailyhot_api", - "version": "0.0.2", + "version": "1.0.0", "description": "一个今日热榜", "main": "index.js", "scripts": { diff --git a/routes/36kr.js b/routes/36kr.js index e91aa9c..3f75350 100644 --- a/routes/36kr.js +++ b/routes/36kr.js @@ -3,6 +3,12 @@ const krRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "36氪", + subtitle: "热榜", +}; + // 缓存键名 const cacheKey = "krData"; @@ -56,8 +62,7 @@ krRouter.get("/36kr", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "36氪", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -67,8 +72,7 @@ krRouter.get("/36kr", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "36氪", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } @@ -95,8 +99,7 @@ krRouter.get("/36kr/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "36氪", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -114,8 +117,7 @@ krRouter.get("/36kr/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "36氪", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -124,12 +126,12 @@ krRouter.get("/36kr/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "36氪", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); +krRouter.info = routerInfo; module.exports = krRouter; diff --git a/routes/baidu.js b/routes/baidu.js index 8722f04..f9b022e 100644 --- a/routes/baidu.js +++ b/routes/baidu.js @@ -3,6 +3,9 @@ const baiduRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { title: "百度", subtitle: "热搜榜" }; + // 缓存键名 const cacheKey = "baiduData"; @@ -54,8 +57,7 @@ baiduRouter.get("/baidu", async (ctx) => { if (!data) { ctx.body = { code: 500, - title: "百度", - subtitle: "热搜榜", + ...routerInfo, message: "获取失败", }; return false; @@ -66,8 +68,7 @@ baiduRouter.get("/baidu", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "百度", - subtitle: "热搜榜", + ...routerInfo, from, total: data.length, updateTime, @@ -96,8 +97,7 @@ baiduRouter.get("/baidu/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "百度", - subtitle: "热搜榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -115,8 +115,7 @@ baiduRouter.get("/baidu/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "百度", - subtitle: "热搜榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -125,12 +124,12 @@ baiduRouter.get("/baidu/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "百度", - subtitle: "热搜榜", + ...routerInfo, message: "获取失败", }; } } }); +baiduRouter.info = routerInfo; module.exports = baiduRouter; diff --git a/routes/bilibili.js b/routes/bilibili.js index 978d675..b8ee553 100644 --- a/routes/bilibili.js +++ b/routes/bilibili.js @@ -3,6 +3,12 @@ const bilibiliRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "哔哩哔哩", + subtitle: "热门榜", +}; + // 缓存键名 const cacheKey = "bilibiliData"; @@ -50,8 +56,7 @@ bilibiliRouter.get("/bilibili", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "哔哩哔哩", - subtitle: "热门榜", + ...routerInfo, from, total: data.length, updateTime, @@ -61,9 +66,8 @@ bilibiliRouter.get("/bilibili", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "哔哩哔哩", - subtitle: "热门榜", - message: "哔哩哔哩热门榜获取失败", + ...routerInfo, + message: "获取失败", }; } }); @@ -82,8 +86,7 @@ bilibiliRouter.get("/bilibili/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "哔哩哔哩", - subtitle: "热门榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -101,8 +104,7 @@ bilibiliRouter.get("/bilibili/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "哔哩哔哩", - subtitle: "热门榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -111,12 +113,12 @@ bilibiliRouter.get("/bilibili/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "哔哩哔哩", - subtitle: "热门榜", + ...routerInfo, message: "获取失败", }; } } }); +bilibiliRouter.info = routerInfo; module.exports = bilibiliRouter; diff --git a/routes/douyin.js b/routes/douyin.js index 38212bc..856bca1 100644 --- a/routes/douyin.js +++ b/routes/douyin.js @@ -3,6 +3,12 @@ const douyinRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "抖音", + subtitle: "热点榜", +}; + // 缓存键名 const cacheKey = "douyinHotData"; const cacheCookieKey = "douyinCookieData"; @@ -95,8 +101,7 @@ douyinRouter.get("/douyin", async (ctx) => { if (!data) { ctx.body = { code: 500, - title: "抖音热点榜", - subtitle: "热点榜", + ...routerInfo, message: "获取失败", }; return false; @@ -107,8 +112,7 @@ douyinRouter.get("/douyin", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音热点榜", - subtitle: "热点榜", + ...routerInfo, from, total: data.length, updateTime, @@ -118,6 +122,7 @@ douyinRouter.get("/douyin", async (ctx) => { console.error(error); ctx.body = { code: 500, + ...routerInfo, message: "获取失败", }; } @@ -142,8 +147,7 @@ douyinRouter.get("/douyin/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音热点榜", - subtitle: "热点榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -161,8 +165,7 @@ douyinRouter.get("/douyin/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音热点榜", - subtitle: "热点榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -171,12 +174,12 @@ douyinRouter.get("/douyin/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "抖音热点榜", - subtitle: "热点榜", + ...routerInfo, message: "获取失败", }; } } }); +douyinRouter.info = routerInfo; module.exports = douyinRouter; diff --git a/routes/index.js b/routes/index.js index 0b58d68..3c7a9a1 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,6 +4,14 @@ const Router = require("koa-router"); const router = new Router(); +// 全部路由数据 +const allRouterInfo = { + name: "全部接口", + subtitle: "除了特殊接口外的全部接口列表", + total: 0, + data: [], +}; + // 根目录 router.get("/", async (ctx) => { await ctx.render("index"); @@ -17,10 +25,43 @@ fs.readdirSync(__dirname) const routerModule = require(routerPath); // 自动注册路由 if (routerModule instanceof Router) { + // 写入路由数据 + if (routerModule?.info) { + allRouterInfo.total++; + allRouterInfo.data.push({ + ...routerModule.info, + stack: routerModule.stack, + }); + } + // 引用路由 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 路由 router.use(async (ctx) => { await ctx.render("404"); diff --git a/routes/ithome.js b/routes/ithome.js index 1da2e5c..246a575 100644 --- a/routes/ithome.js +++ b/routes/ithome.js @@ -4,6 +4,12 @@ const axios = require("axios"); const cheerio = require("cheerio"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "IT之家", + subtitle: "热榜", +}; + // 缓存键名 const cacheKey = "itHomeData"; @@ -78,8 +84,7 @@ itHomeRouter.get("/ithome", async (ctx) => { if (!data) { ctx.body = { code: 500, - title: "IT之家", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; return false; @@ -90,8 +95,7 @@ itHomeRouter.get("/ithome", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "IT之家", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -101,8 +105,7 @@ itHomeRouter.get("/ithome", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "IT之家", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } @@ -122,8 +125,7 @@ itHomeRouter.get("/ithome/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "IT之家", - subtitle: "热榜", + ...routerInfo, updateTime, total: data.length, data: newData, @@ -141,8 +143,7 @@ itHomeRouter.get("/ithome/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "IT之家", - subtitle: "热榜", + ...routerInfo, total: data.length, updateTime, data: cachedData, @@ -151,12 +152,12 @@ itHomeRouter.get("/ithome/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "IT之家", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); +itHomeRouter.info = routerInfo; module.exports = itHomeRouter; diff --git a/routes/juejin.js b/routes/juejin.js index 18cda81..e19c95e 100644 --- a/routes/juejin.js +++ b/routes/juejin.js @@ -3,6 +3,12 @@ const juejinRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "稀土掘金", + subtitle: "热榜", +}; + // 缓存键名 const cacheKey = "juejinData"; @@ -47,8 +53,7 @@ juejinRouter.get("/juejin", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "稀土掘金", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -58,8 +63,7 @@ juejinRouter.get("/juejin", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "稀土掘金", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } @@ -79,8 +83,7 @@ juejinRouter.get("/juejin/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "稀土掘金", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -98,8 +101,7 @@ juejinRouter.get("/juejin/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "稀土掘金", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -108,12 +110,12 @@ juejinRouter.get("/juejin/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "稀土掘金", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); +juejinRouter.info = routerInfo; module.exports = juejinRouter; diff --git a/routes/lol.js b/routes/lol.js index 6ca85a0..40e9f3d 100644 --- a/routes/lol.js +++ b/routes/lol.js @@ -3,6 +3,12 @@ const lolRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "英雄联盟", + subtitle: "更新公告", +}; + // 缓存键名 const cacheKey = "lolData"; @@ -10,24 +16,29 @@ const cacheKey = "lolData"; 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) => { if (!data) return []; const dataList = []; -try { + try { const pattern = /jQuery191002324053053181463_1687855508930\((.*?)\)/s; const matchResult = data.match(pattern); - const jsonObject = JSON.parse(matchResult[1])['data'].result; + const jsonObject = JSON.parse(matchResult[1])["data"].result; jsonObject.forEach((v) => { dataList.push({ title: v.sTitle, desc: v.sAuthor, pic: `https:${v.sIMG}`, hot: Number(v.iTotalPlay), - url: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent(v.iDocID)}`, - mobileUrl: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent(v.iDocID)}`, + url: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent( + v.iDocID + )}`, + mobileUrl: `https://lol.qq.com/news/detail.shtml?docid=${encodeURIComponent( + v.iDocID + )}`, }); }); return dataList; @@ -54,8 +65,7 @@ lolRouter.get("/lol", async (ctx) => { if (!data) { ctx.body = { code: 500, - title: "英雄联盟", - subtitle: "更新公告", + ...routerInfo, message: "获取失败", }; return false; @@ -66,8 +76,7 @@ lolRouter.get("/lol", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "英雄联盟", - subtitle: "更新公告", + ...routerInfo, from, total: data.length, updateTime, @@ -96,8 +105,7 @@ lolRouter.get("/lol/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "英雄联盟", - subtitle: "更新公告", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -115,8 +123,7 @@ lolRouter.get("/lol/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "英雄联盟", - subtitle: "更新公告", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -125,12 +132,12 @@ lolRouter.get("/lol/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "英雄联盟", - subtitle: "更新公告", + ...routerInfo, message: "获取失败", }; } } }); +lolRouter.info = routerInfo; module.exports = lolRouter; diff --git a/routes/newsqq.js b/routes/newsqq.js index f61d4a9..a615546 100644 --- a/routes/newsqq.js +++ b/routes/newsqq.js @@ -3,6 +3,12 @@ const newsqqRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "腾讯新闻", + subtitle: "热点榜", +}; + // 缓存键名 const cacheKey = "newsqqData"; @@ -49,8 +55,7 @@ newsqqRouter.get("/newsqq", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "腾讯新闻", - subtitle: "热点榜", + ...routerInfo, from, total: data.length, updateTime, @@ -60,8 +65,7 @@ newsqqRouter.get("/newsqq", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "腾讯新闻", - subtitle: "热点榜", + ...routerInfo, message: "获取失败", }; } @@ -81,8 +85,7 @@ newsqqRouter.get("/newsqq/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "腾讯新闻", - subtitle: "热点榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -100,8 +103,7 @@ newsqqRouter.get("/newsqq/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "腾讯新闻", - subtitle: "热点榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -110,12 +112,12 @@ newsqqRouter.get("/newsqq/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "腾讯新闻", - subtitle: "热点榜", + ...routerInfo, message: "获取失败", }; } } }); +newsqqRouter.info = routerInfo; module.exports = newsqqRouter; diff --git a/routes/sspai.js b/routes/sspai.js index 7fcdb51..351b642 100644 --- a/routes/sspai.js +++ b/routes/sspai.js @@ -3,6 +3,12 @@ const sspaiRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "少数派", + subtitle: "热榜", +}; + // 缓存键名 const cacheKey = "sspaiData"; @@ -49,8 +55,7 @@ sspaiRouter.get("/sspai", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "少数派", - subtitle: "最热", + ...routerInfo, from, total: data.length, updateTime, @@ -60,8 +65,7 @@ sspaiRouter.get("/sspai", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "少数派", - subtitle: "最热", + ...routerInfo, message: "获取失败", }; } @@ -81,8 +85,7 @@ sspaiRouter.get("/sspai/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "少数派", - subtitle: "最热", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -100,8 +103,7 @@ sspaiRouter.get("/sspai/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "少数派", - subtitle: "最热", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -110,12 +112,12 @@ sspaiRouter.get("/sspai/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "少数派", - subtitle: "最热", + ...routerInfo, message: "获取失败", }; } } }); +sspaiRouter.info = routerInfo; module.exports = sspaiRouter; diff --git a/routes/thepaper.js b/routes/thepaper.js index 7f28e91..b2b8b3e 100644 --- a/routes/thepaper.js +++ b/routes/thepaper.js @@ -3,6 +3,12 @@ const thepaperRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "澎湃新闻", + subtitle: "热榜", +}; + // 缓存键名 const cacheKey = "thepaperData"; @@ -48,8 +54,7 @@ thepaperRouter.get("/thepaper", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "澎湃新闻", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -59,8 +64,7 @@ thepaperRouter.get("/thepaper", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "澎湃新闻", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } @@ -80,8 +84,7 @@ thepaperRouter.get("/thepaper/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "澎湃新闻", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -99,8 +102,7 @@ thepaperRouter.get("/thepaper/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "澎湃新闻", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -109,12 +111,12 @@ thepaperRouter.get("/thepaper/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "澎湃新闻", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); +thepaperRouter.info = routerInfo; module.exports = thepaperRouter; diff --git a/routes/tieba.js b/routes/tieba.js index fd73bf1..16fbbd9 100644 --- a/routes/tieba.js +++ b/routes/tieba.js @@ -3,6 +3,12 @@ const tiebaRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "百度贴吧", + subtitle: "热议榜", +}; + // 缓存键名 const cacheKey = "tiebaData"; @@ -48,8 +54,7 @@ tiebaRouter.get("/tieba", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "百度贴吧", - subtitle: "热议榜", + ...routerInfo, from, total: data.length, updateTime, @@ -59,8 +64,7 @@ tiebaRouter.get("/tieba", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "百度贴吧", - subtitle: "热议榜", + ...routerInfo, message: "获取失败", }; } @@ -80,8 +84,7 @@ tiebaRouter.get("/tieba/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "百度贴吧", - subtitle: "热议榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -99,8 +102,7 @@ tiebaRouter.get("/tieba/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "百度贴吧", - subtitle: "热议榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -109,12 +111,12 @@ tiebaRouter.get("/tieba/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "百度贴吧", - subtitle: "热议榜", + ...routerInfo, message: "获取失败", }; } } }); +tiebaRouter.info = routerInfo; module.exports = tiebaRouter; diff --git a/routes/toutiao.js b/routes/toutiao.js index eeb2842..0ee35ba 100644 --- a/routes/toutiao.js +++ b/routes/toutiao.js @@ -3,6 +3,12 @@ const toutiaoRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "今日头条", + subtitle: "热榜", +}; + // 缓存键名 const cacheKey = "toutiaoData"; @@ -47,8 +53,7 @@ toutiaoRouter.get("/toutiao", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "今日头条", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -58,8 +63,7 @@ toutiaoRouter.get("/toutiao", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "今日头条", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } @@ -79,8 +83,7 @@ toutiaoRouter.get("/toutiao/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "今日头条", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -98,8 +101,7 @@ toutiaoRouter.get("/toutiao/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "今日头条", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -108,12 +110,12 @@ toutiaoRouter.get("/toutiao/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "今日头条", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); +toutiaoRouter.info = routerInfo; module.exports = toutiaoRouter; diff --git a/routes/weibo.js b/routes/weibo.js index b4b1f1b..9fa0fc6 100644 --- a/routes/weibo.js +++ b/routes/weibo.js @@ -4,6 +4,12 @@ const axios = require("axios"); // const cheerio = require("cheerio"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "微博", + subtitle: "热搜榜", +}; + // 缓存键名 const cacheKey = "weiboData"; @@ -50,6 +56,7 @@ weiboRouter.get("/weibo", async (ctx) => { if (!data) { ctx.body = { code: 500, + ...routerInfo, message: "获取失败", }; return false; @@ -60,8 +67,7 @@ weiboRouter.get("/weibo", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "微博", - subtitle: "热搜榜", + ...routerInfo, from, total: data.length, updateTime, @@ -71,8 +77,7 @@ weiboRouter.get("/weibo", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "微博", - subtitle: "热搜榜", + ...routerInfo, message: "获取失败", }; } @@ -92,8 +97,7 @@ weiboRouter.get("/weibo/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "微博", - subtitle: "热搜榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -111,8 +115,7 @@ weiboRouter.get("/weibo/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "微博", - subtitle: "热搜榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -121,12 +124,12 @@ weiboRouter.get("/weibo/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "微博", - subtitle: "热搜榜", + ...routerInfo, message: "获取失败", }; } } }); +weiboRouter.info = routerInfo; module.exports = weiboRouter; diff --git a/routes/weread.js b/routes/weread.js index 016f7f4..4427888 100644 --- a/routes/weread.js +++ b/routes/weread.js @@ -3,6 +3,12 @@ const wereadRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "微信读书", + subtitle: "飙升榜", +}; + // 缓存键名 const cacheKey = "wereadData"; @@ -10,22 +16,23 @@ const cacheKey = "wereadData"; let updateTime = new Date().toISOString(); // 调用路径 -const url = - "https://weread.qq.com/web/bookListInCategory/rising?maxIndex=0&rank=1"; +const url = "https://weread.qq.com/web/bookListInCategory/rising?rank=1"; // 数据处理 const getData = (data) => { if (!data) return []; return data.map((v) => { const book = v.bookInfo; + console.log(book); return { - id: book.topic_id, - title: v.topic_name, - desc: v.topic_desc, - pic: v.topic_pic, - hot: v.discuss_num, - url: v.topic_url, - mobileUrl: v.topic_url, + id: book.bookId, + title: book.title, + desc: book.intro, + pic: book.cover.replace("s_", "t9_"), + hot: v.readingCount, + author: book.author, + 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("从服务端重新获取微信读书"); // 从服务器拉取数据 - 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); updateTime = new Date().toISOString(); if (!data) { ctx.body = { code: 500, - title: "微信读书", - subtitle: "飙升榜", + ...routerInfo, message: "获取失败", }; return false; @@ -59,8 +70,7 @@ wereadRouter.get("/weread", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "微信读书", - subtitle: "飙升榜", + ...routerInfo, from, total: data.length, updateTime, @@ -81,7 +91,7 @@ wereadRouter.get("/weread/new", async (ctx) => { try { // 从服务器拉取最新数据 const response = await axios.get(url); - const newData = getData(response.data); + const newData = getData(response.data.books); updateTime = new Date().toISOString(); console.log("从服务端重新获取微信读书"); @@ -89,8 +99,7 @@ wereadRouter.get("/weread/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "微信读书", - subtitle: "飙升榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -108,8 +117,7 @@ wereadRouter.get("/weread/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "微信读书", - subtitle: "飙升榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -118,12 +126,12 @@ wereadRouter.get("/weread/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "微信读书", - subtitle: "飙升榜", + ...routerInfo, message: "获取失败", }; } } }); +wereadRouter.info = routerInfo; module.exports = wereadRouter; diff --git a/routes/zhihu.js b/routes/zhihu.js index d7cbac0..e6e2ed4 100644 --- a/routes/zhihu.js +++ b/routes/zhihu.js @@ -2,6 +2,13 @@ const Router = require("koa-router"); const zhihuRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +const router = require("."); + +// 接口信息 +const routerInfo = { + title: "知乎", + subtitle: "热榜", +}; // 缓存键名 const cacheKey = "zhihuData"; @@ -59,6 +66,7 @@ zhihuRouter.get("/zhihu", async (ctx) => { if (!data) { ctx.body = { code: 500, + ...routerInfo, message: "获取失败", }; return false; @@ -69,8 +77,7 @@ zhihuRouter.get("/zhihu", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "知乎", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -80,8 +87,7 @@ zhihuRouter.get("/zhihu", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "知乎", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } @@ -101,8 +107,7 @@ zhihuRouter.get("/zhihu/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "知乎", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -120,8 +125,7 @@ zhihuRouter.get("/zhihu/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "知乎", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -130,12 +134,12 @@ zhihuRouter.get("/zhihu/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "知乎", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); +zhihuRouter.info = routerInfo; module.exports = zhihuRouter; diff --git a/utils/test.json b/utils/test.json new file mode 100644 index 0000000..7e3c880 --- /dev/null +++ b/utils/test.json @@ -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": "热榜" } +}