d3pipes はてなコメントパーサ

「ジョイントを作ってみよう(2)」を読んで,しばらく「何に使えばいいんかなあ」といい利用法が思い浮かばなかったのですが,ありました。ブログのコメントを集めたいんでした。

ブログ本文はどこも RSS を出しているので GoogleLivedoor のリーダなどで読めるのですが,コメントまで RSS で提供しているところは少なく,自分がどのブログに発言したのか忘れがちなので,これで少しでも思い出せればいいなあと(はてなのコメント一覧は情報が貧弱なので思いだせんかも(^ ^;)。

またコメント一覧の出力自体はブログアプリケーションで出しているので d3pipes で取得するにはうってつけです。


ふだん正規表現は避けてきたので,基本的なところで躓いていましたがなんとかできました。

やっつけ仕事で言語ファイルはつかっていません(^ ^;。

<?php

#Hatena Comment Parser for d3pipes
#HIKAWA kilica	2007-06-16
#http://d.hatena.ne.jp/kilica or http://www.trpg-labo.com/


require_once dirname(dirname(__FILE__)).'/D3pipesParseAbstract.class.php' ;

class D3pipesParseHatenacomment extends D3pipesParseAbstract {

	function execute($html_source, $max_entries='')
	{
		
		$item = array();
		preg_match('#\<ul class=\"hatena-recentcomment\"\>(.*)?\</ul\>#siU', $html_source, $comment);
		if(! $comment){
			$this->errors[] = 'NO recentcomment div class in this page, or invalid pattern'
			return array();
		}

		preg_match_all('#\<li\>.*href=\"([^"]+)\"\>(.*)\</a\>&nbsp;(.*)?\</li\>#iU', $comment[1], $matches, PREG_SET_ORDER);
		if(! $matches){
			$this->errors[] = 'Invalid pattern for this Parser';
		}
		foreach($matches as $match){
			$link = "http://d.hatena.ne.jp" .$match[1];
			$headline = $match[2]. ' Hatena Diary (commented by ' .$match[3]. ')';
			preg_match('/.*#c([0-9]+)/', $match[1], $pub_unixtime);
			$pubtime = $pub_unixtime[1];
			
			$items[] = array(
				'headline' => $headline,
				'pubtime' => $pubtime,
				'link' => $link,
				'fingerprint' => $link
			);
		}
		
		return $items;
	
	}

}

?>

※XUGJ で GIJOE さんからご指摘のあった die の行は修正しました。