Skip to content

Commit

Permalink
A simple Twitter search results counter in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelbetio authored Jul 31, 2018
1 parent 720bd1e commit 5e1868a
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
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.
105 changes: 105 additions & 0 deletions index.html
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>

0 comments on commit 5e1868a

Please sign in to comment.