forked from cutting-room-floor/live-tweets-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetRace.js
201 lines (173 loc) · 5.72 KB
/
tweetRace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var tweetRace = {};
// Array for tweets queue
tweetRace.tweets = [];
// Start the race with settings from tweets[]
tweetRace.start = _.throttle(function(){
$('th.first').text(tweets[0] + ' tweets');
$('th.second').text(tweets[1] + ' tweets');
$('th.third').text(tweets[2] + ' tweets');
$('th.fourth').text(tweets[3] + ' tweets');
$('th.fifth').text(tweets[4] + ' tweets');
tweetRace.getTweets(tweets[0] + ' OR ' + tweets[1] + ' OR ' + tweets[2] + ' OR ' + tweets[3] + ' OR ' + tweets[4],tweets[5]);
}, 2000);
tweetRace.params = {
rpp: 100,
callback: 'callback',
result_type: 'recent'
};
// Fetch tweets from Twitter
tweetRace.getTweets = function(query, geo) {
tweetRace.params.q = query;
tweetRace.params.geocode = geo;
params = '?' + _.map(tweetRace.params, function(num, key) {
return key + "=" + num;
}).join('&');
reqwest({
url: 'http://search.twitter.com/search.json' + params,
type: 'jsonp',
jsonCallback: 'callback',
success: function(d) {
tweetRace.processTweet(d);
}
});
};
// Extract relevant data from tweets
tweetRace.processTweet = function(d) {
_.each(d.results, function(element, index) {
if (element.geo && element.geo.type === 'Point') {
var lat = element.geo.coordinates[0], // Twitter seems to reverse the
lon = element.geo.coordinates[1]; // order of geojson coordinates
} else if (element.location && element.location.indexOf(': ') > 0) {
var coords = element.location.split(': ')[1],
$lat = coords.split(',')[0] || 0,
$lon = coords.split(',')[1] || 0;
if (!isNaN(parseFloat($lat)) && !isNaN(parseFloat($lon))) {
var lon = parseFloat($lon),
lat = parseFloat($lat);
}
}
if (lat && lon) {
tweetRace.tweets.push({
lon: lon,
lat: lat,
time: formatDate(new Date(element.created_at)),
text: element.text,
user: '@' + element.from_user,
category: (element.text.toLowerCase().indexOf(tweets[0].toLowerCase()) >= 0) ? 'first' : 'second' : 'third' : 'fourth' : 'fifth'
});
}
});
tweetRace.params.since_id = d.max_id_str;
tweetRace.map();
function formatDate(d) {
var hours = d.getHours();
var minutes = d.getMinutes();
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) hours = 12;
if (minutes < 10) minutes = "0" + minutes;
return hours + ":" + minutes + " " + suffix;
}
};
// Update counters
tweetRace.counters = function() {
var first = $('.mmg-first').length,
second = $('.mmg-second').length,
third = $('.mmg-third').length,
fourth = $('.mmg-fourth').length,
fifth = $('.mmg-fifth').length;
if (first > 0) {
$('td.first').text(first);
} else {
$('td.first').text('--');
}
if (second > 0) {
$('td.second').text(second);
} else {
$('td.second').text('--');
}
if (third > 0) {
$('td.third').text(third);
} else {
$('td.third').text('--');
}
if (fourth > 0) {
$('td.fourth').text(fourth);
} else {
$('td.fourth').text('--');
}
if (fifth > 0) {
$('td.fifth').text(fifth);
} else {
$('td.fifth').text('--');
}
};
// Map the tweets
tweetRace.map = function() {
var points = { 'type': 'FeatureCollection',
'features': []
};
_.each(_.rest(tweetRace.tweets, tweetRace.last || 0), function(tweet) {
points.features.push({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [tweet.lon, tweet.lat]
},
properties: {
time: tweet.time,
text: tweet.text,
user: tweet.user,
category: tweet.category
}
});
});
tweetRace.last = tweetRace.tweets.length;
if (MM_map.tweetLayer) {
MM_map.tweetLayer.geojson(points);
} else {
MM_map.tweetLayer = mmg().factory(function(x) {
var d = document.createElement('div'),
overlay = document.createElement('div'),
arrow = document.createElement('div'),
user = document.createElement('div'),
time = document.createElement('div');
d.className = 'mmg mmg-' + x.properties.category;
overlay.innerHTML = x.properties.text;
overlay.className = 'overlay';
user.innerHTML = x.properties.user;
user.className = 'user';
time.innerHTML = x.properties.time;
time.className = 'time';
arrow.className = 'arrow-down';
overlay.appendChild(user);
overlay.appendChild(time);
d.appendChild(overlay);
d.appendChild(arrow);
return d;
}).geojson(points);
MM_map.addLayer(MM_map.tweetLayer);
}
MM_map.setCenter({
lat: MM_map.getCenter().lat,
lon: MM_map.getCenter().lon
});
tweetRace.counters();
if (!tweetRace.stop) tweetRace.start();
};
// Pause / play button
$('#stop').click(function(e){
e.preventDefault();
$(this).toggleClass('start');
if (tweetRace.stop) {
$(this).text('Pause');
tweetRace.stop = false;
tweetRace.start();
} else {
$(this).text('Start');
tweetRace.stop = true;
}
});