WordPress 3.1 の新しい XMLRPC API

RC3 まできてもうすぐ出ると思われる WordPress 3.1!。

つらつら changelog をみていますと、XMLRPC の API にメディアライブラリ系の API が追加になっていましたので、ここ hiromasa.another の 3.1 テストもかねまして遊んでみました。 :)。

新しく追加になったのは、wp.getMediaLibrary、wp.getMediaItem、wp.getPostFormats で前2つはメディアライブラリに関するもの、最後は投稿フォーマットに関するものです。

というわけで、せっかくですので練習もかねて Groovy  からレッツアクセス。 😀

import groovy.net.xmlrpc.*
 
class WPXMLRPCProxy {
    def proxy
    def user
    def passwd
    def currentArgs
    
    def methods = [
        getMediaLibrary :
            { [blog_id, this.@user, this.@passwd, struct]},
        getMediaItem :
            { [blog_id, this.@user, this.@passwd, attachment_id] },
        getPostFormats :
            { [blog_id, this.@user, this.@passwd] }
    ]
    
    def invokeMethod(String method, Object args) {
        currentArgs = args[0]
        def wpargs = methods[(method)]()
        method = "wp." + method
        proxy."$method"(wpargs)
    }
    
    def getProperty(String name) { currentArgs[name] }
}
 
def wp = new WPXMLRPCProxy(
    proxy : new XMLRPCServerProxy("https://another.maple4ever.net/xmlrpc.php")
    , user : ""
    , passwd : "")
 
wp.getMediaLibrary(blog_id:0
    , struct:[number: 1, mime_type:"image/jpeg"]).each {
    println it
}
 
println wp.getMediaItem(blog_id:0, attachment_id: 1394).metadata
 
println wp.getPostFormats(blog_id:0)

 

わざわざこんなことしなくてもいいかもですが、invokeMethod と getProperty の練習がてら Groovy に備わるリフレクションの仕組みでメソッドとプロパティを動的に生成して WordPress の API と同じ形で呼べるようにしています。

フィールドの methods マップにメソッド名と引数クロージャを追加していけば、wp.* 系の API がそのまま呼べるはずです。(今は新 API だけ入れています)

実行!

wp.getMediaLibrary

[title:DVC00169.jpg, thumbnail:https://another.maple4ever.net/wp-content/uploads/2011/01/DVC00169-150x150.jpg, date_created_gmt:Sun Jan 23 09:33:11 JST 2011, description:, link:https://another.maple4ever.net/wp-content/uploads/2011/01/DVC00169.jpg, parent:1396, caption:, metadata:[sizes:[thumbnail:[height:150, file:DVC00169-150x150.jpg, width:150], medium:[height:225, file:DVC00169-300x225.jpg, width:300]], height:308, file:2011/01/DVC00169.jpg, width:410, hwstring_small:height='96' width='128', image_meta:[focal_length:0, title:DVC00169, shutter_speed:0, iso:0, camera:SH906i, created_timestamp:1295293049, caption:copy="NO" SH906i, copyright:, credit:, aperture:0]]]

wp.getMediaItem

[sizes:[thumbnail:[height:150, file:DVC00169-150x150.jpg, width:150], medium:[height:225, file:DVC00169-300x225.jpg, width:300]], height:308, file:2011/01/DVC00169.jpg, width:410, hwstring_small:height='96' width='128', image_meta:[focal_length:0, title:DVC00169, shutter_speed:0, iso:0, camera:SH906i, created_timestamp:1295293049, caption:copy="NO" SH906i, copyright:, credit:, aperture:0]]

wp.getPostFormats

audio:Audio, standard:Standard, status:Status, gallery:Gallery, quote:Quote, link:Link, image:Image, chat:Chat, aside:Aside, video:Video

なーるほどざ、にゃんこ。 🙂

あとはちょいちょいプログラムかけば、条件指定してクライアントアプリから画像ひっぱるとか簡単ですね。

xmlrpc01 

よいよい、簡単。 これは swing ですが次は SWT バインディングに挑戦してみたいです。 😀

コメントを残す