获取所有github用户的详细信息

时间:2013-06-15 22:52:36

标签: python github github-api github3.py

我想获得github用户及其位置。我知道有Github apI(GET / users)可以为我提供用户列表。目前我正在使用PyGithub来访问github,但似乎这个库还没有实现这个API。任何人都可以建议我如何使用任何github API库获取github用户及其位置?

EDIT1: 我更新了如下代码。但有些我无法获得电子邮件ID和位置。

import github3
from datetime import datetime

def main():
        g = github3.login(username="rakeshcusat", password="mypassword")
    for user in g.iter_emails():
        print user
    current_time = datetime.now()   
    fhandler = open("githubuser_"+current_time.strftime("%d-%m-%y-%H:%M:%S"), "w")

    for user in g.iter_all_users():
            fhandler.write(" user: {0}, email: {1}, location: {2}\n".format(user, user.email, user.location))
            #fhandler.flush()

    fhandler.close()        

if __name__ == "__main__":
    main()

示例输出

 user: andywatts, email: None, location: 
 user: mueller, email: None, location: 
 user: cp, email: None, location: 
 user: davea, email: None, location: 
 user: vrieskist, email: None, location: 

1 个答案:

答案 0 :(得分:2)

github3 library支持对所有用户的迭代:

import github3

for user in github3.iter_all_users():
    user.refresh()
    print user.location

github3.iter_all_users()生成User objects

此处需要.refresh()调用,因为/users endpoint仅返回较小的用户信息子集,并且该位置不包含在内。这需要另一个API请求,因此您可能需要调整脚本以避免达到GitHub速率限制。

未来版本的github3(比0.7.0更新)增加了对指定页面(批量)大小的支持,以减少您需要进行的API请求的数量; GitHub API每页默认为30个结果,但允许您load up to 100 results per page代替:

for user in github3.iter_all_users(per_page=100):
    user.refresh()
    print user.location