把博客园的博客导出为MovableType的文本格式
最近把以前的wordpress建的博客删了,用textpattern重新建了一个独立博客,http://www.shenlongbin.com,可以把它当成博客园上内容的备份,但导入我以前的一大堆文章非常有难度。经过一番折腾,终于把博客园的内容导出为文本,再导入到textpattern中了。
第一步:C#编程把博客园内容读取出来
博客园的管理端提供了博客备份功能,可以生成一个xml文件,但只能备份博客的主要内容,并不包含博客的摘要信息和关键词信息,需要用metaweblogAPI进行访问才能获得详细的信息。快速学习了metaweblog编程知识,内部采用了XML-RPC调用,从网上搜索相关资料和类库。很多地方都引用了XML-RPC.NET项目的类库(名字空间以CookComputing开头),并修改了相关代码,可惜下载网址被伟大的墙挡住了,通过VPN才好不容易把xml-rpc.net.2.5.0.zip(.NET framework 2.0)下载下来。原始地址在这里:http://xmlrpcnet.googlecode.com/files/xml-rpc.net.2.5.0.zip。大CC有一篇文章介绍了metaweblogAPI,另外这篇文章介绍了调用方法,只需要稍微修改一点,就可以获取博客园上的博客内容了。
博客园的metaweblog的访问接口可以访问:http://www.cnblogs.com/speeding/services/metaweblog.aspx,从而获得详细的描述信息。实际上在xml-rpc.net2.5.0压缩包中的interfaces/MetaWeblogAPI.cs文件中可以找到主要类或结构的定义,稍微添加或修改即可。
[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 object mt_allow_comments; public object mt_allow_pings; public object mt_convert_breaks; public string mt_text_more; public string mt_excerpt; public string mt_keywords; // add by shenlb, for cnblogs public string wp_slug; // add by shenlb, for cnblogs; }
我们单位访问互联网要用到代理,还要密码验证,所以关键代码得添加几行:
MetaWeblogCnblogs blog = new MetaWeblogCnblogs(); blog.Url = "http://www.cnblogs.com/speeding/services/metaweblog.aspx"; Uri proxyURI = new Uri("http://myproxyhost.myproxydomain.com:80"); System.Net.WebProxy proxyObject = new System.Net.WebProxy(proxyURI, false); proxyObject.Credentials = new System.Net.NetworkCredential("proxy_username", "proxy_password"); blog.Proxy = proxyObject; Post[] posts = blog.getRecentPosts("speeding", "speeding", "my_blog_admin_password", 50);
这样就可以获得博客内容了,但需要再调用getPost才能获得详细的信息,这里就可以看到摘要和关键词了。
Post detail = blog.getPost(post.postid.ToString(), "speeding", "my_blog_admin_password");
第二步:利用pandoc把html内容转换为textile标记
Post中的description中都是html标记,而textpattern默认的标记语言是textile,所以需要将其转换为textile,关于textile标记的百科知识见这里。
这里要用到著名的pandoc了,这个神奇的工具竟然是用haskell写成了,以前学习haskell语言的时候以为只是一种教学语言,真有人写出了实用程序!
把博客中内容写入temp.html文件中,再用下面的命令行就可以转换了。
pandoc.exe -t textile -o textile.txt temp.html
主要代码:
ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; startInfo.FileName = "..\\..\\pandoc.exe"; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.Arguments = "-t textile -o textile.txt temp.html"; using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } return File.ReadAllText("textile.txt");
第三步:生成import.txt文件
对几百篇博客文章循环处理,追加到import.txt文件即可,最后的文件用UTF8保存。
Movable Type博客的文本格式说明文档可以看这里: https://movabletype.org/documentation/appendices/import-export-format.html#example
简单说明一下:文件用UTF8编码存储,前面几行是单行文本信息,后面的BODY、KEYWORDS和EXCERPT是多行文本,多行文本需要用5个短横分开,每篇文章用8个短横分开,最简单的一个例子:
TITLE: A dummy title
AUTHOR: shenlongbin
DATE: 01/31/2012 03:31:05 PM
PRIMARY CATEGORY: reading
CATEGORY: reading
-----
BODY:
This is the body.
Another paragraph here.
Another paragraph here.
-----
EXCERPT:
See, this entry does not have an extended piece; but it does have an excerpt. It is special.
-----
--------
里面的日期格式有要格要求,关键代码:
IFormatProvider culture = new CultureInfo("en-US", true);
string date = post.dateCreated.ToString("dd/MM/yyyy hh:mm:ss tt", culture); // 08/05/2002 04:05:23 PM
第四步:上传import.txt文件,导入
文件必须放在public_html/textpattern/include/import
目录下,并且文件名一定是
import.txt。
实际上textpattern的管理端可以导入Movable Type(File/MySQL)、Blogger、b2、WordPress等格式的博客,但支持文本文件导入的只有Movable Type和Blogger。
在textpattern的管理界面上执行import操作,导入成功时会出现博客文章的列表。
其它
以前都是用blog_backup这个小程序来备份我的博客,现在发现自己写的这个小程序可以备份得更为彻底,还可以稍微修改导出到wordpress。一番折腾,学到了这些知识点:movableType, metaweblog, xml-rpc, textile, pandoc, c# culture in date.ToString(), WebProxy……
----==== Email: slofslb (GTD) qq.com 请将(GTD)换成@ ====----
版权声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证)
作者:申龙斌的程序人生
---- 魔方、桥牌、象棋、游戏人生...
---- BASIC、C++、JAVA、C#、Haskell、Objective-C、Open Inventor、程序人生...
---- GTD伴我实现人生目标
---- 区块链生存训练
---- 用欧拉计划学Rust编程
---- 申龙斌的读书笔记(2011-2019)
----

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构