diff --git a/docs/programming.md b/docs/programming.md index f62d29c649..0939e059ba 100644 --- a/docs/programming.md +++ b/docs/programming.md @@ -95,6 +95,21 @@ GitHub 官方也提供了一些 RSS: + + +| 用户名 | 仓库名 | 分支名 | 文件路径 | +| -------- | -------- | -------- | --------------- | +| `DIYgod` | `RSSHub` | `master` | `lib/router.js` | + +> - **分支名**中如果有 `/` 等特殊字符需使用 urlencode 进行编码,通常 `/` 需要被替换成 `%2f` +> - **文件路径**中如果有特殊字符同样需使用 urlencode 进行编码,但文件路径可以正常识别 `/` 字符 +> - **文件路径**如果以 `.rss`, `.atom`, `.json` 结尾,需要将后缀中的 `.` 替换成 `%2e` +> > Reeder 订阅 `%2erss` 或类似后缀的时候会出错,此时再在路由后面加上 `.rss` 即可正常订阅 +> > +> > 如: `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/router%2ejs` 替换成 `https://rsshub.app/github/file/DIYgod/RSSHub/master/lib/router%2ejs.rss` 即可 + + + | 排序选项 | sort | diff --git a/lib/router.js b/lib/router.js index 74b0764d74..1e61702e04 100755 --- a/lib/router.js +++ b/lib/router.js @@ -345,6 +345,7 @@ 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')); router.get('/github/branches/:user/:repo', require('./routes/github/branches')); +router.get('/github/file/:user/:repo/:branch/:filepath+', require('./routes/github/file')); // f-droid router.get('/fdroid/apprelease/:app', require('./routes/fdroid/apprelease')); diff --git a/lib/routes/github/file.js b/lib/routes/github/file.js new file mode 100644 index 0000000000..2205adb217 --- /dev/null +++ b/lib/routes/github/file.js @@ -0,0 +1,47 @@ +const axios = require('../../utils/axios'); +const config = require('../../config'); + +module.exports = async (ctx) => { + const user = ctx.params.user; + const repo = ctx.params.repo; + const branch = ctx.params.branch; + const filepath = ctx.params.filepath; + + const fileUrl = `https://github.com/${user}/${repo}/commits/${branch}/${filepath}`; + + const reqParams = { + sha: branch, + path: filepath, + }; + if (config.github.access_token) { + reqParams.access_token = config.github.access_token; + } + + const res = await axios.get(`https://api.github.com/repos/${user}/${repo}/commits`, { + params: reqParams, + }); + const list = res.data; + const count = []; + for (let i = 0; i < Math.min(list.length, 10); i++) { + count.push(i); + } + const resultItems = await Promise.all( + count.map(async (i) => { + const each = list[i]; + const item = { + title: each.commit.message.split('\n')[0], + description: `
${each.commit.message}
`, + link: each.html_url, + author: each.commit.author.name, + pubDate: new Date(each.commit.committer.date).toUTCString(), + }; + return Promise.resolve(item); + }) + ); + + ctx.state.data = { + title: `GitHub File - ${user}/${repo}/${branch}/${filepath}`, + link: fileUrl, + item: resultItems, + }; +};