Created
November 6, 2011 20:39
-
-
Save itsbth/1343443 to your computer and use it in GitHub Desktop.
Uploaded by gist.py
This file contains hidden or 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
| contentimport urllib2 | |
| from subprocess import Popen, PIPE | |
| from os import path | |
| import json | |
| import base64 | |
| from io import open | |
| CREATE_URL = u'https://gist.github.com/gists' | |
| def config(key): | |
| cmd = Popen([u'git', u'config', u'--global', key], stdout=PIPE) | |
| return cmd.communicate()[0].decode(u'utf-8').strip() | |
| def create_auth(login, token): | |
| #print('{}/token:{}'.format(login, token)) | |
| return u'Basic {}'.format(base64.b64encode(u'{}/token:{}'.format(login, token).encode(u'utf-8')).decode(u'utf-8')) | |
| def create(files, private=False, description=None): | |
| data = { | |
| u'files': {}, | |
| u'public': not private, | |
| u'description': description if description else u'Uploaded by gist.py', | |
| u'login': config(u'github.user'), | |
| u'token': config(u'github.token'), | |
| } | |
| for fn in files: | |
| data[u'files'][path.basename(fn)] = { | |
| u'content': open(fn).read(), | |
| } | |
| print json.dumps(data) | |
| req = urllib2.Request(CREATE_URL, | |
| headers={ | |
| u'Content-Type': u'application/json', | |
| u'Accept': u'application/json'}, | |
| #'Authorization': create_auth(config('github.user'), config('github.token'))}, | |
| data=json.dumps(data).encode(u'utf-8')) | |
| return urllib2.urlopen(req).geturl() | |
| if __name__ == u'__main__': | |
| import sys | |
| print create(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment