From 78e25f8b3a1f045ea1569e6f71e688ce5af7c5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= <75709526+wp0403@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:03:27 +0800 Subject: [PATCH 1/8] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E6=8A=96=E9=9F=B3?= =?UTF-8?q?=E7=83=AD=E6=90=9C=E5=92=8C=E6=8A=96=E9=9F=B3=E9=9F=B3=E4=B9=90?= =?UTF-8?q?=E6=A6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/douyin.js | 136 +++++++++++++++++++++++++++++++++++++++++ routes/douyinmusic.js | 138 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 274 insertions(+) create mode 100644 routes/douyin.js create mode 100644 routes/douyinmusic.js diff --git a/routes/douyin.js b/routes/douyin.js new file mode 100644 index 0000000..1b41f77 --- /dev/null +++ b/routes/douyin.js @@ -0,0 +1,136 @@ +/* + * @Descripttion: + * @version: + * @Author: WangPeng + * @Date: 2023-07-10 16:56:01 + * @LastEditors: WangPeng + * @LastEditTime: 2023-07-11 16:33:56 + */ +const Router = require("koa-router"); +const douyinRouter = new Router(); +const axios = require("axios"); +const { get, set, del } = require("../utils/cacheData"); + +// 缓存键名 +const cacheKey = "douyinData"; + +// 调用时间 +let updateTime = new Date().toISOString(); + +// 调用路径 +const url = "https://aweme.snssdk.com/aweme/v1/hot/search/list/"; +const HEADERS = { + 'user-agent': 'okhttp3' +} +const QUERIES = { + 'device_platform': 'android', + 'version_name': '13.2.0', + 'version_code': '130200', + 'aid': '1128' +} + +// 数据处理 +const getData = (data) => { + if (!data) return []; + return data.map((v) => { + return { + id: v.group_id, + title: v.word, + pic: v.word_cover?.url_list[0] || null, + hot: v.hot_value, + url: `https://www.douyin.com/hot?modal_id=${v.group_id}`, + mobileUrl: `https://m.douyin.com/share/video/${v.group_id}`, + }; + }); +}; + +// 抖音热榜 +douyinRouter.get("/douyin", async (ctx) => { + console.log("获取抖音热榜"); + try { + // 从缓存中获取数据 + let data = await get(cacheKey); + const from = data ? "cache" : "server"; + if (!data) { + // 如果缓存中不存在数据 + console.log("从服务端重新获取抖音热榜"); + // 从服务器拉取数据 + const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); + data = getData(response.data.data.word_list); + updateTime = new Date().toISOString(); + // 将数据写入缓存 + await set(cacheKey, data); + } + ctx.body = { + code: 200, + message: "获取成功", + title: "抖音", + subtitle: "热榜", + from, + total: data.length, + updateTime, + data, + }; + } catch (error) { + console.error(error); + ctx.body = { + code: 500, + title: "抖音", + subtitle: "热榜", + message: "获取失败", + }; + } +}); + +// 抖音热榜 - 获取最新数据 +douyinRouter.get("/douyin/new", async (ctx) => { + console.log("获取抖音热榜 - 最新数据"); + try { + // 从服务器拉取最新数据 + const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); + const newData = getData(response.data.word_list); + updateTime = new Date().toISOString(); + console.log("从服务端重新获取抖音热榜"); + + // 返回最新数据 + ctx.body = { + code: 200, + message: "获取成功", + title: "抖音", + subtitle: "热榜", + total: newData.length, + updateTime, + data: newData, + }; + + // 删除旧数据 + await del(cacheKey); + // 将最新数据写入缓存 + await set(cacheKey, newData); + } catch (error) { + // 如果拉取最新数据失败,尝试从缓存中获取数据 + console.error(error); + const cachedData = await get(cacheKey); + if (cachedData) { + ctx.body = { + code: 200, + message: "获取成功", + title: "抖音", + subtitle: "热榜", + total: cachedData.length, + updateTime, + data: cachedData, + }; + } else { + // 如果缓存中也没有数据,则返回错误信息 + ctx.body = { + code: 500, + title: "抖音", + subtitle: "热榜", + message: "获取失败", + }; + } + } +}); + +module.exports = douyinRouter; diff --git a/routes/douyinmusic.js b/routes/douyinmusic.js new file mode 100644 index 0000000..dd3c477 --- /dev/null +++ b/routes/douyinmusic.js @@ -0,0 +1,138 @@ +/* + * @Descripttion: + * @version: + * @Author: WangPeng + * @Date: 2023-07-11 16:41:48 + * @LastEditors: WangPeng + * @LastEditTime: 2023-07-11 16:56:12 + */ +/* + * @Descripttion: + * @version: + * @Author: WangPeng + * @Date: 2023-07-10 16:56:01 + * @LastEditors: WangPeng + * @LastEditTime: 2023-07-11 16:33:56 + */ +const Router = require("koa-router"); +const douyinRouter = new Router(); +const axios = require("axios"); +const { get, set, del } = require("../utils/cacheData"); + +// 缓存键名 +const cacheKey = "douyinData"; + +// 调用时间 +let updateTime = new Date().toISOString(); + +// 调用路径 +const url = "https://aweme.snssdk.com/aweme/v1/chart/music/list/"; +const HEADERS = { + 'user-agent': 'okhttp3' +} +const QUERIES = { + 'device_platform': 'android', + 'version_name': '13.2.0', + 'version_code': '130200', + 'aid': '1128', + 'chart_id': '6853972723954146568', + 'count': '100' +} + +// 数据处理 +const getData = (data) => { + if (!data) return []; + return data; +}; + +// 抖音music热榜 +douyinRouter.get("/douyinmusic", async (ctx) => { + console.log("获取抖音music热榜"); + try { + // 从缓存中获取数据 + let data = await get(cacheKey); + const from = data ? "cache" : "server"; + if (!data) { + // 如果缓存中不存在数据 + console.log("从服务端重新获取抖音music热榜"); + // 从服务器拉取数据 + const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); + console.log(response.data); + data = getData(response.data.music_list); + updateTime = new Date().toISOString(); + // 将数据写入缓存 + await set(cacheKey, data); + } + ctx.body = { + code: 200, + message: "获取成功", + title: "抖音music", + subtitle: "热榜", + from, + total: data.length, + updateTime, + data, + }; + } catch (error) { + console.error(error); + ctx.body = { + code: 500, + title: "抖音music", + subtitle: "热榜", + message: "获取失败", + }; + } +}); + +// 抖音music热榜 - 获取最新数据 +douyinRouter.get("/douyinmusic/new", async (ctx) => { + console.log("获取抖音music热榜 - 最新数据"); + try { + // 从服务器拉取最新数据 + const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); + const newData = getData(response.data.word_list); + updateTime = new Date().toISOString(); + console.log("从服务端重新获取抖音music热榜"); + + // 返回最新数据 + ctx.body = { + code: 200, + message: "获取成功", + title: "抖音music", + subtitle: "热榜", + total: newData.length, + updateTime, + data: newData, + }; + + // 删除旧数据 + await del(cacheKey); + // 将最新数据写入缓存 + await set(cacheKey, newData); + } catch (error) { + // 如果拉取最新数据失败,尝试从缓存中获取数据 + console.error(error); + const cachedData = await get(cacheKey); + if (cachedData) { + ctx.body = { + code: 200, + message: "获取成功", + title: "抖音music", + subtitle: "热榜", + total: cachedData.length, + updateTime, + data: cachedData, + }; + } else { + // 如果缓存中也没有数据,则返回错误信息 + ctx.body = { + code: 500, + title: "抖音music", + subtitle: "热榜", + message: "获取失败", + }; + } + } +}); + +module.exports = douyinRouter; From 5b63365423edd84417fdaa68ce0cbeb956cf7538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= <75709526+wp0403@users.noreply.github.com> Date: Tue, 11 Jul 2023 17:26:34 +0800 Subject: [PATCH 2/8] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/{douyin.js => douyin2.js} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename routes/{douyin.js => douyin2.js} (96%) diff --git a/routes/douyin.js b/routes/douyin2.js similarity index 96% rename from routes/douyin.js rename to routes/douyin2.js index 1b41f77..c9ba690 100644 --- a/routes/douyin.js +++ b/routes/douyin2.js @@ -4,7 +4,7 @@ * @Author: WangPeng * @Date: 2023-07-10 16:56:01 * @LastEditors: WangPeng - * @LastEditTime: 2023-07-11 16:33:56 + * @LastEditTime: 2023-07-11 17:26:05 */ const Router = require("koa-router"); const douyinRouter = new Router(); @@ -45,7 +45,7 @@ const getData = (data) => { }; // 抖音热榜 -douyinRouter.get("/douyin", async (ctx) => { +douyinRouter.get("/douyin2", async (ctx) => { console.log("获取抖音热榜"); try { // 从缓存中获取数据 @@ -83,7 +83,7 @@ douyinRouter.get("/douyin", async (ctx) => { }); // 抖音热榜 - 获取最新数据 -douyinRouter.get("/douyin/new", async (ctx) => { +douyinRouter.get("/douyin2/new", async (ctx) => { console.log("获取抖音热榜 - 最新数据"); try { // 从服务器拉取最新数据 From 87f0ca3c782000f6fcf1b0eb5a998099bc182ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= <75709526+wp0403@users.noreply.github.com> Date: Wed, 12 Jul 2023 11:35:35 +0800 Subject: [PATCH 3/8] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=8A=96=E9=9F=B3?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E7=AB=AFurl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/douyin2.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/douyin2.js b/routes/douyin2.js index c9ba690..8eb4409 100644 --- a/routes/douyin2.js +++ b/routes/douyin2.js @@ -4,7 +4,7 @@ * @Author: WangPeng * @Date: 2023-07-10 16:56:01 * @LastEditors: WangPeng - * @LastEditTime: 2023-07-11 17:26:05 + * @LastEditTime: 2023-07-12 11:34:50 */ const Router = require("koa-router"); const douyinRouter = new Router(); @@ -39,7 +39,7 @@ const getData = (data) => { pic: v.word_cover?.url_list[0] || null, hot: v.hot_value, url: `https://www.douyin.com/hot?modal_id=${v.group_id}`, - mobileUrl: `https://m.douyin.com/share/video/${v.group_id}`, + mobileUrl: `snssdk1128://share/video/${v.group_id}`, }; }); }; From 30f99cd0105e0fc4e0484b5dc45ec28a8418f425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= <75709526+wp0403@users.noreply.github.com> Date: Wed, 12 Jul 2023 15:10:29 +0800 Subject: [PATCH 4/8] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=8A=96=E9=9F=B3?= =?UTF-8?q?=E8=B7=B3=E8=BD=AC=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/douyin2.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routes/douyin2.js b/routes/douyin2.js index 8eb4409..b82ce4a 100644 --- a/routes/douyin2.js +++ b/routes/douyin2.js @@ -4,7 +4,7 @@ * @Author: WangPeng * @Date: 2023-07-10 16:56:01 * @LastEditors: WangPeng - * @LastEditTime: 2023-07-12 11:34:50 + * @LastEditTime: 2023-07-12 15:09:39 */ const Router = require("koa-router"); const douyinRouter = new Router(); @@ -38,8 +38,8 @@ const getData = (data) => { title: v.word, pic: v.word_cover?.url_list[0] || null, hot: v.hot_value, - url: `https://www.douyin.com/hot?modal_id=${v.group_id}`, - mobileUrl: `snssdk1128://share/video/${v.group_id}`, + url: `https://www.douyin.com/hot/${v.sentence_id}`, + mobileUrl: `https://www.douyin.com/hot/${v.sentence_id}`, }; }); }; From c71e4267ffa19c08b736377ac591ebf77967897a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=94=A8=E6=88=B7?= Date: Wed, 12 Jul 2023 16:39:23 +0800 Subject: [PATCH 5/8] Update and rename douyinmusic.js to douyinMusic.js --- routes/{douyinmusic.js => douyinMusic.js} | 112 +++++++++++++--------- 1 file changed, 65 insertions(+), 47 deletions(-) rename routes/{douyinmusic.js => douyinMusic.js} (53%) diff --git a/routes/douyinmusic.js b/routes/douyinMusic.js similarity index 53% rename from routes/douyinmusic.js rename to routes/douyinMusic.js index dd3c477..16da525 100644 --- a/routes/douyinmusic.js +++ b/routes/douyinMusic.js @@ -1,26 +1,24 @@ /* - * @Descripttion: - * @version: - * @Author: WangPeng - * @Date: 2023-07-11 16:41:48 - * @LastEditors: WangPeng - * @LastEditTime: 2023-07-11 16:56:12 - */ -/* - * @Descripttion: - * @version: - * @Author: WangPeng - * @Date: 2023-07-10 16:56:01 - * @LastEditors: WangPeng - * @LastEditTime: 2023-07-11 16:33:56 + * @version: 1.0.0 + * @author: WangPeng + * @date: 2023-07-11 16:41:48 + * @customEditors: imsyy + * @lastEditTime: 2023-07-11 16:03:12 */ + const Router = require("koa-router"); -const douyinRouter = new Router(); +const douyinMusicRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "抖音", + subtitle: "热歌榜", +}; + // 缓存键名 -const cacheKey = "douyinData"; +const cacheKey = "douyinMusicData"; // 调用时间 let updateTime = new Date().toISOString(); @@ -28,35 +26,56 @@ let updateTime = new Date().toISOString(); // 调用路径 const url = "https://aweme.snssdk.com/aweme/v1/chart/music/list/"; const HEADERS = { - 'user-agent': 'okhttp3' -} + "user-agent": "okhttp3", +}; const QUERIES = { - 'device_platform': 'android', - 'version_name': '13.2.0', - 'version_code': '130200', - 'aid': '1128', - 'chart_id': '6853972723954146568', - 'count': '100' -} + device_platform: "android", + version_name: "13.2.0", + version_code: "130200", + aid: "1128", + chart_id: "6853972723954146568", + count: "100", +}; // 数据处理 const getData = (data) => { if (!data) return []; - return data; + try { + return data.map((v) => { + const item = v.music_info; + return { + id: item.id, + title: item.title, + album: item.album, + artist: item.author, + pic: item?.cover_large.url_list[0], + lyric: item.lyric_url, + url: item.play_url.uri, + mobileUrl: item.play_url.uri, + // h5Url: item.matched_song?.h5_url, + }; + }); + } catch (error) { + console.error("数据处理出错" + error); + return []; + } }; -// 抖音music热榜 -douyinRouter.get("/douyinmusic", async (ctx) => { - console.log("获取抖音music热榜"); +// 抖音热歌榜 +douyinMusicRouter.get("/douyinmusic", async (ctx) => { + console.log("获取抖音热歌榜"); try { // 从缓存中获取数据 let data = await get(cacheKey); const from = data ? "cache" : "server"; if (!data) { // 如果缓存中不存在数据 - console.log("从服务端重新获取抖音music热榜"); + console.log("从服务端重新获取抖音热歌榜"); // 从服务器拉取数据 - const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); + const response = await axios.get(url, { + headers: HEADERS, + params: QUERIES, + }); console.log(response.data); data = getData(response.data.music_list); updateTime = new Date().toISOString(); @@ -66,8 +85,7 @@ douyinRouter.get("/douyinmusic", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音music", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -77,29 +95,30 @@ douyinRouter.get("/douyinmusic", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "抖音music", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } }); -// 抖音music热榜 - 获取最新数据 -douyinRouter.get("/douyinmusic/new", async (ctx) => { - console.log("获取抖音music热榜 - 最新数据"); +// 抖音热歌榜 - 获取最新数据 +douyinMusicRouter.get("/douyinmusic/new", async (ctx) => { + console.log("获取抖音热歌榜 - 最新数据"); try { // 从服务器拉取最新数据 - const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); + const response = await axios.get(url, { + headers: HEADERS, + params: QUERIES, + }); const newData = getData(response.data.word_list); updateTime = new Date().toISOString(); - console.log("从服务端重新获取抖音music热榜"); + console.log("从服务端重新获取抖音热歌榜"); // 返回最新数据 ctx.body = { code: 200, message: "获取成功", - title: "抖音music", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -117,8 +136,7 @@ douyinRouter.get("/douyinmusic/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音music", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -127,12 +145,12 @@ douyinRouter.get("/douyinmusic/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "抖音music", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); -module.exports = douyinRouter; +douyinMusicRouter.info = routerInfo; +module.exports = douyinMusicRouter; From eaba3db4de1ce26eee8c7f10d229e3231bdcb384 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=94=A8=E6=88=B7?= Date: Wed, 12 Jul 2023 16:43:36 +0800 Subject: [PATCH 6/8] Rename douyinMusic.js to douyin_music.js --- routes/{douyinMusic.js => douyin_music.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename routes/{douyinMusic.js => douyin_music.js} (100%) diff --git a/routes/douyinMusic.js b/routes/douyin_music.js similarity index 100% rename from routes/douyinMusic.js rename to routes/douyin_music.js From 4ae8e6cfd86c21b310898acea7768346421342bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=94=A8=E6=88=B7?= Date: Wed, 12 Jul 2023 16:58:03 +0800 Subject: [PATCH 7/8] Update douyin_music.js --- routes/douyin_music.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/routes/douyin_music.js b/routes/douyin_music.js index 16da525..39b9a51 100644 --- a/routes/douyin_music.js +++ b/routes/douyin_music.js @@ -62,7 +62,7 @@ const getData = (data) => { }; // 抖音热歌榜 -douyinMusicRouter.get("/douyinmusic", async (ctx) => { +douyinMusicRouter.get("/douyin_music", async (ctx) => { console.log("获取抖音热歌榜"); try { // 从缓存中获取数据 @@ -76,7 +76,6 @@ douyinMusicRouter.get("/douyinmusic", async (ctx) => { headers: HEADERS, params: QUERIES, }); - console.log(response.data); data = getData(response.data.music_list); updateTime = new Date().toISOString(); // 将数据写入缓存 @@ -102,7 +101,7 @@ douyinMusicRouter.get("/douyinmusic", async (ctx) => { }); // 抖音热歌榜 - 获取最新数据 -douyinMusicRouter.get("/douyinmusic/new", async (ctx) => { +douyinMusicRouter.get("/douyin_music/new", async (ctx) => { console.log("获取抖音热歌榜 - 最新数据"); try { // 从服务器拉取最新数据 From 9e9fc713de68ed70bab7ad648bd7cc22c3d8e95a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=95=E5=B1=82=E7=94=A8=E6=88=B7?= Date: Wed, 12 Jul 2023 17:02:46 +0800 Subject: [PATCH 8/8] Update and rename douyin2.js to douyin_new.js --- routes/{douyin2.js => douyin_new.js} | 111 +++++++++++++++------------ 1 file changed, 63 insertions(+), 48 deletions(-) rename routes/{douyin2.js => douyin_new.js} (51%) diff --git a/routes/douyin2.js b/routes/douyin_new.js similarity index 51% rename from routes/douyin2.js rename to routes/douyin_new.js index b82ce4a..ba43848 100644 --- a/routes/douyin2.js +++ b/routes/douyin_new.js @@ -1,18 +1,24 @@ /* - * @Descripttion: - * @version: - * @Author: WangPeng - * @Date: 2023-07-10 16:56:01 - * @LastEditors: WangPeng - * @LastEditTime: 2023-07-12 15:09:39 + * @version: 1.0.0 + * @author: WangPeng + * @date: 2023-07-10 16:56:01 + * @customEditors: imsyy + * @lastEditTime: 2023-07-11 16:54:38 */ + const Router = require("koa-router"); -const douyinRouter = new Router(); +const douyinNewRouter = new Router(); const axios = require("axios"); const { get, set, del } = require("../utils/cacheData"); +// 接口信息 +const routerInfo = { + title: "抖音", + subtitle: "热点榜", +}; + // 缓存键名 -const cacheKey = "douyinData"; +const cacheKey = "douyinHotNewData"; // 调用时间 let updateTime = new Date().toISOString(); @@ -20,43 +26,53 @@ let updateTime = new Date().toISOString(); // 调用路径 const url = "https://aweme.snssdk.com/aweme/v1/hot/search/list/"; const HEADERS = { - 'user-agent': 'okhttp3' -} + "user-agent": "okhttp3", +}; const QUERIES = { - 'device_platform': 'android', - 'version_name': '13.2.0', - 'version_code': '130200', - 'aid': '1128' -} + device_platform: "android", + version_name: "13.2.0", + version_code: "130200", + aid: "1128", +}; // 数据处理 const getData = (data) => { if (!data) return []; - return data.map((v) => { - return { - id: v.group_id, - title: v.word, - pic: v.word_cover?.url_list[0] || null, - hot: v.hot_value, - url: `https://www.douyin.com/hot/${v.sentence_id}`, - mobileUrl: `https://www.douyin.com/hot/${v.sentence_id}`, - }; - }); + try { + const jsonObject = data.data.word_list; + return jsonObject.map((v) => { + return { + title: v.word, + pic: `${v.word_cover.url_list[0]}`, + hot: Number(v.hot_value), + url: `https://www.douyin.com/hot/${encodeURIComponent(v.sentence_id)}`, + mobileUrl: `https://www.douyin.com/hot/${encodeURIComponent( + v.sentence_id + )}`, + }; + }); + } catch (error) { + console.error("数据处理出错" + error); + return []; + } }; -// 抖音热榜 -douyinRouter.get("/douyin2", async (ctx) => { - console.log("获取抖音热榜"); +// 抖音热点榜 +douyinNewRouter.get("/douyin_new", async (ctx) => { + console.log("获取抖音热点榜"); try { // 从缓存中获取数据 let data = await get(cacheKey); const from = data ? "cache" : "server"; if (!data) { // 如果缓存中不存在数据 - console.log("从服务端重新获取抖音热榜"); + console.log("从服务端重新获取抖音热点榜"); // 从服务器拉取数据 - const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); - data = getData(response.data.data.word_list); + const response = await axios.get(url, { + headers: HEADERS, + params: QUERIES, + }); + data = getData(response.data); updateTime = new Date().toISOString(); // 将数据写入缓存 await set(cacheKey, data); @@ -64,8 +80,7 @@ douyinRouter.get("/douyin2", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音", - subtitle: "热榜", + ...routerInfo, from, total: data.length, updateTime, @@ -75,29 +90,30 @@ douyinRouter.get("/douyin2", async (ctx) => { console.error(error); ctx.body = { code: 500, - title: "抖音", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } }); -// 抖音热榜 - 获取最新数据 -douyinRouter.get("/douyin2/new", async (ctx) => { - console.log("获取抖音热榜 - 最新数据"); +// 抖音热点榜 - 获取最新数据 +douyinNewRouter.get("/douyin_new/new", async (ctx) => { + console.log("获取抖音热点榜 - 最新数据"); try { // 从服务器拉取最新数据 - const response = await axios.get(url, { headers: HEADERS, params: QUERIES }); - const newData = getData(response.data.word_list); + const response = await axios.get(url, { + headers: HEADERS, + params: QUERIES, + }); + const newData = getData(response.data); updateTime = new Date().toISOString(); - console.log("从服务端重新获取抖音热榜"); + console.log("从服务端重新获取抖音热点榜"); // 返回最新数据 ctx.body = { code: 200, message: "获取成功", - title: "抖音", - subtitle: "热榜", + ...routerInfo, total: newData.length, updateTime, data: newData, @@ -115,8 +131,7 @@ douyinRouter.get("/douyin2/new", async (ctx) => { ctx.body = { code: 200, message: "获取成功", - title: "抖音", - subtitle: "热榜", + ...routerInfo, total: cachedData.length, updateTime, data: cachedData, @@ -125,12 +140,12 @@ douyinRouter.get("/douyin2/new", async (ctx) => { // 如果缓存中也没有数据,则返回错误信息 ctx.body = { code: 500, - title: "抖音", - subtitle: "热榜", + ...routerInfo, message: "获取失败", }; } } }); -module.exports = douyinRouter; +douyinNewRouter.info = routerInfo; +module.exports = douyinNewRouter;