From e48858d59fddd8e3930a2855dc608672faaae116 Mon Sep 17 00:00:00 2001 From: Ethan Shen Date: Sat, 27 Nov 2021 14:29:30 +0800 Subject: [PATCH] feat(route): add Research Gate Publications (#7544) --- docs/en/new-media.md | 6 +++ docs/new-media.md | 6 +++ lib/router.js | 3 ++ lib/routes/researchgate/publications.js | 60 +++++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 lib/routes/researchgate/publications.js diff --git a/docs/en/new-media.md b/docs/en/new-media.md index e6a1f175a4..a9aeac7d7d 100644 --- a/docs/en/new-media.md +++ b/docs/en/new-media.md @@ -372,6 +372,12 @@ Compared to the official one, this feed: +## Research Gate + +### Publications + + + ## Semiconductor Industry Association ### Latest News diff --git a/docs/new-media.md b/docs/new-media.md index 5c7e2498d2..6dc4dc0abb 100644 --- a/docs/new-media.md +++ b/docs/new-media.md @@ -633,6 +633,12 @@ IPFS 网关有可能失效,那时候换成其他网关。 +## Research Gate + +### Publications + + + ## Simons Foundation ### 文章 diff --git a/lib/router.js b/lib/router.js index 9fc7af08b4..d5c06a774f 100644 --- a/lib/router.js +++ b/lib/router.js @@ -4212,4 +4212,7 @@ router.get('/s-hentai/:id?', lazyloadRouteHandler('./routes/s-hentai')); // Deprecated: DO NOT ADD ROUTE HERE +// Research Gate +router.get('/researchgate/publications/:id', require('./routes/researchgate/publications')); + module.exports = router; diff --git a/lib/routes/researchgate/publications.js b/lib/routes/researchgate/publications.js new file mode 100644 index 0000000000..6d469ea921 --- /dev/null +++ b/lib/routes/researchgate/publications.js @@ -0,0 +1,60 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); + +module.exports = async (ctx) => { + const id = ctx.params.id; + + const rootUrl = 'https://www.researchgate.net'; + const currentUrl = `${rootUrl}/profile/${id}`; + const response = await got({ + method: 'get', + url: currentUrl, + }); + + const $ = cheerio.load(response.data); + + const list = $('div[itemprop="headline"] a') + .slice(0, 15) + .map((_, item) => { + item = $(item); + return { + title: item.text(), + link: item.attr('href'), + }; + }) + .get(); + + const items = await Promise.all( + list.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); + + item.doi = content('meta[property="citation_doi"]').attr('content'); + item.pubDate = Date.parse(content('meta[property="citation_publication_date"]').attr('content')); + + const authors = []; + + content('meta[property="citation_author"]').each(function () { + authors.push(content(this).attr('content')); + }); + + item.author = authors.join(', '); + + item.description = content('div[itemprop="description"]').html(); + + return item; + }) + ) + ); + + ctx.state.data = { + title: `${$('meta[property="profile:username"]').attr('content')}'s Publications - ResearchGate`, + link: currentUrl, + item: items, + }; +};