-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgist_cli.py
51 lines (42 loc) · 1.56 KB
/
gist_cli.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
import argparse
from ConfigParser import ConfigParser
import os.path
import urllib2
import json
import sys
import base64
def main():
parser = argparse.ArgumentParser( description = 'Create a github gist from a file, or from stdin' )
parser.add_argument( 'infile_list', nargs = '*', type = argparse.FileType( 'r' ))
parser.add_argument( '--description', '-d', default = '' )
parser.add_argument( '--private', '-p', action = 'store_true', default = False )
arguments = parser.parse_args()
if len(arguments.infile_list) > 0:
files = {}
for infile in arguments.infile_list:
files[infile.name] = {'content': infile.read()}
else:
files = {'stdin': {'content': sys.stdin.read()}}
uri = 'https://api.github.com'
request = urllib2.Request( '%s/gists' % (uri) )
try:
gitconfig = ConfigParser()
gitconfig.readfp( open( os.path.expanduser( '~/.gitconfig' ) ) )
username = gitconfig.get( 'github', 'user' )
password = gitconfig.get( 'github', 'password' )
raw = '%s:%s' % (username, password)
auth = 'Basic %s' % base64.b64encode(raw).strip()
request.add_header('authorization', auth)
except: #Empty Excepts kill puppies
pass
request.add_data(json.dumps( {
'description': arguments.description,
'public': not arguments.private,
'files': files,
}))
response = urllib2.urlopen(request)
json_response = json.loads(response.read())
print json_response['html_url']
if __name__ == '__main__':
main()