This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Total tweets</title> | |
<style type="text/css"> | |
/* | |
input { | |
float: left; | |
clear: both; | |
} | |
*/ | |
</style> | |
<script src="http://code.jquery.com/jquery-latest.js"></script> | |
<script type="text/javascript"> | |
var getPage = new RegExp( /page=([^&]+)/ ); | |
var url = "http://search.twitter.com/search.json?q="; | |
var search, page, pageTotal, tweets, loading; | |
var beforeCounter = "<b>"; | |
var afterCounter = "</b>"; | |
var totalTweets = 0; | |
function getTweets(search, page, pageTotal) { | |
$.getJSON( url + search + '&page=' + page + '&callback=?', | |
function(data) { | |
if( data.results.length != 0 && page != pageTotal ) { | |
$('#pagesDone span').html(page); | |
getData(data); | |
} | |
else { | |
showTotal(); | |
} | |
} | |
); | |
} | |
function showTotal() { | |
$('#totalTweets span').html(beforeCounter + totalTweets + afterCounter); | |
$('#pagesDone span').html('0'); | |
totalTweets = 0; | |
loading = false; | |
} | |
function getData(data) { | |
tweets = data.results; | |
page = getPage.exec(data.next_page); | |
if( page == null ) { | |
showTotal(); | |
return; | |
} | |
page = page[1]; | |
totalTweets += tweets.length; | |
getTweets(search, page, pageTotal); | |
} | |
function submitTerms() { | |
$('#totalTweets span').html(''); | |
$('#pagesDone span').html('0'); | |
search = encodeURIComponent($('#query').prop('value')); | |
page = $('#startPage').prop('value'); | |
pageTotal = $('#pageTotal').prop('value'); | |
if( search == '' ) { | |
alert('Please enter search query'); | |
return; | |
} | |
if( page == 0 ) { | |
alert('0 not allowed as start page'); | |
return; | |
} | |
loading = true; | |
getTweets(search, page, pageTotal); | |
} | |
function status() { | |
if( loading ) $('#loading,#pagesDone').show(); | |
else $('#loading,#pagesDone').hide(); | |
} | |
$(function() { | |
loading = false; | |
setInterval(status, 10); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Get your twitter counts here!</h1> | |
<form action="" method="get" onsubmit="submitTerms(); return false;"> | |
<label for="query">Search for: </label> | |
<input id="query" name="query" type="text" /> | |
<br /> | |
<label for="startPage">Start on what page (must be greater than 1) </label> | |
<input id="startPage" name="startPage" type="text" value="1" /> | |
<br /> | |
<label for="pageTotal">How many pages (0 to search until no more results) </label> | |
<input id="pageTotal" name="pageTotal" type="text" value="0" /> | |
<input id="submit" name="submit" type="submit" value="Go" /> | |
<span>or press enter</span> | |
</form> | |
<div id="totalTweets">Total tweets: <span></span></div> | |
<br /> | |
<div id="loading" style="display:none;">Loading!</div> | |
<div id="pagesDone" style="display:none;">Pages done: <span></span></div> | |
<span style="display:none;">Do Electric Cars Pollute More Than Their Gasoline Counterparts</span> | |
</body> | |
</html> |
No comments:
Post a Comment