最适合 GithubAPI v3的 Python 库

我正在为 GithubAPIv3寻找一个适合我的 Python 库。

我找到了 GH API 文档中提到的一个库 (python-github3)。在 ipython 中玩了一两个小时之后,我发现探索/使用它真的很不直观。我又查看了一些资料,发现至少有相当多的人试图编写这样一个库。看起来更有希望的是 PyGithub另一条巨蟒—— github3,它们显然与第一个不同。

在我花费接下来的几天连续尝试一个又一个库之前,我想问问 SO 社区是否有一个可以接受的、明确的、显而易见的选择?

我不喜欢第一个库的原因是(对我来说)不直观的获取数据的方式——有些东西是作为属性获取的,有些是作为方法的返回值获取的,返回值是一些复杂的对象,需要进行分页和迭代,等等。

在这方面,PyGithub 乍看起来更有吸引力——显然,通过对象层次结构向下钻取,然后得到包含您想要的属性:

对于 g.get _ user () . get _ repos ()中的 repo: 打印 repo.name

有什么至理名言要分享吗?我知道我没有足够的技能来快速判断图书馆的质量,这就是为什么我转向 SO 社区。

编辑: fwiw,我最终使用了 PyGithub。它工作得很好,作者真的能够接受反馈和 bug 报告。:-)

46020 次浏览

Since you mentioned you are a beginner python programmer, I would suggest you to try to use the JSON API without any Github library first. It really isn't that difficult and it will help you a lot later in your programming life since same approach can be applied to any JSON API. Especially if it seems that trying out libraries will take days.

I'm not saying that some library isn't easier to use, I'm just saying the small extra effort to use the API directly might be worth it in the long run. At least it will help you understand why some of those libraries seem "unintuitive" (as you said).

Simple example to fetch creation time of django repository:

import requests
import json
r = requests.get('https://api.github.com/repos/django/django')
if(r.ok):
repoItem = json.loads(r.text or r.content)
print "Django repository created: " + repoItem['created_at']

This is using the popular requests library. In your code you'll naturally need to handle the error cases too.

If you need access with authentication it will be a bit more complex.

In the end, I ended up using PyGithub. It works well, and the author is really receptive for feedback and bug reports. :-)

(Adapted from my edit to the original question, for better visibility)

Documentation is horrible for PyGitHub, but the product is great. Here is a quick sample for actually retrieving a file, changing it with a new comment at the beginning of the file and committing it back

from github import Github
gh = Github(login_or_token='.....', base_url='...../api/v3')
user = gh.get_user()
repo = user.get_repo("RepoName")
file = repo.get_file_contents("/App/forms.py")
decoded_content = "# Test " + "\r\n" + file.decoded_content
repo.update_file("/"RepoName"/forms.py", "Commit Comments", decoded_content, file.sha)