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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
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.
By Me May 20, 2008 - 11:14 amGreat idear XML-RPC and .Net as a API
By Silverlight Travel December 9, 2008 - 9:51 amThanks
can you give this project?
By Yaroslav December 25, 2008 - 12:41 pmSee my latest posting.
By Francois Vanderseypen December 26, 2008 - 8:10 pm