From 0d21758052e765a62f167dff39f98ec03e82cbf5 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 22 Jan 2023 02:53:33 -1200 Subject: [PATCH] fix(route): bjp (#11667) --- docs/picture.md | 2 +- lib/router.js | 2 +- lib/routes/bjp/apod.js | 48 ---------------------------------------- lib/v2/bjp/apod.js | 43 +++++++++++++++++++++++++++++++++++ lib/v2/bjp/maintainer.js | 3 +++ lib/v2/bjp/radar.js | 13 +++++++++++ lib/v2/bjp/router.js | 3 +++ 7 files changed, 64 insertions(+), 50 deletions(-) delete mode 100644 lib/routes/bjp/apod.js create mode 100644 lib/v2/bjp/apod.js create mode 100644 lib/v2/bjp/maintainer.js create mode 100644 lib/v2/bjp/radar.js create mode 100644 lib/v2/bjp/router.js diff --git a/docs/picture.md b/docs/picture.md index 4fa32331a..44d023243 100644 --- a/docs/picture.md +++ b/docs/picture.md @@ -468,7 +468,7 @@ R18 显示 ### 每日一图 - + ## 不羞涩 diff --git a/lib/router.js b/lib/router.js index 369e33dc3..db3d4f83f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/bjp/apod.js b/lib/routes/bjp/apod.js deleted file mode 100644 index 9b0a66cd9..000000000 --- a/lib/routes/bjp/apod.js +++ /dev/null @@ -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, - }; -}; diff --git a/lib/v2/bjp/apod.js b/lib/v2/bjp/apod.js new file mode 100644 index 000000000..0a1eedd7a --- /dev/null +++ b/lib/v2/bjp/apod.js @@ -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, + }; +}; diff --git a/lib/v2/bjp/maintainer.js b/lib/v2/bjp/maintainer.js new file mode 100644 index 000000000..793f6fc71 --- /dev/null +++ b/lib/v2/bjp/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/apod': ['HenryQW'], +}; diff --git a/lib/v2/bjp/radar.js b/lib/v2/bjp/radar.js new file mode 100644 index 000000000..a86014193 --- /dev/null +++ b/lib/v2/bjp/radar.js @@ -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', + }, + ], + }, +}; diff --git a/lib/v2/bjp/router.js b/lib/v2/bjp/router.js new file mode 100644 index 000000000..840883bb9 --- /dev/null +++ b/lib/v2/bjp/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/apod', require('./apod')); +};