fix(route): PubMed Trending articles (#9402)

This commit is contained in:
Ethan Shen
2022-03-28 20:20:30 +08:00
committed by GitHub
parent 668286a58b
commit 5927f28121
9 changed files with 109 additions and 70 deletions
+12 -2
View File
@@ -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
+12 -2
View File
@@ -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 系列
+1 -1
View File
@@ -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'));
-65
View File
@@ -1,65 +0,0 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const base = 'https://pubmed.ncbi.nlm.nih.gov';
module.exports = async (ctx) => {
const link = `${base}/trending/`;
const response = await got.get(encodeURI(link));
const pageCapture = cheerio.load(response.data);
const list = pageCapture('.docsum-content')
.map((_, elem) => {
const $ = cheerio.load(elem);
const partial = $('a').attr('href');
return {
title: $('a').text().trim(),
link: url.resolve(base, partial),
author: $('.full-authors').text(),
};
})
.get();
const out = await Promise.all(
list.map((item) =>
ctx.cache.tryGet(item.link, async () => {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
item.doi = detailCapture('meta[name="citation_doi"]').attr('content');
item.pubDate = date(detailCapture('meta[name="citation_date"]').attr('content'));
let authorContents = '';
if (item.author !== '') {
authorContents = `
<div id="author-content">
<span style="color: grey">${item.author}</span>
</div>
`;
}
const abs = detailCapture('#enc-abstract').html();
let absContents = '';
if (abs !== null) {
absContents = `
<div id="abstract-content">
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
item.description = authorContents + absContents;
return item;
})
)
);
ctx.state.data = {
title: 'PubMed | Trending Articles',
description: 'Trending Articles from PubMed Website',
link,
item: out,
};
};
+3
View File
@@ -0,0 +1,3 @@
module.exports = {
'/trending/:filters?': ['nczitzk'],
};
+13
View File
@@ -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
View File
@@ -0,0 +1,3 @@
module.exports = function (router) {
router.get('/trending/:filters?', require('./trending'));
};
+7
View File
@@ -0,0 +1,7 @@
{{ if authors }}
{{@ authors }}
{{ /if}}
<br>
{{ if abs }}
{{@ abs }}
{{ /if}}
+58
View File
@@ -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,
};
};