国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

把博客園的博客導出為MovableType的文本格式

2019-11-14 13:38:24
字體:
來源:轉載
供稿:網友

最近把以前的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.survivalescaperooms.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 VEVb        public string wp_slug;     // add by shenlb, for VEVb;    }

 

我們單位訪問互聯網要用到代理,還要密碼驗證,所以關鍵代碼得添加幾行:

MetaWeblogCnblogs blog = new MetaWeblogCnblogs();blog.Url = "http://www.survivalescaperooms.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操作,導入成功時會出現博客文章的列表。

3787-20151225081538812-115235259

其它

以前都是用blog_backup這個小程序來備份我的博客,現在發現自己寫的這個小程序可以備份得更為徹底,還可以稍微修改導出到wordpress。一番折騰,學到了這些知識點:movableType, metaweblog, xml-rpc, textile, pandoc, c# culture in date.ToString(), WebProxy……


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闽清县| 肇东市| 阳朔县| 玉林市| 台中市| 珲春市| 余庆县| 南靖县| 房山区| 潮安县| 措美县| 靖州| 师宗县| 利辛县| 军事| 辽源市| 云龙县| 玉环县| 泗阳县| 安岳县| 确山县| 云浮市| 洱源县| 聊城市| 济宁市| 乌什县| 隆化县| 古交市| 庆阳市| 岑溪市| 安义县| 寻乌县| 邛崃市| 曲麻莱县| 湘乡市| 天柱县| 咸丰县| 彰化县| 新宁县| 太和县| 邯郸市|