From e31ce8cf6105fd2b56ccf0d77b929c3711eea4ef Mon Sep 17 00:00:00 2001 From: Laam Pui Date: Mon, 14 Dec 2020 19:53:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=9F=A5=E4=B9=8E=E4=B8=93=E6=A0=8F=20i?= =?UTF-8?q?ssue=20(#6438)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/zhihu/zhuanlan.js | 88 ++++++++---------------------------- 1 file changed, 19 insertions(+), 69 deletions(-) diff --git a/lib/routes/zhihu/zhuanlan.js b/lib/routes/zhihu/zhuanlan.js index 4971b42f6..d3b2ae589 100644 --- a/lib/routes/zhihu/zhuanlan.js +++ b/lib/routes/zhihu/zhuanlan.js @@ -7,84 +7,34 @@ module.exports = async (ctx) => { const listRes = await got({ method: 'get', - url: `https://zhuanlan.zhihu.com/api/columns/${id}/articles`, - headers: { - ...utils.header, - Referer: `https://zhuanlan.zhihu.com/${id}`, - }, - }); - const infoRes = await got({ - method: 'get', - url: `https://zhuanlan.zhihu.com/api/columns/${id}`, + url: `https://www.zhihu.com/api/v4/columns/${id}/items`, headers: { ...utils.header, Referer: `https://zhuanlan.zhihu.com/${id}`, }, }); - if (listRes.status === 403) { - throw 'list resource api returned status code ' + listRes.status; - } + const infoRes = await got.get(`https://zhuanlan.zhihu.com/${id}`); + const $ = cheerio.load(infoRes.data); + const title = $('.css-zyehvu').text(); + const description = $('.css-1bnklpv').text(); - if (infoRes.status === 403) { - throw 'info resource api returned status code ' + infoRes.status; - } - - const list = listRes.data.data; - const info = infoRes.data; - - const articles = await Promise.all( - list.map(async (article) => { - const url = article.url; - const item = { - title: article.title, - description: '', - pubDate: new Date(article.updated * 1000).toUTCString(), - link: article.url, - }; - const key = 'zhuanlan' + article.url; - const value = await ctx.cache.get(key); - - if (value) { - item.description = value; - } else { - const articleOriginal = await got({ - method: 'get', - url: url, - headers: { - Referer: url, - }, - }); - - const articleContent = cheerio.load(articleOriginal.data)('.Post-RichText').html(); - - if (articleContent !== null) { - item.description = utils.ProcessImage(articleContent); - } else { - item.description = utils.ProcessImage(cheerio.load(articleOriginal.data)('.PostIndex-warning').html()); - } - - ctx.cache.set(key, item.description); - } - - return Promise.resolve(item); - }) - ); - - if (!Array.isArray(list)) { - throw JSON.stringify(list); - } + const item = listRes.data.data.map((item) => { + const $ = cheerio.load(item.content); + $('img').css('max-width', '100%'); + return { + title: item.title, + link: item.url, + description: $.html(), + pubDate: new Date(item.created), + author: item.author.name, + }; + }); ctx.state.data = { - title: `知乎专栏-${info.title}`, + description, + item, + title: `知乎专栏-${title}`, link: `https://zhuanlan.zhihu.com/${id}`, - description: info.description, - item: articles, - // list.map((item) => ({ - // title: item.title, - // description: item.content.replace(/ src="/g, ' src="https://pic4.zhimg.com/'), - // pubDate: new Date(item.publishedTime).toUTCString(), - // link: `https://zhuanlan.zhihu.com${item.url}`, - // })), }; };