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), + }; + }); }) );