Adding Categories to the Python Blogger Client

Added by to Blogspot on and tagged · blogger · gdata · python ·

About 2 minutes to read (259 words)

Xah's Elisp Tutorial Reinstalling JRuby

Update 2009-06-09

I’ve revisited the code for Blogger posting with Python. Start with the Python Blogger refresh to see the new starting point.

The Original Tale

I’ve already used my Python Blogger client for a couple of postings, and I’ve been pretty happy with it so far. It still desperately needs tags, though. Actually, Blogger calls them “labels.” Actually actually, the Atom API calls them “categories.” Well, whatever they are called it looks like they are pretty easy to add.

You already know that tags are defined in my config header, and are simply a comma-delimited list like so:

tags: python,gdata,blogger

Here’s the new sendPost method:

def sendPost(self):
    """Log into Blogger and submit my already parsed post"""

    # Authenticate using ClientLogin
    blogger = service.GDataService(self.__account, self.__password)
    blogger.source = 'post-to-blog.py_v01.0'
    blogger.service = 'blogger'
    blogger.server = 'www.blogger.com'
    blogger.ProgrammaticLogin()

    # Get the blog ID
    query = service.Query()
    query.feed = '/feeds/default/blogs'
    feed = blogger.Get(query.ToUri())
    blog_id = feed.entry[0].GetSelfLink().href.split("/")[-1]

    # Create the entry to insert.
    entry = gdata.GDataEntry()
    entry.author.append(atom.Author(atom.Name(text=self.__author)))
    entry.title = atom.Title('xhtml', self.config['title'])
    entry.content = atom.Content(content_type='html', text=self.body)

    # Assemble labels, if any
    if 'tags' in self.config:
        tags = self.config['tags'].split(',')
        for tag in tags:
            category = atom.Category(term=tag, scheme="http://www.blogger.com/atom/ns#")
            entry.category.append(category)

    # Decide whether this is a draft.
    control = atom.Control()
    control.draft = atom.Draft(text='yes')
    entry.control = control

    # Submit it!
    blogger.Post(entry, '/feeds/' + blog_id + '/posts/default')

I hope this works. If it does work, then I am going to do a little refactoring as time allows to make this mess a little cleaner. If it doesn’t work, then I guess I’ll have to … you know … fix it.