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