fix(route): 修复牛客网讨论将简介提取到 title (#9189)

* fix:更改title提取规则

* fix: pubDate legacy url.resolve
This commit is contained in:
Jinkin 2022-02-25 00:38:00 +08:00 committed by GitHub
parent 77f3e710fa
commit b44b7aa92a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 22 deletions

View File

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