Skip to content

Commit

Permalink
fix(route): PubMed Trending articles (#9402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Shen authored Mar 28, 2022
1 parent 668286a commit 5927f28
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 70 deletions.
14 changes: 12 additions & 2 deletions docs/en/journal.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,19 @@ Subscribe to the cover images of the Nature journals, and get the latest publica

## PubMed

### Trending
### Trending articles

<RouteEn author="yech1990" example="/pubmed/trending" path="/pubmed/trending" supportScihub="1"/>
<RouteEn author="yech1990 nczitzk" example="/pubmed/trending" path="/pubmed/trending/:filter?" :paramsDesc="['Filters, can be found in URL']" supportScihub="1">

::: tip Tip

For the parameter **filter**, the `filter` parameter in the URL should be split into a string by `,`, here is an example.

In <https://pubmed.ncbi.nlm.nih.gov/trending/?filter=simsearch1.fha&filter=pubt.clinicaltrial&filter=pubt.randomizedcontrolledtrial>, the filter parameters are `simsearch1.fha`, `pubt.clinicaltrial`, and `pubt.randomizedcontrolledtrial`. Therefore, the filter corresponding to the route should be filled with `simsearch1.fha,pubt.clinicaltrial,pubt.randomizedcontrolledtrial`, and the route is [`/pubmed/trending/simsearch1.fha,pubt .clinicaltrial,pubt.randomizedcontrolledtrial`](https://rsshub.app/pubmed/trending/simsearch1.fha,pubt.clinicaltrial,pubt.randomizedcontrolledtrial)

:::

</RouteEn>

## Science Journal

Expand Down
14 changes: 12 additions & 2 deletions docs/journal.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,19 @@ pageClass: routes

## PubMed

### 热门文章
### Trending articles

<Route author="yech1990" example="/pubmed/trending" path="/pubmed/trending" supportScihub="1"/>
<Route author="yech1990 nczitzk" example="/pubmed/trending" path="/pubmed/trending/:filter?" :paramsDesc="['过滤条件,可在 URL 中找到']" supportScihub="1">

::: tip 提示

对于参数 **过滤条件**,应将 URL 中的 filter 参数用 `,` 分割成一个字段填入,下面是一个例子。

<https://pubmed.ncbi.nlm.nih.gov/trending/?filter=simsearch1.fha&filter=pubt.clinicaltrial&filter=pubt.randomizedcontrolledtrial> 中 filter 参数有 `simsearch1.fha` `pubt.clinicaltrial` `pubt.randomizedcontrolledtrial` 三者。所以,对应到路由的 filter 应填入 `simsearch1.fha,pubt.clinicaltrial,pubt.randomizedcontrolledtrial`,于是可获得路由 [`/pubmed/trending/simsearch1.fha,pubt.clinicaltrial,pubt.randomizedcontrolledtrial`](https://rsshub.app/pubmed/trending/simsearch1.fha,pubt.clinicaltrial,pubt.randomizedcontrolledtrial)

:::

</Route>

## Science 系列

Expand Down
2 changes: 1 addition & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,7 @@ router.get('/kaggle/competitions/:category?', lazyloadRouteHandler('./routes/kag
router.get('/kaggle/user/:user', lazyloadRouteHandler('./routes/kaggle/user'));

// PubMed Trending
router.get('/pubmed/trending', lazyloadRouteHandler('./routes/pubmed/trending'));
// router.get('/pubmed/trending', lazyloadRouteHandler('./routes/pubmed/trending'));

// 领科 (linkresearcher.com)
router.get('/linkresearcher/:params', lazyloadRouteHandler('./routes/linkresearcher/index'));
Expand Down
65 changes: 0 additions & 65 deletions lib/routes/pubmed/trending.js

This file was deleted.

3 changes: 3 additions & 0 deletions lib/v2/pubmed/maintainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
'/trending/:filters?': ['nczitzk'],
};
13 changes: 13 additions & 0 deletions lib/v2/pubmed/radar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
'pubmed.ncbi.nlm.nih.gov': {
_name: 'PubMed',
'.': [
{
title: 'Trending articles',
docs: 'https://docs.rsshub.app/journal.html#pubmed-trending-articles',
source: ['/trending', '/'],
target: (params, url) => `/pubmed/trending/${new URL(url).searchParams.getAll('filter').join(',')}`,
},
],
},
};
3 changes: 3 additions & 0 deletions lib/v2/pubmed/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/trending/:filters?', require('./trending'));
};
7 changes: 7 additions & 0 deletions lib/v2/pubmed/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ if authors }}
{{@ authors }}
{{ /if}}
<br>
{{ if abs }}
{{@ abs }}
{{ /if}}
58 changes: 58 additions & 0 deletions lib/v2/pubmed/trending.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const { parseDate } = require('@/utils/parse-date');
const { art } = require('@/utils/render');
const path = require('path');

module.exports = async (ctx) => {
const filters = ctx.params.filters;

const rootUrl = 'https://pubmed.ncbi.nlm.nih.gov';
const currentUrl = `${rootUrl}/trending${filters ? `?filter=${filters.replace(/,/g, '&filter=')}` : ''}`;

const response = await got({
method: 'get',
url: currentUrl,
});

const $ = cheerio.load(response.data);

let items = $('a[data-article-id]')
.toArray()
.map((item) => {
item = $(item);

return {
title: item.text(),
link: `${rootUrl}/${item.attr('data-article-id')}`,
};
});

items = await Promise.all(
items.map((item) =>
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[name="citation_doi"]').attr('content');
item.pubDate = parseDate(content('meta[name="citation_date"]').attr('content'));
item.description = art(path.join(__dirname, 'templates/description.art'), {
authors: content('.authors-list').html(),
abs: content('#enc-abstract').html(),
});

return item;
})
)
);

ctx.state.data = {
title: 'Trending page - PubMed',
link: currentUrl,
item: items,
};
};

0 comments on commit 5927f28

Please sign in to comment.