Using Python + metaweblog to manage your blog

Source: Internet
Author: User
1 Motivation

For the taskBlogging, 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 always ways to generate HTML documents, you can choose which you like. Especially, you can create clean and readable text file usingMarkdownSyntax and convert them to HTML file. I personally chooseEmacs/org-ModeFor this task.
    • You can easily backup and reuse the content of your posts.
    • You can use version control tool suchGitTo track down the history.

A possible workflow is presented below:

We deliver a Python scriptBlog. pyTo 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 linesConfig. pyProperly.

 
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 recent12Posts, You can omit the number, then it will list10Posts.

    • Upload or update your post from local HTML file:
 
./Blog. py post your-blog.html

This will uploadYour-blog.htmlTo your blog site as a new post, or update existing post if you have posted it before.Blog. pyWill scan recent 10 posts, when it found a post whose title is the sameYour-blog.html, Then it will thinkYour-blog.htmlHas been posted already, and update this post instead of create a new one.

For convenience, you can postOrg-ModeOrAsciidocText File directly. such:

 
./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. pyUseXmlrpclibFor this task.
    • Upload mediaobject (such as image/video) and replace their references in locally generated HTML:Blog. pyWill Extract all local image references embed in HTML file, then upload them to blog site, replace the local image references using the returnedURL. Image files will not be uploaded again if they are not modified.Blog. pyThink a file has not been modified if itsMD5Has not been changed.

We will present how to wrap metaweblog API usingXmlrpclibOf 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 leastTitleAndDescriptionAttributes, 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 representedStruct. You can upload the blog to site without publish to others, so comesPublishParameterNewpostAndEditpost, ThisBoolValue Control whether the blog shocould 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 wrapNewpostMethods 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.