From e942cefde86c7de589eef239724c196c72ade8d2 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 30 Jan 2021 13:27:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=9F=A5=E8=AF=86=E5=88=86=E5=AD=90?= =?UTF-8?q?=E8=B5=84=E8=AE=AF=20(#6804)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/zhishifenzi/news.js | 100 ++++++++++++++++----------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/lib/routes/zhishifenzi/news.js b/lib/routes/zhishifenzi/news.js index 6fb38de96..4b43b9a94 100644 --- a/lib/routes/zhishifenzi/news.js +++ b/lib/routes/zhishifenzi/news.js @@ -1,67 +1,67 @@ -const got = require('@/utils/got'); -const cheerio = require('cheerio'); const url = require('url'); +const cheerio = require('cheerio'); +const got = require('@/utils/got'); const date = require('@/utils/date'); -const base = 'http://zhishifenzi.com'; +const titles = { + multiple: '综合', + biology: '生物', + medicine: '医药', + ai: '人工智能', + physics: '物理', + chymistry: '化学', + astronomy: '天文', + other: '其他', +}; module.exports = async (ctx) => { - let link = `${base}/news/`; - - const type = ctx.params.type; // type can be on of: - // ["biology", "medicine", "ai", "physics", "chymistry", "astronomy", "others"] - if (typeof type !== 'undefined') { - link = `${base}/news/${type}`; - } - const response = await got.get(encodeURI(link)); - const pageCapture = cheerio.load(response.data); + // ["biology", "medicine", "ai", "physics", "chymistry", "astronomy", "other"] + const type = ctx.params.type || 'multiple'; - let title = pageCapture('div.all_categories > ul > li.category').text().split(':').pop(); - if (title === '') { - title = '全部資訊'; - } else { - title = `資訊「${title}」`; - } - const list = pageCapture('div.inner_news_list > ul > li.clearfix').get(); - const out = await Promise.all( - list.map(async (elem) => { - const $ = cheerio.load(elem); - const title = $('div > h5 > a').text(); - const partial = $('div > h5 > a').attr('href'); - const address = url.resolve(base, partial); - const pubDate = date($('span').first().text()); + const rootUrl = 'http://zhishifenzi.com'; + const currentUrl = url.resolve(rootUrl, '/news/ajaxarticles'); + const response = await got({ + method: 'post', + url: currentUrl, + form: { + category: type, + }, + }); - // const keywords = $('span') - // .last() - // .html(); - const item = { - title, - pubDate, - link: encodeURI(address), + const $ = cheerio.load(response.data); + const list = $('h5 a') + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: url.resolve(rootUrl, item.attr('href')), }; - - const value = await ctx.cache.get(address); - if (value) { - item.description = value; - } else { - const detail = await got.get(item.link); - const detailCapture = cheerio.load(detail.data); - - item.description = detailCapture('.main div.inner_content') - .html() - .replace(/src="\//g, `src="${base}/`); - ctx.cache.set(address, item.description); - } - - return Promise.resolve(item); }) + .get(); + + const out = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + item.description = content('.inner_content').html(); + item.pubDate = date(content('.inner_view_from').text()); + + return item; + }) + ) ); ctx.state.data = { - title: `知識分子 | ${title}`, + title: `知識分子 | 資訊「${titles[type] || '综合'}」`, description: `知識分子 | 科學 文明 智慧`, - link: link, + link: url.resolve(rootUrl, '/news'), item: out, }; };