Posting to Blogger.com from the command line

Well, you can tell I am procrastinating working today (but you can forgive me after the PIDA GSoc Application was rejected as a mentoring organisation), but I have hacked up an early version of a library (for Python) to manage blogger.com posting.

I was becoming slightly upset with Blogger.com, since it does some insane things like puts BR tags in the middle of PRE elements, which meant the syntax highlighting script I wanted to use was going nuts. I also gradually realised that none of the blogging clients work with the "new Blogger.com" although for me (who has only been blogging a few months) it has been the "only Blogger.com".

So, with a quick search around for documentation from code.google.com, I was able to discover that Blogger.com uses the google GData API with a bit of Atom mixed in. I have no real ideas about these specifications, but http://code.google.com/apis/blogger/gdata.html explains it. Note that another document I found: http://code.blogspot.com/archives/atom-docs.html seems depracated, but I can't confirm that at all.

Python is my language of choice for these things, so I started hacking.

Step 1: Authenticate with Google Login stuff. This is explained in http://code.google.com/apis/accounts/AuthForInstalledApps.html but basically you need to:

  • Send a post request to https://www.google.com/accounts/ClientLogin
  • Set the service value of the data to "blogger" (this had me for a while)
  • Parse the body of the response for an "Auth" token
  • Include the header Authorization: GoogleLogin auth=yourAuthValue in all your requests from thereon in (not obvious from the authentication docs)
Once you have successfully logged in you will want to get some information. Firstly the posting API works on the blog ID, not the blog name. The ID appears in the source of the blog page in this tag <link rel="service.post" type="application/atom+xml" ... but you can also fetch it from the blog's feed from the blog's name (see the API documentation).

Once you have this ID, posting is a matter of using the ID with your auth token, and entering data in an XML format as the post's body. Not forgetting to set the correct content-type for your post to "application/atom+xml". If you have posted correctly, the server returns a 201 Created response.

I have spared you the boring details of making urllib/urllib2 work with these things, but I have written some sample code and a command line client which you can browse online here: http://pygbloggerlib.googlecode.com/svn/trunk/gblogger/client.py as well as accessing the googlecode page (no releases yet) here: http://code.google.com/p/pygbloggerlib/

An example:


>>> from gblogger.client import BlogClient
>>> import getpass
>>> passwd = getpass.getpass()
Password:
>>> c = BlogClient('my-test-atom-blog', 'aafshar@gmail.com', passwd)
>>> c.post('this is a test post', 'this is the body of the test post with some html')
HTTP Error 201: Created
>>>
You can actually see this post online (if I haven't deleted the test blog) here: http://my-test-atom-blog.blogspot.com/2007/03/this-is-test-post.html

Since I never have any spare time, my plans for this library are a bit shaky. It is a toy library in that there are no real tests, and failure conditions are not really dealt with properly. Ideally I will:

  1. Write a PIDA plugin for blogging
  2. Merge it into something like BloGTK, or write a miniature gui client of my own.

I have no idea how to publicize this to the Blogger.com community, but if any of you big Blogger admins or google developers are interested in spreading the word, (or silencing the word!) please let me know.

UPDATE:
(some more useful links)

http://code.google.com/apis/gdata/basics.html
http://www.google.com/base/api/demo/html/demo.html

UPDATE:

Well, editing now works in the library! I know this because I accidentally overwrote this post! Lucky for the unnoficial planet python it was still lying around there!

Popular posts from this blog

PyGTK, Py2exe, and Inno setup for single-file Windows installers

ESP-IDF for Arduino Users, Tutorials Part 2: Delay

How to install IronPython2 with Mono on Ubuntu