diff --git a/docs/README.md b/docs/README.md index 11e1bae258..cb6c8b8d20 100644 --- a/docs/README.md +++ b/docs/README.md @@ -668,6 +668,15 @@ GitHub 官方也提供了一些 RSS: + + +| 排序选项 | sort | +| ------------------ | --------- | +| 最佳匹配 | bestmatch | +| 根据 star 数量排序 | stars | +| 根据 fork 数量排序 | forks | +| 根据更新时间排序 | updated | + ### 开源中国 diff --git a/lib/router.js b/lib/router.js index 4c25a72e21..93241b174a 100644 --- a/lib/router.js +++ b/lib/router.js @@ -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')); diff --git a/lib/routes/github/search.js b/lib/routes/github/search.js new file mode 100644 index 0000000000..9fcb85cddb --- /dev/null +++ b/lib/routes/github/search.js @@ -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, + }; +};