MSN博客搬家工具

 

1.    说明
最近MSN的博客即将关闭,而使用MSN上推荐的迁移到workpress,又总是不成功。在网上下了几个工具,都是各个blog开发的,只能搬到特定的blog中去。让人十分郁闷。
后来试写了一段代码,先把从MSN博客备份到本地,挑了些技术文档搬到了CSDN,下面是程序的实现和说明,大家举一反三吧。用此方法也可以搬到其它的blog
编译好的程序可以下载,源码可以下载,大家可以直接使用或者修改加以完善。

2.    原理
使用XML-RPC协议,一个XML-RPC消息就是一个请求体为xmlhttp-post请求,被调用的方法在服务器端执行并将执行结果以xml格式编码后返回,可以通过此协议,读写blog上的文章,如CSDNWORDPRESS,新浪等都支持,利用它可以方便地开发blog的客户端。

3.    程序说明

1)        MSN网页解析
public class Fetcher {
         private String getPage(String file) {      //
读取日志文件
                   BufferedReader reader = null;
                   StringBuilder sb = new StringBuilder();
                   String line = null;
                   try {
                            reader = new BufferedReader(new InputStreamReader(
                                               new FileInputStream((file)), Charset.forName("utf-8")));
                            while ((line = reader.readLine()) != null) {
                                     sb.append(line);
                            }
                            reader.close();
                   } catch (MalformedURLException e) {
                            System.out.println("file:" + file);
                            System.out.println("error:" + e);
                   } catch (IOException e) {
                            System.out.println("file:" + file);
                            System.out.println("error:" + e);
                   }
                   return sb.toString();
         }

//
解析日志文件:标题,日期,正文
         public void getDoc(String file, String server, String user, String passwd) {
                   String html = getPage(file);
                   String titleDivRegex = "<title>.+?</title>";
                   Pattern titleDivPattern = Pattern.compile(titleDivRegex);
                   Matcher titleDivMatcher = titleDivPattern.matcher(html);
                   String title = null;
                   if (titleDivMatcher.find()) { //
标题
                            title = titleDivMatcher.group().replaceAll("<title>", "")
                                               .replaceAll("</title>", "");
                   }
                   String dateDivRegex = "<h5 id=/".+?/">.+?</h5>";
                   Pattern dateDivPattern = Pattern.compile(dateDivRegex);
                   Matcher dateMatcher = dateDivPattern.matcher(html);
                   String dateStr = null;
                   Date postDate = null;
                   if (dateMatcher.find()) {      //      
日期
                            dateStr = dateMatcher.group().replaceAll("<h5 id=/".+?/">", "")
                                              .replaceAll("</h5>", "").trim();
                            String tmp = dateStr.replace("&#58;", ":");
                            postDate = new Date(tmp);
                   }
                   String textDivRegex = "<div id=/".*/" class=/"blogpost/">.+?</div>";
                   Pattern textDivPattern = Pattern.compile(textDivRegex);
                   Matcher textMatcher = textDivPattern.matcher(html);
                   String text = null;
                   if (textMatcher.find()) {       //
正文
                            text = textMatcher.group().replaceAll(
                                               "<div id=/".*/" class=/"blogpost/">", "").replaceAll(
                                               "</div>", "").trim();
                   }
                   String[] categories = { "android", "linux" };
                   text = "<html><meta http-equiv=/"Content-Type/" content=/"text/html; charset=utf-8/">"
                                     + text + "</html>";

                   Post post = new Post(title, text, categories, postDate);
                   post.setServer(server, user, passwd);
                   post.publish();

                   System.out.println("title:" + title);
                   System.out.println("date" + postDate);
         }
}

2)        上转到其它博客
public class Post {
         private Date dateCreated;
         private String description;
         private String title;
         private String[] categories;
         private String mServer;
         private String mUser;
         private String mPasswd;
         static private XmlRpcClientConfigImpl config;
         static private XmlRpcClient client;

         public Post(String title, String description, String[] categories,
                            Date dateCreated) {
                   this.dateCreated = dateCreated;
                   this.description = description;
                   this.title = title;
                   this.categories = categories;
         }

         static {
                   config = new XmlRpcClientConfigImpl();
                   client = new XmlRpcClient();
         }

         private void writelog(String log) {
                   UI.getInstance().showLog(log);
         }

         public void setServer(String server, String user, String passwd) {
                   mServer = server;
                   mUser = user;
                   mPasswd = passwd;
                   try {
                            config.setServerURL(new URL(mServer));
                            client.setConfig(config);
                   } catch (MalformedURLException e) {
                            System.out.println("connect error");
                   }
         }

         public void publish() {
                   Map<String, Object> struct = new HashMap<String, Object>();
                   struct.put("dateCreated", dateCreated);
                   struct.put("description", description);
                   struct.put("title", title);
                   struct.put("categories", categories);
                   Object[] params = new Object[] { mUser, mUser, mPasswd, struct, true };
                   String blogid = null;
                   try {           //
发布日志
                            blogid = (String) client.execute("metaWeblog.newPost", params);
                            writelog("OK:  title=" + title + " id=" + blogid + "/n");
                   } catch (XmlRpcException e) {
                            writelog("ERR: title=" + title + "/n");
                   }
                   struct.clear();
         }
}

4.    使用方法

1)        下载代码

a)         从此处下载
http://download.csdn.net/source/2773467
(注意:本程序只测试过迁移到CSDN,迁移到其它Blog可能需要修改代码)

b)        将下载的软件包解压缩

2)        运行程序

a)         msn blog日期格式以便于程序识别

                                      i.              打开msn blog,并登录

                                    ii.              选项->常规
将日期设置为yyyy/mm/dd格式
将时间设置为hh:mm:ss格式

b)        msn blog日志保存到本地

                                      i.              msn博客登录后,在迁移页面点击将日志下载到PC”

                                    ii.              解包,依据index把需要迁移的日志放入目录X

c)         上传到其它blog

                                      i.              在其它blog注册用户

                                    ii.              cd blogmover

                                  iii.              java -jar blogmover.jar
点击Choose选择迁移日志所存在的目录
X
Server,UserID, Passwd中填写新blog的信息,然后点击Send

3)        编译源码

a)         eclipse打开

b)        File->New->JavaProject->Create project from existing source打开源码

c)         项目名->右键->Build Path->Configure Build Path…->Add Extennal JARs加入软件包一级目录的三个库(wsxxx, xmlxxx, xmlxxx)

d)        编译运行即可

5.    参考

1)        从百度空间到CSDN——博客搬家源码
http://blog.csdn.net/telnetor/archive/2010/05/04/5556539.aspx

2)        各个blogxml-rpc支持(MetaWeblog API)
http://www.discuz.net/thread-946562-1-1.html

 

你可能感兴趣的:(server,String,File,Blog,null,工具)