Last active
May 31, 2021 22:16
-
-
Save ChristopherA/163aff3f1eeba9206d73 to your computer and use it in GitHub Desktop.
Clone or update all the GitHub gists of the current GitHub user into working directory
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 the GitHub gists of the current GitHub user into working directory | |
# Originally forked from | |
# https://gist.github.com/joneskoo/1480022 | |
# via https://gist.github.com/nicerobot/1622504 | |
# https://gist.github.com/fedir/5466075 | |
# https://gist.github.com/proski/a31d49380a38d2c45b2f | |
# Specify GitHub username in the GITHUB_USER environment variable, i.e. | |
# git config --global user.name "ChristopherA" | |
# 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 __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 | |
if 'GITHUB_USER' in os.environ: | |
USER = os.environ['GITHUB_USER'] | |
else: | |
USER = os.environ['USER'] | |
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'].encode('utf8').\ | |
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