summaryrefslogtreecommitdiff
path: root/artima/post.py
blob: 23ad028371f2d4415cb8a6bfb9b5ab917c46d001 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
A script to post articles on my blog
"""

import re, sys
from twill import commands as c

IMAGE = re.compile('e:: ([-+\w\.]+)') # figure/image

def replace_image_links(txt):
    "Remove code-block directives and relative images to make Artima happy"
    txt = re.sub(r'.. code-block:: \w+', '::', txt)
    return IMAGE.sub(r'e:: http://www.phyast.pitt.edu/~micheles/scheme/\1', txt)

if __name__ == '__main__':
    try:
        rstfile, thread  = sys.argv[1:]
    except ValueError:
        sys.exit('Usage: post <rstfile> <artima-thread-number>')
    text = replace_image_links(file(rstfile).read())
    c.go('http://www.artima.com/sign_in?d=%2Findex.jsp')
    c.formvalue('1', 'username', 'micheles')
    c.formvalue('1', 'password', 'pippolippo')
    c.submit()
    c.go('http://www.artima.com/weblogs/editpost.jsp?thread=%s' % thread)
    try:
        c.formvalue('1', 'body', text)
    except:
        print "Saving the offending post on /tmp/x.txt ..."
        file('/tmp/x.txt', 'w').write(text)
        raise
    c.submit()

"""
from ms.http_utils import urlopen2
urlopen2('http://www.artima.com/sign_in?d=%2Findex.jsp', 
  dict(username='micheles', password='pippolippo'))
urlopen2('http://www.artima.com/weblogs/editpost.jsp',
  dict(thread='261364', body='prova'))
"""