Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub API 学习笔记 #14

Closed
hysic opened this issue Dec 11, 2015 · 1 comment
Closed

GitHub API 学习笔记 #14

hysic opened this issue Dec 11, 2015 · 1 comment

Comments

@hysic
Copy link
Collaborator

hysic commented Dec 11, 2015

我先抛砖引玉~

  • github API, 本身提供了许多, 我看了第一个 PyGithub, 教程风格不喜欢, 遂尝试用 requests 库直接get.
  • api 所有 URL 都以 https://api.github.com 开头, 后面以一定格式跟上你想获取的用户名, 仓库名, 以及你想获取的参数.

获取 commit 数

  • 比如我想获取 Octopuppy/Octodog 仓库下每个贡献者的 commit 数, 参考这个页面的说明, 代码如下:
import requests
url = "https://api.github.com/repos/Octopuppy/Octodog/stats/contributors"
r = requests.get(url)
# r.json() 返回所有的 contributors
for person in r.json():
    # person 类型为 dict, keys有 "author", "total", "weeks"
    # "author"的 value 又是一个 dict, "login" 对应的是作者的名字
    print person["author"]["login"] + ":" + str(person["total"])
  • 输出如下
hysic:1
bambooom:6

_这个结果只给出了 master 分支的 commit 数, 并没有考虑 pull requests 的数目, 这个数目与 pulse 页面给出的数目相同. 我理想的数据就是 pulse 页面柱状图中的commit 数.(_待解决*)

获取(首页显示的)commit 数

import requests
url = "https://api.github.com/repos/Octopuppy/Octodog/commits"
r = requests.get(url).json()
print len(r)
  • 这样运行结果是 12, 与仓库首页显示的是一致的.
  • 获取所有分支的 commit 数可以参考这个链接, 但这样给出很多 commit 都是重复的.

获取仓库的 star/watch/fork 数目

import requests
url = "https://api.github.com/repos/Octopuppy/Octodog"
r = requests.get(url).json()
print "Octodog repo has been stared by %d times, watched by %d times, and forked by %d times." % (r["stargazers_count"], r["watchers_count"], r["forks_count"])
  • 输出为:
Octodog repo has been stared by 8 times, watched by 8 times, and forked by 1 times.

其他

  • 此外还可以获得的数据有: 代码增减行数, 每周 commit 次数, 每小时 commit 次数等.
@bambooom
Copy link
Collaborator

#9
转移去技术讨论区

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants