add: github搜索结果 (#1325)

closes #1289
This commit is contained in:
Chenyang Shi
2018-12-31 01:01:51 +08:00
committed by DIYgod
parent 644a86962d
commit a94c087f53
3 changed files with 57 additions and 0 deletions

View File

@@ -668,6 +668,15 @@ GitHub 官方也提供了一些 RSS:
<route name="仓库 Stars" author="HenryQW" example="/github/stars/DIYgod/RSSHub" path="/github/stars/:user/:repo" :paramsDesc="['用户名', '仓库名']"/>
<route name="搜索结果" author="LogicJake" example="/github/search/RSSHub/bestmatch/desc" path="/github/search/:query/:sort?/:order?" :paramsDesc="['搜索关键词', '排序选项默认为bestmatch','排序顺序desc和asc默认desc降序']"/>
| 排序选项 | sort |
| ------------------ | --------- |
| 最佳匹配 | bestmatch |
| 根据 star 数量排序 | stars |
| 根据 fork 数量排序 | forks |
| 根据更新时间排序 | updated |
### 开源中国
<route name="资讯" author="tgly307" example="/oschina/news" path="/oschina/news"/>

View File

@@ -312,6 +312,7 @@ router.get('/github/trending/:since/:language?', require('./routes/github/trendi
router.get('/github/issue/:user/:repo', require('./routes/github/issue'));
router.get('/github/user/followers/:user', require('./routes/github/follower'));
router.get('/github/stars/:user/:repo', require('./routes/github/star'));
router.get('/github/search/:query/:sort?/:order?', require('./routes/github/search'));
// f-droid
router.get('/fdroid/apprelease/:app', require('./routes/fdroid/apprelease'));

View File

@@ -0,0 +1,47 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const url = require('url');
const host = 'https://github.com';
module.exports = async (ctx) => {
const query = ctx.params.query;
let sort = ctx.params.sort || 'bestmatch';
const order = ctx.params.order || 'desc';
if (sort === 'bestmatch') {
sort = '';
}
const suffix = 'search?o='.concat(order, '&q=', query, '&s=', sort, '&type=Repositories');
const link = url.resolve(host, suffix);
const response = await axios.get(link);
const $ = cheerio.load(response.data);
const out = $('.repo-list li')
.slice(0, 10)
.map(function() {
const single = {
title: $(this)
.find('div h3 a')
.text(),
link: host.concat(
$(this)
.find('div h3 a')
.attr('href')
),
description: $(this)
.find('div p')
.text()
.trim(),
};
return single;
})
.get();
ctx.state.data = {
title: `${query}的搜索结果`,
link: link,
item: out,
};
};