sweet-tweet/script.js
changeset 414 e2c1a2386124
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sweet-tweet/script.js	Thu Dec 15 13:45:33 2011 +0100
@@ -0,0 +1,182 @@
+var swTw = {
+    "keyword" : "#enmi",
+    "columns_words" : [
+        "confiance",
+        "croyance",
+        "crédit",
+        "trace",
+        "foi",
+        "risque", 
+        "assurance", 
+        "démocratie", 
+        "expertise",
+        "catastrophe",
+        "transparence", 
+        "politique"
+    ],
+    "tweets" : [],
+    "tweetsIndex" : [],
+    "firstDisplayedTweet" : 0,
+    "cursor" : -1
+}
+
+function nextTweet() {
+    if (!swTw.tweets.length) {
+        return;
+    }
+    if (swTw.cursor < swTw.tweets.length - 1) {
+        swTw.cursor = Math.max(swTw.cursor + 1, swTw.tweets.length - 20);
+        var nTweet = swTw.cursor;
+    } else {
+        var nTweet = swTw.tweets.length - 1 - ~~( Math.random() * Math.min(swTw.tweets.length,50) );
+    }
+    var tweet = swTw.tweets[nTweet];
+    $("#tweetcont").html('<img src="'
+        + tweet.profile_image_url
+        + '" /><p>@'
+        + tweet.from_user
+        + ' ('
+        + tweet.from_user_name
+        + ')'
+        + '</p><p>'
+        + tweet.text
+        + '</p><p>'
+        + (new Date(tweet.created_at).toLocaleString())
+        + '</p>')
+}
+
+function dropOldTweets() {
+    var _newPos = swTw.firstDisplayedTweet + 20;
+    _(swTw.tweets.slice(swTw.firstDisplayedTweet,_newPos)).each(function(tweet) {
+        swTw.twInCol = _(swTw.twInCol).map(function(col) {
+           return _(col).without(tweet.id_str);
+        });
+        _(tweet.elements).each(function(elid) {
+            $("#" + elid).fadeOut(2000, function() {
+                $(this).detach();
+            });
+        });
+    });
+    
+    _(swTw.tweets.slice(_newPos)).each(function(tweet) {
+        _(tweet.elements).each(function(elid) {
+            var iword = parseInt(elid.split('_')[2]),
+                iel = swTw.twInCol[iword].indexOf(tweet.id_str),
+                posy = 500 - 35 * (1 + ~~( iel / 3)),
+                posx = 105 * iword + 35 * (iel % 3);
+            $("#" + elid).delay(2000).animate({
+                "top" : posy + "px",
+                "left" : posx + "px"
+            },
+            500);
+        });
+    });
+    console.log("fin");
+    swTw.firstDisplayedTweet = _newPos;
+}
+
+function callbackTweets(tweets) {
+    _(tweets).each(function(tweet) {
+        var tl = tweet.text.toLowerCase();
+        tweet.columns = swTw.columns_words.filter(function(word) {
+            return tl.search(word) != -1
+        });
+        tweet.elements = [];
+        _(tweet.columns).each(function(word) {
+            var iword = swTw.columns_words.indexOf(word),
+                tcl = swTw.twInCol[iword],
+                tclen = tcl.length;
+            var posy = 500 - 35 * (1 + ~~( tclen / 3)),
+                posx = 105 * iword + 35 * (tclen % 3),
+                el = document.createElement('img'),
+                elid = 'avatar_' + tweet.id_str + '_' + iword;
+            el.className = 'avatar';
+            el.id = elid;
+            el.src = tweet.profile_image_url
+            el.style.top = '-500px';
+            el.style.left = posx + 'px';
+            document.getElementById('columncont').appendChild(el);
+            tweet.elements.push(elid);
+            tcl.push(tweet.id_str);
+            $("#" + elid).animate({
+                "top" : posy + "px"
+            }, 2000);
+        })
+        swTw.tweets.push(tweet);
+        swTw.tweetsIndex.push(tweet.id_str);
+    });
+    while (_(swTw.twInCol).any(function(col) {
+        return col.length > 36
+    })) {
+        dropOldTweets();
+    }
+}
+
+function retrieveTweets() {
+    var options =  {
+        "keyword" : swTw.columns_words.join(" OR "),
+        "lang" : "fr",
+//        "keyword" : "#enmi",
+        "pages" : 1,
+        "rpp" : 100,
+        "cbEnd" : function() {
+            callbackTweets(this.tweets);
+            }
+        }
+    if (swTw.tweets.length) {
+        options.since_id = swTw.tweets[swTw.tweets.length - 1].id_str;
+    }
+    getTweets(options);
+}
+
+function getTweets(options) {
+    function getTweetUrl(url) {
+        $.getJSON(url, function(data) {
+            options.tweets = options.tweets.concat(data.results);
+            options.currentPage = data.page;
+            if (options.cbData) {
+                options.cbData();
+            }
+            if (data.next_page && data.page < options.pages) {
+                getTweetUrl(baseurl + data.next_page + suffix);
+            } else {
+                options.tweets.sort(function(a,b) {
+                   return a.id - b.id; 
+                });
+                if (options.cbEnd) {
+                    options.cbEnd();
+                }
+            }
+        });
+    }
+    
+    options.tweets = [];
+    options.pages || (options.pages = 1);
+    options.rpp || (options.rpp = 100);
+    options.currentPage = 0;
+    
+    var baseurl = "http://search.twitter.com/search.json",
+        suffix = (options.since_id ? "&since_id=" + options.since_id : '' ) + "&callback=?",
+        jsonurl = baseurl + "?q=" + encodeURIComponent(options.keyword)+ "&rpp=" + options.rpp
+            + (options.lang ? "&lang=" + options.lang : '' ) + suffix;
+    getTweetUrl(jsonurl);
+}
+
+$(document).ready(function() {
+    $("#columncont").html( swTw.columns_words.map(
+        function(mot) {
+            return '<div class="column"><div class="column-tube"></div><div class="column-title"><h3>' + mot + '</h3></div></div>'
+        }
+    ).join("") );
+    
+   swTw.twInCol = swTw.columns_words.map(function() {
+       return [];
+   });
+   
+   retrieveTweets();
+   
+   setInterval(retrieveTweets,5000);
+   
+   setInterval(nextTweet, 3000);
+    
+});