assert(random() == 0x2a)

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

1 Motivation

For the task of blogging, you can generate Html file locally, then using metaWeblog API to upload them to your blog hosting site. This workflow has following benefits:

  • There are many ways to generate Html documents, you can choose which you like. Especially, you can create clean and readable text file using markdown syntax and convert them to Html file. I personally choose emacs/org-mode for this task.
  • You can easily backup and reuse the content of your posts.
  • You can use version control tool such as git to track down the history.

A possible workflow is presented below:


We deliver a Python script to upload local Html file to blog site using metaWeblog API. You can access full source code in

2 configuration

To use this script, you need to configure it first, change the following two lines of properly.

serviceUrl, appKey = '', 'ans42'
usr, passwd = 'ans42', 'xxxxx'

3 Usages

After configuration. Now you can:

  • list your recent posts:
./ list 12

It will list recent 12 posts, you can omit the number, then it will list 10 posts.

  • Upload or Update your post from local Html file:
./ post your-blog.html

This will upload your-blog.html to your blog site as a new post, or update existing post if you have posted it before. will scan recent 10 posts, when it found a post whose title is the same as your-blog.html, then it will think your-blog.html has been posted already, and update this post instead of create a new one.

For convenience, you can post org-mode or asciidoc text file directly. Such as:

./ post
  • Delete a post:
./ delete <post-id>

4 Technical Details

There are actually two problems need to address:

  • Access the metaWeblog API: use xmlrpclib for this task.
  • Upload mediaObject(such as Image/Video) and replace their references in locally generated Html: will extract all local image references embed in Html file, then upload them to blog site, replace the local image references using the returned URL. Image files will not be uploaded again if they are not modified. think a file has not been modified if its MD5 has not been changed.

We will present how to wrap MetaWeblog API using xmlrpclib of Python, and how to solve the mediaObject reference in Html.

4.1 Wrapper of MetaWeblog API

See RFC: for reference on MetaWeblog API.

One user on a site may have multiple blogs, one blog contain multiple posts, one post contain at least title and description attributes, as follwing figure shows.


The most import 3 entry points of metaWeblog are:

metaWeblog.newPost (blogid, username, password, struct, publish) returns string
metaWeblog.editPost (postid, username, password, struct, publish) returns true
metaWeblog.getPost (postid, username, password) returns struct

The content of one blog are represented as struct. you can upload the blog to site without publish to others, so comes the publish parameter to newPost and editPost, this bool value control whether the blog should be published.

There are also methods to list posts get/delete a post which won't be listed here. Final piece of the API is handle mediaObject in Html.

MetaWeblog.newMediaObject (blogid, username, passwd, file) return URL of uploaded file

For these who are curisoty, following Python code demo how to wrap newPost methods and how to use it.

import xmlrpclib
class MetaWeblog:
    '''works with atleast'''
    def __init__(self, serviceUrl, appKey, usr, passwd):
        self.serviceUrl, self.appKey, self.usr, self.passwd = serviceUrl, appKey, usr, passwd
        self.server = xmlrpclib.ServerProxy(self.serviceUrl)

    def newPost(self, title='Title used for test', description='this is a test post.', category='no category', publish=True, blogid='', **kw):
        return self.server.MetaWeblog.newPost(blogid, self.usr, self.passwd, dict(kw, title=title, description=description, category=category), publish)

serviceUrl, appKey = '', 'ans42'
usr, passwd = 'ans42', 'xxxxxx'
blog = MetaWeblog(serviceUrl, appKey, usr, passwd)
print blog.newPost('Title', 'content')

4.2 Solve the mediaObject reference in Html


posted on 2011-06-16 21:36  ans42  阅读(934)  评论(0编辑  收藏  举报