fix(route): 36kr搜索文章 (#10112)

This commit is contained in:
Ethan Shen
2022-07-04 23:21:26 +08:00
committed by GitHub
parent 711a359a49
commit 8f8adfb4b2
3 changed files with 15 additions and 58 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ pageClass: routes
### 搜索文章
<Route author="xyqfer kt286" example="/36kr/search/article/ofo" path="/36kr/search/article/:keyword" :paramsDesc="['关键字']" />
<Route author="xyqfer kt286 nczitzk" example="/36kr/search/article/ofo" path="/36kr/search/article/:keyword" :paramsDesc="['关键字']" />
## 52hrtt 华人头条
+1 -1
View File
@@ -2,6 +2,6 @@ module.exports = {
'/motif/:mid': ['nczitzk'],
'/news/:category?': ['nczitzk'],
'/newsflashes': ['hillerliao'],
'/search/article/:keyword': ['xyqfer', 'kt286'],
'/search/article/:keyword': ['xyqfer', 'kt286', 'nczitzk'],
'/user/:uid': ['nczitzk'],
};
+13 -56
View File
@@ -2,69 +2,26 @@ const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
module.exports = async (ctx) => {
const { keyword = '' } = ctx.params;
const csrfresponse = await got.get('https://36kr.com/pp/api/csrf');
const cookies = csrfresponse.headers['set-cookie'].toString();
const token = cookies.match(/M-XSRF-TOKEN=.{64}/)[0];
const acw_tc = cookies.match(/acw_tc=.{61}/)[0];
const krnewsfrontss = cookies.match(/krnewsfrontss=.{32}/)[0];
const keyword = ctx.params.keyword ?? '';
const timestamp = new Date().getTime();
const rootUrl = 'https://www.36kr.com';
const currentUrl = `${rootUrl}/search/articles/${keyword}`;
const response = await got({
method: 'post',
url: `https://gateway.36kr.com/api/mis/nav/search/resultbytype`,
headers: {
'Content-Type': 'application/json',
cookie: `${token};${acw_tc};${krnewsfrontss};`,
'M-X-XSRF-TOKEN': token.replace('M-XSRF-TOKEN=', ''),
},
data: JSON.stringify({
param: {
pageCallback: Buffer.from(
JSON.stringify({
firstId: 1,
lastId: 1,
firstCreateTime: timestamp,
lastCreateTime: timestamp,
})
).toString('base64'),
pageEvent: 1,
pageSize: 20,
platformId: 2,
searchType: 'article',
searchWord: keyword,
siteId: 1,
sort: 'date',
},
partner_id: 'web',
timestamp,
}),
method: 'get',
url: currentUrl,
});
const load = async (link) => {
const response = await got.get(link);
const description = response.data.match(/"widgetContent":"(.*?)","sourceType":/)[1];
return { description };
};
const items = await Promise.all(
(response.data.data.itemList || []).map(async (item) => {
const link = `https://www.36kr.com/p/${item.itemId}`;
const single = {
title: item.widgetTitle,
link,
pubDate: parseDate(item.publishTime),
};
const other = await ctx.cache.tryGet(link, () => load(link));
return { ...single, ...other };
})
);
const items = JSON.parse(response.data.match(/"itemList":(.*),"pageCallback"/)[1]).map((item) => ({
title: item.widgetTitle.replace(/<\/?em>/g, ''),
link: `${rootUrl}/p/${item.itemId}`,
description: `<p>${item.content}</p>`,
pubDate: parseDate(item.publishTime),
}));
ctx.state.data = {
title: `36kr - ${keyword}`,
link: `https://www.36kr.com/search/articles/${encodeURIComponent(keyword)}`,
title: `36氪 - ${keyword}相关文章`,
link: currentUrl,
item: items,
};
};