Last active
December 14, 2015 11:08
-
-
Save FND/5076765 to your computer and use it in GitHub Desktop.
generate HTML overview of a GitHub user's repositories
This file contains hidden or 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
""" | |
generate HTML overview of a GitHub user's repositories | |
results are sent to STDOUT | |
Usage: | |
$ python repo.py <username> | |
""" | |
import sys | |
import re | |
import requests | |
TABLE = """ | |
<table> | |
<tr> | |
<th>%s</th> | |
<th>%s</th> | |
<th>%s</th> | |
<th>%s</th> | |
</tr> | |
%s | |
</table> | |
""" | |
ROW = """ | |
<tr> | |
<td>%s</td> | |
<td>%s</td> | |
<td>%s</td> | |
<td>%s</td> | |
</tr> | |
""" | |
class HTTPException(Exception): | |
pass | |
def main(args): | |
args = [unicode(arg, "utf-8") for arg in args] | |
username = args[1] | |
try: | |
print TABLE % ("Name", "Description", "Last Release", "Last Active", | |
"\n".join(get_repos(username))) | |
except HTTPException, exc: | |
print >> sys.stderr, "ERROR: %s" % exc.message | |
return False | |
return True | |
def get_repos(username): | |
req = _get("https://api.github.com/users/%s/repos" % username) | |
return (repo2html(repo) for repo in req.json()) | |
def repo2html(repo): | |
url = repo["homepage"] or repo["html_url"] # XXX: bad fallback? | |
link = '<a href="%s">%s</a>' % (url, repo["name"]) # TODO: sanitize | |
tag = _get_latest_tag(repo) | |
if not tag: | |
tag = "N/A" | |
return ROW % (link, repo.get("description"), tag, repo["updated_at"]) | |
def _get_latest_tag(repo): # TODO: use grequests for non-blocking approach | |
uri_template_pattern = re.compile(r"{.*?}") | |
tags_url = uri_template_pattern.sub("", repo["tags_url"]) | |
req = _get(tags_url) | |
try: | |
return req.json()[0]["name"] # XXX: correct? | |
except (IndexError, KeyError): | |
return False | |
def _get(url): | |
print >> sys.stderr, "retrieving %s" % url | |
req = requests.get(url) | |
if not req.status_code == 200: | |
raise HTTPException("failed to retrieve %s" % url) | |
return req | |
if __name__ == "__main__": | |
status = not main(sys.argv) | |
sys.exit(status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment