Created
August 25, 2019 18:07
-
-
Save tonyg/b8db8bd3dd83ca90a53945b6482f6d80 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
import sys | |
import gh | |
import os | |
import json | |
if len(sys.argv) > 1: | |
username = sys.argv[1] | |
else: | |
raise Exception('Expected github username as first command-line argument') | |
if len(sys.argv) > 2: | |
full = (sys.argv[2] == '--full') | |
else: | |
full = False | |
gist_auth_token_file = os.getenv('HOME') + '/.gist' ## created by gist ruby gem oauth2 process | |
if os.path.exists(gist_auth_token_file): | |
params = {'access_token': file(gist_auth_token_file).read()} | |
else: | |
print '==========================================' | |
print 'Warning: no oauth token; public gists only' | |
print '==========================================' | |
params = {} | |
v = gh.api('users/%s/gists' % (username,), **params) | |
while v: | |
for gist in v['body']: | |
if full: | |
print json.dumps(gist, indent=2) | |
else: | |
print gist['id'] | |
v = gh.follow_link(v, 'next') |
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
#!/usr/bin/python | |
import sys | |
import gh | |
import os | |
import json | |
if len(sys.argv) > 1: | |
username = sys.argv.pop(1) | |
else: | |
raise Exception('Expected github username as first command-line argument') | |
output_type = 'short' | |
org = False | |
repotype = 'all' | |
while len(sys.argv) > 1: | |
arg = sys.argv.pop(1) | |
if arg == '--full': output_type = 'full' | |
elif arg == '--org': org = True | |
elif arg == '--public': repotype = 'public' | |
elif arg == '--private': repotype = 'private' | |
elif arg == '--scoped': output_type = 'scoped' | |
else: raise Exception('Unhandled command-line argument: ' + arg) | |
params = {} | |
params['type'] = repotype | |
gist_auth_token_file = os.getenv('HOME') + '/.gist' ## created by gist ruby gem oauth2 process | |
if os.path.exists(gist_auth_token_file): | |
params['access_token'] = file(gist_auth_token_file).read() | |
v = gh.api('%s/%s/repos' % ('orgs' if org else 'users', username), **params) | |
while v: | |
for repo in v['body']: | |
if output_type == 'full': | |
print json.dumps(repo, indent=2) | |
elif output_type == 'scoped': | |
print repo['full_name'] | |
elif output_type == 'short': | |
print repo['name'] | |
else: | |
raise Exception('Invalid output_type') | |
v = gh.follow_link(v, 'next') |
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
from urllib2 import urlopen | |
from urllib import urlencode | |
import json | |
import re | |
import link_header | |
def analyze_info(i): | |
result = {} | |
links = {} | |
for h in i.headers: | |
if h.startswith('Link:'): | |
for v in h[5:].split(','): | |
for url, params in link_header.parse_link_value(v.strip()).items(): | |
links[params['rel']] = url | |
result['links'] = links | |
return result | |
def api(path, path_prefix = 'https://api.github.com/', **params): | |
url = path_prefix + path + ('?' + urlencode(params) if params else '') | |
fp = urlopen(url) | |
return {"body": json.load(fp), "headers": analyze_info(fp.info())} | |
def follow_link(response, rel): | |
if rel in response['headers']['links']: | |
return api(response['headers']['links'][rel], path_prefix = '') | |
else: | |
return None |
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
#!/usr/bin/env python | |
""" | |
HTTP Link Header Parsing | |
Simple routines to parse and manipulate Link headers. | |
""" | |
__license__ = """ | |
Copyright (c) 2009 Mark Nottingham | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
""" | |
import re | |
TOKEN = r'(?:[^\(\)<>@,;:\\"/\[\]\?={} \t]+?)' | |
QUOTED_STRING = r'(?:"(?:\\"|[^"])*")' | |
PARAMETER = r'(?:%(TOKEN)s(?:=(?:%(TOKEN)s|%(QUOTED_STRING)s))?)' % locals() | |
LINK = r'<[^>]*>\s*(?:;\s*%(PARAMETER)s?\s*)*' % locals() | |
COMMA = r'(?:\s*(?:,\s*)+)' | |
LINK_SPLIT = r'%s(?=%s|\s*$)' % (LINK, COMMA) | |
def _unquotestring(instr): | |
if instr[0] == instr[-1] == '"': | |
instr = instr[1:-1] | |
instr = re.sub(r'\\(.)', r'\1', instr) | |
return instr | |
def _splitstring(instr, item, split): | |
if not instr: | |
return [] | |
return [ h.strip() for h in re.findall(r'%s(?=%s|\s*$)' % (item, split), instr)] | |
link_splitter = re.compile(LINK_SPLIT) | |
def parse_link_value(instr): | |
""" | |
Given a link-value (i.e., after separating the header-value on commas), | |
return a dictionary whose keys are link URLs and values are dictionaries | |
of the parameters for their associated links. | |
Note that internationalised parameters (e.g., title*) are | |
NOT percent-decoded. | |
Also, only the last instance of a given parameter will be included. | |
For example, | |
>>> parse_link_value('</foo>; rel="self"; title*=utf-8\'de\'letztes%20Kapitel') | |
{'/foo': {'title*': "utf-8'de'letztes%20Kapitel", 'rel': 'self'}} | |
""" | |
out = {} | |
if not instr: | |
return out | |
for link in [h.strip() for h in link_splitter.findall(instr)]: | |
url, params = link.split(">", 1) | |
url = url[1:] | |
param_dict = {} | |
for param in _splitstring(params, PARAMETER, "\s*;\s*"): | |
try: | |
a, v = param.split("=", 1) | |
param_dict[a.lower()] = _unquotestring(v) | |
except ValueError: | |
param_dict[param.lower()] = None | |
out[url] = param_dict | |
return out | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) > 1: | |
print parse_link_value(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment