Topsyから特定のURLに関するつぶやきをWordPressっぽく取得できるClassを書いてみた。

このサイトのリニューアルこの間リニューアルしたときに作りました。晒そうと思ってすっかり忘れてました。このブログの個別記事の下の方のTwitterタイムラインがそれです。
Topsyで特定のURLに関するつぶやきや、そのユーザーなどを取得します。

functions.phpにでも貼っておくと良いと思います。いちおうPHP5用。PHP4系で使いたい場合は__cunstructをGet_Topsy_Trackbackに変えてあげて下さい。

コード

class Get_Topsy_Trackback {
	private $counter = 0;
	public $response;
	public $trackback;

	function __construct($permalink){
		$topsy = json_decode(file_get_contents("http://otter.topsy.com/trackbacks.json?url=".$permalink));
		$this->response = $topsy->response;
	}

	function get_all_topsy(){
		return count($this->response->list);
	}

	function have_topsy(){
		if(isset($this->response->list[$this->counter])){
			return true;
		}else{
			return false;
		}
	}

	function the_topsy(){
		$this->trackback = $this->response->list[$this->counter];
		$this->counter ++;
	}

	function get_topsy(){
		return $this->trackback;
	}

	function get_the_content(){
		$content = $this->trackback->content;
		return preg_replace('/(https?:\/\/[a-zA-Z0-9\.\/:%,!#~*@&_-]+)/','<a href="\0" title="\0">\0</a>',$content);
	}
	function the_content(){
		echo "<p>".$this->get_the_content()."</p>";
	}

	function get_the_author(){
		return $this->trackback->author;
	}

	function get_the_author_nick(){
		return $this->get_the_author()->nick;
	}
	function the_author_nick(){
		echo $this->get_the_author_nick();
	}

	function get_the_author_link(){
		return $this->get_the_author()->url;
	}
	function the_author_link(){
		echo $this->get_the_author_link();
	}

	function the_author(){
		echo '<a href="'.$this->get_the_author_link().'" class="author">'.$this->get_the_author_nick().'</a>';
	}

	function get_the_author_avator(){
		return $this->get_the_author()->photo_url;
	}
	function the_author_avator( $size = 48 ){
		echo '<img src="'.$this->get_the_author_avator().'" alt="'.$this->get_the_author_nick().'" width="'.$size.'" height="'.$size.'" />';
	}

	function get_the_time( $format = "Y-m-d" ){
		return date($format,$this->trackback->date);
	}
	function the_time( $format = "Y-m-d" ){
		echo $this->get_the_time( $format );
	}

	function get_the_permalink(){
		return $this->trackback->permalink_url;
	}
	function the_permalink(){
		echo $this->get_the_permalink();
	}
}

基本的にgetから始まるメソッドはreturnで返し、theから始まるメソッドはechoします。実にWordPressっぽいですよね?

使い方

こんな感じで使います。

<?php
$topsy = new Get_Topsy_Trackback($URL);
if($topsy->have_topsy()):?>
<p><?php echo $topsy->get_all_topsy();?>回つぶやかれました。</p>
<ul>
<?php while($topsy->have_topsy()):$topsy->the_topsy(); ?>
	<li>
		<a href="<?php $topsy->the_author_link();?>"><?php $topsy->the_author_avator()?></a>
		<div class="content"><?php $topsy->the_author();?><?php $topsy->the_content();?></div>
		<div class="meta">Post:<a href="<?php $topsy->the_permalink();?>"><?php $topsy->the_time("Y-m-d");?></a></div>
	</li>
<?php endwhile; ?>
</ul>
<?php else:?>
<p>まだだれもつぶやいていない様子です。</p>
<?php endif;?>

インスタンス化するときに、URLを引数に入れてあげましょう。WordPressで使うのならばget_permalink(get_the_ID())とかですね。
あとはWordPressっぽいループタグを書いて、WordPressっぽいテンプレートタグを書いてあげればオーケーです。

需要があるかどうか解らないけれど結構便利だと思うので、Topsy使ってみたいけど・・・って人は使ってみて下さい!

Topsyから特定のURLに関するつぶやきをWordPressっぽく取得できるClassを書いてみた。Toro_Unit