From ffc9e7cb37279a8e4a933c2a272d707ede3c0860 Mon Sep 17 00:00:00 2001 From: imsyy Date: Mon, 3 Jul 2023 10:47:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=AF=B4=E6=98=8E=20#9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 40 ++++++++++++----------- routes/douyin.js | 83 ++++++++++++++++++++++++------------------------ 2 files changed, 63 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 47f3e81..a83fb7c 100644 --- a/README.md +++ b/README.md @@ -16,21 +16,23 @@ > 🟠 可能失效 > 🔴 无法使用 -| **站点** | **类别** | **调用名称** | **状态** | -| -------- | -------- | ------------ | -------- | -| 哔哩哔哩 | 热门榜 | bilibili | 🟢 | -| 知乎 | 热榜 | zhihu | 🟢 | -| 百度 | 热搜榜 | baidu | 🟢 | -| 百度贴吧 | 热议榜 | tieba | 🟢 | -| 少数派 | 热榜 | sspai | 🟢 | -| IT之家 | 热榜 | ithome | 🟠 | -| 澎湃新闻 | 热榜 | thepaper | 🟢 | -| 今日头条 | 热榜 | toutiao | 🟢 | -| 微博热搜 | 热搜榜 | weibo | 🟢 | -| 36氪 | 热榜 | 36kr | 🟢 | -| 稀土掘金 | 热榜 | juejin | 🟢 | -| 腾讯新闻 | 热点榜 | newsqq | 🟢 | -| 历史上的今天 | 指定日期 | calendar | 🟢 | +| **站点** | **类别** | **调用名称** | **状态** | +| ------------ | -------- | ------------ | -------- | +| 哔哩哔哩 | 热门榜 | bilibili | 🟢 | +| 知乎 | 热榜 | zhihu | 🟢 | +| 百度 | 热搜榜 | baidu | 🟢 | +| 百度贴吧 | 热议榜 | tieba | 🟢 | +| 少数派 | 热榜 | sspai | 🟢 | +| IT 之家 | 热榜 | ithome | 🟠 | +| 澎湃新闻 | 热榜 | thepaper | 🟢 | +| 今日头条 | 热榜 | toutiao | 🟢 | +| 微博热搜 | 热搜榜 | weibo | 🟢 | +| 36 氪 | 热榜 | 36kr | 🟢 | +| 稀土掘金 | 热榜 | juejin | 🟢 | +| 腾讯新闻 | 热点榜 | newsqq | 🟢 | +| 抖音热榜 | 热点榜 | douyin | 🟢 | +| 英雄联盟 | 更新公告 | lol | 🟢 | +| 历史上的今天 | 指定日期 | calendar | 🟢 | ### 特殊接口说明 @@ -61,12 +63,12 @@ pnpm start 1. fork 本项目 2. 在 `Vercel` 官网点击 `New Project` 3. 点击 `Import Git Repository` 并选择你 fork 的此项目并点击 `import` -4. `PROJECT NAME`自己填,`FRAMEWORK PRESET` 选 `Other` 然后直接点 `Deploy` 接着等部署完成即可 +4. `PROJECT NAME`自己填,`FRAMEWORK PRESET` 选 `Other` 然后直接点 `Deploy` 接着等部署完成即可 ## 调用 ### 获取榜单数据 - + > 获取数据只需在域名后面加上上方列表中的调用名称即可 ```http @@ -118,6 +120,7 @@ GET https://api-hot.imsyy.top/bilibili/ ] } ``` + ### 获取榜单最新数据 @@ -172,6 +175,7 @@ GET https://api-hot.imsyy.top/bilibili/new ] } ``` + ## 其他 @@ -184,4 +188,4 @@ GET https://api-hot.imsyy.top/bilibili/new - 本项目提供的 `API` 仅供开发者进行技术研究和开发测试使用。使用该 `API` 获取的信息仅供参考,不代表本项目对信息的准确性、可靠性、合法性、完整性作出任何承诺或保证。本项目不对任何因使用该 `API` 获取信息而导致的任何直接或间接损失负责。本项目保留随时更改 `API` 接口地址、接口协议、接口参数及其他相关内容的权利。本项目对使用者使用 `API` 的行为不承担任何直接或间接的法律责任 - 本项目并未与相关信息提供方建立任何关联或合作关系,获取的信息均来自公开渠道,如因使用该 `API` 获取信息而产生的任何法律责任,由使用者自行承担 - 本项目对使用 `API` 获取的信息进行了最大限度的筛选和整理,但不保证信息的准确性和完整性。使用 `API` 获取信息时,请务必自行核实信息的真实性和可靠性,谨慎处理相关事项 -- 本项目保留对 `API` 的随时更改、停用、限制使用等措施的权利。任何因使用本 `API` 产生的损失,本项目不负担任何赔偿和责任 \ No newline at end of file +- 本项目保留对 `API` 的随时更改、停用、限制使用等措施的权利。任何因使用本 `API` 产生的损失,本项目不负担任何赔偿和责任 diff --git a/routes/douyin.js b/routes/douyin.js index d3ab9ce..38212bc 100644 --- a/routes/douyin.js +++ b/routes/douyin.js @@ -11,16 +11,18 @@ const cacheCookieKey = "douyinCookieData"; let updateTime = new Date().toISOString(); // 调用路径 -const url = "https://www.douyin.com/aweme/v1/web/hot/search/list/?device_platform=webapp&aid=6383&channel=channel_pc_web&detail_list=1&round_trip_time=50"; +const url = + "https://www.douyin.com/aweme/v1/web/hot/search/list/?device_platform=webapp&aid=6383&channel=channel_pc_web&detail_list=1&round_trip_time=50"; -// Token获取路径 -const cookisUrl = "https://www.douyin.com/passport/general/login_guiding_strategy/?aid=6383"; +// Token 获取路径 +const cookisUrl = + "https://www.douyin.com/passport/general/login_guiding_strategy/?aid=6383"; // 数据处理 const getData = (data) => { if (!data) return []; const dataList = []; -try { + try { const jsonObject = data.data.word_list; jsonObject.forEach((v) => { dataList.push({ @@ -28,30 +30,48 @@ try { 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)}`, + mobileUrl: `https://www.douyin.com/hot/${encodeURIComponent( + v.sentence_id + )}`, }); }); return dataList; } catch (error) { console.error("数据处理出错" + error); - return false; + return []; } }; -// 处理Cookis -const getCookies = (data) => { - if(!data) return null; -try { +// 处理抖音 Cookis +const setDouyinCookies = (data) => { + if (!data) return null; + try { const pattern = /passport_csrf_token=(.*); Path/s; const matchResult = data.headers["set-cookie"][0].match(pattern); const cookieData = matchResult[1]; return cookieData; + } catch (error) { + console.error("获取抖音 Cookie 出错" + error); + return null; + } +}; -} catch (error) { - console.error("获取Cookis出错" + error); - return false; -} -} +// 获取抖音 Cookie数据 +const getDouyinCookie = async () => { + try { + let cookie = await get(cacheCookieKey); + if (!cookie) { + const cookisResponse = await axios.get(cookisUrl); + cookie = setDouyinCookies(cookisResponse); + console.log("抖音 Cookie 写入缓存", cookie); + await set(cacheCookieKey, cookie); + } + return cookie; + } catch (error) { + console.error("获取抖音 Cookie 出错", error); + return null; + } +}; // 抖音热点榜 douyinRouter.get("/douyin", async (ctx) => { @@ -59,28 +79,17 @@ douyinRouter.get("/douyin", async (ctx) => { try { // 从缓存中获取数据 let data = await get(cacheKey); - const cookie = await get(cacheCookieKey); + const cookie = await getDouyinCookie(); const from = data ? "cache" : "server"; if (!data) { - if(!cookie) { - // 获取Cookies - let cookisResponse = await axios.get(cookisUrl); - let cookie = getCookies(cookisResponse); - - // 将Cookie写入缓存 - console.log('--- Cookie写入缓存 ---', cookie); - await set(cacheCookieKey, cookie); - } - // 如果缓存中不存在数据 console.log("从服务端重新获取抖音热点榜"); // 从服务器拉取数据 const response = await axios.get(url, { headers: { - 'Cookie': `passport_csrf_token=${cookie}` - } + Cookie: `passport_csrf_token=${cookie}`, + }, }); - data = getData(response.data); updateTime = new Date().toISOString(); if (!data) { @@ -117,23 +126,13 @@ douyinRouter.get("/douyin", async (ctx) => { // 抖音热点榜 - 获取最新数据 douyinRouter.get("/douyin/new", async (ctx) => { console.log("获取抖音热点榜 - 最新数据"); - const cookie = await get(cacheCookieKey); try { - if(!cookie) { - // 获取Cookies - let cookisResponse = await axios.get(cookisUrl); - let cookie = getCookies(cookisResponse); - - // 将Cookie写入缓存 - console.log('--- Cookie写入缓存 ---', cookie); - await set(cacheCookieKey, cookie); - } - // 从服务器拉取最新数据 + const cookie = await getDouyinCookie(); const response = await axios.get(url, { headers: { - 'Cookie': `passport_csrf_token=${cookie}` - } + Cookie: `passport_csrf_token=${cookie}`, + }, }); const newData = getData(response.data); updateTime = new Date().toISOString();