Skip to content

Instantly share code, notes, and snippets.

@SerhatTeker
Last active January 24, 2020 13:21
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 SerhatTeker/3b9bd9c5add01730f67ee5458db13b2e to your computer and use it in GitHub Desktop.
Save SerhatTeker/3b9bd9c5add01730f67ee5458db13b2e to your computer and use it in GitHub Desktop.
Clone or update a user's gists locally
#!/usr/bin/env python3
# Clone or update all a user's gists
# curl -ks https://gist.githubusercontent.com/SerhatTeker/3b9bd9c5add01730f67ee5458db13b2e/raw/a4de62633a8818580d37114b20efbd576388413d/backup-gist.sh | bash
# curl -ks https://gist.githubusercontent.com/SerhatTeker/3b9bd9c5add01730f67ee5458db13b2e/raw/a4de62633a8818580d37114b20efbd576388413d/backup-gist.sh | USER=serhatteker bash
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import json
import math
import os
from subprocess import call
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"] is None:
description = ""
else:
description = gist["description"].replace("\r", " ").replace("\n", " ")
print(gist["id"], gistUrl, description, file=f)
#!/bin/bash
curl -ks https://gist.githubusercontent.com/SerhatTeker/3b9bd9c5add01730f67ee5458db13b2e/raw/086c08de2944af3a2885597ac0666c8b8af19882/backup-gist.py | USER=${USER} python3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment