{"action":"closed","issue":{"url":"https://api.github.com/repos/OctoPuppy/Octodog/issues/14","labels_url":"https://api.github.com/repos/OctoPuppy/Octodog/issues/14/labels{/name}","comments_url":"https://api.github.com/repos/OctoPuppy/Octodog/issues/14/comments","events_url":"https://api.github.com/repos/OctoPuppy/Octodog/issues/14/events","html_url":"https://github.com/OctoPuppy/Octodog/issues/14","id":121706571,"number":14,"title":"GitHub API 学习笔记","user":{"login":"hysic","id":5415646,"avatar_url":"https://avatars.githubusercontent.com/u/5415646?v=3","gravatar_id":"","url":"https://api.github.com/users/hysic","html_url":"https://github.com/hysic","followers_url":"https://api.github.com/users/hysic/followers","following_url":"https://api.github.com/users/hysic/following{/other_user}","gists_url":"https://api.github.com/users/hysic/gists{/gist_id}","starred_url":"https://api.github.com/users/hysic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hysic/subscriptions","organizations_url":"https://api.github.com/users/hysic/orgs","repos_url":"https://api.github.com/users/hysic/repos","events_url":"https://api.github.com/users/hysic/events{/privacy}","received_events_url":"https://api.github.com/users/hysic/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-11T13:18:48Z","updated_at":"2015-12-14T01:10:17Z","closed_at":"2015-12-14T01:10:17Z","body":"我先抛砖引玉~\r\n\r\n* [github API](https://developer.github.com/v3/), 本身提供了许多[库](https://developer.github.com/libraries/), 我看了第一个 [PyGithub](https://github.com/PyGithub/PyGithub), 教程风格不喜欢, 遂尝试用 requests 库直接get.\r\n* api 所有 URL 都以 `https://api.github.com` 开头, 后面以一定格式跟上你想获取的用户名, 仓库名, 以及你想获取的参数.\r\n\r\n### 获取 commit 数\r\n* 比如我想获取 Octopuppy/Octodog 仓库下每个贡献者的 commit 数, 参考[这个页面](https://developer.github.com/v3/repos/statistics/)的说明, 代码如下:\r\n\r\n```python\r\nimport requests\r\nurl = \"https://api.github.com/repos/Octopuppy/Octodog/stats/contributors\"\r\nr = requests.get(url)\r\n# r.json() 返回所有的 contributors\r\nfor person in r.json():\r\n    # person 类型为 dict, keys有 \"author\", \"total\", \"weeks\"\r\n    # \"author\"的 value 又是一个 dict, \"login\" 对应的是作者的名字\r\n    print person[\"author\"][\"login\"] + \":\" + str(person[\"total\"])\r\n```\r\n\r\n* 输出如下\r\n\r\n```\r\nhysic:1\r\nbambooom:6\r\n```\r\n\r\n*这个结果只给出了 master 分支的 commit 数, 并没有考虑 pull requests 的数目, 这个数目与 [pulse 页面](https://github.com/OctoPuppy/Octodog/pulse/monthly)给出的数目相同. 我理想的数据就是 pulse 页面柱状图中的commit 数.(**待解决**)\r\n\r\n### 获取(首页显示的)commit 数\r\n\r\n```python\r\nimport requests\r\nurl = \"https://api.github.com/repos/Octopuppy/Octodog/commits\"\r\nr = requests.get(url).json()\r\nprint len(r)\r\n``` \r\n\r\n* 这样运行结果是 12, 与仓库首页显示的是一致的.\r\n* 获取所有分支的 commit 数可以参考[这个链接](http://stackoverflow.com/questions/9179828/github-api-retrieve-all-commits-for-all-branches-for-a-repo), 但这样给出很多 commit 都是重复的. \r\n\r\n### 获取仓库的 star/watch/fork 数目\r\n\r\n```python\r\nimport requests\r\nurl = \"https://api.github.com/repos/Octopuppy/Octodog\"\r\nr = requests.get(url).json()\r\nprint \"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\"])\r\n```\r\n\r\n* 输出为:\r\n\r\n```\r\nOctodog repo has been stared by 8 times, watched by 8 times, and forked by 1 times.\r\n```\r\n\r\n### 其他\r\n* 此外还可以获得的数据有: 代码增减行数, 每周 commit 次数, 每小时 commit 次数等."}}