From 0e847d2233dc4cd89e4395d856a54437efaf6b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=A1=E5=B1=B1=E7=94=B5=E8=BD=A6?= Date: Wed, 22 Aug 2018 14:40:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=E4=B8=AD=E5=9B=BD=E5=9C=B0=E9=9C=87?= =?UTF-8?q?=E5=B1=80=E4=BF=A1=E6=81=AF=E5=92=8CBangumi.tv=E7=9A=84?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=B7=BB=E5=8A=A0pubDate=20(#509)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为中国地震局信息和Bangumi.tv的信息添加pubDate; 修正地震信息内容显式的BUG --- README.md | 2 ++ package.json | 1 + routes/bangumi/group/reply.js | 9 ++++++ routes/bangumi/subject/comments.js | 25 ++++++++++++++- routes/bangumi/subject/offcial-subject-api.js | 3 ++ routes/earthquake/index.js | 31 ++++++++++++++++--- 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 9f62ea597..beac04946 100644 --- a/README.md +++ b/README.md @@ -249,6 +249,8 @@ RSSHub 是一个轻量、易于扩展的 RSS 生成器,可以给任何奇奇 - 游记 - 江南大学 - 教务处通知 +- 中国地震局 + - 震情速报 diff --git a/package.json b/package.json index d57a2ff49..80a35137c 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "koa-favicon": "2.0.1", "koa-router": "7.4.0", "lru-cache": "4.1.3", + "luxon": "^1.3.3", "markdown-it": "^8.4.2", "path-to-regexp": "2.2.1", "raven": "^2.6.3", diff --git a/routes/bangumi/group/reply.js b/routes/bangumi/group/reply.js index 6cd403d81..b1b9a49dd 100644 --- a/routes/bangumi/group/reply.js +++ b/routes/bangumi/group/reply.js @@ -1,5 +1,6 @@ const axios = require('../../../utils/axios'); const cheerio = require('cheerio'); +const DateTime = require('luxon').DateTime; module.exports = async (ctx) => { // bangumi.tv未提供获取小组话题的API,因此仍需要通过抓取网页来获取 @@ -16,6 +17,13 @@ module.exports = async (ctx) => { id: $el.attr('id'), author: $el.find('.userInfo .l').text(), content: $el.find('.reply_content .message').html(), + date: $el + .children() + .first() + .find('small') + .children() + .remove() + .end().text().slice(3), }; }) .get() @@ -28,6 +36,7 @@ module.exports = async (ctx) => { title: `${c.author}回复了小组话题《${title}》`, description: c.content, guid: c.id, + pubDate: DateTime.fromFormat(c.date, 'yyyy-L-dd HH:mm'), link, })), }; diff --git a/routes/bangumi/subject/comments.js b/routes/bangumi/subject/comments.js index a521ee15d..2afd3cb36 100644 --- a/routes/bangumi/subject/comments.js +++ b/routes/bangumi/subject/comments.js @@ -1,5 +1,6 @@ const axios = require('../../../utils/axios'); const cheerio = require('cheerio'); +const DateTime = require('luxon').DateTime; module.exports = async (subjectID, minLength) => { // bangumi.tv未提供获取“吐槽(comments)”的API,因此仍需要通过抓取网页来获取 @@ -17,10 +18,31 @@ module.exports = async (subjectID, minLength) => { if ($rateEl.length > 0) { rate = $rateEl.attr('class').match(/sstars(\d)/)[1]; } + + let dateString = $el.find('small.grey').text().slice(2); + let date; + if(dateString.includes('ago')) { + // 处理表示相对日期的字符串 + const list = dateString + .match(/(\dd )?(\d{1,2}h )?(\d{1,2}m )?ago/) + .slice(1) + .map(s => s ? Number(s.slice(0, -2)) : 0); + + date = DateTime.local().minus({ + days: list[0], + hours: list[1], + minutes: list[2], + }); + } else { + // 表示绝对日期的字符串 + date = DateTime.fromFormat(dateString, 'yyyy-L-dd HH:mm'); + } + return { user: $el.find('.l').text(), - rate: rate ? rate / 2 : '无', + rate: rate || '无', content: $el.find('p').text(), + date: date.toRFC2822(), }; }) .get() @@ -32,6 +54,7 @@ module.exports = async (subjectID, minLength) => { title: `${c.user}的吐槽`, description: `【评分:${c.rate}】 ${c.content}`, guid: c.user, + pubDate: c.date, link, })), }; diff --git a/routes/bangumi/subject/offcial-subject-api.js b/routes/bangumi/subject/offcial-subject-api.js index 6d464ede9..ce4c91a85 100644 --- a/routes/bangumi/subject/offcial-subject-api.js +++ b/routes/bangumi/subject/offcial-subject-api.js @@ -1,4 +1,6 @@ const axios = require('../../../utils/axios'); +const DateTime = require('luxon').DateTime; + module.exports = (type) => { const mapping = { blog: { @@ -22,6 +24,7 @@ module.exports = (type) => { title: `${article.user.nickname}:${article.title}`, description: article.summary || '', link: article.url, + pubDate: DateTime.fromMillis(article.timestamp * 1000).toRFC2822(), })), }; }; diff --git a/routes/earthquake/index.js b/routes/earthquake/index.js index 8843b3df3..e041092f5 100644 --- a/routes/earthquake/index.js +++ b/routes/earthquake/index.js @@ -1,5 +1,6 @@ const axios = require('../../utils/axios'); const cheerio = require('cheerio'); +const Datetime = require('luxon').DateTime; module.exports = async (ctx) => { const link = 'http://www.cea.gov.cn/publish/dizhenj/464/479/index.html'; @@ -16,12 +17,9 @@ module.exports = async (ctx) => { ctx.cache.set(url, html, 3 * 24 * 60 * 60); } const $1 = cheerio.load(html); - const $content = $1('.detail_main_right_conbg_con > div') - .find('script') - .remove() - .end(); + const $content = $1('.detail_main_right_conbg_con > div'); const $container = $('
'); - $container.append(`

${$content.text().replace('()', '')}

`); + $content .find('img') .each((i, el) => { @@ -34,9 +32,32 @@ module.exports = async (ctx) => { .parent() .appendTo($container); + const $scriptEls = $content.find('script'); + const info = [ + $($scriptEls[0]).html().match(/,"(.+)"/)[1], + $($scriptEls[1]).html().match(/"(.+)"/)[1] + ', ', + $($scriptEls[2]).html().match(/"(.+)"/)[1], + $($scriptEls[3]).html().match(/"(.+)"/)[1], + ]; + + const text = info.reduce( + (text, field) => text.replace(/