feat: OSChina 新闻抓取的时候解析时间字符串 (#2062)

This commit is contained in:
zengxs 2019-05-07 12:08:51 +08:00 committed by DIYgod
parent e47b3eef48
commit 4e5f50fe56
2 changed files with 11 additions and 0 deletions

View File

@ -1,5 +1,6 @@
const url = require('url');
const axios = require('../../utils/axios');
const date = require('../../utils/date');
const cheerio = require('cheerio');
const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0';
@ -60,6 +61,7 @@ module.exports = async (ctx) => {
title: each.find('a', 'h3').attr('title'),
description: each.find('p', '.description').text(),
link: url.resolve('https://www.oschina.net', encodeURI(originalUrl)),
pubDate: date(each.find('.extra > .list > .item:nth-of-type(2)').text()),
};
if (/^https?:\/\/www.oschina.net\/news\/.*$/.test(originalUrl)) {
const key = 'oschina' + item.link;

View File

@ -26,6 +26,9 @@ module.exports = (html, timeZone = -serverOffset) => {
} else if (/昨天 (\d+):(\d+)/.exec(html)) {
math = /昨天 (\d+):(\d+)/.exec(html);
date = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 1, math[1], math[2]);
} else if (/前天\s*(\d+):(\d+)/.exec(html)) {
math = /前天\s*(\d+):(\d+)/.exec(html);
date = new Date(date.getFullYear(), date.getMonth(), date.getDate() - 2, math[1], math[2]);
} else if (/(\d+)年(\d+)月(\d+)日(\d+)时/.exec(html)) {
math = /(\d+)年(\d+)月(\d+)日(\d+)时/.exec(html);
date = new Date(parseInt(math[1]), parseInt(math[2]) - 1, parseInt(math[3]), parseInt(math[4]));
@ -38,6 +41,12 @@ module.exports = (html, timeZone = -serverOffset) => {
} else if (/(\d+)-(\d+) (\d+):(\d+)/.exec(html)) {
math = /(\d+)-(\d+) (\d+):(\d+)/.exec(html);
date = new Date(date.getFullYear(), parseInt(math[1]) - 1, math[2], math[3], math[4]);
} else if (/(\d+)\/(\d+)\/(\d+)\s*(\d+):(\d+)/.exec(html)) {
math = /(\d+)\/(\d+)\/(\d+)\s*(\d+):(\d+)/.exec(html);
date = new Date(math[1], parseInt(math[2]) - 1, math[3], math[4], math[5]);
} else if (/(\d+)\/(\d+)\s*(\d+):(\d+)/.exec(html)) {
math = /(\d+)\/(\d+)\s*(\d+):(\d+)/.exec(html);
date = new Date(date.getFullYear(), parseInt(math[1]) - 1, math[2], math[3], math[4]);
} else if (/(\d+)月(\d+)日 (\d+):(\d+)/.exec(html)) {
math = /(\d+)月(\d+)日 (\d+):(\d+)/.exec(html);
date = new Date(date.getFullYear(), parseInt(math[1]) - 1, math[2], math[3], math[4]);