feat: add arxiv search (#4859)

This commit is contained in:
Ethan Shen
2020-05-27 14:15:36 +08:00
committed by GitHub
parent 99dd1f772a
commit 1b9e450bfd
3 changed files with 43 additions and 0 deletions

View File

@@ -4,6 +4,18 @@ pageClass: routes
# 科学期刊
## arXiv
### 搜索关键字
<Route author="nczitzk" example="/arxiv/search_query=all:electron&start=0&max_results=10" path="/arxiv/:query" :paramsDesc="['查询语句']" anticrawler="1">
参见 [arXiv API 用户手册](https://arxiv.org/help/api/user-manual) 查看所有查询参数。
路由中的参数 query 处填写 `http://export.arxiv.org/api/query?` 后的内容。
</Route>
## Cell
### 主刊

View File

@@ -2768,4 +2768,7 @@ router.get('/stork/keyword/:trackID/:displayKey', require('./routes/stork/keywor
// 致美化
router.get('/zhutix/latest', require('./routes/zhutix/latest'));
// arXiv
router.get('/arxiv/:query', require('./routes/arxiv/query'));
module.exports = router;

28
lib/routes/arxiv/query.js Normal file
View File

@@ -0,0 +1,28 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const currentUrl = `http://export.arxiv.org/api/query?${ctx.params.query}`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = cheerio.load(response.data);
const list = $('entry')
.map((_, item) => {
item = $(item);
return {
title: item.find('title').text(),
link: item.find('link[type="text/html"]').attr('href'),
pubDate: new Date(item.find('published').text()).toUTCString(),
description: item.find('summary').text(),
};
})
.get();
ctx.state.data = {
title: 'arXiv (' + ctx.params.query + ')',
link: currentUrl,
item: list,
};
};