diff --git a/docs/new-media.md b/docs/new-media.md index 0d7e9ef3a..5623e00d9 100755 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -9,6 +9,7 @@ pageClass: routes ### 首页更新 + ## 36kr ### 资讯 diff --git a/lib/routes/36kr/motif.js b/lib/routes/36kr/motif.js index e9ebcab5e..6f4f29b86 100644 --- a/lib/routes/36kr/motif.js +++ b/lib/routes/36kr/motif.js @@ -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; }) ) diff --git a/lib/routes/36kr/search/article.js b/lib/routes/36kr/search/article.js index 215aa9378..4f9b2fa8a 100644 --- a/lib/routes/36kr/search/article.js +++ b/lib/routes/36kr/search/article.js @@ -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)); diff --git a/lib/routes/36kr/user.js b/lib/routes/36kr/user.js index dc41bbab4..0ad6670fc 100644 --- a/lib/routes/36kr/user.js +++ b/lib/routes/36kr/user.js @@ -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; }) )