Skip to content

Instantly share code, notes, and snippets.

@lyoshenka
Forked from fedir/gist-backup.py
Last active January 25, 2019 06:15
Show Gist options
  • Save lyoshenka/de4d3a0e82d8be987d69 to your computer and use it in GitHub Desktop.
Save lyoshenka/de4d3a0e82d8be987d69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Clone or update all a user's gists
# curl -ks https://gist.githubusercontent.com/lyoshenka/de4d3a0e82d8be987d69/raw/gist-backup.py | python
# curl -ks https://gist.githubusercontent.com/lyoshenka/de4d3a0e82d8be987d69/raw/gist-backup.py | USERNAME=lyoshenka python
# USERNAME=lyoshenka python gist-backup.py
import json
import urllib
from subprocess import call
from urllib import urlopen
import os
import math
USER = os.environ['USERNAME'] if 'USERNAME' in os.environ else 'lyoshenka'
perpage=30.0
userurl = urlopen('https://api.github.com/users/' + USER)
public_gists = json.load(userurl)
gistcount = public_gists['public_gists']
pages = int(math.ceil(float(gistcount)/perpage))
for page in range(pages):
u = urlopen('https://api.github.com/users/' + USER + '/gists?page=' + str(page) + '&per_page=' + str(int(perpage)))
gists = json.load(u)
startd = os.getcwd()
for gist in gists:
gistd = gist['id']
if os.path.isdir(gistd):
os.chdir(gistd)
call(['git', 'pull', 'git://gist.github.com/' + gistd + '.git'])
os.chdir(startd)
else:
call(['git', 'clone', 'git://gist.github.com/' + gistd + '.git'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment