fix(route): bjp (#11667)

This commit is contained in:
Tony 2023-01-22 02:53:33 -12:00 committed by GitHub
parent 7e6fbdb37f
commit 0d21758052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 50 deletions

View File

@ -468,7 +468,7 @@ R18 显示
### 每日一图
<Route author="HenryQW" example="/bjp/apod" path="/bjp/apod"/>
<Route author="HenryQW" example="/bjp/apod" path="/bjp/apod" radar="1"/>
## 不羞涩

View File

@ -1352,7 +1352,7 @@ router.get('/ui-cn/user/:id', lazyloadRouteHandler('./routes/ui-cn/user'));
// router.get('/dcard/:section/:type?', lazyloadRouteHandler('./routes/dcard/section'));
// 北京天文馆每日一图
router.get('/bjp/apod', lazyloadRouteHandler('./routes/bjp/apod'));
// router.get('/bjp/apod', lazyloadRouteHandler('./routes/bjp/apod'));
// 洛谷
// router.get('/luogu/daily/:id?', lazyloadRouteHandler('./routes/luogu/daily'));

View File

@ -1,48 +0,0 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
module.exports = async (ctx) => {
const baseUrl = 'http://www.bjp.org.cn';
const indexUrl = baseUrl + '/mryt/index.shtml';
const res = await got.get(indexUrl);
const $ = cheerio.load(res.data);
const list = $('b > a')
.slice(0, 10)
.get()
.map((e) => $(e));
const items = await Promise.all(
list.map(async (e) => {
const link = `${baseUrl}${e.attr('href')}`;
const cache = await ctx.cache.get(link);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const data = await got.get(link);
const $ = cheerio.load(data.data);
const content = $($('body > table')[1]);
const item = {
title: $(e).attr('title'),
pubDate: new Date(data.data.match(/\d{4}-\d{2}-\d{2}/)[0]).toUTCString(),
description: content.find('table').last().html(),
link,
guid: link,
};
ctx.cache.set(link, JSON.stringify(item));
return item;
})
);
ctx.state.data = {
title: '北京天文馆每日一图',
link: indexUrl,
item: items,
};
};

43
lib/v2/bjp/apod.js Normal file
View File

@ -0,0 +1,43 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');
const timezone = require('@/utils/timezone');
module.exports = async (ctx) => {
const baseUrl = 'https://www.bjp.org.cn';
const listUrl = `${baseUrl}/APOD/list.shtml`;
const res = await got(listUrl);
const $ = cheerio.load(res.data);
const list = $('td[align=left] b')
.toArray()
.map((e) => {
e = $(e);
return {
title: e.find('a').attr('title'),
link: `${baseUrl}${e.find('a').attr('href')}`,
pubDate: timezone(parseDate(e.find('span').text().replace('', ''), 'YYYY-MM-DD'), 8),
};
});
const items = await Promise.all(
list.map((e) =>
ctx.cache.tryGet(e.link, async () => {
const { data } = await got.get(e.link);
const $ = cheerio.load(data);
e.description = $('.juzhong').html();
return e;
})
)
);
ctx.state.data = {
title: $('head title').text(),
description: '探索宇宙!每天发布一张迷人宇宙的影像,以及由专业天文学家撰写的简要说明。',
link: listUrl,
item: items,
};
};

3
lib/v2/bjp/maintainer.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
'/apod': ['HenryQW'],
};

13
lib/v2/bjp/radar.js Normal file
View File

@ -0,0 +1,13 @@
module.exports = {
'bjp.org.cn': {
_name: '北京天文馆',
'.': [
{
title: '每日一图',
docs: 'https://docs.rsshub.app/picture.html#bei-jing-tian-wen-guan',
source: ['/APOD/today.shtml', '/APOD/list.shtml', '/'],
target: '/bjp/apod',
},
],
},
};

3
lib/v2/bjp/router.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = (router) => {
router.get('/apod', require('./apod'));
};