好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

metawebblog api

metawebblog api  是一个博客标准,这样我们就可以通过实现这个标准来实现

我们自己的博客支持word2007,windows liver writer来写博客.

metablog的.net接口如下.

using System;

using System.Collections.Generic;

using System.Text;

using CookComputing.XmlRpc;


namespace blog

{

    #region Structs

    public struct BlogInfo

    {

        public string blogid;

        public string url;

        public string blogName;

    }

    public struct Category

    {

        public string categoryId;

        public string categoryName;

    }

    [Serializable]

    public struct CategoryInfo

    {

        public string description;

        public string htmlUrl;

        public string rssUrl;

        public string title;

        public string categoryid;

    }

    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct Enclosure

    {

        public int length;

        public string type;

        public string url;

    }


    [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;


        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;


        //public string mt_excerpt;

    }


    [XmlRpcMissingMapping(MappingAction.Ignore)]

    public struct Source

    {

        public string name;

        public string url;

    }


    public struct UserInfo

    {

        public string userid;

        public string firstname;

        public string lastname;

        public string nickname;

        public string email;

        public string url;

    }


    public struct MediaObjectUrl

        {

            public string url;

        }


    public struct MediaObject

        {

            public string name;

            public string type;

            public byte[] bits;

        }

    #endregion

    public interface IMetaWeblog

    {

        #region MetaWeblog API


        [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.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.getPost",

             Description = "Retrieves an existing post using the MetaWeblog "

             + "API. Returns the MetaWeblog struct.")]

        Post getPost(

            string postid,

            string username,

            string password);


        [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.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.newMediaObject", Description = "Add a media object to a post using the metaWeblog API. Returns media url as a string.")]

        MediaObjectUrl newMediaObject(string blogid, string username, string password, MediaObject mediaObject);


        #endregion


        #region BloggerAPI


        [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);


        [XmlRpcMethod("blogger.getUserInfo",

             Description = "Returns information about the given user.")]

        UserInfo getUserInfo(

            string appKey,

            string username,

            string password);


        #endregion


    }


}


 


我们再实现这个接口:


using System;

using System.Collections.Generic;

using System.Text;

using System.Collections;

using System.Data;

using System.IO;

using individual;

using CookComputing.XmlRpc;

using article;

using System.Web;


namespace blog

{

    public class MetaWeblog : XmlRpcService, IMetaWeblog

    {


        #region IMetaWeblog 成员


        public string newPost(string blogid, string username, string password, Post post, bool publish)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");


            ArticleCategory cat = new ArticleCategory();

            DataSet ds= cat.GetAllDSByBlogId(int.Parse(blogid));


            DataTable dt = ds.Tables[0];

            DataRowCollection drs = ds.Tables[0].Rows;


            Article article = new Article();

            ArticleModel model = new ArticleModel();

            model.AddTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

            model.BlogId = int.Parse(blogid);

            model.Contents = post.description;

            model.CanRemark = 1;

            model.Subject = post.title;

            model.State = (publish) ? 1 : 2;


            if (post.categories.Length > 0)

            {

                string catName = post.categories[0];

                DataRow[] aryDr = dt.Select("name='" + catName +"'");

                model.CategoryId = int.Parse(aryDr[0]["categoryId"].ToString());

            }

            else

            {

                model.CategoryId = int.Parse(drs[0]["categoryId"].ToString());

            }


            article.Model = model;

            long postId = article.Add();


            if (postId > 0)

                return postId.ToString();

            else

                throw new XmlRpcFaultException(0, "发表文章不成功");


        }


        public bool editPost(string postid, string username, string password, Post post, bool publish)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");

            try

            {

                Article article = new Article();

                int articleId = int.Parse(postid);

                ArticleModel model = (ArticleModel)article.GetById(articleId);


                model.AddTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");

                model.Contents = post.description;

                model.Subject = post.title;

                model.State = (publish) ? 1 : 2;

                if (post.categories.Length > 0)

                {

                    ArticleCategory cat = new ArticleCategory();

                    DataSet ds = cat.GetAllDSByBlogId(model.BlogId);

                    DataTable dt = ds.Tables[0];

                    string catName = post.categories[0];

                    DataRow[] aryDr = dt.Select("name='" + catName +"'");

                    model.CategoryId = int.Parse(aryDr[0]["categoryId"].ToString());

                }

                article.Model = model;

                article.Update();

                return true;

            }

            catch (Exception ex)

