Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created April 25, 2015 19:51
Show Gist options
  • Save thomasballinger/64e529415936ddc8985b to your computer and use it in GitHub Desktop.
Save thomasballinger/64e529415936ddc8985b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
import sys
import urllib2
import json
def do_gist_json(s):
""" Use json to post to github. """
gist_public = False
gist_url = 'https://api.github.com/gists'
data = {'description': None,
'public': None,
'files' : {
'sample': { 'content': None }
}}
data['description'] = 'Gist from BPython'
data['public'] = gist_public
data['files']['sample']['content'] = s
req = urllib2.Request(gist_url, json.dumps(data), {'Content-Type': 'application/json'})
try:
res = urllib2.urlopen(req)
except urllib2.HTTPError, e:
return e
try:
json_res = json.loads(res.read())
return json_res['html_url']
except urllib2.HTTPError, e:
return e
if __name__ == "__main__":
if len(sys.argv) > 1:
do_gist_json('command line flag detected')
s = sys.stdin.read()
print do_gist_json(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment