fix(route): fix pubDate for 新华社新闻 (#7982)

This commit is contained in:
auto-bot-ty 2021-09-23 12:48:48 +08:00 committed by GitHub
parent ee5a81be09
commit 8a8e91f04e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 13 deletions

View File

@ -1,5 +1,7 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const rootUrl = 'http://www.news.cn';
@ -12,7 +14,7 @@ module.exports = async (ctx) => {
const $ = cheerio.load(response.data);
const list = $('h3 a')
.slice(0, 15)
.slice(0, 35)
.map((_, item) => {
item = $(item);
return {
@ -25,20 +27,26 @@ module.exports = async (ctx) => {
const items = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
let detailResponse;
try {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const content = cheerio.load(detailResponse.data);
item.description = content('#detail').html();
item.pubDate = new Date(detailResponse.data.match(/(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/)[1] + ' GMT+8').toUTCString();
return item;
} catch (e) {
return Promise.resolve('');
detailResponse = await got(item.link);
} catch (error) {
item.status = 404;
}
if (item.status !== 404) {
const content = cheerio.load(detailResponse.data);
const date = content('.header-time.left').text();
const author =
content('.editor').text() ||
content('.p-jc')
.text()
.replace(/[\r\n]/g, '');
item.author = author.match(/责任编辑.(.*)/)[1].trim();
content('#articleEdit').remove();
item.description = content('#detail').html();
item.pubDate = timezone(parseDate(date, 'YYYYMM/DD HH:mm:ss'), +8);
}
return item;
})
)
);