add 北京天文馆每日一图 (#1653)

This commit is contained in:
Ming Dai
2019-03-02 23:16:02 +08:00
committed by DIYgod
parent bf7dfc345b
commit b61990fc69
3 changed files with 58 additions and 0 deletions

View File

@@ -3094,6 +3094,10 @@ type 为 all 时category 参数不支持 cost 和 free
<route name="最新动态" author="LogicJake" example="/12306/zxdt" path="/12306/zxdt/:id?" :paramsDesc="['铁路局id可在 URL 中找到,不填默认显示所有铁路局动态']"/> <route name="最新动态" author="LogicJake" example="/12306/zxdt" path="/12306/zxdt/:id?" :paramsDesc="['铁路局id可在 URL 中找到,不填默认显示所有铁路局动态']"/>
### 北京天文馆
<route name="每日一图" author="radaiming" example="/bjp/apod" path="/bjp/apod"/>
### 洛谷 ### 洛谷
<route name="日报" author="LogicJake" example="/luogu/daily" path="/luogu/daily/:id?" :paramsDesc="['年度日报所在帖子id可在 URL 中找到不填默认为2019年日报']"/> <route name="日报" author="LogicJake" example="/luogu/daily" path="/luogu/daily/:id?" :paramsDesc="['年度日报所在帖子id可在 URL 中找到不填默认为2019年日报']"/>

View File

@@ -1104,6 +1104,9 @@ router.get('/dcard/posts/:type?', require('./routes/dcard/posts'));
// 12306 // 12306
router.get('/12306/zxdt/:id?', require('./routes/12306/zxdt')); router.get('/12306/zxdt/:id?', require('./routes/12306/zxdt'));
// 北京天文馆每日一图
router.get('/bjp/apod', require('./routes/bjp/apod'));
// 洛谷日报 // 洛谷日报
router.get('/luogu/daily/:id?', require('./routes/luogu/daily')); router.get('/luogu/daily/:id?', require('./routes/luogu/daily'));

51
lib/routes/bjp/apod.js Normal file
View File

@@ -0,0 +1,51 @@
const cheerio = require('cheerio');
const axios = require('../../utils/axios');
module.exports = async (ctx) => {
const baseUrl = 'http://www.bjp.org.cn';
const indexUrl = baseUrl + '/col/col89/index.html';
let limit = parseInt(ctx.query.limit);
if (isNaN(limit) || limit === 0) {
limit = 5;
}
const res = await axios.get(indexUrl);
const data = res.data;
const $ = cheerio.load(data);
const list = $('b > a');
const urls = [];
const titles = [];
for (let i = 0; i < limit && i < list.length; i++) {
urls.push(baseUrl + list[i].attribs.href);
titles.push(list[i].attribs.title);
}
const items = await Promise.all(
urls.map(async (url, i) => {
const cache = await ctx.cache.get(url);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const data = (await axios.get(url)).data;
const $ = cheerio.load(data);
const time = $('meta[name=pubDate]').attr('content');
const title = titles[i];
const item = {
title,
pubDate: new Date(time).toUTCString(),
description: $('body > table > tbody')
.eq(1)
.html(),
link: url,
guid: url,
};
ctx.cache.set(url, JSON.stringify(item), limit * 24 * 60 * 60);
return Promise.resolve(item);
})
);
ctx.state.data = {
title: '北京天文馆每日一图',
link: indexUrl,
item: items,
};
};