-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHubCaptor.py
67 lines (59 loc) · 1.99 KB
/
GitHubCaptor.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
from Captor import Captor
import requests
class GitHubCaptor(Captor):
url_users = "https://api.github.com/users"
url_rate_limit = "https://api.github.com/rate_limit"
oauth_client_id = "YOUR_CLIENT_ID"
oauth_client_secret = "YOUR_CLIENT_SECRET"
class GitHubApiError(Exception):
pass
def __init__(self, logger):
self.logger = logger
def __requests_api(self, url, payload):
r = requests.get(url, params=payload, headers=self.headers)
print r.url
try:
loaded_json = r.json()
except:
r.raise_for_status()
if r.status_code <> requests.codes.ok:
error = loaded_json['message']
if 'errors' in loaded_json:
for error in loaded_json['errors']:
error += "\n\tfield %s: %s"% (error['field'], error['code'])
raise self.GitHubApiError(error)
return {'headers': r.headers, 'loaded_json': loaded_json}
def get_rate_limit_remaining(self):
payload = {
'client_id': self.oauth_client_id,
'client_secret': self.oauth_client_secret
}
loaded_json = self.__requests_api(self.url_rate_limit, payload)['loaded_json']
return int(loaded_json['rate']['remaining'])
def get_users(self, since):
payload = {
'client_id': self.oauth_client_id,
'client_secret': self.oauth_client_secret,
'since': since
}
r = self.__requests_api(self.url_users, payload)
options = r['headers']['Link'].split(';')[0].lstrip('<').rstrip('>').split('?')[1].split('&')
since = 0
for option in options:
if option.startswith('since='):
since = int(option.lstrip('since='))
if since == 0:
raise self. GitHubApiError("Could not extract the value for since from the GitHub header")
try:
remaining = int(r['headers']['X-RateLimit-Remaining'])
except KeyError:
remaining = None
return {'remaining': remaining,
'since': since,
'users': r['loaded_json']}
def get_user(self, login):
payload = {
'client_id': self.oauth_client_id,
'client_secret': self.oauth_client_secret
}
return self.__requests_api(self.url_users + "/"+ login, payload)['loaded_json']