Conveyorを使ってBloggerからはてなダイアリーへ移行する

はてなダイアリーでは、はてなの日記データ形式Movable Type形式のデータを「hisabohさんの日記 > 管理ツールトップ > インポート/エクスポート」からインポートすることができる*1

ただ、Bloggerからははてなの日記データ形式はもちろん、Movable Type形式でもデータをエクスポートすることができない。そこで、今回はConveyorを使って、Bloggerrssからはてなの日記データ形式に変換することにした。

Conveyorのインストール

Conveyorのサイト参照。

Bloggerrss

atomではなく、rss形式の方を利用。conveyorのFeedIn workerがcategoryデータをattributeではなくvalueとして取ってくる仕様となっていたため。
なお、今回の方法では元のエントリに複数のタグが張ってあってもそのうちの1つだけしか移行できない。

Bloggerrsshttp://yher2.blogspot.com/posts/default?alt=rss&max-result=999から取得。max-resultが肝で、これを指定することで全てのエントリ分のrssを取得することができた。

rssはてな日記形式へ変換するworker

こんな感じ。
これを%CONVEYOR_HOME%/library/workers/PublishHatenaDiary/PublishHatenaDiary.phpとして保存すればよい。
あとは、上記のrssをFeedIn workerで取得し、PublishHatenaDiary workerに渡すようにConveyorのラインを作ればよい。
ちなみに、ソースを見れば分かるが、元のエントリではコード系のものを<code>タグで囲っていたため、<code>タグははてなスーパーpre記法に変換、<blockquote>タグは引用記法youtubehttp記法に変換している。

<?php
Rhaco::import("model.PublishBase");
Rhaco::import("io.FileUtil");
/**
 *
 * PublishText
 * @author hisaboh
 * 
 * @license New BSD License
 * @copyright Copyright 2006- The Rhacophorus Project. All rights reserved.
 */
class PublishHatenaDiary extends PublishBase{
    
    function execute($rss)
    {
    	$filer = new FileUtil();
    	$channel = $rss->getChannel();
        $items = $rss->getItem();
        $source = '<?xml version="1.0" encoding="UTF-8"?>'."\n<diary>\n";
        foreach ($items as $item) {
			$category = $item->getCategory();
			if ($category) {
				$category = "[".$category."]";
			}
	
			$source = $source.'<day date="'.date("Y-m-d", strtotime($item->getPubDate())).'" title="">';
			$source = $source."<body>\n";
			$source = $source."*".$category.$item->getTitle();
			$source = $source.TemplateFormatter::getCdata($item->getDescription());

			$source = $source."</body>\n</day>\n";
			$source = str_replace("&lt;br /&gt;", "\n", $source);
			$source = str_replace("&lt;code&gt;", "\n>|?|\n", $source);
			$source = str_replace("&lt;/code&gt;", "\n||<\n", $source);
			$source = str_replace("&lt;blockquote&gt;", "\n>>\n", $source);
			$source = str_replace("&lt;/blockquote&gt;", "\n<<\n", $source);
			$source = ereg_replace('&lt;object.*http://www.youtube.com/v/([^"]+).*object&gt;', "[http://www.youtube.com/watch?v=\\1:movie]", $source);

        }
		$source = $source."</diary>\n";
        if($this->variable("append")) {
	        $filer->append(FileUtil::path(Rhaco::constant("PUBLISH_PATH"),$this->variable("filename")),$source,$this->variable("encode"));
        }else{
	        $filer->write(FileUtil::path(Rhaco::constant("PUBLISH_PATH"),$this->variable("filename")),$source,$this->variable("encode"));
        }
        return $rss;
    }
        
    function description() {
    	return "はてなダイアリー形式のファイルを出力する";
    }

	function config()
	{
		return array(
		"filename"=>array("ファイル名","text","rss",true),
		"encode"=>array("エンコード","select",array("UTF-8"=>"UTF-8","EUC-JP"=>"EUC-JP","SJIS"=>"Shift JIS","JIS"=>"JIS")),
		"append"=>array("書き込み方法","select",array(0=>"上書き",1=>"追記"))
		);
	}

	function rhacover() {
		return "1.3.0";
	}
}
?>

*1:MT形式の方は試してないので嘘かも