diff --git a/docs/README.md b/docs/README.md index d76babd7a1..780415a94c 100644 --- a/docs/README.md +++ b/docs/README.md @@ -3094,6 +3094,10 @@ type 为 all 时,category 参数不支持 cost 和 free +### 北京天文馆 + + + ### 洛谷 diff --git a/lib/router.js b/lib/router.js index c24a54ec27..38668de5f0 100644 --- a/lib/router.js +++ b/lib/router.js @@ -1104,6 +1104,9 @@ router.get('/dcard/posts/:type?', require('./routes/dcard/posts')); // 12306 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')); diff --git a/lib/routes/bjp/apod.js b/lib/routes/bjp/apod.js new file mode 100644 index 0000000000..2e86997dd1 --- /dev/null +++ b/lib/routes/bjp/apod.js @@ -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, + }; +};