-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitter.js
458 lines (400 loc) · 13.8 KB
/
twitter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
dojo.provide("social.twitter");
dojo.require("esri.map");
dojo.require("esri.geometry");
dojo.require("esri.layers.FeatureLayer");
dojo.require("esri.dijit.Popup");
dojo.addOnLoad(function(){
dojo.declare("social.twitter", null, {
// Doc: http://docs.dojocampus.org/dojo/declare#chaining
"-chains-": {
constructor: "manual"
},
constructor: function(options){
this._map = options.map || null;
if (this._map === null) {
throw "social.twitter says: Reference to esri.Map object required";
}
this.autopage = options.autopage || true;
this.maxpage = options.maxpage || 5;
//create feature layer for Tweets
this.featureCollection = {
layerDefinition: {
"geometryType": "esriGeometryPoint",
"drawingInfo": {
"renderer": {
"type": "simple",
"symbol": {
"type": "esriPMS",
"url": "images/twitter-point-16x20.png",
"contentType": "image/png",
"width": 16,
"height": 20
}
}
},
"fields": [{
"name": "OBJECTID",
"type": "esriFieldTypeOID"
}, {
"name": "created_at",
"type": "esriFieldTypeDate",
"alias": "Created"
}, {
"name": "id",
"type": "esriFieldTypeString",
"alias": "id",
"length": 100
}, {
"name": "from_user",
"type": "esriFieldTypeString",
"alias": "User",
"length": 100
}, {
"name": "location",
"type": "esriFieldTypeString",
"alias": "Location",
"length": 1073741822
}, {
"name": "place",
"type": "esriFieldTypeString",
"alias": "Place",
"length": 100
}, {
"name": "text",
"type": "esriFieldTypeString",
"alias": "Text",
"length": 1073741822
}, {
"name": "profile_image_url",
"type": "esriFieldTypeString",
"alias": "ProfileImage",
"length": 255
}],
"globalIdField": "id",
"displayField": "from_user"
},
featureSet: {
"features": [],
"geometryType": "esriGeometryPoint"
}
};
var popupTemplate = new esri.dijit.PopupTemplate({
title: "User:{from_user}",
description: "Location:{location}"
});
this.infoTemplate = new esri.InfoTemplate();
this.infoTemplate.setTitle(function(graphic){
return graphic.attributes.from_user;
});
this.infoTemplate.setContent(this.getWindowContent);
this.featureLayer = new esri.layers.FeatureLayer(this.featureCollection, {
id: 'twitterFeatureLayer',
outFields: ["*"],
name: "Twitter",
infoTemplate: this.infoTemplate
});
this._map.addLayer(this.featureLayer);
dojo.connect(this.featureLayer, "onClick", dojo.hitch(this, function(evt){
var query = new esri.tasks.Query();
query.geometry = this.pointToExtent(this._map, evt.mapPoint, 20);
var deferred = this.featureLayer.selectFeatures(query, esri.layers.FeatureLayer.SELECTION_NEW);
this._map.infoWindow.setFeatures([deferred]);
this._map.infoWindow.show(evt.mapPoint);
}));
this.stats = {
geoPoints: 0,
geoNames: 0,
noGeo: 0
};
this.dataPoints = [];
this.deferreds = [];
this.geocoded_ids = {};
this.name = "Twitter";
this.loaded = true;
},
/*****************
* Public Methods
*****************/
update: function(searchTerm){
this.clear();
this.constructQuery(searchTerm);
},
pointToExtent: function(map, point, toleranceInPixel){
var pixelWidth = map.extent.getWidth() / map.width;
var toleraceInMapCoords = toleranceInPixel * pixelWidth;
return new esri.geometry.Extent(point.x - toleraceInMapCoords, point.y - toleraceInMapCoords, point.x + toleraceInMapCoords, point.y + toleraceInMapCoords, map.spatialReference);
},
getStats: function(){
var x = this.stats;
x.total = this.stats.geoPoints + this.stats.noGeo + this.stats.geoNames;
return x;
},
getPoints: function(){
return this.dataPoints;
},
clear: function(){
//cancel any outstanding requests
this.continueQuery = false;
dojo.forEach(this.deferreds, function(def){
def.cancel();
});
if (this.deferreds) {
this.deferreds.length = 0;
}
//remove existing tweets
if (this._map.infoWindow.isShowing) {
this._map.infoWindow.hide();
}
if (this.featureLayer.graphics.length > 0) {
this.featureLayer.applyEdits(null, null, this.featureLayer.graphics);
}
// clear stats and points
this.stats = {
geoPoints: 0,
noGeo: 0,
geoNames: 0
};
this.dataPoints = [];
this.geocoded_ids = {};
this.onClear();
},
show: function(){
this.featureLayer.setVisibility(true);
},
hide: function(){
this.featureLayer.setVisibility(false);
},
setVisibility: function(val){
if(val){
this.show();
}
else{
this.hide();
}
},
getExtent: function(){
return esri.graphicsExtent(this.featureLayer.graphics);
},
getRadius: function(){
var map = this._map;
var extent = this.extent || map.extent;
var radius = Math.min(932, Math.ceil(esri.geometry.getLength(new esri.geometry.Point(extent.xmin, extent.ymin, map.spatialReference), new esri.geometry.Point(extent.xmax, extent.ymin, map.spatialReference)) * 3.281 / 5280 / 2));
radius = Math.round(radius, 0);
return {
radius: radius,
center: map.extent.getCenter(),
units: "mi"
};
},
setSearchExtent: function(extent){
this.extent = extent;
},
/*******************
* Internal Methods
*******************/
getWindowContent: function(graphic){
//define content for the tweet pop-up window.
var reg_exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i;
var tweetText = graphic.attributes.text.replace(reg_exp, "<br/><a href='$1' target='_blank'>$1</a><br/>");
var content = "<table><tr><td valign='top'><img align='left' style='margin:0 5px 5px 0;' class='round shadow' src='" + graphic.attributes.profile_image_url + "' width='64' height='64' /><b>" + graphic.attributes.from_user + ":</b><br/>" + tweetText + "</td></tr></table>";
return content;
},
constructQuery: function(searchValue){
//limit is the number of results returned per page - max 100
//maximum number of results that can be returned are 1500.
var limit = "100";
//specify search radius - has to be smaller than 1500 kilometers (932 miles) and
//greater than 1 meter
//radius is half the width of the bottom border of the map
var map = this._map;
var radius = this.getRadius().radius;
var baseurl = "http://search.twitter.com/search.json";
var search = dojo.trim(searchValue);
if (search.length === 0) {
search = "";
}
var extent = this.extent || map.extent;
var center = extent.getCenter();
center = esri.geometry.webMercatorToGeographic(center);
var d1= new Date();
var since_secs = (d1.getTime()-d1.getMilliseconds()) - (5*24*60*60*1000)
var query = {
q: search,
rpp: limit,
since_id: since_secs,
result_type: "mixed",
geocode: center.y + "," + center.x + "," + radius + "mi"
};
//start Twitter API call of several pages
this.continueQuery = true;
this.pageCount = 1;
this.sendRequest(baseurl + "?" + dojo.objectToQuery(query));
},
sendRequest: function(url){
//get the results from twitter for each page
var deferred = esri.request({
url: url,
handleAs: "json",
timeout: 10000,
callbackParamName: "callback",
preventCache: false,
load: dojo.hitch(this, function(data){
var res = this.unbindDef(deferred);
if (data.results.length > 0) {
this.mapResults(data);
//display results for multiple pages
if ((this.autopage) && (this.maxpage > this.pageCount) && (data.next_page !== undefined) && (this.continueQuery)) {
this.pageCount++;
this.sendRequest("http://search.twitter.com/search.json" + data.next_page)
}
else{
this.onUpdateEnd();
}
}
else {
// No results found, try another search term
this.onUpdateEnd();
}
}),
error: dojo.hitch(this, function(e){
if (deferred.canceled) {
console.log("Search Cancelled");
}
else {
console.log("Search error : " + e.message);
var res = this.unbindDef(deferred);
}
this.onError(e);
})
});
this.deferreds.push(deferred);
},
unbindDef: function(dfd){
//if deferred has already finished, remove from deferreds array
var index = dojo.indexOf(this.deferreds, dfd);
if (index === -1) {
return; // did not find
}
this.deferreds.splice(index, 1);
if (!this.deferreds.length) {
return 2; // indicates we received results from all expected deferreds
}
return 1; // found and removed
},
mapResults: function(j){
if (j.error) {
console.log("mapResults error: " + j.error);
this.onError(j.error);
return
}
var b = [];
var k = j.results;
dojo.forEach(k, dojo.hitch(this, function(result){
// eliminate Tweets which we have on the map
if (this.geocoded_ids[result.id]) {
return
}
this.geocoded_ids[result.id] = true;
var geoPoint = null;
if(result.geo) {
var g = result.geo.coordinates;
geoPoint = new esri.geometry.Point(parseFloat(g[1]), parseFloat(g[0]))
}
else {
var n = result.location;
if(n){
// try some different parsings for result.location
if (n.indexOf("iPhone:") > -1) {
n = n.slice(7);
var f = n.split(",");
geoPoint = new esri.geometry.Point(parseFloat(f[1]), parseFloat(f[0]))
}
else if (n.indexOf("�T") > -1) {
n = n.slice(3);
var e = n.split(",");
geoPoint = new esri.geometry.Point(parseFloat(e[1]), parseFloat(e[0]))
}
else if (n.indexOf("T") == 1) {
n = n.slice(3);
var e = n.split(",");
geoPoint = new esri.geometry.Point(parseFloat(e[1]), parseFloat(e[0]))
}
else if (n.indexOf("Pre:") > -1) {
n = n.slice(4);
var d = n.split(",");
geoPoint = new esri.geometry.Point(parseFloat(d[1]), parseFloat(d[0]));
}
else if (n.split(",").length == 2) {
var c = n.split(",");
if (c.length == 2 && parseFloat(c[1]) && parseFloat(c[0])) {
geoPoint = new esri.geometry.Point(parseFloat(c[1]), parseFloat(c[0]));
}
else {
// location cannot be interpreted by this geocoder
this.stats.geoNames++;
return;
}
}
else {
// location cannot be interpreted by this geocoder
this.stats.geoNames++;
return;
}
}
else{
// location cannot be interpreted by this geocoder
this.stats.geoNames++;
return;
}
}
if (geoPoint) {
//last check to make sure we parsed it right
if (isNaN(geoPoint.x) || isNaN(geoPoint.y)) {
//discard bad geopoints
this.stats.noGeo++;
}
else {
// convert the Point to WebMercator projection
var a = new esri.geometry.geographicToWebMercator(geoPoint);
// make the Point into a Graphic
var attr = {};
attr.from_user = result.from_user;
attr.location = result.location;
attr.text = result.text;
attr.id = result.id;
attr.profile_image_url = result.profile_image_url;
attr.created_at = result.created_at;
attr.place = "";
if (result.place) {
attr.place = result.place.full_name || "";
}
var graphic = new esri.Graphic(a);
graphic.setAttributes(attr);
b.push(graphic);
this.dataPoints.push({
x: a.x,
y: a.y
});
this.stats.geoPoints++;
}
}
else {
this.stats.noGeo++;
}
}));
this.featureLayer.applyEdits(b, null, null);
this.onUpdate();
},
/****************
* Miscellaneous
****************/
onUpdate: function(){
},
onUpdateEnd: function(){
},
onClear: function(){
},
onError: function(info){
}
}); // end of class declaration
}); // end of addOnLoad