blogging :: atom :: python
Well, today I decided I might as well start posting my blogs to
blogger.com in addition to my Advogato posts. A couple friends of mine
are on blogger, so I imagine I'll start posting more comments there,
and I thought it was a shame for the lack of content in my blogger
account (I was using it as a placeholder).I figured it would take no extra work to update it, really -- my
Advogato posts are all via email. All I needed to do was write a script
to post to blogger too. Well, working with the blogger Atom API was a
little frustrating. It's very sensitive, only accepting ASCII-encoded
strings, needs certain characters escaped, and returns HTTP 500 with no
clue as to what's going on. Anyway, I finally got everything tweaked
just right, and figured I would post the python code for doing it, as
many of the examples on the net are old.The code below is copied and pasted from a script of mine, with most of
the guts ripped out (I parse from email and do authentication, to make
sure I'm the only one sending blog emails). There might be something
left out, but this should be enough to get you started.
from xml.sax.saxutils import escape
username = 'your_blogger_name'
password = 'your_pass'
bloggerid = 'some_number'
blogger = "https://www.blogger.com/atom/%s" % bloggerid
title = 'blog entry title'
blog_entry = 'your blog entry'
created = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime())
body = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<title mode="escaped" type="text/plain">%s</title>
<generator url="http://your_url/atom">Your Client
</entry>""" % (title, created, blog_entry)
base64string = base64.encodestring('%s:%s' % (username,
req.add_header("Authorization", "Basic %s" % base64string)
f = urllib2.urlopen(req)