-
Notifications
You must be signed in to change notification settings - Fork 5
/
get_gist.py
executable file
·36 lines (30 loc) · 1.04 KB
/
get_gist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#! /usr/local/bin/python
import urllib2
import json
import argparse
import os
def gist_url(gistId):
return "https://api.github.com/gists/%s" % gistId
def get_gist(gistId, dest_dir):
# gistInfo = json.parse(urllib.urlretrieve(gist_url(gistId)))
print gist_url(gistId)
response = urllib2.urlopen(gist_url(gistId))
text = response.read()
gistInfo = json.loads(text)
cwd = os.getcwd();
newDir = os.path.join(cwd, dest_dir, gistId)
if not os.path.exists(newDir): os.makedirs(newDir)
for file in gistInfo['files'].keys():
fileContents = urllib2.urlopen(gistInfo['files'][file]['raw_url'])
with open(os.path.join(newDir,file), 'w') as f:
print "Writing %s" % os.path.join(newDir,file)
f.write(fileContents.read())
def main():
parser = argparse.ArgumentParser(description='Grab and save a gist by id.')
parser.add_argument('gistId')
parser.add_argument('--dest_dir')
args = parser.parse_args();
dest_dir = "" if args.dest_dir == None else args.dest_dir
get_gist(args.gistId, dest_dir);
if __name__ == '__main__':
main()