Friday, January 06, 2006

Automated Blogging: Advogato & Blogger

blogging :: atom :: python

Well, today I decided I might as well start posting my blogs to 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.

import time
import base64
import urllib2
from xml.sax.saxutils import escape

username = 'your_blogger_name'
password = 'your_pass'
bloggerid = 'some_number'
blogger = "" % 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"?>
<entry xmlns="">
<title mode="escaped" type="text/plain">%s</title>
<generator url="http://your_url/atom">Your Client
<content type="application/xhtml+xml">
<div xmlns="">%s</div>
</entry>""" % (title, created, blog_entry)

req= urllib2.Request(url=blogger)
base64string = base64.encodestring('%s:%s' % (username,
req.add_header("Authorization", "Basic %s" % base64string)
req.add_header("Content-type", "application/atom+xml")
f = urllib2.urlopen(req)

Now playing:
Yoko Kanno & The Seatbelts - Gotta Knock A Little Harder (Friday,  9:59pm MST)

No comments:

Post a Comment