web/res/js/live-polemic.js
changeset 878 bead0341e247
parent 827 288135d65fc2
child 880 ac8b456b8874
--- a/web/res/js/live-polemic.js	Wed Apr 10 17:41:03 2013 +0200
+++ b/web/res/js/live-polemic.js	Wed Apr 10 18:13:33 2013 +0200
@@ -109,7 +109,7 @@
     
     function getTweetUrl(url) {
         $.getJSON(url, function(data) {
-            options.tweets = options.tweets.concat(data.results);
+            options.tweets = options.tweets.concat(data.statuses);
             options.currentPage++;
             if (options.cbData) {
                 options.cbData();
@@ -139,10 +139,10 @@
     options.pages || (options.pages = 1);
     options.rpp || (options.rpp = 100);
     options.currentPage = 0;
-    var baseurl = "http://search.twitter.com/search.json",
+    var baseurl = "search_tweets.php",
         firstparams = "?q=" + encodeURIComponent(options.keyword)+ "&rpp=" + options.rpp
             + (options.lang ? "&lang=" + options.lang : '' ),
-        lastparams = (options.since_id ? "&since_id=" + options.since_id : '' ) + "&callback=?",
+        lastparams = (options.since_id ? "&since_id=" + options.since_id : '' ),
         jsonurl = baseurl + firstparams + lastparams;
     getTweetUrl(jsonurl);
 }
@@ -205,53 +205,41 @@
         return;
     }
     
-    tweet.entities = []
+    tweet.html_parts = []
     
-    var _users = tweet.text.match(/@[\w_]+/gm),
-        _lastpos = 0;
-    if (_users) {
-        for (var _i = 0; _i < _users.length; _i++) {
-            var _m = _users[_i],
-                _start = _lastpos + tweet.text.substr(_lastpos).search(_m),
-                _lastpos = _start + _m.length;
-            tweet.entities.push({
-                "text" : _m,
-                "start" : _start,
-                "end" : _lastpos,
-                "link" :'<a href="http://twitter.com/' + _m.substr(1) + '" onclick="filtrerTexte(\'' + _m + '\'); return false;" target="_blank">'
+    if (tweet.entities && tweet.entities.user_mentions) {
+        for (var _i = 0; _i < tweet.entities.user_mentions.length; _i++) {
+            var _m = tweet.entities.user_mentions[_i];
+            tweet.html_parts.push({
+                "text" : "@" + _m.screen_name,
+                "start" : _m.indices[0],
+                "end" : _m.indices[1],
+                "link" :'<a href="http://twitter.com/' + _m.screen_name + '" onclick="filtrerTexte(\'' + _m.screen_name + '\'); return false;" target="_blank">'
             });
         }
     }
     
-    var _hashes = tweet.text.match(/([^&]|^)#[^\s,.!?=#@&;()]+/gm),
-        _lastpos = 0;
-    if (_hashes) {
-        for (var _i = 0; _i < _hashes.length; _i++) {
-            var _m = _hashes[_i],
-                _h = ( _m[0] == '#' ? _m : _m.substr(1) ),
-                _start = _lastpos + tweet.text.substr(_lastpos).search(_h),
-                _lastpos = _start + _h.length;
-            tweet.entities.push({
+    if (tweet.entities && tweet.entities.hashtags) {
+        for (var _i = 0; _i < tweet.entities.hashtags.length; _i++) {
+            var _m = tweet.entities.hashtags[_i],
+                _h = "#" + _m.text;
+            tweet.html_parts.push({
                 "text" : _h,
-                "start" : _start,
-                "end" : _lastpos,
+                "start" : _m.indices[0],
+                "end" : _m.indices[1],
                 "link" :'<a href="http://twitter.com/search?q=' + encodeURIComponent(_h) + '" onclick="filtrerTexte(\'' + _.escape(_h) + '\'); return false;" target="_blank">'
             });
         }
     }
     
-    var _urls = tweet.text.match(/(www\.|https?:\/\/)[\w./_\-]+/gim),
-        _lastpos = 0;
-    if (_urls) {
-        for (var _i = 0; _i < _urls.length; _i++) {
-            var _m = _urls[_i],
-                _start = _lastpos + tweet.text.substr(_lastpos).search(_m),
-                _lastpos = _start + _m.length;
-            tweet.entities.push({
-                "text" : _m,
-                "start" : _start,
-                "end" : _lastpos,
-                "link" :'<a href="' + _m + '" target="_blank">'
+    if (tweet.entities && tweet.entities.urls) {
+        for (var _i = 0; _i < tweet.entities.urls.length; _i++) {
+            var _m = tweet.entities.urls[_i];
+            tweet.html_parts.push({
+                "text" : _m.display_url || _m.url,
+                "start" : _m.indices[0],
+                "end" : _m.indices[1],
+                "link" :'<a href="' + _m.url + '" target="_blank">'
             });
         }
     }
@@ -273,17 +261,10 @@
     if (tweet.in_reply_to_status_id) {
         backRef( tweet.id, tweet.in_reply_to_status_id, "reply" );
     }
-    var _retweet = tweet.text.match(/RT @[\w_]+:? /)
-    if (_retweet) {
-        var _user = _retweet[0].match(/@[\w_]+/)[0].substr(1).toLowerCase(),
-            _originalText = tweet.text.substr(tweet.text.search(_retweet[0]) + _retweet[0].length);
-        for (var i = 0; i < twCx.tweets.length; i++) {
-            if (twCx.tweets[i].from_user && twCx.tweets[i].from_user.toLowerCase() == _user && twCx.tweets[i].text == _originalText) {
-                tweet.retweeted_status_id = twCx.tweets[i].id;
-                backRef( tweet.id, twCx.tweets[i].id, "retweet" );
-                break;
-            }
-        }
+    
+    if (tweet.retweeted_status && tweet.retweeted_status.id_str) {
+    	tweet.retweeted_status_id = tweet.retweeted_status.id_str;
+        backRef( tweet.id, tweet.retweeted_status_id, "retweet" );
     }
     
     
@@ -471,11 +452,11 @@
     	+ '" id="tweet_'
     	+ tweet.id
     	+ '" data-title="Tweet by '
-    	+ _(tweet.from_user_name).escape()
+    	+ _(tweet.user.name).escape()
     	+ '" data-description="'
     	+ _(tweet.text).escape()
     	+ '" data-uri="http://twitter.com/'
-    	+ tweet.from_user
+    	+ tweet.user.screen_name
     	+ '/status/'
     	+ tweet.id
     	+ '"';
@@ -495,8 +476,8 @@
         html += '</div>';
     }
     html += '<div class="twmain">';
-    var a_user = '<a href="http://twitter.com/' + tweet.from_user + '"  onclick="filtrerTexte(\'@' + tweet.from_user + '\'); return false;" target="_blank">';
-    html += '<div class="around_img"><img class="profile_image" src="' + tweet.profile_image_url + '" />';
+    var a_user = '<a href="http://twitter.com/' + tweet.user.screen_name + '"  onclick="filtrerTexte(\'@' + tweet.user.screen_name + '\'); return false;" target="_blank">';
+    html += '<div class="around_img"><img class="profile_image" src="' + tweet.user.profile_image_url + '" />';
     if (className == 'full') {
         html += '<p class="created_at">' + new Date(tweet.date_value).toTimeString().substr(0,8) + '</a></p>';
     }
@@ -504,15 +485,15 @@
     if (className != 'icons') {
         lastend = 0;
         var txt = '';
-        tweet.entities.sort(function(a, b) { return a.start - b.start });
-        _(tweet.entities).each(function(_e) {
+        tweet.html_parts.sort(function(a, b) { return a.start - b.start });
+        _(tweet.html_parts).each(function(_e) {
             txt += highlight( tweet.text.substring(lastend, _e.start) ) + _e.link + highlight( _e.text ) + '</a>';
             lastend = _e.end;
         });
         txt += highlight( tweet.text.substring(lastend) );
-        html += '<p class="tweet_text"><b>' + a_user + highlight('@' + tweet.from_user) + '</a>' + ( className == 'full' ? ' (' + tweet.from_user_name + ')</b><br />' : '</b> : ') + txt + '</p>';
+        html += '<p class="tweet_text"><b>' + a_user + highlight('@' + tweet.user.screen_name) + '</a>' + ( className == 'full' ? ' (' + tweet.user.name + ')</b><br />' : '</b> : ') + txt + '</p>';
         if (className == 'full' && el == 'li') {
-            html += '<div class="tweet_actions"><a href="http://twitter.com/' + tweet.from_user + '/status/' + tweet.id + '" onclick="tweetPopup(this.href); return false;" target="_blank">afficher tweet</a> - ';
+            html += '<div class="tweet_actions"><a href="http://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id + '" onclick="tweetPopup(this.href); return false;" target="_blank">afficher tweet</a> - ';
             html += '<a href="http://twitter.com/intent/tweet?in_reply_to=' + tweet.id + '" onclick="tweetPopup(this.href); return false;" target="_blank">répondre</a> · ';
             html += '<a href="http://twitter.com/intent/retweet?tweet_id=' + tweet.id + '" onclick="tweetPopup(this.href); return false;" target="_blank">retweeter</a> · ';
             html += '<a href="http://twitter.com/intent/favorite?tweet_id=' + tweet.id + '" onclick="tweetPopup(this.href); return false;" target="_blank">favori</a></div>';
@@ -710,7 +691,7 @@
     }
     if (twCx.filtre) {
         var tweets = _(twCx.tweets).filter(function(tweet) {
-            var mention = '@' + tweet.from_user;
+            var mention = '@' + tweet.user.screen_name;
             return ( tweet.text.search(twCx.filtre) != -1 ) || ( mention.search(twCx.filtre) != -1 );
         });
         $("#inp_q").val(twCx.filtreTexte + ' (' + tweets.length + ' tweets)');
@@ -1059,21 +1040,6 @@
     document.location.href = "data:text/json;base64," + btoa(_buf);
 }
 
-function saveCSV() {
-    function csvEncode(tableau) {
-      return _(tableau).map(function(el) {
-          return '"' + unescape(encodeURIComponent(el)).replace(/"/gm, '""') + '"';
-      }).join(",")
-    };
-    var _csvfields = [ "id", "from_user", "from_user_name", "created_at", "text" ],
-        _csvtxt = csvEncode(_csvfields) + "\n" + _(twCx.tweets).map(function(tw) {
-            return csvEncode(_(_csvfields).map(function(field) {
-                return tw[field];
-            }));
-        }).join("\n");
-    document.location.href = "data:text/csv;base64," + btoa(_csvtxt);
-}
-
 $(document).ready(function() {
     twCx.tlWidth = $("#timeline").width();
     twCx.tlHeight = $("#timeline").height();