fix: pubmed trending articles (#6225)

This commit is contained in:
Ethan Shen 2020-11-30 03:00:35 +08:00 committed by GitHub
parent 481773c281
commit 77f1f57643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 37 deletions

View File

@ -3,61 +3,58 @@ const cheerio = require('cheerio');
const url = require('url');
const date = require('@/utils/date');
const base = 'https://www.ncbi.nlm.nih.gov';
const base = 'https://pubmed.ncbi.nlm.nih.gov';
module.exports = async (ctx) => {
const link = `${base}/pubmed/trending/`;
const link = `${base}/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 list = pageCapture('.docsum-content')
.map((_, 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 partial = $('a').attr('href');
return {
title: $('a').text().trim(),
link: url.resolve(base, partial),
author: $('.full-authors').text(),
};
})
.get();
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);
const out = await Promise.all(
list.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detail = await got.get(item.link);
const detailCapture = cheerio.load(detail.data);
let authorContents = '';
if (author !== '') {
authorContents = `
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">${author}</span>
<span style="color: grey">${item.author}</span>
</div>
`;
}
const abs = detailCapture('div.abstr > div').html();
let absContents = '';
if (abs !== null) {
absContents = `
}
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;
ctx.cache.set(address, item.description);
}
}
item.description = authorContents + absContents;
return Promise.resolve(item);
})
return item;
})
)
);
ctx.state.data = {