Twitterのまとめに使っているプラグインTwitter Digestが、なんとTwitter API 1.1に対応! Ver.2.8になりました。で、前のバージョンで施していた、表示フォーマットの変更と、サムネイル画像の挿入を行いました。
時刻を先頭にするために、最初のフォーマット部分を削除します。
// Returns an html formatted $tweet. This is almost directly borrowed from Twitter Tools
function ws_format_tweet($tweet) {
$output = '<li class="ws_tweet">';
// $output .= ws_make_clickable(wp_specialchars($tweet->text));
// if (!empty($tweet->in_reply_to_screen_name)
// && (!empty($tweet->in_reply_to_status_id))) {
// $output .= ' <a href="'.ws_status_url($tweet->in_reply_to_screen_name, $tweet->in_reply_to_status_id).'">'.sprintf(__('in reply to %s', 'twitter-digest'), $tweet->in_reply_to_screen_name).'</a>';
// }
// Show the date/time if the options are selected
$showTime = get_option('ws_td_showtime');
if (!$showTime) { $showTime = 0; }
// Show the date if the option is selectd
$showDate = get_option('ws_td_showdate');
if (!$showDate) { $showDate = 0; }
新しいフォーマット部分と、サムネイルの挿入部分です。短縮されたURLの復元も行います。追加のみです。
// Add the status link
$username = get_option('ws_td_username');
$output .= ' <a class="ws_tweet_time" href="'.ws_status_url($username, $tweet->id_str).'">'.$time_display.'</a>';
$body_tmp=$tweet->text;
if (preg_match('/http:\/\/t\.co\/[\w]+/',$body_tmp,$matches)) {
$tco_url=$matches[0];
$long_url = simplexml_load_string(file_get_contents('http://untiny.me/api/1.0/extract?url='.$tco_url));
$tmp_url = $long_url->org_url;
$body_tmp = str_replace($tco_url,$tmp_url,$body_tmp);
}
$output .= ' <span>' .ws_make_clickable(wp_specialchars($body_tmp));
$body_tmp=getThumbnailHtml(ws_make_clickable(wp_specialchars($body_tmp)));
$output .= $body_tmp;
if (!empty($tweet->in_reply_to_screen_name)
&& (!empty($tweet->in_reply_to_status_id))) {
$output .= ' <a href="'.ws_status_url($tweet->in_reply_to_screen_name, $tweet->in_reply_to_status_id).'">'.sprintf(__('in reply to %s', 'twitter-digest'), $tweet->in_reply_to_screen_name).'</a>';
}
$output .= '</span></li>';
return $output;
}
URLからサムネイルを取得する関数です。ここは追加のみ。
function getThumbnailHtml($status_text) {
$html = '';
$patterns = array(
// twitpic
array('/http:\/\/twitpic[.]com\/(\w+)/', '<img src="http://twitpic.com/show/thumb/$1" width="150" height="150" />'),
// Mobypicture
array('/http:\/\/moby[.]to\/(\w+)/', '<img src="http://moby.to/$1:small" />'),
// yFrog
array('/http:\/\/yfrog[.]com\/(\w+)/', '<img src="http://yfrog.com/$1.th.jpg" />'),
// 携帯百景
array('/http:\/\/movapic[.]com\/pic\/(\w+)/', '<img src="http://image.movapic.com/pic/s_$1.jpeg" />'),
// はてなフォトライフ
array('/http:\/\/f[.]hatena[.]ne[.]jp\/(([\w\-])[\w\-]+)\/((\d{8})\d+)/', '<img src="http://img.f.hatena.ne.jp/images/fotolife/$2/$1/$4/$3_120.jpg" />'),
// PhotoShare
array('/http:\/\/(?:www[.])?bcphotoshare[.]com\/photos\/\d+\/(\d+)/', '<img src="http://images.bcphotoshare.com/storages/$1/thumb180.jpg" width="180" height="180" />'),
// PhotoShare の短縮 URL
array('/http:\/\/bctiny[.]com\/p(\w+)/e', '\'<img src="http://images.bcphotoshare.com/storages/\' . base_convert("$1", 36, 10) . \'/thumb180.jpg" width="180" height="180" />\''),
// img.ly
array('/http:\/\/img[.]ly\/(\w+)/', '<img src="http://img.ly/show/thumb/$1" width="150" height="150" />'),
// brightkite
array('/http:\/\/brightkite[.]com\/objects\/((\w{2})(\w{2})\w+)/', '<img src="http://cdn.brightkite.com/$2/$3/$1-feed.jpg" />'),
// Twitgoo
array('/http:\/\/twitgoo[.]com\/(\w+)/', '<img src="http://twitgoo.com/$1/mini" />'),
// pic.im
array('/http:\/\/pic[.]im\/(\w+)/', '<img src="http://pic.im/website/thumbnail/$1" />'),
// youtube
array('/http:\/\/(?:www[.]youtube[.]com\/watch(?:\?|#!)v=|youtu[.]be\/)([\w\-]+)(?:[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]*)/', '<img src="http://i.ytimg.com/vi/$1/hqdefault.jpg" width="240" height="180" />'),
// imgur
array('/http:\/\/imgur[.]com\/(\w+)[.]jpg/', '<img src="http://i.imgur.com/$1l.jpg" />'),
// TweetPhoto, Plixi, Lockerz
array('/http:\/\/tweetphoto[.]com\/\d+|http:\/\/plixi[.]com\/p\/\d+|http:\/\/lockerz[.]com\/s\/\d+/', '<img src="http://api.plixi.com/api/TPAPI.svc/imagefromurl?size=mobile&url=$0" />'),
// Ow.ly
array('/http:\/\/ow[.]ly\/i\/(\w+)/', '<img src="http://static.ow.ly/photos/thumb/$1.jpg" width="100" height="100" />'),
// Instagram
array('/http:\/\/instagr[.]am\/p\/([\w\-]+)\//', '<img src="http://instagr.am/p/$1/media/?size=t" width="150" height="150" />'),
array('/http:\/\/instagram[.]com\/p\/([\w\-]+)\//', '<img src="http://instagram.com/p/$1/media/?size=t" width="150" height="150" />'),
// フォト蔵
array('/http:\/\/photozou[.]jp\/photo\/show\/\d+\/([\d]+)/', '<img src="http://photozou.jp/p/thumb/$1" />'),
// ついっぷる フォト
array('/http:\/\/p[.]twipple[.]jp\/([\w]+)/', '<img src="http://p.twipple.jp/show/thumb/$1" />'),
// Shazam
array('/http:\/\/shz[.]am\/t([\d]+)/', '<img src="http://images.shazam.com/GB/webtid/$1" width="150" height="150" />'),
);
foreach ($patterns as $pattern) {
if (preg_match($pattern[0], $status_text, $matches)) {
$url = $matches[0];
$html = preg_replace($pattern[0], $pattern[1], $url);
$html = '<br><a href="' . $url . '" target="_blank">' . $html . '</a>';
break;
}
}
return $html;
}
?>
サムネイルの取得については、次のサイトのコードに、少しサイトを追加しました。
http://blog.irons.jp/2009/12/23/twitter_thumb_url/