scratchpad.py
6 April 2008
21:41
as Python
| 1 | """ |
|---|---|
| 2 | Simple script to post stuff to cmlenz' scratchpad |
| 3 | |
| 4 | 2008-04-06 Tim Hatch <code@timhatch.com> |
| 5 | """ |
| 6 | |
| 7 | import urllib2 |
| 8 | import urllib |
| 9 | |
| 10 | import re |
| 11 | import sys |
| 12 | |
| 13 | R_TOKEN = re.compile(r'name="__token__" value="([0-9a-z]+)') |
| 14 | |
| 15 | BASEURL = "http://scratchpad.cmlenz.net/post/" |
| 16 | |
| 17 | def main(filename, mimetype='text/plain'): |
| 18 | file_data = open(filename, 'rb').read() |
| 19 | |
| 20 | first_page = urllib2.urlopen(BASEURL) |
| 21 | token_match = R_TOKEN.search(first_page.read()) |
| 22 | if not token_match: |
| 23 | print "Cannot find token in post page" |
| 24 | return -1 |
| 25 | token = token_match.group(1) |
| 26 | #print "Token is", token |
| 27 | |
| 28 | postdata = urllib.urlencode({'title': filename, |
| 29 | 'mimetype': mimetype, |
| 30 | 'content': file_data, |
| 31 | '__token__': token}) |
| 32 | req = urllib2.Request(BASEURL, postdata) |
| 33 | req.add_header('Referer', BASEURL) |
| 34 | req.add_header('Cookie', 'form_token=%s' % token) |
| 35 | |
| 36 | second_page = urllib2.urlopen(req) |
| 37 | print second_page.url |
| 38 | |
| 39 | if __name__ == '__main__': |
| 40 | sys.exit(main(*sys.argv[1:])) |
Comments
No comments so far.
