|
3
|
1 |
/* Author: Raphaël Velt, IRI |
|
|
2 |
* |
|
|
3 |
* Licence: CeCILL-B - http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.html |
|
|
4 |
* |
|
|
5 |
* */ |
|
|
6 |
|
|
|
7 |
Btv_TweetArray = function() { |
|
|
8 |
this.tweets = []; |
|
|
9 |
this.idIndex = []; |
|
|
10 |
} |
|
|
11 |
|
|
|
12 |
Btv_TweetArray.prototype.push = function(_tweet) { |
|
|
13 |
this.tweets.push(_tweet); |
|
|
14 |
this.idIndex.push(_tweet.id_str); |
|
|
15 |
} |
|
|
16 |
|
|
|
17 |
Btv_TweetArray.prototype.addTweet = function(_tweet, _inhibitCallback) { |
|
|
18 |
if (this.idIndex.indexOf(_tweet.id_str) != -1) { |
|
|
19 |
return; |
|
|
20 |
} |
|
|
21 |
if (!_tweet.date_value) { |
|
|
22 |
_tweet.date_value = Date.parse(_tweet.created_at.replace(/(\+|-)/,'UTC$1')); |
|
|
23 |
} |
|
|
24 |
var _pos = this.tweets.length; |
|
|
25 |
while (_pos && this.idIndex[_pos - 1] > _tweet.id_str) { |
|
|
26 |
_pos--; |
|
|
27 |
} |
|
|
28 |
this.tweets.splice(_pos,0,_tweet); |
|
|
29 |
this.idIndex.splice(_pos,0,_tweet.id_str); |
|
|
30 |
} |
|
|
31 |
|
|
|
32 |
Btv_TweetArray.prototype.addMultipleTweets = function(_multiTweets) { |
|
|
33 |
var _l = _multiTweets.length; |
|
|
34 |
for (var _i = 0; _i < _l; _i++) { |
|
|
35 |
this.addTweet(_multiTweets[_i], true); |
|
|
36 |
} |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
Btv_TweetArray.prototype.count = function() { |
|
|
40 |
return this.tweets.length; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
Btv_TweetArray.prototype.getTweet = function(_i) { |
|
|
44 |
return this.tweets(_i); |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
Btv_TweetArray.prototype.slice = function(_start, _end) { |
|
|
48 |
var _slice = this.tweets.slice(_start, _end), |
|
|
49 |
_result = new Btv_TweetArray(), |
|
|
50 |
_l = _slice.length; |
|
|
51 |
for (var _i = 0; _i < _l; _i++) { |
|
|
52 |
_result.push(_slice[_i]); |
|
|
53 |
} |
|
|
54 |
return _result; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
Btv_TweetArray.prototype.reverse = function() { |
|
|
58 |
var _result = new Btv_TweetArray(), |
|
|
59 |
_l = this.tweets.length; |
|
|
60 |
for (var _i = _l-1; _i >= 0; _i--) { |
|
|
61 |
_result.push(this.tweets[_i]); |
|
|
62 |
} |
|
|
63 |
return _result; |
|
|
64 |
} |
|
|
65 |
|
|
|
66 |
Btv_TweetArray.prototype.each = function(_callback) { |
|
|
67 |
var _l = this.count(); |
|
|
68 |
for (var _i = 0; _i < _l; _i++) { |
|
|
69 |
_callback(this.tweets[_i]); |
|
|
70 |
} |
|
|
71 |
} |
|
|
72 |
|
|
|
73 |
Btv_TweetArray.prototype.map = function(_callback) { |
|
|
74 |
var _result = []; |
|
|
75 |
this.each(function(_tweet) { |
|
|
76 |
_result.push(_callback(_tweet)) |
|
|
77 |
}); |
|
|
78 |
return _result; |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
Btv_TweetArray.prototype.search = function(_filter) { |
|
|
82 |
var _filtered = new Btv_TweetArray(), |
|
|
83 |
_reFilter = new RegExp(_filter.replace(/\W/gim,'\\$1'),'gim'); |
|
|
84 |
this.each(function(_tweet) { |
|
|
85 |
var _mention = '@' + _tweet.from_user; |
|
|
86 |
if (( _tweet.text.search(_reFilter) != -1 ) || ( _mention.search(_reFilter) != -1 )) { |
|
|
87 |
_filtered.push(_tweet); |
|
|
88 |
} |
|
|
89 |
}); |
|
|
90 |
return _filtered; |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
Btv_TweetArray.prototype.beforeDate = function(_date) { |
|
|
94 |
var _filtered = new Btv_TweetArray(); |
|
|
95 |
this.each(function(_tweet) { |
|
|
96 |
if (_tweet.date_value < _date) { |
|
|
97 |
_filtered.push(_tweet); |
|
|
98 |
} |
|
|
99 |
}); |
|
|
100 |
return _filtered; |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
Btv_TweetArray.prototype.afterDate = function(_date) { |
|
|
104 |
var _filtered = new Btv_TweetArray(); |
|
|
105 |
this.each(function(_tweet) { |
|
|
106 |
if (_tweet.date_value > _date) { |
|
|
107 |
_filtered.push(_tweet); |
|
|
108 |
} |
|
|
109 |
}); |
|
|
110 |
return _filtered; |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
Btv_TweetArray.prototype.tweetById = function(_tweetId) { |
|
|
114 |
var _index = this.idIndex.indexOf(_tweetId); |
|
|
115 |
return (_index ? this.tweets[_index] : null); |
|
|
116 |
} |
|
|
117 |
|
|
|
118 |
Btv_TweetArray.prototype.lastTweet = function() { |
|
|
119 |
return this.tweets[this.tweets.length - 1]; |
|
|
120 |
} |
|
|
121 |
|
|
|
122 |
/* |
|
|
123 |
* |
|
|
124 |
*/ |
|
|
125 |
|
|
|
126 |
Btv_TweetSource = function(_opts) { |
|
|
127 |
Btv_TweetArray.call(this); |
|
|
128 |
this.loading = false; |
|
|
129 |
if (!_opts || !_opts.keywords || !_opts.keywords.length) { |
|
|
130 |
return; |
|
|
131 |
} |
|
|
132 |
this.options = _opts; |
|
|
133 |
var _this = this; |
|
|
134 |
this.retrieveInitialTweets() |
|
|
135 |
setInterval(function() { |
|
|
136 |
_this.retrieveNewTweets(); |
|
|
137 |
}, 5000); |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
Btv_TweetSource.prototype = new Btv_TweetArray(); |
|
|
141 |
|
|
|
142 |
Btv_TweetSource.prototype.retrieveTweets = function(_opts) { |
|
|
143 |
|
|
|
144 |
function getTwitterUrl(url) { |
|
|
145 |
$.getJSON(url, function(data) { |
|
|
146 |
_currentPage++; |
|
|
147 |
var _isLast = true; |
|
|
148 |
if (data.results && data.results.length) { |
|
|
149 |
_this.addMultipleTweets(data.results); |
|
|
150 |
var _oldestTweetId = data.results[data.results.length - 1].id_str, |
|
|
151 |
_maxId = _oldestTweetId; |
|
|
152 |
if (_currentPage < _opts.pages) { |
|
|
153 |
_isLast = false; |
|
|
154 |
getTwitterUrl(_baseurl + _firstparams + '&max_id=' + _maxId + _lastparams); |
|
|
155 |
} |
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
if (_isLast) { |
|
|
159 |
_this.loading = false; |
|
|
160 |
if (_this.tweetsCallback) { |
|
|
161 |
_this.tweetsCallback(); |
|
|
162 |
} |
|
|
163 |
} |
|
|
164 |
}); |
|
|
165 |
} |
|
|
166 |
|
|
|
167 |
if (this.loading) { |
|
|
168 |
return; |
|
|
169 |
} |
|
|
170 |
this.loading = true; |
|
|
171 |
|
|
|
172 |
var _baseurl = "http://search.twitter.com/search.json", |
|
|
173 |
_currentPage = 0, |
|
|
174 |
_firstparams = "?q=" |
|
|
175 |
+ encodeURIComponent(this.options.keywords.join(' OR ')) |
|
|
176 |
+ "&rpp=100" |
|
|
177 |
+ (this.options.lang ? "&lang=" + this.options.lang : '' ), |
|
|
178 |
_lastparams = ( _opts.since_id ? "&since_id=" + _opts.since_id : '' ) |
|
|
179 |
+ "&callback=?", |
|
|
180 |
_jsonurl = _baseurl + _firstparams + _lastparams, |
|
|
181 |
_this = this; |
|
|
182 |
getTwitterUrl(_jsonurl); |
|
|
183 |
|
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
Btv_TweetSource.prototype.retrieveInitialTweets = function() { |
|
|
187 |
this.retrieveTweets({ |
|
|
188 |
"pages": 6 |
|
|
189 |
}); |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
Btv_TweetSource.prototype.retrieveNewTweets = function() { |
|
|
193 |
this.retrieveTweets({ |
|
|
194 |
"pages": 1, |
|
|
195 |
"since_id": this.lastTweet().id_str |
|
|
196 |
}); |
|
|
197 |
} |
|
|
198 |
|
|
|
199 |
Btv_TweetArray.prototype.setTweetsCallback = function(_callback) { |
|
|
200 |
this.tweetsCallback = _callback; |
|
|
201 |
} |
|
|
202 |
|
|
|
203 |
/* |
|
|
204 |
Btv_TweetSubscriber = function(tweetArray) { |
|
|
205 |
this.tweetArray = tweetArray; |
|
|
206 |
this.position = 0; |
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
Btv_TweetSubscriber.getTweets = function() { |
|
|
210 |
var _p = this.position, |
|
|
211 |
_l = this.tweetArray.count(); |
|
|
212 |
this.position = _l; |
|
|
213 |
return this.tweetArray.slice(_p,_l); |
|
|
214 |
} |
|
|
215 |
*/ |