Skip to content

Instantly share code, notes, and snippets.

@tonyg
Created August 25, 2019 18:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tonyg/b8db8bd3dd83ca90a53945b6482f6d80 to your computer and use it in GitHub Desktop.
Save tonyg/b8db8bd3dd83ca90a53945b6482f6d80 to your computer and use it in GitHub Desktop.
#!/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')
#!/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')
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment