Skip to content

Instantly share code, notes, and snippets.

@borndangerous
Created January 23, 2016 06:42
Show Gist options
  • Save borndangerous/d999647459c6d851a9c8 to your computer and use it in GitHub Desktop.
Save borndangerous/d999647459c6d851a9c8 to your computer and use it in GitHub Desktop.
<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"><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>
</pre></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment