Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (134 sloc) 4.08 KB
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
.row {padding-top:1.5em;}
</style>
<script>
$(document).ready(function(){
$("#btn").click(function() {
//replace loading image
var username=$("#username").val();
//make ajax call, callback
var url='https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name='+username+'&count=1000';
if (username!=''){
for (var i=1;i<6;i++) {
fireCall(url,i);
}
}
else {
alert('Please enter Twitter username')
}
})
});
function fireCall (url,page) {
var request = $.ajax({
url: url,
dataType: "jsonp",
data: {page:page},
jsonpCallback: "fetchData"+page,
type: "GET"
});
}
//ajax callback
var list=[]; //unique global list of words
var counter={};
var pages=0;
//breaking into 200 tweet pieces due to Twitter API limitation
function fetchData1 (m) { //probably there is a more elegant solution :)
fetchData(m);
}
function fetchData2 (m) {
fetchData(m);
}
function fetchData3 (m) {
fetchData(m);
}
function fetchData4 (m) {
fetchData(m);
}
function fetchData5 (m) {
fetchData(m);
}
function fetchData (m) {
pages++; //wait till all requests come back
for (i=0;i<m.length;i++){
// console.log(m[i].text);
var words=m[i].text.split(' ');
for (j=0;j<words.length;j++){
// console.log(words[j].substring(0,4))
words[j] = words[j].replace(/\,/g,"");
words[j] = words[j].replace(/\"/g,"");
words[j] = words[j].replace(/\'/g,"");
words[j] = words[j].replace(/\./g,"");
words[j] = words[j].replace(/#/g,"");
words[j] = words[j].replace(/@/g,"");
if (words[j].substring(0,4)!="http"&&words[j]!='') {
if (list.indexOf(words[j])<0) {
list.push(words[j]);
counter[words[j]]=1;
}
else {
//add plus one to word couter
counter[words[j]]++;
}
}
}
}
//sort by number of repetitions
for (i=0;i<list.length;i++){
var max=counter[list[i]];
var p=0;
for (j=i;j<list.length;j++) {
if (counter[list[i]]<counter[list[j]]) {
p=list[i];
list[i]=list[j];
list[j]=p;
maxC=i;
}
}
}
//print sorted
console.log("REQUEST "+pages+"/5"); //this should be nice loading GIF icon
$('#log').val("REQUEST "+pages+"/5");
if (pages==5) {
console.log(list);
// $('#log').val(JSON.stringify(list));
}
//print nicely with number of repetition
var str='';
for (i=0;i<list.length;i++){
str+=counter[list[i]]+": "+list[i]+'\n';
}
$('#log').val(str)
}
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="span6 offset1"><input class="span4" type="text" placeholder="Enter Twitter username, eg: square" id="username" value=""/>
</div>
</div>
<div class="row">
<div class="span6 offset1"><input type="button" class="primary btn" id="btn" value="Show words in last 1000 tweets"/>
</div>
</div>
<div class="row">
<div class="span6 offset1">
<textarea id="log" cols="20" rows="20">Nothing to show yet</textarea>
</div>
<div class="span4 offset 3">
<h2>Task</h2>
<p>Write a library that prints the words in a given twitter user’s last 1000 tweets sorted by frequency of use.

For example, if @jack had tweeted:
“hello world”
“hello everyone, and world”
“hi world”

The result could be:
world
hello
and
hi
everyone

Provide a link to a git repo or a tarball that contains your code.</p>
<hr/>
<p>Requests are broken into 5 pieces. Please open console for more information.</p>
<p>Azat Mardanov (<a href="http://twitter.com/azat_co">@azat_co</a>)</p>
</div>
</div>
</div>
</body>
</html>