Collecting my attempts to improve at tech, art, and life

Adding Categories to the Python Blogger Client

Tags: post

Update 2009-06-09

I’ve revisited the code for Blogger posting with Python. Start with python-blogger-refresh-part-1 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 = ''

    # 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.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="")

    # 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.

Added to vault 2024-01-15. Updated on 2024-06-24