-
-
Save mattmc3/b9f314f29add32330b2ee359e2ad5aba to your computer and use it in GitHub Desktop.
Clone or update all user's gists #backup #github #gists #management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Clone or update all a user's gists | |
# curl -LJO https://gist.githubusercontent.com/mattmc3/b9f314f29add32330b2ee359e2ad5aba/raw/0068945bf19bc50b9847d5938286c74953aa97e3/gist-backup.py | |
# chmod 755 ./gist-backup.py | |
# USER=mattmc3 python3 gist-backup.py | |
from __future__ import absolute_import, division, print_function, unicode_literals | |
import json | |
import urllib | |
from subprocess import call | |
import os | |
import math | |
try: | |
from urllib.request import urlopen | |
except ImportError: | |
from urllib2 import urlopen | |
USER = os.environ['USER'] | |
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)) | |
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) | |
startd = os.getcwd() | |
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'].replace("\r",' ').replace("\n",' ') | |
print(gist['id'], gistUrl, description, file=f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment