-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.py
executable file
·133 lines (100 loc) · 3.56 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
###############################################################################
##
## digger - Digging into some data mines
## Copyright (C) 2010 core, Thammi
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU Affero General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU Affero General Public License for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
###############################################################################
import sys
import json
import re
import urllib
from warnings import warn
from json_batch import save_batch, update_batch
class ServiceFailedException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
def api_call(method, params, query={}):
base_url = "http://github.com/api/v2/json/"
url_parts = {
'query' : urllib.urlencode(query),
'base_url': base_url,
'method': method,
'params' : '/'.join(params),
}
res = urllib.urlopen("{base_url}{method}/{params}?{query}".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 fetch_repo(user, repo, branch='master'):
#step = 200
page = 1
commits = []
repo_id = (user, repo, branch)
# 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_id, {'page': 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
def fetch_user(user):
repos = api_call("repos/show", [user])
batch = {}
for repo in repos['repositories']:
# TODO: fetch all branches or decide which to fetch
name = repo['name']
repo_id = '/'.join((user, name, 'master'))
print "==> Fetching %s" % name
try:
batch[repo_id] = fetch_repo(user, name, 'master')
except ServiceFailedException as ex:
print "Failed in %s: %s" % (name, str(ex))
return batch
if __name__ == '__main__':
batch_fn = "raw_github_commits.json"
if len(sys.argv) < 2:
print "Please specify at least one repository (user/repository/branch)"
sys.exit(1)
elif len(sys.argv) < 3:
user = sys.argv[1]
batch = fetch_user(user)
update_batch(batch, batch_fn)
else:
user = sys.argv[1]
repo = sys.argv[2]
branch = sys.argv[3] if len(sys.argv) >= 4 else 'master'
commits = fetch_repo(user, repo, branch)
if not commits:
print "ERROR: No results!"
else:
repo_id = '/'.join((user, repo, branch))
save_batch(repo, commits, batch_fn)
print "Amount of commits: %i" % len(commits)
print