Created
October 11, 2010 08:10
-
-
Save copyninja/620197 to your computer and use it in GitHub Desktop.
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/python | |
from simplejson import loads | |
from urllib2 import Request,urlopen,HTTPError | |
import sys | |
import os | |
def ensure_dir(folder): | |
""" | |
Arguments: | |
- `folder`: Folder to check | |
Checks if directory exists in the users current directory. (pwd) | |
If doesn't exists it creates the directory | |
""" | |
if not os.path.exists(os.path.abspath(folder)): | |
os.mkdir(os.path.abspath(folder)) | |
def get_gists(username): | |
""" | |
Arguments: | |
- `username`: Github user whose public gists need to be retrived | |
- returns : list object containing gist information | |
""" | |
request = Request(url="http://gist.github.com/api/v1/json/gists/" + username) | |
request.add_header("User-Agent","Python Gist retrival Script") | |
response = None | |
try: | |
response = urlopen(request) | |
except HTTPError, e: | |
print "Some error occured " + loads(e.read()) | |
if response: | |
return loads(response.read())['gists'] | |
def backup(gists,folder): | |
""" | |
- `gists`: List containing gists information | |
- `folder`: Folder to backup the gists | |
""" | |
print "Backing up Please wait..." | |
ensure_dir(folder) | |
for gist in gists: | |
filename = gist['files'][0] | |
repo = gist['repo'] | |
print "Backing up gist {0} to file name {1}".format(repo,filename) | |
request = Request(url="http://gist.github.com/raw/"+repo+"/"+filename) | |
request.add_header("User-Agent","Python Gist Retriver") | |
try: | |
response = urlopen(request) | |
file_content = response.read() | |
fp = open(os.path.join(os.path.abspath(folder),filename),"w") | |
fp.write(file_content) | |
fp.close() | |
except HTTPError, e: | |
print "Error Occured {0} Bailing out! ".format(e.read()) | |
sys.exit(1) | |
def main(): | |
print """ | |
-------------------------------------------------------------------------- | |
Welcome to Gist retriver | |
You can backup all your gists from github using this script. | |
Alternative you can also retrive specific file from a users | |
public gist. | |
Only public gists can be accessed using this | |
This Program comes with zero warranty. Use it at your own risk. | |
-------------------------------------------------------------------------- | |
""" | |
user = raw_input("Enter github user name: ") | |
gists = get_gists(user) | |
if len(gists) == 0: | |
print """ | |
Sorry I couldn't find any gists for the username :( | |
Either user doesn't have any public gists or user name is invalid | |
""" | |
sys.exit(1) | |
folder = raw_input("Please Give a directory to Store files: ") | |
backup(gists,folder) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment