|
1 var THRESHOLD = 10, |
|
2 DROPCOUNT = 12; |
|
3 |
|
4 var swTw = { |
|
5 "tweets" : [], |
|
6 "tweetsIndex" : [], |
|
7 "twInCol" : [], |
|
8 "firstDisplayedTweet" : 0, |
|
9 "cursor" : -1, |
|
10 "annotations" : { |
|
11 "positive" : { |
|
12 "keyword" : "++", |
|
13 "colors" : { |
|
14 "tweet" : "#c5e7cd", |
|
15 "timeline" : "#1D973D" |
|
16 } |
|
17 }, |
|
18 "negative" : { |
|
19 "keyword" : "--", |
|
20 "colors" : { |
|
21 "tweet" : "#f6ced0", |
|
22 "timeline" : "#CE0A15" |
|
23 } |
|
24 }, |
|
25 "reference" : { |
|
26 "keyword" : "==", |
|
27 "colors" : { |
|
28 "tweet" : "#efefa1", |
|
29 "timeline" : "#C5A62D" |
|
30 } |
|
31 }, |
|
32 "question" : { |
|
33 "keyword" : "??", |
|
34 "colors" : { |
|
35 "tweet" : "#bfdbec", |
|
36 "timeline" : "#036AAE" |
|
37 } |
|
38 } |
|
39 } |
|
40 } |
|
41 |
|
42 function highlightKeyword(stra, strb, color) { |
|
43 var rgxp = RegExp( '(' + strb.replace(/(\W)/gm, '\\$1') + ')', "gim"); |
|
44 return stra.replace(rgxp, '<span class="highlight" style="background:' + color + ';">$1</span>'); |
|
45 } |
|
46 |
|
47 function highlightText(txt) { |
|
48 res = suggested_keywords.reduce(function(a, b) { |
|
49 return highlightKeyword(a,b, '#333333'); |
|
50 }, txt); |
|
51 res = _(swTw.annotations).reduce(function(a, b) { |
|
52 return (b.keyword ? highlightKeyword(a,b.keyword,b.colors.timeline) : a); |
|
53 }, res); |
|
54 return res; |
|
55 } |
|
56 |
|
57 function nextTweet() { |
|
58 if (!swTw.tweets.length) { |
|
59 return; |
|
60 } |
|
61 if (swTw.cursor < swTw.tweets.length - 1) { |
|
62 swTw.cursor = Math.max(swTw.cursor + 1, swTw.tweets.length - 120); |
|
63 var tweet = swTw.tweets[swTw.cursor], |
|
64 html = '<img src="' |
|
65 + tweet.profile_image_url |
|
66 + '" /><p>@' |
|
67 + tweet.from_user |
|
68 + ' (' |
|
69 + tweet.from_user_name |
|
70 + ')' |
|
71 + '</p><p class="tweet_text">' |
|
72 + highlightText(tweet.text) |
|
73 + '</p>'; |
|
74 $("#tweetcont").html(html) |
|
75 } else { |
|
76 $("#tweetcont").html("") |
|
77 } |
|
78 } |
|
79 |
|
80 function dropOldTweets() { |
|
81 var _newPos = swTw.firstDisplayedTweet + DROPCOUNT; |
|
82 _(swTw.tweets.slice(swTw.firstDisplayedTweet,_newPos)).each(function(tweet) { |
|
83 swTw.twInCol = _(swTw.twInCol).map(function(col) { |
|
84 return _(col).without(tweet.id_str); |
|
85 }); |
|
86 _(tweet.elements).each(function(elid) { |
|
87 $("#" + elid).animate({ |
|
88 "width": "0px" |
|
89 }, |
|
90 2000, |
|
91 function() { |
|
92 $(this).detach(); |
|
93 }) |
|
94 }); |
|
95 }); |
|
96 swTw.firstDisplayedTweet = _newPos; |
|
97 } |
|
98 |
|
99 function callbackTweets(tweets) { |
|
100 _(tweets).each(function(tweet) { |
|
101 var tl = tweet.text.toLowerCase(); |
|
102 tweet.columns = suggested_keywords.filter(function(word) { |
|
103 return tl.search(word) != -1 |
|
104 }); |
|
105 tweet.elements = []; |
|
106 _(tweet.columns).each(function(word) { |
|
107 var iword = suggested_keywords.indexOf(word), |
|
108 icol = ( swTw.twInCol[iword*2+1].length < swTw.twInCol[iword*2].length ? iword*2+1 : iword*2 ), |
|
109 elid = 'avatar_' + tweet.id_str + '_' + iword, |
|
110 colA = swTw.colAnnot[iword], |
|
111 ttl = 0; |
|
112 _(swTw.annotations).each(function(v,k) { |
|
113 if (tweet.text.indexOf(v.keyword) != -1) { |
|
114 colA[k]++; |
|
115 ttl++; |
|
116 } |
|
117 }); |
|
118 colA.total += Math.max(1,ttl); |
|
119 $('#tube_' + icol).append( |
|
120 '<div class="avatar" id="' |
|
121 + elid |
|
122 + '" style="margin-right: 500px;"><img src="' |
|
123 + tweet.profile_image_url |
|
124 + '" /></div>' |
|
125 ); |
|
126 tweet.elements.push(elid); |
|
127 swTw.twInCol[icol].push(tweet.id_str); |
|
128 $("#" + elid).animate({ |
|
129 "margin-right" : "0px" |
|
130 }, |
|
131 2000); |
|
132 }) |
|
133 swTw.tweets.push(tweet); |
|
134 swTw.tweetsIndex.push(tweet.id_str); |
|
135 }); |
|
136 while (_(swTw.twInCol).any(function(col) { |
|
137 return col.length > THRESHOLD |
|
138 })) { |
|
139 dropOldTweets(); |
|
140 } |
|
141 _(swTw.colAnnot).each(function(colA, i) { |
|
142 _(swTw.annotations).each(function(v, k) { |
|
143 $("#polemic_" + i + "_" + k).css("width", ~~(100 * colA[k] / colA.total) + "%"); |
|
144 }); |
|
145 }) |
|
146 } |
|
147 |
|
148 function retrieveTweets() { |
|
149 var options = { |
|
150 // "keyword" : swTw.columns_words.join(" OR "), |
|
151 // "lang" : "fr", |
|
152 "keyword" : tracking_keywords.join(" OR "), |
|
153 "pages" : 1, |
|
154 "rpp" : 100, |
|
155 "cbEnd" : function() { |
|
156 callbackTweets(this.tweets); |
|
157 } |
|
158 } |
|
159 if (swTw.tweets.length) { |
|
160 options.since_id = swTw.tweets[swTw.tweets.length - 1].id_str; |
|
161 } |
|
162 getTweets(options); |
|
163 } |
|
164 |
|
165 function getTweets(options) { |
|
166 function getTweetUrl(url) { |
|
167 $.getJSON(url, function(data) { |
|
168 options.tweets = options.tweets.concat(data.results); |
|
169 options.currentPage = data.page; |
|
170 if (options.cbData) { |
|
171 options.cbData(); |
|
172 } |
|
173 if (data.next_page && data.page < options.pages) { |
|
174 getTweetUrl(baseurl + data.next_page + suffix); |
|
175 } else { |
|
176 options.tweets.sort(function(a,b) { |
|
177 return a.id - b.id; |
|
178 }); |
|
179 if (options.cbEnd) { |
|
180 options.cbEnd(); |
|
181 } |
|
182 } |
|
183 }); |
|
184 } |
|
185 |
|
186 options.tweets = []; |
|
187 options.pages || (options.pages = 1); |
|
188 options.rpp || (options.rpp = 100); |
|
189 options.currentPage = 0; |
|
190 |
|
191 var baseurl = "http://search.twitter.com/search.json", |
|
192 suffix = (options.since_id ? "&since_id=" + options.since_id : '' ) + "&callback=?", |
|
193 jsonurl = baseurl + "?q=" + encodeURIComponent(options.keyword)+ "&rpp=" + options.rpp |
|
194 + (options.lang ? "&lang=" + options.lang : '' ) + suffix; |
|
195 getTweetUrl(jsonurl); |
|
196 } |
|
197 |
|
198 $(document).ready(function() { |
|
199 $("#columncont").html( suggested_keywords.map( |
|
200 function(mot, i) { |
|
201 return '<div class="column" id="column_' |
|
202 + i |
|
203 + '"><div class="column-tube"><div class="tube" id="tube_' |
|
204 + (2*i) |
|
205 + '"></div><div class="tube" id="tube_' |
|
206 + (2*i+1) |
|
207 + '"></div></div><div class="column-title">' |
|
208 + _(swTw.annotations).map(function(v,k) { |
|
209 return '<div class="polemicvol" id="polemic_' |
|
210 + i |
|
211 + '_' |
|
212 + k |
|
213 + '" style="background:' |
|
214 + v.colors.timeline |
|
215 + '"></div>' |
|
216 }).join('') |
|
217 +'<h3>' |
|
218 + mot |
|
219 + '</h3></div></div>' |
|
220 } |
|
221 ).join("") ); |
|
222 |
|
223 for (var i = 0; i < suggested_keywords.length * 2; i++) { |
|
224 swTw.twInCol.push([]); |
|
225 } |
|
226 |
|
227 swTw.colAnnot = suggested_keywords.map(function() { |
|
228 var res = { "total" : 0 } |
|
229 _(swTw.annotations).map(function(v,k) { |
|
230 res[k] = 0; |
|
231 }); |
|
232 return res; |
|
233 }); |
|
234 |
|
235 retrieveTweets(); |
|
236 |
|
237 setInterval(retrieveTweets, 5000); |
|
238 |
|
239 setInterval(nextTweet, 6000); |
|
240 |
|
241 }); |