Skip to content

Instantly share code, notes, and snippets.

@erikng
Last active June 12, 2019 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikng/eff2812c82107f20b2372d974f72005f to your computer and use it in GitHub Desktop.
Save erikng/eff2812c82107f20b2372d974f72005f to your computer and use it in GitHub Desktop.
gist_dler.py
#!/usr/bin/python
'''Downloads gists of a specified user on github. Useful if they are say...
joining a fruit company.'''
import json
import optparse
import os
import requests
def main():
'''main'''
usage = '%prog [options]'
o = optparse.OptionParser(usage=usage)
o.add_option('--mastergisturl', help=(
'API gist url. Example: https://api.github.com/users/pudquick/gists'))
opts, args = o.parse_args()
if not opts.mastergisturl:
print o.print_help()
exit(1)
else:
master_gist_path = '/Users/Shared/gist_dler'
url = opts.mastergisturl
cheap_username = url.split('/users/')[-1].split('/gists')[0]
username_gist_path = os.path.join(master_gist_path, cheap_username)
if not os.path.exists(username_gist_path):
os.makedirs(username_gist_path)
querystring = {'per_page': '100'}
response = requests.request("GET", url, params=querystring)
data = json.loads(response.text)
if response.status_code == 200:
count = 0
while True:
if count == 0:
try:
next_link = response.links['next']['url']
except:
break
next_response = requests.request("GET", next_link, params=querystring)
next_data = json.loads(next_response.text)
for x in next_data:
data.append(x)
if count >= 1:
try:
next_link = next_response.links['next']['url']
except:
break
count = count + 1
gists = []
for x in data:
gist_data = {}
amount_of_files = len(x['files'].keys())
id = x['id']
description = x['description']
if amount_of_files > 1:
for k, v in x['files'].items():
gist_data['id'] = id
gist_data['description'] = description
gist_data['file_name'] = k
gist_data['raw_url'] = v['raw_url']
gists.append(gist_data)
gist_data = {}
else:
gist_data['id'] = x['id']
gist_data['description'] = description
initial_file = x['files'].keys()[0]
gist_data['file_name'] = initial_file
gist_data['raw_url'] = x['files'][initial_file]['raw_url']
gists.append(gist_data)
for gist in gists:
if not gist['description']:
name = str(gist['id'])
else:
name = str(gist['id'] + ' - ' + gist['description'])
gist_root_path = os.path.join(username_gist_path, name)
gist_path = os.path.join(gist_root_path, gist['file_name'])
if not os.path.exists(gist_root_path):
print 'Creating gist dir: %s ' % str(gist_root_path)
os.makedirs(gist_root_path)
if os.path.exists(gist_path):
print 'Gist already exists: %s' % str(gist['file_name'])
else:
print 'Downloading gist: %s' % str(gist['file_name'])
print 'URL: %s' % str(gist['raw_url'])
gist_response = requests.request('GET', gist['raw_url'])
gist_content = gist_response.text
try:
with open(gist_path, 'w') as f:
f.write(gist_content)
except:
with open(gist_path, 'w') as f:
f.write(gist_content.encode('utf-8'))
else:
print 'Error downloading files'
print data['message']
if __name__ == '__main__':
main()
@LcTrKiD
Copy link

LcTrKiD commented Jun 12, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment