ans42

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:

blog-workflow.png

We deliver a Python script blog.py to upload local Html file to blog site using metaWeblog API. You can access full source code in https://github.com/huafengxi/pblog

2 configuration

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

serviceUrl, appKey = 'http://www.cnblogs.com/ans42/services/metaweblog.aspx', 'ans42'
usr, passwd = 'ans42', 'xxxxx'

3 Usages

After configuration. Now you can:

  • list your recent posts:
./blog.py 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:
./blog.py 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. blog.py 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:

./blog.py post your-blog.org
  • Delete a post:
./blog.py delete <post-id>

4 Technical Details

There are actually two problems need to address:

  • Access the metaWeblog API: blog.py use xmlrpclib for this task.
  • Upload mediaObject(such as Image/Video) and replace their references in locally generated Html: blog.py 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. blog.py 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: http://www.xmlrpc.com/MetaWeblogApi 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.

blog-structure.png

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 www.cnblogs.com 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 = 'http://www.cnblogs.com/ans42/services/MetaWeblog.aspx', 'ans42'
usr, passwd = 'ans42', 'xxxxxx'
blog = MetaWeblog(serviceUrl, appKey, usr, passwd)
print blog.newPost('Title', 'content')

4.2 Solve the mediaObject reference in Html

Todo

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