From da3fc383bf255c47e659533727f15d8ef915df12 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Wed, 2 Sep 2020 20:48:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20add=20NASA=E4=B8=AD=E6=96=87=E6=AF=8F?= =?UTF-8?q?=E6=97=A5=E4=B8=80=E5=9B=BE=20(#5518)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/picture.md | 12 +++++++ lib/router.js | 1 + lib/routes/nasa/apod-cn.js | 64 ++++++++++++++++++++++++++++++++++++ lib/routes/nasa/apod-ncku.js | 2 +- lib/routes/nasa/apod.js | 2 +- 5 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 lib/routes/nasa/apod-cn.js diff --git a/docs/picture.md b/docs/picture.md index 88311eaad0..c474be23f1 100644 --- a/docs/picture.md +++ b/docs/picture.md @@ -101,6 +101,18 @@ pageClass: routes +### NASA 中文 + + + +::: tip 提示 + +[NASA 中文](https://www.nasachina.cn/) 提供了每日天文图的中英双语图文说明,但在更新上偶尔略有一两天的延迟。 + +::: + + + ## nHentai ### 分类筛选 diff --git a/lib/router.js b/lib/router.js index b2830b5251..bc84f0b107 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3102,6 +3102,7 @@ router.get('/soomal/topics/:category/:language?', require('./routes/soomal/topic // NASA router.get('/nasa/apod', require('./routes/nasa/apod')); router.get('/nasa/apod-ncku', require('./routes/nasa/apod-ncku')); +router.get('/nasa/apod-cn', require('./routes/nasa/apod-cn')); // 爱Q生活网 router.get('/iqshw/latest', require('./routes/iqshw/latest')); diff --git a/lib/routes/nasa/apod-cn.js b/lib/routes/nasa/apod-cn.js new file mode 100644 index 0000000000..bb73069791 --- /dev/null +++ b/lib/routes/nasa/apod-cn.js @@ -0,0 +1,64 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const rootUrl = `https://www.nasachina.cn/apotd`; + const response = await got({ + method: 'get', + url: rootUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('div.post-archive article') + .slice(0, 3) + .map((_, item) => { + item = $(item); + const a = item.find('h2.entry-title a'); + return { + title: a.text(), + link: a.attr('href'), + }; + }) + .get(); + + const items = await Promise.all( + list.map( + async (item) => + await ctx.cache.tryGet(item.link, async () => { + const detailResponse = await got({ + method: 'get', + url: item.link, + }); + const content = cheerio.load(detailResponse.data); + + content('#wpd-post-rating').remove(); + + item.description = content('#main article').html(); + + let date; + + if (content('h4').text()) { + date = content('h4').text().replace(/(年|月)/g, '-').replace(/日/, ''); + item.title += ` | ${content('h4').text()}`; + } else { + date = new Date(content('time.entry-date').attr('datetime')); + const year = date.getFullYear().toString(); + const month = (date.getMonth() + 1).toString().padStart(2, '0'); + const day = date.getDate().toString().padStart(2, '0'); + item.title += ` | ${year}年${month}月${day}日`; + } + + item.pubDate = new Date(date).toUTCString(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: 'NASA中文 - 天文·每日一图', + link: rootUrl, + item: items, + }; +}; diff --git a/lib/routes/nasa/apod-ncku.js b/lib/routes/nasa/apod-ncku.js index c7e191264d..983f16c61f 100644 --- a/lib/routes/nasa/apod-ncku.js +++ b/lib/routes/nasa/apod-ncku.js @@ -2,7 +2,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); module.exports = async (ctx) => { - const date = new Date(); + const date = new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60 * 1000 + -5 * 60 * 60 * 1000); const year = date.getFullYear().toString(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); diff --git a/lib/routes/nasa/apod.js b/lib/routes/nasa/apod.js index fe7a4840e5..924d233082 100644 --- a/lib/routes/nasa/apod.js +++ b/lib/routes/nasa/apod.js @@ -2,7 +2,7 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); module.exports = async (ctx) => { - const date = new Date(); + const date = new Date(new Date().getTime() + new Date().getTimezoneOffset() * 60 * 1000 + -5 * 60 * 60 * 1000); const year = date.getFullYear().toString(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0');