-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultigeo.pl
694 lines (521 loc) · 18.3 KB
/
multigeo.pl
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE":
# <[email protected]> wrote this file. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.
# ----------------------------------------------------------------------------
#
#
# Multi-geo extension for TTYtter.
#
# This extension builds upon the gpsd, geoip, place, teleport, where and where-inline extensions, trying to unifying all of them to provide an improved geolocation experience on TTYtter.
#
# Read http://ivan.sanchezortega.es/ttytter for details on the commands provided.
#
#
#
#
# TODO: Implement geocoder cache
# FIXME: Somehow turn on localization for geocoders. At least GeoNames allows a "lang=xx" parameter to be passed.
# FIXME: Let /place and /teleport override GPSD, at least when GPS is offline / there is no fix.
# This shall be accomplished by setting a override flag, cleaned up upon "/place off".
# Thus, GPSD would check that flag and NOT undef(lat) and undef(lon) if flag is on.
# TODO: Warn users that Geo::IP needs STUN::Client (as scraping whatismyip.org no longer works)
# TODO: Add Twitter's geocode API (see https://dev.twitter.com/docs/api#places-geo)
# TODO: Resolve FourSquare URLs into lat-long if tweet is not geoenabled (regexp for "lat":-1.23456,"lng":-1.23456, )
# TODO: Make a PlaceID-based geocoder, and allow tweeting with that.
# TODO: Make /teleport work with place IDs besides coordinates.
use Data::Dumper qw{Dumper};
print "-- Loading MultiGeo extension\n";
our %geocoder_cache = ();
our %store = ( "cache_misses", 0, "cache_limit", 2000, "cache_flushes", 0, "cache_hit_count", 0);
my $reversegeocoder;
my $geocoder;
# TODO: allow remote hosts for GPSD, via extpref variables.
my $gpsd;
my $gpsd_host;
my $gpsd_port;
my $gpsd_active = false;
if ($extpref_multigeo_gpsd)
{
print "-- MultiGeo will use GPSD to locate your tweets\n";
require Net::GPSD3;
$host=shift || undef;
$port=shift || undef;
$gpsd=Net::GPSD3->new(host=>$host, port=>$port); #default host port as undef
$gpsd_active = true;
}
else
{
print "-- Will not use GPSD\n";
}
if ($extpref_multigeo_geoip)
{
print "-- MultiGeo will use GeoIP to locate your tweets\n";
print "-- This will use STUN (Simple Traversal of UDP through NATs) to determine your public IP address.\n";
require Geo::IP;
require STUN::Client;
}
else
{
print "-- Will not use GeoIP\n";
}
if (not $extpref_multigeo_geocoder)
{
print "-- No geocoder specified, falling back to OpenStreetMap's Nominatim.\n";
$extpref_multigeo_geocoder = "nominatim";
}
# Which geocoder will we use?
if ($extpref_multigeo_geocoder eq "geonames")
{
print "-- Will use GeoNames for geocoding. Have a look at http://www.geonames.org .\n";
$reversegeocoder = sub
{
my $latitude = shift;
my $longitude = shift;
my $r = &grabjson("http://api.geonames.org/findNearbyPlaceNameJSON?formatted=true".
"&lat=" . $latitude .
"&lng=" . $longitude .
"&username=ttytter&style=medium",0,1);
if ($r->{'geonames'}->[0])
{
my $location = $r->{'geonames'}->[0]->{'name'} . ", " . $r->{'geonames'}->[0]->{'adminName1'} . ", " . $r->{'geonames'}->[0]->{'countryName'} ;
return $location;
}
else
{
our $exception;
&$exception(30,"*** Reverse geocoder didn't find any results for given coordinates ($latitude,$longitude). Daily capacity (30000 requests for all ttytter users) might have been reached. Check if geonames.org returns any result.\n");
return;
}
};
$geocoder = sub
{
my $placename = shift;
my $latitude ;
my $longitude ;
my $r = &grabjson("http://api.geonames.org/searchJSON?maxRows=1&formatted=true".
"&q=" . URLEncode($placename) .
"&username=ttytter&style=medium",0,1);
if ($r->{'geonames'}->[0])
{
$latitude = $r->{'geonames'}->[0]->{'lat'} ;
$longitude = $r->{'geonames'}->[0]->{'lng'} ;
$placename = $r->{'geonames'}->[0]->{'name'} . ", " . $r->{'geonames'}->[0]->{'adminName1'} . ", " . $r->{'geonames'}->[0]->{'countryName'} ; ;
}
else
{
our $exception;
&$exception(2,"*** Geocoder didn't find any results for given placename ($placename). Daily capacity (30000 requests for all ttytter users) might have been reached. Check if geonames.org returns any result.\n");
}
# my %result = ("latitude", $latitude, "longitude", $longitude, "placename", $placename);
return [$latitude, $longitude, $placename];
};
}
elsif ($extpref_multigeo_geocoder eq "nominatim")
{
print "-- Will use the OpenStreetMap geocoding service, Nominatim. Geolocation data is CC-by-sa OpenStreetMap contributors. Have a look at http://wiki.openstreetmap.org/wiki/Nominatim\n";
$reversegeocoder = sub
{
my $latitude = shift;
my $longitude = shift;
my $r = &grabjson("http://nominatim.openstreetmap.org/reverse".
"?lat=" . $latitude .
"&lon=" . $longitude .
"&format=json&user-agent=ttytter",0,1);
if ($r->{'display_name'})
{
my $location = $r->{'display_name'};
return $location;
}
else
{
&$exception(30,"*** No results found for the given coordinates, check their validity. Or visit www.osm.org to see if there is anything at all over there.\n");
return;
}
};
$geocoder = sub
{
my $placename = shift;
my $latitude ;
my $longitude ;
my $r = &grabjson("http://nominatim.openstreetmap.org/search".
"?q=" . URLEncode($placename) .
"&format=json&limit=1&user-agent=ttytter",0,1);
# print Dumper("http://nominatim.openstreetmap.org/search".
# "?q=" . URLEncode($command) .
# "&format=json&limit=1&user-agent=ttytter",0,1);;
# print Dumper($r);
# print Dumper($r->[0]);
if ($r->[0])
{
$placename = $r->[0]->{'display_name'} . " (a " . $r->[0]->{'type'} . ")";
$latitude = $r->[0]->{'lat'};
$longitude = $r->[0]->{'lon'};
}
else
{
our $exception;
&$exception(30,"*** No results found for that place. Check your spelling. Or visit www.osm.org to see if the place is listed under other spelling.\n");
}
# my %result = ("latitude", $latitude, "longitude", $longitude, "placename", $placename);
return [$latitude, $longitude, $placename];
};
}
elsif ($extpref_multigeo_geocoder eq "twitter")
{
print "-- Will use the Twitter geocoding service via its API v1.1\n";
$reversegeocoder = sub
{
my $latitude = shift;
my $longitude = shift;
my $r = &grabjson("https://api.twitter.com/1.1/geo/reverse_geocode.json".
"?lat=" . $latitude .
"&long=" . $longitude .
"&granularity=poi&max_results=1&user-agent=ttytter",0,0);
# print Dumper($r);
my $location = $r->{'result'}->{'places'}->[0]->{'name'};
if ($r->{'result'}->{'places'}->[0]->{'contained_within'})
{
$location = $location . ", " .
$r->{'result'}->{'places'}->[0]->{'contained_within'}->[0]->{'name'} . ", " .
$r->{'result'}->{'places'}->[0]->{'contained_within'}->[0]->{'country'};
}
return $location;
};
$geocoder = sub
{
my $placename = shift;
my $latitude ;
my $longitude ;
my $r = &grabjson("https://api.twitter.com/1.1/geo/search.json".
"?query=" . URLEncode($placename) .
"&granularity=poi&max_results=1&user-agent=ttytter",0,0);
# print Dumper($r);
if ($r->{'result'}->{'places'}->[0])
{
$placename = $r->{'result'}->{'places'}->[0]->{'name'} . " (a " . $r->{'result'}->{'places'}->[0]->{'place_type'} . ")";
$latitude = ($r->{'result'}->{'places'}->[0]->{'bounding_box'}->{'coordinates'}->[0]->[0]->[0] +
$r->{'result'}->{'places'}->[0]->{'bounding_box'}->{'coordinates'}->[0]->[2]->[0] ) /2;
$longitude = ($r->{'result'}->{'places'}->[0]->{'bounding_box'}->{'coordinates'}->[0]->[0]->[1] +
$r->{'result'}->{'places'}->[0]->{'bounding_box'}->{'coordinates'}->[0]->[2]->[1] ) /2;
}
else
{
our $exception;
&$exception(30,"*** No results found for that place..\n");
}
# my %result = ("latitude", $latitude, "longitude", $longitude, "placename", $placename);
return [$latitude, $longitude, $placename];
};
}
elsif ($extpref_multigeo_geocoder eq "none")
{
print "-- Will not use any geocoder at all.\n";
$reversegeocoder = sub
{
my $latitude = shift;
my $longitude = shift;
return ;
};
$geocoder = sub
{
my $placename = shift;
return ;
};
}
else
{
our $exception;
&$exception(2,"*** Invalid geocoder specified for multigeo.pl. Please check your configuration.\n");
$reversegeocoder = sub
{
my $latitude = shift;
my $longitude = shift;
return ;
};
$geocoder = sub
{
my $placename = shift;
return ;
};
}
# Auxiliary stuff for direct geocoder (to turn placenames into URL-friendly strings)
sub URLEncode {
my $theURL = $_[0];
$theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
return $theURL;
};
# Prepost will handle GeoIP and GPSD.
$prepost = sub {
our ($lat,$long,$superverbose);
# Let's try GPSD first
if ($extpref_multigeo_gpsd and $gpsd_active)
{
# ...fetch lat and long from GPSD ...
my $poll=$gpsd->poll;
print Dumper($poll) if $superverbose;
if ($poll->active == 0) {
print "-- GPS offline, not using geolocation.\n";
$lat = undef;
$long = undef;
} elsif ($poll->tpv->mode == 0) {
print "-- No GPS fix, not using geolocation.\n";
$lat = undef;
$long = undef;
} else {
# ... and update ttytter's lat and long vars...
# $lat = $poll->tpv->lat + rand() - 0.5;
# $long = $poll->tpv->lon + rand() - 0.5;
$lat = $poll->tpv->lat ;
$long = $poll->tpv->lon ;
print "-- GPSD returned coordinates $lat,$lon\n" if ($verbose);
}
}
# Now that GPSD has had a chance to get a fix, run through GeoIP in case GPSD didn't work.
# The order of extension loading should not affect the result of using GPSD over GeoIP,
# GeoIP is disabled for the time being, due to whatismyip.org being down. If anyone knows about a good method to fetch the computer's current IP address, please let me know. And don't say "STUN", because the CPAN packages don't work.
# if ($extpref_multigeo_geoip)
# {
# if(!$Lib_firstrun){ # Make sure we only tun GeoIP logic just once.
# $Lib_firstrun = true;
#
#
# if ($lat || $long ) # Alas, this will not work on Null Island
# {
# print "-- Coordinates already set, GeoIP will not run\n";
# }
# else
# {
# print "-- GeoIP extension will try to locate your public IP address now\n";
# $ip_addr = &backticks($baseagent, '/dev/null', undef, 'http://whatismyip.org', undef, 0, undef);
#
# # FIXME: How do we make this work on win32 systems?
# my $gi = Geo::IP->open("/usr/share/GeoIP/GeoIPCity.dat", GEOIP_STANDARD);
# my $record = $gi->record_by_addr($ip_addr);
#
# if (!$record ||
# !$record->latitude ||
# !$record->longitude )
# {
# print "-- GeoIP: Sorry, your IP address $ip_addr could not be geolocated.\n";
# }
# else
# {
# my $city = $record->city;
# my $region = $record->region_name;
# my $country = $record->country_name;
# $lat = $record->latitude;
# $long = $record->longitude;
#
# print "-- GeoIP: $ip_addr is near $city, $region, $country\n";
# }
# }
# }
# }
my $tweet = shift;
return $tweet;
};
# Addaction will handle /place, /teleport and /gpsd
$addaction = sub {
my $command = shift;
our ($lat,$long);
# /place
if ($command =~ s#^/place ## && length($command)) {
if ($extpref_multigeo_gpsd and $gpsd_active)
{
print $stdout ("-- Automatically turning off GPSD support. Turn on again with \"/gpsd on\".\n");
$gpsd_active = false;
}
if ($command eq "off") {
print $stdout ("-- Turning geolocation off\n");
undef($lat);
undef($long);
return 1;
} else {
&$utf8_encode($command);
$data = &$geocoder($command); # Returns [lat, lon, placename]
if ($data->[0] ne undef and $data->[1] ne undef)
{
# &$utf8_decode($location);
my $placename = &descape($data->[2]);
$lat = $data->[0];
$long = $data->[1];
print $stdout ("-- Your next tweets will be sent at " . $lat . "," . $long . ", which is near " . $placename . "\n" );
}
else
{
&$exception (30,"*** Geocoder failed, will not geolocate your next tweets.\n")
}
return 1;
}
}
else
{
if ($command =~ m/^place$/) # Empty /place command, just turn geolocation off.
{
print $stdout ("-- Turning geolocation off\n");
undef($lat);
undef($long);
return 1;
}
}
# /teleport
if ($command =~ s#^/teleport ## && length($command)) {
if ($extpref_multigeo_gpsd and $gpsd_active)
{
print $stdout ("-- Automatically turning off GPSD support. Turn on again with \"/gpsd on\".\n");
$gpsd_active = false;
}
my $tweet = &get_tweet($command);
if (!$tweet->{'id_str'}) {
print $stdout "-- sorry, no such tweet (yet?).\n";
return 1;
}
if ($tweet->{'user'}->{'geo_enabled'} ne 'true' ||
($tweet->{'geo'}->{'coordinates'}->[0] eq 'undef')) {
print $stdout "-- sorry, no geoinformation in that tweet.\n";
return 1;
}
$lat = $tweet->{'geo'}->{'coordinates'}->[0];
$long = $tweet->{'geo'}->{'coordinates'}->[1];
print $stdout ("-- Your next tweets will be sent at " . $lat . "," . $long . ".\n" );
return 1;
}
# /gpsd
if ($command =~ s#^/gpsd ## && length($command)) {
if (not $extpref_multigeo_gpsd)
{
our $exception;
&$exception(31,"*** GPSD functionality disabled.\n");
return 1;
}
if ($command eq "on")
{
print $stdout ("-- GPSD support is now enabled.\n");
$gpsd_active = true;
}
elsif ($command eq "off")
{
print $stdout ("-- GPSD support is now disabled.\n");
$gpsd_active = false;
}
else
{
our $exception;
&$exception(31,"*** Use either \"/gpsd on\" or \"/gpsd off\" to enable/disable GPSD, or just \"/gpsd\" to query.\n");
}
return 1;
}
elsif ($command =~ m/^gpsd$/) # Empty /place command, just turn geolocation off.
{
if (not $extpref_multigeo_gpsd)
{
our $exception;
&$exception(31,"*** GPSD functionality disabled.\n");
return 1;
}
# ...fetch lat and long from GPSD ...
my $poll=$gpsd->poll;
print Dumper($poll) if $debug;
if ($poll->active == 0) {
print $stdout "-- GPS offline.\n";
} elsif ($poll->tpv->mode == 0) {
print $stdout "-- No GPS fix.\n";
} else {
my $lat = $poll->tpv->lat ;
my $long = $poll->tpv->lon ;
print $stdout "-- GPSD returned coordinates $lat,$lon\n";
}
return 1;
}
# No matches, run through any other command hooks.
return 0;
};
# $handle will manage the where-inline funcionality, printing the reverse-geocoded placename with every geolocated tweet.
# Now supercharged with geocoder cache!
$handle = sub {
my $tweet = shift;
our $verbose;
&defaulthandle($tweet);
if ($tweet->{'user'}->{'geo_enabled'} eq 'true'
&& $tweet->{'geo'}->{'coordinates'}->[0] ne undef
&& $tweet->{'geo'}->{'coordinates'}->[1] ne undef
&& $tweet->{'geo'}->{'coordinates'}->[0] ne 'undef'
&& $tweet->{'geo'}->{'coordinates'}->[1] ne 'undef'
&& ($tweet->{'geo'}->{'coordinates'}->[0] ne 0 || $tweet->{'geo'}->{'coordinates'}->[1] ne 0)
)
{
# If tweet has coordinates, call the (selected) geocoder.
# print $stdout "-- Tweet has lat,lon coordinates, geocoding" if ($verbose);
my $tw_lat = $tweet->{'geo'}->{'coordinates'}->[0];
my $tw_lon = $tweet->{'geo'}->{'coordinates'}->[1];
# print $stdout "-- Debug Geocoder coordinates: " . Dumper($tweet->{'geo'}) . Dumper($tw_lat) . "," . Dumper($tw_lon) . "\n";
if ($geocoder_cache{$tw_lat.",".$tw_lon})
{
$placename = $geocoder_cache{$tw_lat.",".$tw_lon};
print $stdout "-- Geocoder cache hit: " . $tw_lat . "," . $tw_lon . " -> " . $placename . "\n" if ($verbose);
$store->{cache_hit_count} += 1;
}
else
{
print $stdout "-- Querying geocoder: " . $tw_lat . "," . $tw_lon . "\n" if ($verbose);
$placename = &$reversegeocoder( $tw_lat , $tw_lon );
$placename = &descape($placename); # This has changed with ttytter 2.0.02 - before, utf8_decode was used.
$geocoder_cache{$tw_lat.",".$tw_lon} = $placename;
$store->{cache_misses} += 1;
}
}
elsif ($tweet->{'user'}->{'geo_enabled'} eq 'true' && $tweet->{'place'}->{'id'} ne '' )
{
# If tweet has Place ID, chances are we already loaded the placename from the twitter API when the tweet was pulled off the stream, so no extra processing is needed.
print $stdout "-- Tweet has place ID and name.\n" if ($verbose);
$placename = $tweet->{'place'}->{'full_name'} . ", " . $tweet->{'place'}->{'country_code'} . " (" . $tweet->{'place'}->{'place_type'} . ")";
}
else
{
# No coordinates and no place ID? Then do nothing.
print $stdout "-- Tweet not geoenabled.\n" if ($verbose);
return 1;
}
# Basically copied from ttytter.pl's defaulthandle
my $menu_select = $tweet->{'menu_select'};
$menu_select = (length($menu_select) && !$script)
? (($menu_select =~ /^z/) ?
"${EM}${menu_select}+${OFF} " :
"${menu_select}+ ")
: '';
print $streamout ($menu_select . " " . $placename . "\n" );
return 1;
};
# Show cache statistics. Not really useful, but hey.
# $shutdown = sub {
# our $verbose;
# our $is_background;
#
# if ($is_background)
# {
# our $store;
# $cache_hits = $store->{'cache_hit_count'};
# $cache_miss = $store->{'cache_misses'} + ($store->{'cache_limit'} * $store->{'cache_flushes'} );
# $cache_flush = $store->{'cache_flushes'};
# $context = "background";
# }
# else
# {
# print "-- Fetching geocoder cache stats from background process\n" if $verbose;
# $cache_hits = getbackgroundkey('cache_hit_count');
# $cache_miss = getbackgroundkey('cache_misses') + (getbackgroundkey('cache_limit') * getbackgroundkey('cache_flushes') );
# $cache_flush = getbackgroundkey('cache_flushes');
# $context = "foreground";
# print "-- Fetched geocoder cache stats from background process\n" if $verbose;
# }
#
# # $store->{deshortify_cache_misses} += ($store->{deshortify_cache_limit} * $store->{deshortify_cache_flushes});
# # print $stdout "-- Deshortify cache stats (misses/hits/flushes): $store->{deshortify_cache_misses}/$store->{deshortify_cache_hit_count}/$store->{deshortify_cache_flushes}\n" if $verbose;
# # sendbackgroundkey('deshortify_cache_misses', getbackgroundkey('deshortify_cache_misses') + (getbackgroundkey('deshortify_cache_limit') * getbackgroundkey('deshortify_cache_flushes')) );
#
# print $stdout "-- Geocoder cache stats (misses/hits/flushes/context): $cache_hits/$cache_miss/$cache_flush/$context\n" if ($verbose);
#
# return 0;
# }