feat: 更新接口说明 #9

This commit is contained in:
imsyy 2023-07-03 10:47:35 +08:00
parent 4ad20b1aed
commit ffc9e7cb37
2 changed files with 63 additions and 60 deletions

View File

@ -16,21 +16,23 @@
> 🟠 可能失效 > 🟠 可能失效
> 🔴 无法使用 > 🔴 无法使用
| **站点** | **类别** | **调用名称** | **状态** | | **站点** | **类别** | **调用名称** | **状态** |
| -------- | -------- | ------------ | -------- | | ------------ | -------- | ------------ | -------- |
| 哔哩哔哩 | 热门榜 | bilibili | 🟢 | | 哔哩哔哩 | 热门榜 | bilibili | 🟢 |
| 知乎 | 热榜 | zhihu | 🟢 | | 知乎 | 热榜 | zhihu | 🟢 |
| 百度 | 热搜榜 | baidu | 🟢 | | 百度 | 热搜榜 | baidu | 🟢 |
| 百度贴吧 | 热议榜 | tieba | 🟢 | | 百度贴吧 | 热议榜 | tieba | 🟢 |
| 少数派 | 热榜 | sspai | 🟢 | | 少数派 | 热榜 | sspai | 🟢 |
| IT之家 | 热榜 | ithome | 🟠 | | IT 之家 | 热榜 | ithome | 🟠 |
| 澎湃新闻 | 热榜 | thepaper | 🟢 | | 澎湃新闻 | 热榜 | thepaper | 🟢 |
| 今日头条 | 热榜 | toutiao | 🟢 | | 今日头条 | 热榜 | toutiao | 🟢 |
| 微博热搜 | 热搜榜 | weibo | 🟢 | | 微博热搜 | 热搜榜 | weibo | 🟢 |
| 36氪 | 热榜 | 36kr | 🟢 | | 36 氪 | 热榜 | 36kr | 🟢 |
| 稀土掘金 | 热榜 | juejin | 🟢 | | 稀土掘金 | 热榜 | juejin | 🟢 |
| 腾讯新闻 | 热点榜 | newsqq | 🟢 | | 腾讯新闻 | 热点榜 | newsqq | 🟢 |
| 历史上的今天 | 指定日期 | calendar | 🟢 | | 抖音热榜 | 热点榜 | douyin | 🟢 |
| 英雄联盟 | 更新公告 | lol | 🟢 |
| 历史上的今天 | 指定日期 | calendar | 🟢 |
### 特殊接口说明 ### 特殊接口说明
@ -118,6 +120,7 @@ GET https://api-hot.imsyy.top/bilibili/
] ]
} }
``` ```
</details> </details>
### 获取榜单最新数据 ### 获取榜单最新数据
@ -172,6 +175,7 @@ GET https://api-hot.imsyy.top/bilibili/new
] ]
} }
``` ```
</details> </details>
## 其他 ## 其他

View File

@ -11,16 +11,18 @@ const cacheCookieKey = "douyinCookieData";
let updateTime = new Date().toISOString(); 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获取路径 // Token 获取路径
const cookisUrl = "https://www.douyin.com/passport/general/login_guiding_strategy/?aid=6383"; const cookisUrl =
"https://www.douyin.com/passport/general/login_guiding_strategy/?aid=6383";
// 数据处理 // 数据处理
const getData = (data) => { const getData = (data) => {
if (!data) return []; if (!data) return [];
const dataList = []; const dataList = [];
try { try {
const jsonObject = data.data.word_list; const jsonObject = data.data.word_list;
jsonObject.forEach((v) => { jsonObject.forEach((v) => {
dataList.push({ dataList.push({
@ -28,30 +30,48 @@ try {
pic: `${v.word_cover.url_list[0]}`, pic: `${v.word_cover.url_list[0]}`,
hot: Number(v.hot_value), hot: Number(v.hot_value),
url: `https://www.douyin.com/hot/${encodeURIComponent(v.sentence_id)}`, 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; return dataList;
} catch (error) { } catch (error) {
console.error("数据处理出错" + error); console.error("数据处理出错" + error);
return false; return [];
} }
}; };
// 处理Cookis // 处理抖音 Cookis
const getCookies = (data) => { const setDouyinCookies = (data) => {
if(!data) return null; if (!data) return null;
try { try {
const pattern = /passport_csrf_token=(.*); Path/s; const pattern = /passport_csrf_token=(.*); Path/s;
const matchResult = data.headers["set-cookie"][0].match(pattern); const matchResult = data.headers["set-cookie"][0].match(pattern);
const cookieData = matchResult[1]; const cookieData = matchResult[1];
return cookieData; return cookieData;
} catch (error) {
console.error("获取抖音 Cookie 出错" + error);
return null;
}
};
} catch (error) { // 获取抖音 Cookie数据
console.error("获取Cookis出错" + error); const getDouyinCookie = async () => {
return false; 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) => { douyinRouter.get("/douyin", async (ctx) => {
@ -59,28 +79,17 @@ douyinRouter.get("/douyin", async (ctx) => {
try { try {
// 从缓存中获取数据 // 从缓存中获取数据
let data = await get(cacheKey); let data = await get(cacheKey);
const cookie = await get(cacheCookieKey); const cookie = await getDouyinCookie();
const from = data ? "cache" : "server"; const from = data ? "cache" : "server";
if (!data) { 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("从服务端重新获取抖音热点榜"); console.log("从服务端重新获取抖音热点榜");
// 从服务器拉取数据 // 从服务器拉取数据
const response = await axios.get(url, { const response = await axios.get(url, {
headers: { headers: {
'Cookie': `passport_csrf_token=${cookie}` Cookie: `passport_csrf_token=${cookie}`,
} },
}); });
data = getData(response.data); data = getData(response.data);
updateTime = new Date().toISOString(); updateTime = new Date().toISOString();
if (!data) { if (!data) {
@ -117,23 +126,13 @@ douyinRouter.get("/douyin", async (ctx) => {
// 抖音热点榜 - 获取最新数据 // 抖音热点榜 - 获取最新数据
douyinRouter.get("/douyin/new", async (ctx) => { douyinRouter.get("/douyin/new", async (ctx) => {
console.log("获取抖音热点榜 - 最新数据"); console.log("获取抖音热点榜 - 最新数据");
const cookie = await get(cacheCookieKey);
try { 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, { const response = await axios.get(url, {
headers: { headers: {
'Cookie': `passport_csrf_token=${cookie}` Cookie: `passport_csrf_token=${cookie}`,
} },
}); });
const newData = getData(response.data); const newData = getData(response.data);
updateTime = new Date().toISOString(); updateTime = new Date().toISOString();