feat: Add PubMed Trending Support (Router: /pubmed/trending) (#4078)

This commit is contained in:
Chang Y
2020-02-25 10:25:59 +08:00
committed by GitHub
parent b66684c018
commit 4e7974bff1
5 changed files with 85 additions and 1 deletions

View File

@@ -68,6 +68,10 @@ pageClass: routes
## Search Engine
### PubMed Trending
<RouteEn author="yech1990" example="/pubmed/trending" path="/pubmed/trending" />
### X-MOL Platform - News
<RouteEn author="cssxsh" example="/x-mol/news/3" path="/x-mol/news/:tag?" :paramsDesc="['数字编号可从新闻列表URL得到。为空时从新闻主页获取新闻。']" />

View File

@@ -6,6 +6,10 @@ pageClass: routes
## 聚合平台
### PubMed-热门文章
<Route author="yech1990" example="/pubmed/trending" path="/pubmed/trending" />
### X-MOL 平台-新闻
<Route author="cssxsh" example="/x-mol/news/3" path="/x-mol/news/:tag?" :paramsDesc="['数字编号可从新闻列表URL得到。为空时从新闻主页获取新闻。']" />

View File

@@ -1987,6 +1987,9 @@ router.get('/hatena/anonymous_diary/archive', require('./routes/hatena/anonymous
router.get('/kaggle/discussion/:forumId/:sort?', require('./routes/kaggle/discussion'));
router.get('/kaggle/competitions/:category?', require('./routes/kaggle/competitions'));
// PubMed Trending
router.get('/pubmed/trending', require('./routes/pubmed/trending'));
// eLife [Sci Journal]
router.get('/elife/latest', require('./routes/elife/latest'));
router.get('/elife/:tid', require('./routes/elife/subject'));

View File

@@ -0,0 +1,73 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const base = 'https://www.ncbi.nlm.nih.gov';
module.exports = async (ctx) => {
const link = `${base}/pubmed/trending/`;
const response = await got.get(encodeURI(link));
const pageCapture = cheerio.load(response.data);
const list = pageCapture('.content div.rprt > div.rslt').get();
const out = await Promise.all(
list.map(async (elem) => {
const $ = cheerio.load(elem);
const title = $('p > a').text();
const partial = $('p > a').attr('href');
const address = url.resolve(base, partial);
const author = $('div.supp > p.desc').text();
const pubDate = date(
$('div.supp > p.details')
.text()
.split('. ')[1]
);
const item = {
title,
author,
pubDate,
link: encodeURI(address),
};
const value = await ctx.cache.get(address);
if (value) {
item.description = value;
} else {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
let authorContents = '';
if (author !== '') {
authorContents = `
<div id="author-content">
<span style="color: grey">${author}</span>
</div>
`;
}
const abs = detailCapture('div.abstr > div').html();
let absContents = '';
if (abs !== null) {
absContents = `
<div id="abstract-content">
<h2 align="left">Abstract</h2>
<p>${abs}</p>
</div>
`;
}
item.description = authorContents + absContents;
ctx.cache.set(address, item.description);
}
return Promise.resolve(item);
})
);
ctx.state.data = {
title: 'PubMed | Trending Articles',
description: 'Trending Articles from PubMed Website',
link: link,
item: out,
};
};

View File

@@ -14,7 +14,7 @@ module.exports = async (ctx) => {
const list = pageCapture('div.inner_depth_list > ul > li').get();
const out = await Promise.all(
list.slice(0, 3).map(async (elem) => {
list.map(async (elem) => {
const $ = cheerio.load(elem);
const title = $('h5 > a').text();
const partial = $('h5 > a').attr('href');