Skip to content

Instantly share code, notes, and snippets.

@dhepper
Created November 30, 2010 13:31
Show Gist options
  • Save dhepper/721676 to your computer and use it in GitHub Desktop.
Save dhepper/721676 to your computer and use it in GitHub Desktop.
tidypub
#!/usr/bin/python
import urllib
import urllib2
import re
import sys
URL = 'http://tidypub.org/'
r = urllib2.urlopen(URL)
for l in r.readlines():
m = re.search(r"<input type='hidden' name='csrfmiddlewaretoken' value='(.*)' />", l)
if m:
csrf = m.group(1)
break
else:
print 'could not load csrf token'
sys.exit(1)
text = sys.stdin.read()
params = {'text': text, 'csrfmiddlewaretoken': csrf, 'some_in': '', 'title': ''}
if len(sys.argv) == 3 and sys.argv[1] == '-t':
params['title'] = sys.argv[2]
data = urllib.urlencode(params)
req = urllib2.Request(URL, data, {'Cookie': 'csrftoken=%s'%csrf})
try:
p = urllib2.urlopen(req)
except:
print 'an error occured'
sys.exit(1)
print p.geturl()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment