Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不超过github API使用限制的情况下获取所有用户的位置_Python_Github_Github Api_Github3.py - Fatal编程技术网

Python 如何在不超过github API使用限制的情况下获取所有用户的位置

Python 如何在不超过github API使用限制的情况下获取所有用户的位置,python,github,github-api,github3.py,Python,Github,Github Api,Github3.py,目前我正在尝试获取所有Github用户位置。我正在使用github3 python库获取位置。但当我的API调用超过5K时,它会给我带来过度API使用错误。这是我的密码 import github3 from datetime import datetime import sys def main(pswd): g = github3.login(username="rakeshcusat", password=pswd) current_time = datetime.now

目前我正在尝试获取所有Github用户位置。我正在使用github3 python库获取位置。但当我的API调用超过5K时,它会给我带来过度API使用错误。这是我的密码

import github3
from datetime import datetime
import sys

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

    for user in g.iter_all_users():
        user.refresh()
        try:
            fhandler.write(" user: {0}, email: {1}, location: {2}\n".format(str(user), str(user.email), str(user.location)))
        except:
            print "Something wrong, user id : {0}".format(user.id);


    fhandler.close()        

if __name__ == "__main__":

    if len(sys.argv) == 2:

        main(sys.argv[1])
    else:
        print "Please provide your password"

我可以先下载所有用户名,这将是唯一一个API调用。然后迭代下载用户位置。如果使用率过高,请等待一个小时,然后在原来的位置继续api调用。但这似乎是一个蹩脚的解决方案,肯定需要更多的时间(大约25个多小时)。有人能给我提供更好的方法吗?

因此,如果您使用github3.py的开发版本,您可以使用peru_page参数,例如

for user in g.iter_all_users(per_page=200):
    user.refresh()
    #: other logic
问题是,您将使用每页
保存7个请求(如果我没记错的话,1个请求现在返回25个请求,因此您将在1中获得相当于8个请求的值)。问题是,使用
User#refresh
时,您很快就使用了200个请求。为了避免速率限制,您可以在代码中使用sleep来分隔请求。3600秒内拆分的5000个请求为每秒1.389个请求。如果每个请求都需要半秒钟的时间(我个人认为这是低估了),您可以这样做

import time

for user in g.iter_all_users(per_page=200):
    user.refresh()
    #: other logic
    time.sleep(0.5)
这将确保每秒发出一个请求,并且您从未达到费率限制。不管怎样,这是相当蹩脚的

将来,我将使用用户id作为数据库中的id将这些值存储在数据库中,然后只查找最大值并尝试从那里开始。我必须检查
/users
是否支持类似于
的东西,因为
参数。或者,你也可以这样工作

import time

i = g.iter_all_users(per_page=200):
for user in i:
    user.refresh()
    #: other logic
    time.sleep(0.5)

# We have all users
# store i.etag somewhere then later
i = g.iter_all_users(per_page=200, etag=i.etag)
for user in i:
    user.refresh()
    #: etc

如果我没记错的话,第二个迭代器应该会为您提供自上次请求中的最后一个迭代器以来的所有新用户,但是我现在很累,所以可能记错了什么东西。

@sigmavirus24知道怎么做吗?很抱歉它没有ping我。对于github API文档中的用户API“注意:分页仅由'since'参数提供动力。使用链接头获取下一页用户的URL。“这意味着用户不支持自参数。@sigmavirus24您能给我举个例子说明如何在iter_all_users()中使用自参数吗?”api调用?@Rakesh我刚刚意识到我从来没有回答过你。如果你保留最后一个用户的id,你就可以在新版本的github3.py中通过
since=id
。我显然从未将该参数添加到
iter\u all\u用户