Created
March 14, 2021 20:58
-
-
Save ozh/aa29a6a6b41217c33da5a22272726c9f to your computer and use it in GitHub Desktop.
Backup all gists python script
This file contains hidden or 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 | |
# To execute, copy script into a ~/gists directory, and 'python gist-backup.py' | |
# This will git clone every public and private gist of GITHUB-USER into that folder | |
# as well as a contents.txt folder. | |
# From : https://gist.github.com/ChristopherA/163aff3f1eeba9206d73 | |
USER = 'ozh' | |
from __future__ import print_function | |
import json | |
import urllib | |
from subprocess import call | |
import os | |
import math | |
try: | |
from urllib.request import urlopen | |
except ImportError: | |
from urllib2 import urlopen | |
perpage = 30 | |
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 = 'https://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'] | |
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