forked from thammi/digger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.py
executable file
·93 lines (66 loc) · 2.01 KB
/
github.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
import sys
import json
import re
import urllib
from warnings import warn
from json_batch import save_batch
class UnknownServiceException(Exception):
def __init__(self, service):
self.service = service
def __str__(self):
return self.service
class ServiceFailedException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def api_call(method, repo, page):
base_url = "http://github.com/api/v2/json/"
url_parts = {
'page' : page,
'base_url': base_url,
'method': method,
'repo' : repo
}
res = urllib.urlopen("{base_url}{method}/{repo}?page={page}".format(**url_parts))
if res.getcode() < 300:
return json.load(res)
#TODO fix end of listing
else:
msg = "Unable to fetch: %i" % res.getcode()
raise ServiceFailedException(msg)
def get_commits(repo):
#step = 200
page = 1
commits = []
# how many dents are there?
#count = api_call('users/show', {'id': user})['statuses_count']
while True:
print "Fetching page %i" % page
# fetch them
new_commits = api_call("commits/list", repo, page)
# update the count
count = len(new_commits['commits'])
# save the commits
commits.extend(new_commits['commits'])
if count < 35:
break
# next page
page += 1
return commits
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Please specify at least one repository (user/repository/branch)"
sys.exit(1)
else:
repos = sys.argv[1:]
for repo in repos:
print "===> Fetching %s" % (repo)
commits = get_commits(repo)
if not commits:
print "ERROR: No results!"
else:
save_batch(repo, commits, "raw_github_commits.json")
print "Amount of commits: %i" % len(commits)
print