Created
November 6, 2011 21:41
-
-
Save itsbth/1343551 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
| import urllib2 | |
| from subprocess import Popen, PIPE | |
| from os import path | |
| import json | |
| import base64 | |
| import httplib | |
| 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 post_urllib2(url, data): | |
| req = urllib2.Request(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() | |
| def post_curl(url, data): | |
| from tempfile import NamedTemporaryFile | |
| with NamedTemporaryFile() as f: | |
| f.write(data) | |
| f.flush() | |
| cmd = [u'curl', u'-H', 'Content-Type: application/json', u'-w', | |
| u'%{url_effective}', u'-L', u'--data', u'@{0}'.format(f.name), | |
| u'-o', u'/dev/null', url] | |
| out, err = Popen(cmd, stdout=PIPE).communicate() | |
| return out.decode(u'utf-8').strip() | |
| if hasattr(httplib, "HTTPSConnection"): | |
| post = post_urllib2 | |
| else: | |
| post = post_curl | |
| 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(), | |
| #} | |
| data[u'files'][path.basename(fn)] = open(fn).read() | |
| return post(CREATE_URL, json.dumps(data)) | |
| 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