Skip to content

Instantly share code, notes, and snippets.

@bougui505
Last active September 6, 2016 08:22
Show Gist options
  • Save bougui505/9231208 to your computer and use it in GitHub Desktop.
Save bougui505/9231208 to your computer and use it in GitHub Desktop.
Clone or update all a user's gists
#!/usr/bin/env python
# Clone or update all a user's gists
# curl -ks https://raw.github.com/gist/5466075/gist-backup.py | USER=fedir python
# USER=fedir python gist-backup.py
# From: http://stackoverflow.com/a/16233710/1679629
import json
import urllib
from subprocess import call
from urllib import urlopen
import os
import math
USER = 'bougui505'
perpage=30.0
userurl = urlopen('https://api.github.com/users/' + USER)
public_gists = json.load(userurl)
gistcount = public_gists['public_gists']
print "Found gists : " + str(gistcount)
pages = int(math.ceil(float(gistcount)/perpage))
print "Found pages : " + str(pages)
startd = '/home/bougui/source/gists/'
os.chdir(startd)
f=open('./contents.txt', 'w+')
for page in range(pages):
pageNumber = str(page + 1)
print "Processing page number " + pageNumber
pageUrl = 'https://api.github.com/users/' + USER + '/gists?page=' + pageNumber + '&per_page=' + str(int(perpage))
u = urlopen (pageUrl)
gists = json.load(u)
for gist in gists:
gistd = gist['id']
gistUrl = 'git://gist.github.com/' + gistd + '.git'
if os.path.isdir(gistd):
os.chdir(gistd)
call(['git', 'pull', gistUrl])
os.chdir(startd)
else:
call(['git', 'clone', gistUrl])
if gist['description'] == None:
description = ''
else:
description = gist['description'].encode('utf8').replace("\r",' ').replace("\n",' ')
print >> f, gist['id'], gistUrl, description
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment