通过GitHub的API获取代码
GitHub的API功能很强大,也很规范。我对于Git都还没搞清楚,也懒得折腾其他的功能,主要是想实现通过API获取源代码来在线安装插件等功能。看了API的文档,想要取得代码还是比较容易的,就是查询次数限制,没认证的只能60次每小时,有认证的每小时5000次。
基本都是以GET方式从https://api.github.com/
取得。
要获得代码库里的readme,使用GET /repos/:owner/:repo/readme
比如TE的肥皂库https://api.github.com/repos/typecho-fans/plugins/readme
通过浏览器输入地址,或是使用curl或者其他方法取得页面内容,注意通过curl等方式要设置User-Agent。
返回值为json编码的stdClass,content属性为源码的base64编码,去掉'n'在用base64方式解码就取得文件源码。
如果要取得代码文件内容,需要获取相应的目录和文件内容:GET /repos/:owner/:repo/contents/:path
比如要获取肥皂库的根目录下的内容:https://api.github.com/repos/typecho-fans/plugins/contents/
[
{
"name": "At",
"path": "At",
"sha": "668a750a5aae5d9187cebfd74f10e43e47d338bc",
"size": null,
"url": "https://api.github.com/repos/typecho-fans/plugins/contents/At?ref=master",
"html_url": "https://github.com/typecho-fans/plugins/tree/master/At",
"git_url": "https://api.github.com/repos/typecho-fans/plugins/git/trees/668a750a5aae5d9187cebfd74f10e43e47d338bc",
"type": "dir",
"_links": {
"self": "https://api.github.com/repos/typecho-fans/plugins/contents/At?ref=master",
"git": "https://api.github.com/repos/typecho-fans/plugins/git/trees/668a750a5aae5d9187cebfd74f10e43e47d338bc",
"html": "https://github.com/typecho-fans/plugins/tree/master/At"
}
},
{
"name": "AutoSlug",
"path": "AutoSlug",
"sha": "e0832144c0af0f5bccfd4f44faa4cfde1e5017cf",
"size": null,
"url": "https://api.github.com/repos/typecho-fans/plugins/contents/AutoSlug?ref=master",
"html_url": "https://github.com/typecho-fans/plugins/tree/master/AutoSlug",
"git_url": "https://api.github.com/repos/typecho-fans/plugins/git/trees/e0832144c0af0f5bccfd4f44faa4cfde1e5017cf",
"type": "dir",
"_links": {
"self": "https://api.github.com/repos/typecho-fans/plugins/contents/AutoSlug?ref=master",
"git": "https://api.github.com/repos/typecho-fans/plugins/git/trees/e0832144c0af0f5bccfd4f44faa4cfde1e5017cf",
"html": "https://github.com/typecho-fans/plugins/tree/master/AutoSlug"
}
},
//省略掉了很多
{
"name": "XiaMiPlayer",
"path": "XiaMiPlayer",
"sha": "6fe21b2edf53e975e8ee4268aff5b757f2ea744d",
"size": null,
"url": "https://api.github.com/repos/typecho-fans/plugins/contents/XiaMiPlayer?ref=master",
"html_url": "https://github.com/typecho-fans/plugins/tree/master/XiaMiPlayer",
"git_url": "https://api.github.com/repos/typecho-fans/plugins/git/trees/6fe21b2edf53e975e8ee4268aff5b757f2ea744d",
"type": "dir",
"_links": {
"self": "https://api.github.com/repos/typecho-fans/plugins/contents/XiaMiPlayer?ref=master",
"git": "https://api.github.com/repos/typecho-fans/plugins/git/trees/6fe21b2edf53e975e8ee4268aff5b757f2ea744d",
"html": "https://github.com/typecho-fans/plugins/tree/master/XiaMiPlayer"
}
}
]
获取目录的返回值为一个json编码的数组,数组的每项为一个stdClass。通过循环读取对应的url就可以取得所有文件内容。
库内其他文件或目录的获取方式就在contents/后面加上对于的路径,比如:
https://api.github.com/repos/typecho-fans/plugins/contents/GoLinks
https://api.github.com/repos/typecho-fans/plugins/contents/GoLinks/Plugin.php
不懂,做个沙发~
顺便丢个肥皂给你~
呀,掉地上了,请你帮我拣下哈...
这个好,支持肥皂库在线安装实在是经典之作。
大家说的肥皂是神马东西哦
基友群建的github项目:https://github.com/typecho-fans
前排支持
各种看不懂。。。
tag 页怎么做啊。我也想做一个
CommentToMail不兼容typecho0.9了,求更新哇~
呃是smtp挂了,换mail()ok,话说新版默认不能自定义邮件模板了?0.0
搞懂了,不过0.9后貌似不支持评论后台回复发邮件提醒了?抱歉在此骚扰..
羽中大哥,你在自编自导自演吗?
羽中曾经做过导演/编剧的。。。
……
支持!!