MetaWeblog API 在 Subtext中應用。
當你讀我的這篇文章的時候,我正用著Windows Live Writer在編輯著文章,並且通過它來發表這篇文章,可以實現這個功能,正因為有MetaWeblog API。
MetaWeblog API定義
MetaWeblog API是一個程序接口,遵循XMLRPC通訊協議,它允許外部程序得到和設置文章的內容和屬性。
順便說句,MetaWeblog API 是Blogger API接口的改進,後者只能得到文章的內容。
ps:關於XMLRPC的相關內容,將在下篇文章給出。
下面結合Subtext中的代碼來看。
public interface IMetaWeblog
...{
[XMLRpcMethod("metaWeblog.editPost",Description="Updates and existing post to a designated blog "
+ "using the metaWeblog API. Returns true if completed.")]
bool editPost(
string postid,
string username,
string passWord,
Post post,
bool publish);
[XMLRpcMethod("metaWeblog.getCategorIEs",
Description="Retrieves a list of valid categorIEs for a post "
+ "using the metaWeblog API. Returns the metaWeblog categorIEs "
+ "struct collection.")]
CategoryInfo[] getCategorIEs(
string blogid,
string username,
string passWord);
[XMLRpcMethod("metaWeblog.getPost",
Description="RetrIEves an existing post using the metaWeblog "
+ "API. Returns the metaWeblog struct.")]
Post getPost(
string postid,
string username,
string passWord);
[XMLRpcMethod("metaWeblog.getRecentPosts",
Description="RetrIEves a list of the most recent existing post "
+ "using the metaWeblog API. Returns the metaWeblog struct collection.")]
Post[] getRecentPosts(
string blogid,
string username,
string passWord,
int numberOfPosts);
[XMLRpcMethod("metaWeblog.newPost",
Description="Makes a new post to a designated blog using the "
+ "metaWeblog API. Returns postid as a string.")]
string newPost(
string blogid,
string username,
string passWord,
Post post,
bool publish);
[XMLRpcMethod("metaWeblog.newMediaObject",
Description = "Uploads an image, movIE, song, or other media "
+ "using the metaWeblog API. Returns the metaObject struct.")]
mediaObjectInfo newMediaObject(object blogid, string username, string passWord, mediaObject mediaobject);
BloggerAPI Members#region BloggerAPI Members
[XMLRpcMethod("blogger.deletePost",
Description="Deletes a post.")]
[return: XMLRpcReturnValue(Description="Always returns true.")]
bool deletePost(
string appKey,
string postid,
string username,
string passWord,
[XMLRpcParameter(
Description="Where applicable, this specifIEs whether the blog "
+ "should be republished after the post has been deleted.")]
bool publish);
[XMLRpcMethod("blogger.getUsersBlogs",
Description="Returns information on all the blogs a given user "
+ "is a member.")]
BlogInfo[] getUsersBlogs(
string appKey,
string username,
string passWord);
#endregion
}
先不必理會XMLRpcMethod之類的,簡單理解成調用方法。可以看到IMetaWeblog接口中定義了這麼幾個方法:newPost,getPost,editPost,getCategorIEs,getRecentPosts,另外還有BloggerAPI中的兩個方法:deletePost,getUsersBlogs。
在newPost,getPost裡面有個Post,根據MeteWeblog API的RFC說明,它是一個Struct結構體。同樣mediaObjectInfo,mediaObject,CategoryInfo,BlogInfo,Enclosure,Source等都是結構體,以Post舉例說明。
Post結構體的定義:
[XMLRpcMissingMapping(MappingAction.Ignore)]
public struct Post
...{
[XMLRpcMissingMapping(MappingAction.Error)]
[XMLRpcMember(Description="Required when posting.")]
public DateTime dateCreated;
[XMLRpcMissingMapping(MappingAction.Error)]
[XMLRpcMember(Description="Required when posting.")]
public string description;
[XMLRpcMissingMapping(MappingAction.Error)]
[XMLRpcMember(Description="Required when posting.")]
public string title;
[XMLRpcMember("categories", Description="Contains categorIEs for the post.")]
public string[] categorIEs;
public Enclosure enclosure;
public string link;
public string permalink;
[XMLRpcMember(
Description="Not required when posting. Depending on server may "
+ "be either string or integer. "
+ "Use Convert.ToInt32(postid) to treat as integer or "
+ "Convert.ToString(postid) to treat as string")]
public object postid;
public Source source;
public string userid;
}
其中創建日期dateCreated、描述description、標題title三個字段是必須的。
另外還想說一點就是newMediaObject這個方法也很重要,對於處理上傳圖片、視頻等媒體文件是個關鍵。通過更改它就可以設置相關屬性,如上傳圖片、視頻位置的選擇等。