From 934efb840b39f634135c2e43f723f56d90b2c2cf Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 13 Oct 2022 20:21:18 +0400 Subject: [PATCH] fix(route); zhihu posts (#11084) --- lib/v2/zhihu/posts.js | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/v2/zhihu/posts.js b/lib/v2/zhihu/posts.js index f357f9a49e..95211315f4 100644 --- a/lib/v2/zhihu/posts.js +++ b/lib/v2/zhihu/posts.js @@ -7,15 +7,12 @@ module.exports = async (ctx) => { const id = ctx.params.id; const usertype = ctx.params.usertype; - const response = await got({ - method: 'get', - url: `https://www.zhihu.com/${usertype}/${id}/posts`, + const { data } = await got(`https://www.zhihu.com/${usertype}/${id}/posts`, { headers: { ...utils.header, Referer: `https://www.zhihu.com/${usertype}/${id}/`, }, }); - const data = response.data; const $ = cheerio.load(data); const jsondata = $('#js-initialData'); const authorname = $('.ProfileHeader-name') @@ -27,22 +24,30 @@ module.exports = async (ctx) => { const parsed = JSON.parse(jsondata.html()); const articlesdata = parsed.initialState.entities.articles; - const list = []; - Object.keys(articlesdata).forEach((v) => { - list.push(articlesdata[v]); + const list = Object.keys(articlesdata).map((key) => { + const $ = cheerio.load(articlesdata[key].content, null, false); + $('noscript').remove(); + $('img').each((_, item) => { + if (item.attribs['data-actualsrc'] || item.attribs['data-original']) { + item.attribs['data-actualsrc'] = item.attribs['data-actualsrc'] ? item.attribs['data-actualsrc'].split('?source')[0] : null; + item.attribs['data-original'] = item.attribs['data-original'] ? item.attribs['data-original'].split('?source')[0] : null; + item.attribs.src = item.attribs['data-original'] || item.attribs['data-actualsrc']; + delete item.attribs['data-actualsrc']; + delete item.attribs['data-original']; + } + }); + return { + title: articlesdata[key].title, + description: $.html(), + link: articlesdata[key].url, + pubDate: parseDate(articlesdata[key].created, 'X'), + }; }); ctx.state.data = { - title: `${authorname}的知乎文章`, + title: `${authorname} 的知乎文章`, link: `https://www.zhihu.com/${usertype}/${id}/posts`, description: authordescription, - item: - list.length > 0 && - list.map((item) => ({ - title: item.title, - description: item.content, - link: item.url, - pubDate: parseDate(item.updated * 1000), - })), + item: list, }; };