fix: 36kr search & user (#4988)

This commit is contained in:
Ethan Shen 2020-06-14 22:09:55 +08:00 committed by GitHub
parent 3b6d3007ae
commit d290152d0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 20 deletions

View File

@ -9,6 +9,7 @@ pageClass: routes
### 首页更新
<Route author="xfangbao" example="/199it" path="/199it" />
## 36kr
### 资讯

View File

@ -1,5 +1,4 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const motifUrl = `https://36kr.com/motif/${ctx.params.mid}`;
@ -13,7 +12,7 @@ module.exports = async (ctx) => {
const motifArticleList = data.motifArticleList.data.itemList;
const articleList = motifArticleList.map((item) => ({
title: item.templateMaterial.widgetTitle,
link: `https://36kr.com/p/${item.itemId}`,
link: `https://36kr.com/${item.route.split('?')[0] === 'detail_video' ? 'video' : 'p'}/${item.itemId}`,
pubDate: new Date(item.templateMaterial.publishTime).toUTCString(),
}));
@ -25,8 +24,9 @@ module.exports = async (ctx) => {
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const contentResponse = await got({ method: 'get', url: item.link });
const content = cheerio.load(contentResponse.data);
item.description = content('div.common-width.content.articleDetailContent.kr-rich-text-wrapper').html();
const result = contentResponse.data.match(/"articleDetailData":(.*?),"articleRecommendData":/) || contentResponse.data.match(/"videoDetail":(.*?),"authorVideos":/);
const contentData = JSON.parse(result[1]);
item.description = contentData.data.widgetContent;
return item;
})
)

View File

@ -8,36 +8,52 @@ module.exports = async (ctx) => {
const acw_tc = cookies.match(/acw_tc=.{61}/)[0];
const krnewsfrontss = cookies.match(/krnewsfrontss=.{32}/)[0];
const timestamp = new Date().getTime();
const response = await got({
method: 'post',
url: `https://36kr.com/pp/api/search/entity-search`,
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({
page: 1,
per_page: 20,
sort: 'date',
entity_type: 'post',
keyword: keyword,
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: timestamp,
}),
});
const load = async (link) => {
const response = await got.get(link);
const description = response.data.data.content;
const description = response.data.match(/"widgetContent":"(.*?)","sourceType":/)[1];
return { description };
};
const items = await Promise.all(
(response.data.data.items || []).map(async (item) => {
const link = `https://36kr.com/api/post/${item.id}`;
(response.data.data.itemList || []).map(async (item) => {
const link = `https://www.36kr.com/p/${item.itemId}`;
const single = {
title: item.title,
link: `https://www.36kr.com/p/${item.id}`,
pubDate: new Date(item.published_at).toUTCString(),
title: item.widgetTitle,
link: link,
pubDate: new Date(item.publishTime).toUTCString(),
};
const other = await ctx.cache.tryGet(link, async () => await load(link));

View File

@ -1,5 +1,4 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const userUrl = `https://36kr.com/user/${ctx.params.uid}`;
@ -13,7 +12,7 @@ module.exports = async (ctx) => {
const authorFlowList = data.authorFlowList.data.itemList;
const articleList = authorFlowList.map((item) => ({
title: item.templateMaterial.widgetTitle,
link: `https://36kr.com/p/${item.itemId}`,
link: `https://36kr.com/${item.route.split('?')[0] === 'detail_video' ? 'video' : 'p'}/${item.itemId}`,
pubDate: new Date(item.templateMaterial.publishTime).toUTCString(),
}));
@ -25,8 +24,9 @@ module.exports = async (ctx) => {
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const contentResponse = await got({ method: 'get', url: item.link });
const content = cheerio.load(contentResponse.data);
item.description = content('div.common-width.content.articleDetailContent.kr-rich-text-wrapper').html();
const result = contentResponse.data.match(/"articleDetailData":(.*?),"articleRecommendData":/) || contentResponse.data.match(/"videoDetail":(.*?),"authorVideos":/);
const contentData = JSON.parse(result[1]);
item.description = contentData.data.widgetContent;
return item;
})
)