mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-05 20:27:52 +08:00
47
lib/routes/github/search.js
Normal file
47
lib/routes/github/search.js
Normal 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,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user