Skip to content

Commit

Permalink
fix: pubmed trending articles (DIYgod#6225)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Shen authored Nov 29, 2020
1 parent 481773c commit 77f1f57
Showing 1 changed file with 34 additions and 37 deletions.
71 changes: 34 additions & 37 deletions lib/routes/pubmed/trending.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

item.doi = detailCapture('meta[name="citation_doi"]').attr('content');
item.pubDate = date(detailCapture('meta[name="citation_date"]').attr('content'));

let authorContents = '';
if (author !== '') {
authorContents = `
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 = {
Expand Down

0 comments on commit 77f1f57

Please sign in to comment.