diff --git a/docs/new-media.md b/docs/new-media.md index 1e7db3d58..766124410 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -58,7 +58,7 @@ pageClass: routes ### 搜索文章 - + ## 52hrtt 华人头条 diff --git a/lib/v2/36kr/maintainer.js b/lib/v2/36kr/maintainer.js index ce068a47e..2062fa272 100644 --- a/lib/v2/36kr/maintainer.js +++ b/lib/v2/36kr/maintainer.js @@ -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'], }; diff --git a/lib/v2/36kr/search/article.js b/lib/v2/36kr/search/article.js index 487465a30..3baeaffdf 100644 --- a/lib/v2/36kr/search/article.js +++ b/lib/v2/36kr/search/article.js @@ -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: `

${item.content}

`, + 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, }; };