Skip to content

Instantly share code, notes, and snippets.

@itsbth
Created November 6, 2011 20:37
Show Gist options
  • Select an option

  • Save itsbth/1343439 to your computer and use it in GitHub Desktop.

Select an option

Save itsbth/1343439 to your computer and use it in GitHub Desktop.
Uploaded by gist.py
contentimport urllib.request
from subprocess import Popen, PIPE
from os import path
import json
import base64
CREATE_URL = 'https://gist.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',
'login': config('github.user'),
'token': config('github.token'),
}
for fn in files:
data['files'][path.basename(fn)] = {
'content': open(fn).read(),
}
print(json.dumps(data))
req = urllib.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 urllib.request.urlopen(req).geturl()
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