From ecbd064dbf740600d08a6379c49c5187754159a9 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sun, 6 Feb 2022 18:59:07 +0800 Subject: [PATCH] feat(route): add BioOne (#8994) * feat(route): add BioOne * fix typo * fix typo --- docs/en/journal.md | 4 ++ docs/journal.md | 4 ++ lib/router.js | 2 +- lib/{routes => v2}/bioone/featured.js | 17 ++++---- lib/v2/bioone/journal.js | 58 +++++++++++++++++++++++++++ lib/v2/bioone/maintainer.js | 4 ++ lib/v2/bioone/radar.js | 19 +++++++++ lib/v2/bioone/router.js | 4 ++ 8 files changed, 103 insertions(+), 9 deletions(-) rename lib/{routes => v2}/bioone/featured.js (75%) create mode 100644 lib/v2/bioone/journal.js create mode 100644 lib/v2/bioone/maintainer.js create mode 100644 lib/v2/bioone/radar.js create mode 100644 lib/v2/bioone/router.js diff --git a/docs/en/journal.md b/docs/en/journal.md index 63afefef78..9cb0c18e6b 100644 --- a/docs/en/journal.md +++ b/docs/en/journal.md @@ -41,6 +41,10 @@ Fill in parameter `query` with content after `http://export.arxiv.org/api/query? +### Journals + + + ## Cell Journal diff --git a/docs/journal.md b/docs/journal.md index e2a4075d0f..f2ebe0562f 100644 --- a/docs/journal.md +++ b/docs/journal.md @@ -41,6 +41,10 @@ pageClass: routes +### Journals + + + ## Cell ### 主刊 diff --git a/lib/router.js b/lib/router.js index adea8b8528..d1c4d8e510 100644 --- a/lib/router.js +++ b/lib/router.js @@ -3898,7 +3898,7 @@ router.get('/sznews/ranking', lazyloadRouteHandler('./routes/sznews/ranking')); router.get('/shuax/project/:name?', lazyloadRouteHandler('./routes/shuax/project')); // BioOne -router.get('/bioone/featured', lazyloadRouteHandler('./routes/bioone/featured')); +// router.get('/bioone/featured', lazyloadRouteHandler('./routes/bioone/featured')); // Obsidian router.get('/obsidian/announcements', lazyloadRouteHandler('./routes/obsidian/announcements')); diff --git a/lib/routes/bioone/featured.js b/lib/v2/bioone/featured.js similarity index 75% rename from lib/routes/bioone/featured.js rename to lib/v2/bioone/featured.js index a68b0c7f90..80b8a77461 100644 --- a/lib/routes/bioone/featured.js +++ b/lib/v2/bioone/featured.js @@ -1,5 +1,6 @@ const got = require('@/utils/got'); const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); module.exports = async (ctx) => { const rootUrl = 'https://bioone.org'; @@ -10,9 +11,10 @@ module.exports = async (ctx) => { const $ = cheerio.load(response.data); - const list = $('.items h4 a') - .slice(0, 10) - .map((_, item) => { + let items = $('.items h4 a') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10) + .toArray() + .map((item) => { item = $(item); const link = item.attr('href'); @@ -20,11 +22,10 @@ module.exports = async (ctx) => { title: item.text(), link: link.indexOf('http') < 0 ? `${rootUrl}${link}` : link, }; - }) - .get(); + }); - const items = await Promise.all( - list.map((item) => + items = await Promise.all( + items.map((item) => ctx.cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', @@ -34,7 +35,7 @@ module.exports = async (ctx) => { item.description = content('#divARTICLECONTENTTop').html(); item.doi = content('meta[name="dc.Identifier"]').attr('content'); - item.pubDate = new Date(content('meta[name="dc.Date"]').attr('content')).toUTCString(); + item.pubDate = parseDate(content('meta[name="dc.Date"]').attr('content')); return item; }) diff --git a/lib/v2/bioone/journal.js b/lib/v2/bioone/journal.js new file mode 100644 index 0000000000..e6faaaf4dc --- /dev/null +++ b/lib/v2/bioone/journal.js @@ -0,0 +1,58 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); + +module.exports = async (ctx) => { + const journal = ctx.params.journal ?? 'acta-chiropterologica'; + + const rootUrl = 'https://bioone.org'; + const currentUrl = `${rootUrl}/journals/${journal}/current`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + let items = $('.TOCLineItemBoldText') + .slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10) + .toArray() + .map((item) => { + item = $(item).parent(); + + return { + link: `${rootUrl}${item.attr('href')}`, + }; + }); + + items = await Promise.all( + items.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('.ProceedingsArticleOpenAccessPanel').remove(); + content('#divNotSignedSection, #rightRail').remove(); + + item.description = content('.panel-body').html(); + item.title = content('meta[name="dc.Title"]').attr('content'); + item.author = content('meta[name="dc.Creator"]').attr('content'); + item.doi = content('meta[name="dc.Identifier"]').attr('content'); + item.pubDate = parseDate(content('meta[name="dc.Date"]').attr('content')); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${response.data.match(/'journalpaper', '(.*)'\)\\" class=\\"/)[1]} - BioOne`, + link: currentUrl, + item: items, + }; +}; diff --git a/lib/v2/bioone/maintainer.js b/lib/v2/bioone/maintainer.js new file mode 100644 index 0000000000..c5f6e20636 --- /dev/null +++ b/lib/v2/bioone/maintainer.js @@ -0,0 +1,4 @@ +module.exports = { + '/featured': ['nczitzk'], + '/journals/:journal?': ['nczitzk'], +}; diff --git a/lib/v2/bioone/radar.js b/lib/v2/bioone/radar.js new file mode 100644 index 0000000000..8e491c5286 --- /dev/null +++ b/lib/v2/bioone/radar.js @@ -0,0 +1,19 @@ +module.exports = { + 'bioone.org': { + _name: 'BioOne', + '.': [ + { + title: 'Featured articles', + docs: 'https://docs.rsshub.app/journal.html#bioone-featured-articles', + source: '/', + target: '/bioone/featured', + }, + { + title: 'Journals', + docs: 'https://docs.rsshub.app/journal.html#bioone-journals', + source: ['/journals/:journal', '/'], + target: '/bioone/journals/:journal', + }, + ], + }, +}; diff --git a/lib/v2/bioone/router.js b/lib/v2/bioone/router.js new file mode 100644 index 0000000000..87bfd285f8 --- /dev/null +++ b/lib/v2/bioone/router.js @@ -0,0 +1,4 @@ +module.exports = function (router) { + router.get('/featured', require('./featured')); + router.get('/journals/:journal?', require('./journal')); +};