This gist is basically a snapshot of my project twitter-viz on GitHub. My goals with this project is to:
- Play with the twitter API in javascript.
- Play a little with jQuery, css-selectors and effects.
- Try twitter bootstrap for the layout/formatting of the page.
- Write code to download all the tweets for a user.
- Extract the code for downloading the tweets into an object.
- Create visualizations of the data
- Analyze the data in web-workers.
This gist contains the first 5 points.
It seems like the REST resources support JSONP. At least the resources I have tried. Only the the search API explicitly listed the callback parameter.
I tried the getJSON function from jQuery but I did not manage to get notification about errors. I only tried the error-function. I found jQuery-JSONP on google code which did what I wanted it to do.
I first used an class invisible from Twitter bootstrap to hide the progress bar when it wasn't used. Then I started to use fadeout from jQuery to hide it after it had been shown. Now I had two ways of hiding it, the invisible class and the jQuery way with display none. This caused me some confusion at first, when I wanted to show the progress bar a second time. I dropped the invisible class, I did not need both the hiding mechanisms.
When I understood the fadeout used a display none to hide the element I tried to unhide it with the code below. But that code did not work if the element did not have a style attribute. I ended up using the function show() instead, much shorter.
$('#element').removeStyle('display');
I did not manage to work with Twitter bootstrap to place my progress bar and warning note at the position I wanted so I ended up writing some custom css to get OK placement vertically.
#alert_container {
position:absolute;
top:15px;
}
I first tried to write the div for the alert in html, but that did not work very well since when the error was closed the element was removed from the dom and then I could not show another error. Instead I had to write a new div with the alert and activate it with the alert() function.
Twitter has a good article about how to work with timelines, Working with Timelines. Which I followed. But I ran into some problems. I did get duplicate entries even though I followed the instructions. When I debugged the code I found that a big number in javascript plus or minus one is usually the same big number. The problem is that the tweet ID:s is 64 bit and javascript only support numbers up to 53 bit then they loose precision. I had previously noticed that the the id field and id_str did not match. Now I knew why. I ended up using a BigInteger implementation to handle my max_id and since_id variables to avoid getting duplicate entries.
I also added some code to store the tweets in localStorage so they do not have to be downloaded each time the same user is queried. The localStorage seems to be able to fit 2000-2500 tweets on Chrome. When they do not fit a exception is thrown, which I did not catch at first. I did not find the problem until I enabled the debug feature in Chrome "stop on all unhandled exceptions", the "pause" button in the bottom in the scripts view in the developer tools.
I found a good article from Douglas Crockford Private Members in JavaScript on how to do this. I wanted to have a few public methods accessing private content and a bunch of private methods, not accessible from outside the object. For the public methods I ended up with "privileged" methods as described in the article.