From 0e9a5de6692e7c85fb673612a6f8a501663d7b03 Mon Sep 17 00:00:00 2001 From: tgly307 Date: Mon, 31 Dec 2018 00:58:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9F=A5=E4=B9=8E=E4=B8=93?= =?UTF-8?q?=E6=A0=8F=E5=92=8C=E5=BC=80=E6=BA=90=E4=B8=AD=E5=9B=BD=20(#1321?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 怎么还顺便改了一些其他路由的格式……eslint抽了? --- lib/routes/oschina/news.js | 4 +-- lib/routes/zhihu/utils.js | 8 +++++- lib/routes/zhihu/zhuanlan.js | 53 ++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/lib/routes/oschina/news.js b/lib/routes/oschina/news.js index f0db6876d4..1ad78883a1 100644 --- a/lib/routes/oschina/news.js +++ b/lib/routes/oschina/news.js @@ -25,8 +25,7 @@ module.exports = async (ctx) => { description: each.find('p', '.description').text(), link: originalUrl, }; - if (/^\/news\/.*$/.test(originalUrl)) { - item.link = 'https://www.oschina.net' + originalUrl; + if (/^https:\/\/www.oschina.net\/news\/.*$/.test(originalUrl)) { const key = 'oschina' + item.link; const value = await ctx.cache.get(key); @@ -41,6 +40,7 @@ module.exports = async (ctx) => { }, }); const content = cheerio.load(detail.data); + content('.ad-wrap').remove(); item.description = content('#articleContent').html(); ctx.cache.set(key, item.description, 24 * 60 * 60); } diff --git a/lib/routes/zhihu/utils.js b/lib/routes/zhihu/utils.js index 7eb086ae71..c5134439c8 100644 --- a/lib/routes/zhihu/utils.js +++ b/lib/routes/zhihu/utils.js @@ -8,13 +8,19 @@ module.exports = { ProcessImage: function(content) { const $ = cheerio.load(content, { xmlMode: true }); - $('img.content_image, img.origin_image, img.content-image').each((i, e) => { + $('img.content_image, img.origin_image, img.content-image, img.data-actualsrc').each((i, e) => { if (e.attribs['data-original']) { $(e).attr({ src: e.attribs['data-original'], width: null, height: null, }); + } else if (!e.attribs['data-original'] && e.attribs['data-actualsrc']) { + $(e).attr({ + src: e.attribs['data-actualsrc'], + width: null, + height: null, + }); } else { $(e).attr({ src: e.attribs.src.replace('_b.jpg', '_r.jpg'), diff --git a/lib/routes/zhihu/zhuanlan.js b/lib/routes/zhihu/zhuanlan.js index 0da82a8e3f..ae8c5fb8aa 100644 --- a/lib/routes/zhihu/zhuanlan.js +++ b/lib/routes/zhihu/zhuanlan.js @@ -1,12 +1,13 @@ const axios = require('../../utils/axios'); const utils = require('./utils'); +const cheerio = require('cheerio'); module.exports = async (ctx) => { const id = ctx.params.id; const listRes = await axios({ method: 'get', - url: `https://zhuanlan.zhihu.com/api/columns/${id}/posts?limit=20`, + url: `https://zhuanlan.zhihu.com/api2/columns/${id}/articles`, headers: { ...utils.header, Referer: `https://zhuanlan.zhihu.com/${id}`, @@ -29,22 +30,56 @@ module.exports = async (ctx) => { throw 'info resource api returned status code ' + infoRes.status; } - const list = listRes.data; + 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: '', + link: article.url, + }; + const key = 'zhuanlan' + article.url; + const value = await ctx.cache.get(key); + + if (value) { + item.description = value; + } else { + const articleOriginal = await axios({ + method: 'get', + url: url, + headers: { + Referer: url, + }, + }); + item.description = utils.ProcessImage( + cheerio + .load(articleOriginal.data)('.Post-RichText') + .html() + ); + ctx.cache.set(key, item.description, 24 * 60 * 60); + } + + return Promise.resolve(item); + }) + ); + if (!Array.isArray(list)) { throw JSON.stringify(list); } ctx.state.data = { - title: `知乎专栏-${info.name}`, + title: `知乎专栏-${info.title}`, link: `https://zhuanlan.zhihu.com/${id}`, description: info.description, - item: list.map((item) => ({ - title: item.title, - description: item.content.replace(/ src="/g, ' referrerpolicy="no-referrer" src="https://pic4.zhimg.com/'), - pubDate: new Date(item.publishedTime).toUTCString(), - link: `https://zhuanlan.zhihu.com${item.url}`, - })), + item: articles, + // list.map((item) => ({ + // title: item.title, + // description: item.content.replace(/ src="/g, ' referrerpolicy="no-referrer" src="https://pic4.zhimg.com/'), + // pubDate: new Date(item.publishedTime).toUTCString(), + // link: `https://zhuanlan.zhihu.com${item.url}`, + // })), }; };