feat: add NASA中文每日一图 (#5518)

This commit is contained in:
Ethan Shen
2020-09-02 20:48:33 +08:00
committed by GitHub
parent 29f2e73d70
commit da3fc383bf
5 changed files with 79 additions and 2 deletions

View File

@@ -101,6 +101,18 @@ pageClass: routes
<Route author="nczitzk" example="/nasa/apod-ncku" path="/nasa/apod-ncku" />
### NASA 中文
<Route author="nczitzk" example="/nasa/apod-cn" path="/nasa/apod-cn">
::: tip 提示
[NASA 中文](https://www.nasachina.cn/) 提供了每日天文图的中英双语图文说明,但在更新上偶尔略有一两天的延迟。
:::
</Route>
## nHentai
### 分类筛选

View File

@@ -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'));

View File

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

View File

@@ -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');

View File

@@ -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');