From dff523d194f3b9897d61d7514c0cb9a3ac2874cf Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 23 Jan 2022 00:12:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=B8=AD=E5=9B=BD=E4=BA=BA?= =?UTF-8?q?=E6=B0=91=E5=A4=A7=E5=AD=A6=E4=BA=BA=E4=BA=8B=E5=A4=84=20(#8620?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/v2/ruc/hr.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/lib/v2/ruc/hr.js b/lib/v2/ruc/hr.js index c016a5f31..18befc901 100644 --- a/lib/v2/ruc/hr.js +++ b/lib/v2/ruc/hr.js @@ -15,29 +15,35 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data); - const list = $('a[title]') - .map((_, item) => { + let items = $('a[title]') + .toArray() + .map((item) => { item = $(item); + const link = item.attr('href'); + return { title: item.text(), - link: `${rootUrl}/${category}/${item.attr('href')}`, + link: `${rootUrl}${link.indexOf('..') === 0 ? link.replace(/\.\./, '') : `/${category}/${link}`}`, }; - }) - .get(); + }); - const items = await Promise.all( - list.map((item) => + items = await Promise.all( + items.map((item) => ctx.cache.tryGet(item.link, async () => { - const detailResponse = await got({ - method: 'get', - url: item.link, - }); + try { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); - const content = cheerio.load(detailResponse.data); + const content = cheerio.load(detailResponse.data); - item.description = content('.neirong').html(); - item.pubDate = parseDate(detailResponse.data.match(/日期:(\d{4}-\d{2}-\d{2})/)[1]); + item.description = content('.neirong').html(); + item.pubDate = parseDate(detailResponse.data.match(/日期:(\d{4}-\d{2}-\d{2})/)[1]); + } catch (err) { + item.description = 'Not Found'; + } return item; })