-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A simple Twitter search results counter in JS
- Loading branch information
1 parent
720bd1e
commit 5e1868a
Showing
2 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Tweet Count | ||
|
||
### Get the number of tweets for any search. | ||
|
||
Useful for hashtag trending. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<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; | ||
totalTweets += tweets.length; | ||
nextPage = getPage.exec(data.next_page); | ||
if( nextPage == null ) { | ||
showTotal(); | ||
return; | ||
} | ||
nextPage = nextPage[1]; | ||
getTweets(search, nextPage, 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> |