            {

                throw new XmlRpcFaultException(0, "更新文章失败");

            }

        }


        public Post getPost(string postid, string username, string password)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");

            Article article = new Article();

            int articleId = int.Parse(postid);

            ArticleModel model = (ArticleModel)article.GetById(articleId);


            Post post = ConvertModelToPost(model);

            return post;

        }


        public CategoryInfo[] getCategories(string blogid, string username, string password)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");

            ArticleCategory cat = new ArticleCategory();

            int blogId=int.Parse(blogid);

            DataSet ds= cat.GetAllDSByBlogId(blogId);

            DataRowCollection drs = ds.Tables[0].Rows;

            ArrayList al = new ArrayList(drs.Count);

            foreach (DataRow dr in drs)

            {

                CategoryInfo catInfo = new CategoryInfo();

                catInfo.categoryid = dr["CategoryId"].ToString();

                catInfo.title = dr["name"].ToString();

                catInfo.description = "";

                catInfo.htmlUrl = "";

                catInfo.rssUrl = "";

                al.Add(catInfo);

            }

            return al.ToArray(typeof(CategoryInfo)) as CategoryInfo[];

        }


        public Post[] getRecentPosts(string blogid, string username, string password, int numberOfPosts)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");


            int blogId=int.Parse(blogid);


            Article article = new Article();

            DataSet ds= article.GetAllByBlogId(blogId, 1, numberOfPosts).PageDataSet;


            DataRowCollection drs=ds.Tables[0].Rows;


            ArrayList al = new ArrayList(drs.Count);


            foreach (DataRow dr in drs)

            {

                Post post = new Post();

                post.postid = dr["articleId"].ToString();

                post.title = dr["subject"].ToString();

                post.description = dr["Contents"].ToString();

                post.categories = new string[] { dr["CategoryId"].ToString()};

                al.Add(post);

            }


            return (Post[])al.ToArray(typeof(Post));

        }


//实现文件上传


        public MediaObjectUrl newMediaObject(string blogid, string username, string password, MediaObject mediaObject)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");


            Blog blog = new Blog();

            BlogModel model = (BlogModel)blog.GetById(int.Parse(blogid));


            string filename =  HttpContext.Current.Server.MapPath("/data/" + model.UserId + "/" + mediaObject.name);

            if (!Directory.Exists(Path.GetDirectoryName(filename)))

                Directory.CreateDirectory(Path.GetDirectoryName(filename));

            File.WriteAllBytes(filename, mediaObject.bits);

            MediaObjectUrl mediaObjectUrl = new MediaObjectUrl();

            mediaObjectUrl.url = "http://博客地址/data/" + model.UserId + "/" + mediaObject.name;

            return mediaObjectUrl;

        }


        public bool deletePost(string appKey, string postid, string username, string password, bool publish)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");

            Article article = new Article();

            article.Delete(int.Parse(postid));

            return true;

        }


        public BlogInfo[] getUsersBlogs(string appKey, string username, string password)

        {

            if (!ValidateUser(username, password)) throw new XmlRpcFaultException(0, "用户不存在");

            Blog blog = new Blog();


            BlogModel model =(BlogModel) blog.GetByNickname(username);

            BlogInfo bloginfo = new BlogInfo();

            bloginfo.blogid = model.BlogId.ToString();

            bloginfo.blogName = username;

            bloginfo.url = http://博客地址/ + username;

            return new BlogInfo[] { bloginfo };

        }

        public UserInfo getUserInfo(string appKey, string username, string password)

        {

            if(!ValidateUser(username,password)) throw new XmlRpcFaultException(0, "用户不存在");

            Individual indi = new Individual();

            IndividualModel model= indi.GetByNickName(username);

            UserInfo userInfo = new UserInfo();

            userInfo.nickname = model.Nickname;

            userInfo.email = model.Account;

            userInfo.userid = model.UserId.ToString();

            return userInfo;

        }


        private Post ConvertModelToPost(ArticleModel model)

        {

            Post post = new Post();

            post.postid = model.ArticleId.ToString();

            post.title = model.Subject;

            post.description = model.Contents;

            post.categories = new string[] { model.CategoryId.ToString() };

            return post;

        }


        private bool ValidateUser(string username, string password)

        {

            try

            {

                bool isLogin = Individual.LoginNickname(username, password);

                return isLogin;

            }

            catch (Exception ex)

            {

                return false;

            }

        }

        #endregion

    }

}

这样就实现了博客支持客户端写作了

查看更多关于metawebblog api的详细内容...

  阅读:32次

上一篇: 各地地铁

下一篇:今天拆窗进去把门开开