From b44b7aa92a0434b86b7225c6d01e17da609af64d Mon Sep 17 00:00:00 2001 From: Jinkin Date: Fri, 25 Feb 2022 00:38:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=BF=AE=E5=A4=8D=E7=89=9B?= =?UTF-8?q?=E5=AE=A2=E7=BD=91=E8=AE=A8=E8=AE=BA=E5=B0=86=E7=AE=80=E4=BB=8B?= =?UTF-8?q?=E6=8F=90=E5=8F=96=E5=88=B0=20title=20(#9189)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:更改title提取规则 * fix: pubDate legacy url.resolve --- lib/routes/nowcoder/discuss.js | 39 +++++++++++++++------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/routes/nowcoder/discuss.js b/lib/routes/nowcoder/discuss.js index 4c2292147..577d999ee 100644 --- a/lib/routes/nowcoder/discuss.js +++ b/lib/routes/nowcoder/discuss.js @@ -1,7 +1,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); -const url = require('url'); -const date = require('@/utils/date'); +const { parseDate } = require('@/utils/parse-date'); +const timezone = require('@/utils/timezone'); const host = 'https://www.nowcoder.com'; @@ -19,7 +19,7 @@ module.exports = async (ctx) => { const list = $('li.clearfix') .map(function () { const info = { - title: $(this).find('div.discuss-main.clearfix a').text().trim().replace('\n', ' '), + title: $(this).find('div.discuss-main.clearfix a:first').text().trim().replace('\n', ' '), link: $(this).find('div.discuss-main.clearfix a[rel]').attr('href'), }; return info; @@ -27,30 +27,25 @@ module.exports = async (ctx) => { .get(); const out = await Promise.all( - list.map(async (info) => { + list.map((info) => { const title = info.title || 'tzgg'; - const itemUrl = url.resolve(host, info.link).replace(new RegExp('^(.*)[?](.*)$'), '$1'); + const itemUrl = new URL(info.link, host).href.replace(new RegExp('^(.*)[?](.*)$'), '$1'); - const cache = await ctx.cache.get(itemUrl); - if (cache) { - return Promise.resolve(JSON.parse(cache)); - } + return ctx.cache.tryGet(itemUrl, async () => { + const response = await got.get(itemUrl); + const $ = cheerio.load(response.data); - const response = await got.get(itemUrl); - const $ = cheerio.load(response.data); + const date_value = $('span.post-time').text(); - const date_value = $('span.post-time').text().split(' ')[1]; + const description = $('.nc-post-content').html(); - const description = $('.nc-post-content').html(); - - const single = { - title, - link: itemUrl, - description, - pubDate: date(date_value, 8), - }; - ctx.cache.set(itemUrl, JSON.stringify(single)); - return Promise.resolve(single); + return { + title, + link: itemUrl, + description, + pubDate: timezone(parseDate(date_value), +8), + }; + }); }) );