rss: add github trending

This commit is contained in:
DIYgod 2018-05-30 00:26:05 +08:00
parent 4f77c3fede
commit dbc7de1f6d
No known key found for this signature in database
GPG Key ID: EC0B76A252D3EF67
4 changed files with 100 additions and 19 deletions

View File

@ -994,3 +994,39 @@ key: 产品密钥
- 过去一周:[https://rsshub.app/yande.re/post/popular_recent/1w](https://rsshub.app/yande.re/post/popular_recent/1w)
- 过去一月:[https://rsshub.app/yande.re/post/popular_recent/1m](https://rsshub.app/yande.re/post/popular_recent/1m)
- 过去一年:[https://rsshub.app/yande.re/post/popular_recent/1y](https://rsshub.app/yande.re/post/popular_recent?period=1y)
## GitHub
::: tip 提示
GitHub 官方也提供了一些 RSS:
- 仓库 releases: https://github.com/:owner/:repo/releases.atom
- 仓库 commits: https://github.com/:owner/:repo/commits.atom
- 用户动态: https://github.com/:user.atom
:::
### 用户仓库
举例: [https://rsshub.app/github/repos/DIYgod](https://rsshub.app/github/repos/DIYgod)
路由: `/github/repos/:user`
参数: user用户名
### Trending
举例:
[https://rsshub.app/github/trending/daily](https://rsshub.app/github/trending/daily)
[https://rsshub.app/github/trending/daily/javascript](https://rsshub.app/github/trending/daily/javascript)
路由: `/github/trending/:since/:language?`
参数:
since时间跨度可在 [Trending 页](https://github.com/trending/javascript?since=monthly) URL 中找到,可选 daily weekly monthly
language语言可在 [Trending 页](https://github.com/trending/javascript?since=monthly) URL 中找到

View File

@ -240,8 +240,9 @@ router.get('/readhub/category/:category', require('./routes/readhub/category'));
if (config.github && config.github.access_token) {
router.get('/github/repos/:user', require('./routes/github/repos'));
} else {
logger.warn('GitHub RSS is disabled for lacking config.');
logger.warn('GitHub Repos RSS is disabled for lacking config.');
}
router.get('/github/trending/:since/:language?', require('./routes/github/trending'));
// konachan
router.get('/konachan/post', require('./routes/konachan/post'));

View File

@ -3,34 +3,29 @@ const config = require('../../config');
module.exports = async (ctx) => {
const user = ctx.params.user;
const uri = `https://api.github.com/users/${user}/repos` + `?access_token=${config.github.access_token}`;
const response = await axios({
method: 'get',
url: uri,
url: `https://api.github.com/users/${user}/repos`,
headers: {
'User-Agent': config.ua,
Referer: uri,
},
params: {
sort: 'created',
access_token: config.github.access_token,
},
});
const data = response.data;
ctx.state.data = {
title: `GitHub Repos By ${user}`,
link: uri,
description: `GitHub Repos By ${user}`,
title: `${user}'s GitHub repositories`,
link: `https://github.com/${user}`,
item:
data &&
data.map((item) => {
let repoDescription = item.description;
if (repoDescription === null) {
repoDescription = 'No description added';
}
return {
title: `${item.name}`,
guid: `${item.id}`,
description: `${repoDescription}`,
link: `${item.url}`,
};
}),
data.map((item) => ({
title: item.name,
description: item.description || 'No description',
pubDate: new Date(item.created_at).toUTCString(),
link: item.html_url,
})),
};
};

49
routes/github/trending.js Normal file
View File

@ -0,0 +1,49 @@
const axios = require('../../utils/axios');
const cheerio = require('cheerio');
const config = require('../../config');
module.exports = async (ctx) => {
const since = ctx.params.since;
const language = ctx.params.language || '';
const url = `https://github.com/trending/${language}?since=${since}`;
const response = await axios({
method: 'get',
url: url,
headers: {
'User-Agent': config.ua,
Referer: url,
},
});
const data = response.data;
const $ = cheerio.load(data);
const list = $('.repo-list li');
ctx.state.data = {
title: $('title').text(),
link: url,
item:
list &&
list
.map((index, item) => {
item = $(item);
return {
title: item.find('h3').text(),
description: `${item.find('.py-1').text()}<br>
<br>Language: ${item.find('span[itemprop="programmingLanguage"]').text() || 'unknown'}
<br>Star: ${item
.find('.muted-link')
.eq(0)
.text()}
<br>Fork: ${item
.find('.muted-link')
.eq(1)
.text()}`,
link: `https://github.com${item.find('h3 a').attr('href')}`,
};
})
.get(),
};
};