Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet
Forked from joneskoo/gist-backup.py
Last active May 21, 2017 09:46
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 HorlogeSkynet/968eaa45ec02744375b6126f31d83008 to your computer and use it in GitHub Desktop.
Save HorlogeSkynet/968eaa45ec02744375b6126f31d83008 to your computer and use it in GitHub Desktop.
GitHub's Gists Backup
#!/usr/bin/env python3
import os
import json
import subprocess
import urllib.request
# Just edit this with your GitHub's username
USERNAME = 'HorlogeSkynet'
# Your gists will be into a folder named '`USERNAME`_Gists'
PATH = USERNAME + '_Gists'
# Checks existence (or creates) a directory which will contain the backup gists
if not os.path.exists(PATH):
os.makedirs(PATH)
# Move into it !
os.chdir(PATH)
# Clone each gists of your account
for gist in json.loads(urllib.request.urlopen("https://api.github.com/users/" + USERNAME + "/gists").read().decode()):
# If the project does not exist on the filesystem yet...
if not os.path.exists(gist['id']):
# ... let's clone it
subprocess.call(['git', 'clone', gist['git_pull_url']])
# It DOES exist...
else:
# Move into the directory and pull it from the remote repository
os.chdir(gist['id'])
subprocess.call(['git', 'pull', 'origin', 'master'])
# Don't forget to go back to the root saves's directory
os.chdir('..')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment