我使用Github API V3来获取一个存储库的分叉计数,我使用:
GET /repos/:owner/:repo/forks
请求只给我带来30个结果,即使存储库包含更多,我搜索了一下,我发现由于内存限制,API每页只返回30个结果,如果我想要下一个结果,我必须指定页面的数量。
我不需要所有这些信息,我只需要分叉的数量。
有没有办法只得到叉子的数量?
因为如果我开始逐页循环,如果存储库包含数千个结果,我的脚本可能会崩溃。
您可以尝试使用搜索查询。
例如,我的repo VonC/b2d,我会使用:
https://api.github.com/search/repositories?q=user%3AVonC +回购% 3 ab2d + b2d
json的答案给我一个"forks_count": 5
这里有一个超过4000个分叉(只考虑第一个结果,意思是"full_name
"实际上是"strongloop/express
")
https://api.github.com/search/repositories?q=user%3Astrongloop +回购% 3 aexpress +表达
"forks_count": 4114,
我有一份工作,我需要将所有分支作为github项目的git-remotes。
我写了一个简单的python脚本https://gist.github.com/urpylka/9a404991b28aeff006a34fb64da12de4
程序的基础是一个递归函数,用于获取一个分支的分支。我遇到了同样的问题(GitHub API只返回我30个项目)。
我通过增加?page=1
的增量和增加服务器null响应的检查来解决这个问题。
def get_fork(username, repo, forks, auth=None):
page = 1
while 1:
r = None
request = "https://api.github.com/repos/{}/{}/forks?page={}".format(username, repo, page)
if auth is None: r = requests.get(request)
else: r = requests.get(request, auth=(auth['login'], auth['secret']))
j = r.json()
r.close()
if 'message' in j:
print("username: {}, repo: {}".format(username, repo))
print(j['message'] + " " + j['documentation_url'])
if str(j['message']) == "Not Found": break
else: exit(1)
if len(j) == 0: break
else: page += 1
for item in j:
forks.append({'user': item['owner']['login'], 'repo': item['name']})
if auth is None:
get_fork(item['owner']['login'], item['name'], forks)
else:
get_fork(item['owner']['login'], item['name'], forks, auth)