Note: there is a newer version available here.
While upgrading and consolidating some older stuff to WP 2.5.1 I needed a .Net wrapper to the metaWebLog API of WordPress. The XML-RPC.Net library helps you a long way in achieving this but the last bits are somewhat tricky and each new version of WP is altering things. So, I cooked some code together which you can find below.
The API is self-descriptive but the novel thing is the possibility to upload media straight to your blog, like the following:
WP.Publisher api = new WP.Publisher("admin", "Unknown"); WP.MediaObject media = new WP25API.MediaObject(); media.type = "jpg"; media.name = "test.jpg"; media.bits = WP.Publisher.ConvertImageToByteArray(new Bitmap(@"C:\temp\SomeImage.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg); WP.MediaObjectInfo info= api.AddMedia(media); |
Easy. Would in fact allow one to use the blog as a file repository (I noticed my host moved the max to 380GB recently).
using System; using System.Collections.Generic; using System.Linq; using System.Text; using CookComputing.XmlRpc; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace WP25API { public class Publisher { #region Properties public string UserName = "admin"; public string Password = "Unknown"; #endregion #region Constructor ///<summary> ///Default constructor ///</summary> public Publisher(string user, string pass) { UserName = user; Password = pass; } #endregion #region Methods public static byte[] ConvertImageToByteArray(Image imageToConvert, ImageFormat formatOfImage) { byte[] Ret; try { using (MemoryStream ms = new MemoryStream()) { imageToConvert.Save(ms, formatOfImage); Ret = ms.ToArray(); } } catch (Exception) { throw; } return Ret; } public Post GetPost(int postid) { IWP proxy = XmlRpcProxyGen.Create<iwp>(); string[] args = new string[] { postid.ToString(), UserName, Password }; return proxy.GetPost(args); } public List<post> GetRecentPost(int amount) { IWP proxy = XmlRpcProxyGen.Create<iwp>(); string[] args = new string[] { "0", UserName, Password, amount.ToString() }; Post[] posts = proxy.GetRecentPosts(args); if (posts == null) return null; else { return posts.ToList<post>(); } } public string AddPost(Post post, bool publish) { IWP proxy = XmlRpcProxyGen.Create<iwp>(); object[] args = new object[] { "0", UserName, Password, post, publish }; return proxy.AddPost(args); } public bool UpdatePost(string postid, Post post, bool publish) { IWP proxy = XmlRpcProxyGen.Create</iwp><iwp>(); object[] args = new object[] { postid, UserName, Password, post, publish }; return proxy.UpdatePost(args); } public List<categoryinfo> GetCategories() { IWP proxy = XmlRpcProxyGen.Create<iwp>(); string[] args = new string[] { "0", UserName, Password }; CategoryInfo[] ret = proxy.GetCategories(args); if (ret == null) return null; else { return ret.ToList<categoryinfo>(); } } public MediaObjectInfo AddMedia(MediaObject mediaObject) { IWP proxy = XmlRpcProxyGen.Create<iwp>(); object[] args = new object[] { "0", UserName, Password, mediaObject }; return proxy.NewMediaObject(args); } #endregion } [XmlRpcUrl("http://www.whatever.net/xmlrpc.php")] public interface IWP : IXmlRpcProxy { #region MetaWeblog API [XmlRpcMethod("metaWeblog.newPost")] string AddPost(object[] args); [XmlRpcMethod("metaWeblog.editPost")] bool UpdatePost(object[] args); [XmlRpcMethod("metaWeblog.getPost")] Post GetPost(string[] args); [XmlRpcMethod("metaWeblog.getCategories")] CategoryInfo[] GetCategories(string[] args); [XmlRpcMethod("metaWeblog.getRecentPosts")] Post[] GetRecentPosts(string[] args); [XmlRpcMethod("metaWeblog.newMediaObject")] MediaObjectInfo NewMediaObject(object[] args); #endregion } } </iwp></categoryinfo></iwp></categoryinfo></iwp></post></iwp></post></iwp> |





Ouch, notice that the codebox doesn’t like the generic stuff in C#, watch out for some obvious quirks.
Great idear XML-RPC and .Net as a API
Thanks
can you give this project?
See my latest posting.