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

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

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

コード

[php]
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();
}
}
[/php]

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

使い方

こんな感じで使います。
[php]
<?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;?>
[/php]

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

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

“Topsyから特定のURLに関するつぶやきをWordPressっぽく取得できるClassを書いてみた。” への1件のコメント

  1. Takuyamori 0203のアバター
    Takuyamori 0203

     はじめまして。
    ちょうどサイト内に特定URLに関するつぶやきを表示したく検索していたら、こちらにたどりつきました。
    このような記事を書いていただきありがとうございます。

    さっそく上記のコードを実装したのですが、何点かご教授お願いします。
    ■質問1
    「●●回つぶやかれました」の部分ですが、
    仮に100件つぶやかれても「10件」と表示されるようです。
    最大件数が表示されるようするにはどこを修正したら良いのでしょうか?

    ■質問2
    現在のままだと表示件数は10件となりますが、
    10件以上のつぶやきがある場合は、「全てのつぶやき見る」ボタンを設置し、
    TOPSYの検索結果ページリンクしたいです。

    大変あつかましい質問ですが、
    よろしくお願いいたします。