-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubApiInteraction.py
More file actions
executable file
·60 lines (51 loc) · 1.81 KB
/
githubApiInteraction.py
File metadata and controls
executable file
·60 lines (51 loc) · 1.81 KB
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
#!/usr/bin/env python
import requests, re, sys, os
base = 'https://api.github.com/'
token = os.getenv('GHTOKEN')
if not token:
message = """GH Token Unset
Your Github token needs to be set to interact with their
api and not hit the rate limit.
export GHTOKEN=<your github token>"""
sys.exit(message)
class ApiError(Exception):
def __init__(self, status):
self.status = status
def __str__(self):
return "APIError: status={}".format(self.status)
def callGithubApi(api):
resp = requests.get(base+api)
if resp.status_code != 200:
raise ApiError('Get /'+api+' {}'.format(resp.status_code))
return (resp, api)
def userRepos(response):
i=0
for responseItem in response[0].json():
print('{} {}'.format(responseItem['name'], responseItem['owner']['login']))
i+=1
print '\n{} has {} repositories'.format(re.findall(r'\/(.*?)\/', response[1])[0], str(i))
def repoCommits(response):
i=0
for item in response[0].json():
author = item['author']
if author:
print '{}'.format(author['login'])
i+=1
print i
return i
def getAllAuthorsForARepo(org, repo):
total_count = 0
page_num = 0
while total_count < 13061:
page_num+=1
print "\nLooking at page: {}\nCurrent Total Count: {:,}".format(str(page_num), total_count)
repoCommitsApi = 'repos/{}/{}/commits'.format(org, repo)
queryString = '?page={}&per_page=100&access_token={}'.format(str(page_num), token)
page_total = repoCommits(callGithubApi(repoCommitsApi+queryString))
if page_total == 0:
sys.exit("No authors found on this page.")
else:
total_count += page_total
# userOrgs = 'users/aln787/repos'
# userRepos(callGithubApi(userOrgs))
getAllAuthorsForARepo('fastlane', 'fastlane')