指定したユーザー宛のリプライツイートを検索し、結果を表示します(1ページ分)。
【検索】ボタンをクリックすると1ページ分取得します。
さらに検索後に表示される「もっと読む」をクリックすると、次のページを取得します。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="ja" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="imagetoolbar" content="no" />
<title>Twitter API:検索関連メソッド:search(Twitter検索) | 設置サンプル</title>
<link rel="stylesheet" type="text/css" href="/content/lib/global.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="/content/lib/jquery/jquery.jsonp.js"></script>
<!-- JS -->
<script type="text/javascript">
(function($){
var pcnt=1;
$.fn.twSearch=function(options){
var settings=$.extend({
url:"http://search.twitter.com/search.json",
page:"1",
holderID:"res",
readmore:false
},options);
this.click(function(){
//検索タイプ
var _type=$("#result_type").val();
if(!_type) _type="mixed";
//キーワード1
var _mentions=$("#mentions").val();
//クエリ
var _query="@"+_mentions;
//API
if(settings.readmore){
var _url=settings.url+"?q="+encodeURIComponent(_query)+"&type="+_type+"&page="+pcnt+"&callback={callback}";
pcnt++;
}else{
var _url=settings.url+"?q="+encodeURIComponent(_query)+"&type="+_type+"&page="+settings.page+"&callback={callback}";
}
//alert(_url);
$.getJSONP(_url, function(json){
$("#loading").show();
if(json.results==""){
$("#btn_more").hide();
$("#"+settings.holderID).append("<p id='caution'>※もうデータがありません</p>");
return false;
}
var s="";
$("#"+settings.holderID+" > h2").html("Results for "+_query);
$.each(json.results, function(i, item){
s+="<li>";
s+="<a href='http://twitter.com/"+item.from_user+"'><img src='"+item.profile_image_url+"' /></a> ";
s+="<a href='http://twitter.com/"+item.from_user+"'>"+item.from_user+"</a>: "+formatStr(item.text)+"";
s+="<div>";
s+="<span><a href='http://twitter.com/"+item.from_user+"/status/"+item.id_str+"'>"+relativeTime(item.created_at)+"</a></span>";
s+="<span class='hoge'>"+formatSource(item.source)+"から</span>";
s+="</div>";
s+="</li>";
});
if(settings.readmore){
// もっと読む
$("#"+settings.holderID+" > ul").append(s);
}else{
// 検索
$("#"+settings.holderID+" > ul").html(s);
$("#btn_more").show();
pcnt=settings.page;
pcnt++;
}
$("#searchurl").html("検索URL:"+_url).show();
$("#loading").hide();
return false;
});
});
var formatStr=function(str){
str = str.replace(/((ftp|http|https?):\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.-]*(\?\S+)?)?)?)/gm,'<a href="$1" target="_blank">$1</a>');
str = str.replace(/@(\w+)/gm,'@<a href="http://twitter.com/$1" target="_blank">$1</a>');
str = str.replace(/#(\w+)/gm,'<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>');
return str;
}
var formatSource=function(str){
str = str.replace(/>/gm,">");
str = str.replace(/</gm,"<");
return str;
}
var relativeTime=function(pastTime){
var origStamp = Date.parse(pastTime);
var curDate = new Date();
var currentStamp = curDate.getTime();
var difference = parseInt((currentStamp - origStamp)/1000);
if(difference < 5) return "いまさっき";
if(difference <= 20) return "数秒前";
if(difference <= 60) return "1分以内";
if(difference < 3600) return parseInt(difference/60)+" 分前";
if(difference <= 1.5*3600) return "1時間前";
if(difference < 23.5*3600) return Math.round(difference/3600)+" 時間前";
if(difference < 1.5*24*3600) return "1日前";
var dateArr = pastTime.split(' ');
return (parseInt(difference / 86400)).toString() + '日前';
}
return this;
};
})(jQuery);
$(function(){
$("#btn_serach").twSearch();
$("#btn_more").twSearch({readmore:true});
});
</script>
<style type="text/css">
#loading,#searchurl,#btn_more { display:none; }
#res ul { margin:0; padding:0; list-style:none; border-top:1px solid #ccc; }
#res li { background-color:#fff; margin:0; padding:10px 10px 10px 62px; list-style:none; display:block; position:relative; min-height:42px; height:auto!important; height:42px; font-size:13px; border-bottom:1px solid #ccc; }
#res li:hover { background-color:#d3f7ff; }
#res li img { display:block; width:32px; height:32px; margin:0; padding:1px; outline:1px solid #ccc; background:#fff; position:absolute; top:10px; left:10px; }
#res li div { margin:5px 0; padding:0; font-size:11px; }
#res li div span { margin:0 10px 0 0; padding:0; }
#searchurl { font-size:10px; border:1px dotted #ccc; background:#eee; word-break:break-all; }
#caution { margin:10px 0; padding:10px; background:#ffffcc; }
</style>
<link rel="stylesheet" type="text/css" href="/common/css/example.css"></head>
<body id='example3' class='example'><div class="ads" style="margin:32px auto;text-align:center;"></div><h1 class='h'><a href='/'>PHP & JavaScript Room</a> :: 設置サンプル</h1>
<h3 class='h'>実行結果</h3>
<div id="wrap">
<h1>Twitter API:検索関連メソッド:search(Twitter検索) | 設置サンプル</h1>
<h2>リプライ検索 - @username</h2>
<p>指定したユーザー宛のリプライツイートを検索し、結果を表示します(1ページ分)。<br>【検索】ボタンをクリックすると1ページ分取得します。<br>さらに検索後に表示される「もっと読む」をクリックすると、次のページを取得します。</p>
<!-- CODE -->
<p>
<label for="result_type">検索タイプ:</label>
<select name="result_type" id="result_type">
<option value="mixed" selected="selected">mixed</option>
<option value="recent">recent</option>
<option value="popular">popular</option>
</select>
<label for="phrase">@</label>
<input type="text" value="cocoism" name="mentions" id="mentions" />
<input type="button" value=" 検索 " id="btn_serach" />
</p>
<div id="searchurl"></div>
<div id="res"><div id='loading'>loading...</div><h2></h2><ul></ul></div>
<p><a href="#" id="btn_more" onclick="return false;">もっと見る</a></p>
<!-- / CODE -->
</div>
</body>
</html>