Created
July 30, 2011 18:50
-
-
Save hyle/1115848 to your computer and use it in GitHub Desktop.
fetch files from a public gist
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 | |
__license__ = 'http://www.opensource.org/licenses/mit-license.php (MIT)' | |
__author__ = 'Andrea Baresi' | |
from urllib2 import Request, urlopen | |
import sys | |
import json | |
METADATA_URL = 'http://gist.github.com/api/v1/json/%s' | |
CONTENT_URL = 'http://gist.github.com/raw/%s/%s' | |
def getURL(url): | |
req = Request(url) | |
response = urlopen(req) | |
print url, 'fetched' | |
return response.read() | |
if __name__ == '__main__': | |
if len(sys.argv) > 1 and sys.argv[1].isdigit(): | |
gistid = sys.argv[1] | |
metadata = json.loads(getURL(METADATA_URL % (gistid,))) | |
for filename in metadata['gists'][0]['files']: | |
content = getURL(CONTENT_URL % (gistid, filename)) | |
with open(filename, 'wb') as fh: | |
fh.write(content) | |
else: | |
print 'give me a gist_id' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment