Skip to content

Instantly share code, notes, and snippets.

@doersino
Forked from asw456/downloadGists.py
Last active August 29, 2015 14:12
Show Gist options
  • Save doersino/af1ba2bb16b12542b41d to your computer and use it in GitHub Desktop.
Save doersino/af1ba2bb16b12542b41d to your computer and use it in GitHub Desktop.
Clone or update a user's public gists. Usage: backup_gists.py USERNAME [DIR]
#!/usr/bin/env python
# Clone or update a user's public gists.
# Usage: backup_gists.py USERNAME [DIR]
import json
from subprocess import call
from urllib import urlopen
import os
import sys
if len(sys.argv) < 2:
raise SystemExit("Usage: backup_gists.py USERNAME [DIR]")
user = sys.argv[1]
startd = os.getcwd()
if len(sys.argv) == 3:
startd = os.path.join(os.path.abspath(os.getcwd()), sys.argv[2])
if not os.path.isdir(startd):
os.mkdir(startd)
u = urlopen('https://api.github.com/users/' + user + '/gists')
gists = json.load(u)
for gist in gists:
gistd = os.path.join(startd, gist['id'])
if os.path.isdir(gistd):
os.chdir(gistd)
call(['git', 'pull', 'git://gist.github.com/' + gist['id'] + '.git'])
else:
os.chdir(startd)
call(['git', 'clone', 'git://gist.github.com/' + gist['id'] + '.git'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment