Skip to content

Instantly share code, notes, and snippets.

@kennethreitz
Forked from soeren/gist.py
Created September 9, 2010 00:26
Show Gist options
  • Save kennethreitz/571122 to your computer and use it in GitHub Desktop.
Save kennethreitz/571122 to your computer and use it in GitHub Desktop.
import httplib
import urllib
import re
import os.path
def gist_write(name, content, ext=None, login=None, token=None):
if ext is None:
ext = os.path.splitext(name)[1]
params = {
"file_ext[gistfile1]": ext,
"file_name[gistfile1]": name,
"file_contents[gistfile1]": content
}
if not (login is None or token is None):
params['login'] = login
params['token'] = token
conn = httplib.HTTPConnection("gist.github.com")
conn.request("POST", "/gists", urllib.urlencode(params))
response = conn.getresponse()
ret = None
if response.status == 302:
data = response.read()
ret = re.search("(http://gist.github.com/\d+)", data).group(1)
conn.close()
return ret
def gist_embed(url):
return '<script src="%s.js"></script>' % url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment