MSN博客搬家工具
|Word count:1.2k|Reading time:5min|Post View:
MSN 博客搬家工具
#工具 #编程语言/Java
1. 说明
最近 MSN 的博客即将关闭,而使用 MSN 上推荐的迁移到
workpress,又总是不成功。在网上下了几个工具,都是各个 blog
开发的,只能搬到特定的 blog 中去。让人十分郁闷。
后来试写了一段代码,先把从 MSN 博客备份到本地,挑了些技术文档搬到了
CSDN
,下面是程序的实现和说明,大家举一反三吧。用此方法也可以搬到其它的
blog。
编译好的程序可以下载,源码可以下载,大家可以直接使用或者修改加以完善。
2. 原理
使用 XML-RPC 协议,一个 XML-RPC 消息就是一个请求体为 xml 的
http-post
请求,被调用的方法在服务器端执行并将执行结果以 xml
格式编码后返回,可以通过此协议,读写 blog 上的文章,如 CSDN,
WORDPRESS,新浪等都支持,利用它可以方便地开发 blog 的客户端。
3. 程序说明
- MSN 网页解析
| 12
 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
 
 | ** 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(":", ":");
 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. 使用方法
- 下载代码
- 从此处下载
 http://download.csdn.net/source/2773467
 (注意:本程序只测试过迁移到 CSDN,迁移到其它 Blog
可能需要修改代码)
 
- 将下载的软件包解压缩 
- 运行程序
- 改 msn blog 日期格式以便于程序识别
- 打开 msn blog,并登录 
- 选项 - > 常规
 将日期设置为 yyyy/mm/dd 格式
 将时间设置为 hh:mm:ss 格式
 
- 将 msn blog 日志保存到本地
- msn 博客登录后,在迁移页面点击” 将日志下载到 PC” 
- 解包,依据 index 把需要迁移的日志放入目录 X 
- 上传到其它 blog
- 在其它 blog 注册用户 
- cd blogmover 
- java -jar blogmover.jar
 点击 Choose 选择迁移日志所存在的目录 X
 在 Server,UserID, Passwd 中填写新 blog 的信息,然后点击 Send
 
- 编译源码
- 用 eclipse 打开 
- File->New->JavaProject->Create project from existing
source 打开源码 
- 项目名 -> 右键 ->Build Path->Configure Build
Path…->Add Extennal JARs 
加入软件包一级目录的三个库 (wsxxx, xmlxxx, xmlxxx)
- 编译运行即可
5. 参考
- 从百度空间到 CSDN——博客搬家源码
 [http://blog.csdn.net/telnetor/archive/2010/05/04/5556539.aspx
](http://blog.csdn.net/telnetor/archive/2010/05/04/5556539.aspx)
- 各个 blog 的 xml-rpc 支持 (MetaWeblog API)
 [http://www.discuz.net/thread-946562-1-1.html
](http://www.discuz.net/thread-946562-1-1.html)