Using Python+MetaWeblog to manage your blog

來源:互聯網
上載者:User
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 blog.py to upload local Html file to blog site using metaWeblog API. You can access full source code inhttps://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.

The most import 3 entry points of metaWeblog are:

metaWeblog.newPost (blogid, username, password, struct, publish) returns stringmetaWeblog.editPost (postid, username, password, struct, publish) returns truemetaWeblog.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 tonewPost 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 xmlrpclibclass 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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.