Twitter API (javascript)

Web Programing/JavaScript 2011. 8. 23. 11:34

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

<form id="frm" name="frm" action="/" method="get" onsubmit="javascript:getTweets(); return false;" >
		<h3>Twitter API (javascript)</h3>
		<div style="width:640px; min-height:40px;border:1px solid skyblue; padding:15px;">
			<div>
				screenName : <input type="text" id="screenName" value="wowhyun" />, 
				count : <input type="text" id="count" value="5" />
				<input type="submit" value="트윗 가져오기" />
			</div>
			<div>
				<input type="button" value="Limit 확인" id="getLimitBtn" /> 
				<span id="remaining-hits"></span>
			</div>
		</div>
		<div style="width:640px; min-height:40px; border:1px solid green; margin-top:10px; padding:15px;">
				<ol id="twitterList"></ol>
		</div>
</form>

<script type="text/javascript">
function chkTweetLinks(s){
	var regLink = new RegExp('(http|https|ftp|telnet|news|irc)://([-/.a-zA-Z0-9_~#%$?&=:200-377()]+)','gi');
	var r = s.replace(regLink,'<a href="$1://$2" target="_blank">$1://$2</a>')
		.replace(/(^|\s)@(\w+)/g, '$1@<a href="http://www.twitter.com/$2" target="_blank">$2</a>')
		.replace(/(^|\s)#(\w+)/g, '$1#<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
	return r;
}
function getTweets(){
	jQuery.getJSON(
		'http://api.twitter.com/1/statuses/user_timeline.json?trim_user=t&include_rts=true&callback=?',
		{ screen_name: $('#screenName').val(), count: $('#count').val() },
		function(data){
			$('#twitterList').hide();
			$('#twitterList').empty();
			jQuery.each(data, function(i,item){
				$('#twitterList').append('<li>'+chkTweetLinks(item.text)+'<br /> -'+item.created_at+'</li>');
			});
			$('#twitterList').fadeIn('slow');
			getRemainLimits();
    }
	);
}

function getRemainLimits(){
	$('#remaining-hits').hide();
	jQuery.getJSON(
		'http://api.twitter.com/1/account/rate_limit_status.json?callback=?',
		function(data){
			$('#remaining-hits').html(data['remaining_hits']+' (reset_time : '+data['reset_time']+') ');
			$('#remaining-hits').fadeIn('slow');
    }
	);
}

$(function () {
    $('#getLimitBtn').click(getRemainLimits);
    getTweets();
});
</script>

설정

트랙백

댓글