From 04804d588eb4d969934fcfa6d3512531a74f0777 Mon Sep 17 00:00:00 2001 From: Jeason Lau Date: Tue, 18 Feb 2020 18:54:18 +0800 Subject: [PATCH] fix: neunews (#4018) --- lib/routes/universities/neu/news.js | 83 +++++++++++++++-------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/routes/universities/neu/news.js b/lib/routes/universities/neu/news.js index ee4db7854b..c2645d92e7 100644 --- a/lib/routes/universities/neu/news.js +++ b/lib/routes/universities/neu/news.js @@ -16,48 +16,49 @@ module.exports = async (ctx) => { const title = $('title').text(); const items = $('.column-news-list > .news_list > .news > .news_title') .slice(0, 10) - .children(); - const results = []; - items.each(async (index, item) => { - const label = $(item); - const title = $(label).attr('title'); - const url = baseUrl + $(label).attr('href'); - const description = await ctx.cache.tryGet(url, async () => { - const result = await got.get(url); - const $ = cheerio.load(result.data); - // 处理部分格式 - $('article') - .find('span') - .each(function() { - const temp = $(this).text(); - $(this).replaceWith(temp); - }); - $('article') - .find('div') - .each(function() { - const temp = $(this).html(); - $(this).replaceWith(temp); - }); - $('article') - .find('a') - .remove(); - $('article') - .find('p') - .each(function() { - $(this).removeAttr('style'); - $(this).removeAttr('class'); - }); - return $('article').html(); - }); - const result = { - title: title, - description: description, - link: url, - }; - results.push(result); - }); + .get(); + const results = await Promise.all( + items.map(async (item) => { + const $ = cheerio.load(item); + const title = $('a').attr('title'); + const url = baseUrl + $('a').attr('href'); + const description = await ctx.cache.tryGet(url, async () => { + const result = await got.get(url); + const $ = cheerio.load(result.data); + // 处理部分格式 + $('article') + .find('span') + .each(function() { + const temp = $(this).text(); + $(this).replaceWith(temp); + }); + $('article') + .find('div') + .each(function() { + const temp = $(this).html(); + $(this).replaceWith(temp); + }); + $('article') + .find('a') + .remove(); + $('article') + .find('p') + .each(function() { + $(this).removeAttr('style'); + $(this).removeAttr('class'); + }); + return $('article').html(); + }); + const result = { + title: title, + description: description, + link: url, + }; + Promise.resolve(result); + }) + ); ctx.state.data = { - title: title, + title: `东北大学新闻网-${title}`, item: results, }; };