feat: add nature machine intelligence latest research (#3295)

This commit is contained in:
Chenyang Shi
2019-10-21 15:17:29 +08:00
committed by DIYgod
parent d886166575
commit 6df530f946
3 changed files with 53 additions and 0 deletions

View File

@@ -4,6 +4,12 @@ pageClass: routes
# 学习
## nature machine intelligence
### latest research
<Route author="LogicJake" example="/nature/natmachintell/research" path="/nature/natmachintell/research" />
## X-MOL 平台
### 新闻

View File

@@ -1861,6 +1861,9 @@ router.get('/hatena/anonymous_diary/archive', require('./routes/hatena/anonymous
// kaggle
router.get('/kaggle/discussion/:forumId/:sort?', require('./routes/kaggle/discussion'));
// nature
router.get('/nature/natmachintell/research', require('./routes/nature/natmachintell/research'));
// dlsite
router.get('/dlsite/new/:type', require('./routes/dlsite/new'));

View File

@@ -0,0 +1,44 @@
const cheerio = require('cheerio');
const got = require('@/utils/got');
const url = require('url');
const host = 'https://www.nature.com';
const link = 'https://www.nature.com/natmachintell/research';
module.exports = async (ctx) => {
const responses = await got.get(link);
const $ = cheerio.load(responses.data);
const list = $('article').get();
const out = await Promise.all(
list.map(async (item) => {
const $ = cheerio.load(item);
const title = $('h3 a').text();
const itemUrl = url.resolve(host, $('h3 a').attr('href'));
const cache = await ctx.cache.get(itemUrl);
if (cache) {
return Promise.resolve(JSON.parse(cache));
}
const responses = await got.get(itemUrl);
const $d = cheerio.load(responses.data);
const description = $d('div.c-article-body').html();
const single = {
title,
link: itemUrl,
description,
};
ctx.cache.set(itemUrl, JSON.stringify(single));
return Promise.resolve(single);
})
);
ctx.state.data = {
title: 'nature > nature machine intelligence > latest research',
link: link,
item: out,
};
};