Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created June 20, 2013 14:51
Show Gist options
  • Save tmcw/5823405 to your computer and use it in GitHub Desktop.
Save tmcw/5823405 to your computer and use it in GitHub Desktop.
Gist Backup

Back up your gists: update YOUR_USERNAME and YOUR_PASSWORD and run it. Requires

sudo pip install requests

I use this mainly to be able to quickly search through my thousands of Gists with a tool like ack or ag.

import requests, os, glob, json, codecs
you = 'YOUR_USERNAME'
auth = (you, 'YOUR_PASSWORD')
data = os.path.join(os.path.dirname(__file__), 'gists')
try: os.mkdir(data)
except Exception: pass
def run(page = 1):
print 'on page %s' % page
already = glob.glob("%s/*.json" % data)
start = 'https://api.github.com/users/%s/gists' % you
if page:
start = '%s?page=%s' % (start, page)
r = requests.get(start, auth=auth)
has_new = False
for t in r.json():
if ("%s/%s.json" % (data, t['id'])) not in already:
print 'downloading %s' % t['id']
json.dump(t, open('%s/%s.json' % (data, t['id']), 'w'))
for f, v in t['files'].items():
fi = requests.get(v['raw_url'], auth=auth)
print 'downloading %s/%s_%s' % (data, t['id'], f.encode('ascii', 'ignore'))
codecs.open(u'%s/%s_%s' % (data, t['id'], v['filename']), 'w').write(fi.text.encode('utf-8'))
has_new = True
else:
print '%s already stored' % t['id']
if has_new and 'next' in r.headers['Link']:
run(page + 1)
print 'starting gist archive of @%s' % you
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment