-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-github-commits.py
executable file
·55 lines (43 loc) · 1.41 KB
/
count-github-commits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# src: https://gist.github.com/gdamjan/1241096/cdcc66cbd024c7f34bdf079e524d6fb05ed3880b
import json
import sys
from restkit import request
USER = sys.argv[1]
print("For user " + USER)
if not USER:
exit
def count_user_commits(user):
r = request('https://api.github.com/users/%s/repos' % user)
repos = json.loads(r.body_string())
for repo in repos:
if repo['fork'] is True:
# skip it
continue
n = count_repo_commits(repo['url'] + '/commits')
yield (repo['name'], n)
def count_repo_commits(commits_url, _acc=0):
r = request(commits_url)
commits = json.loads(r.body_string())
n = len(commits)
if n == 0:
return _acc
link = r.headers.get('link')
if link is None:
return _acc + n
next_url = find_next(r.headers['link'])
if next_url is None:
return _acc + n
# try to be tail recursive, even when it doesn't matter in CPython
return count_repo_commits(next_url, _acc + n)
# given a link header from github, find the link for the next url which they use for pagination
def find_next(link):
for l in link.split(','):
a, b = l.split(';')
if b.strip() == 'rel="next"':
return a.strip()[1:-1]
if __name__ == '__main__':
total = 0
for repo, n in count_user_commits(USER):
print "Repo `%s` has %d commits." % (repo, n)
total += n
print "Total commits: %d" % total