-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
""" Tools for querying github. | ||
I tried using pygithub3, but it really sucks. | ||
""" | ||
|
||
import requests | ||
|
||
|
||
def _link_field_to_dict(field): | ||
""" Utility for ripping apart github's Link header field. | ||
It's kind of ugly. | ||
""" | ||
|
||
if not field: | ||
return dict() | ||
|
||
return dict([ | ||
( | ||
part.split('; ')[1][5:-1], | ||
part.split('; ')[0][1:-1], | ||
) for part in field.split(', ') | ||
]) | ||
|
||
|
||
def get_repos(username, auth): | ||
""" username should be a string | ||
auth should be a tuple of username and password. | ||
item can be one of "repos" or "orgs" | ||
""" | ||
|
||
tmpl = "https://api.github.com/users/{username}/repos?per_page=100" | ||
url = tmpl.format(username=username) | ||
return _getter(url, auth) | ||
|
||
|
||
def get_issues(username, repo, auth): | ||
""" username and repo should be strings | ||
auth should be a tuple of username and password. | ||
""" | ||
|
||
tmpl = "https://api.github.com/repos/{username}/{repo}/issues?per_page=100" | ||
url = tmpl.format(username=username, repo=repo) | ||
return _getter(url, auth) | ||
|
||
|
||
def get_comments(username, repo, number, auth): | ||
tmpl = "https://api.github.com/repos/{username}/{repo}/issues/" + \ | ||
"{number}/comments?per_page=100" | ||
url = tmpl.format(username=username, repo=repo, number=number) | ||
return _getter(url, auth) | ||
|
||
|
||
def get_pulls(username, repo, auth): | ||
""" username and repo should be strings | ||
auth should be a tuple of username and password. | ||
""" | ||
|
||
tmpl = "https://api.github.com/repos/{username}/{repo}/pulls?per_page=100" | ||
url = tmpl.format(username=username, repo=repo) | ||
return _getter(url, auth) | ||
|
||
|
||
def _getter(url, auth): | ||
""" Pagination utility. Obnoxious. """ | ||
|
||
results = [] | ||
link = dict(next=url) | ||
while 'next' in link: | ||
response = requests.get(link['next'], auth=auth) | ||
|
||
# And.. if we didn't get good results, just bail. | ||
if response.status_code != 200: | ||
raise IOError( | ||
"Non-200 status code %r; %r" % (response.status_code, url)) | ||
|
||
results += response.json() | ||
link = _link_field_to_dict(response.headers['link']) | ||
|
||
return results | ||
|
||
if __name__ == '__main__': | ||
# Little test. | ||
import getpass | ||
username = raw_input("GitHub Username: ") | ||
password = getpass.getpass() | ||
|
||
results = get_all(username, (username, password)) | ||
print len(results), "repos found." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters