Created
November 6, 2012 19:10
-
-
Save waffle2k/4026793 to your computer and use it in GitHub Desktop.
Take some unknown locations (city and state) and call out to google to get some of the pertinent info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use Data::Dumper; | |
use JSON; | |
sub best_candidate { | |
my ($json_obj) = @_; | |
for( @{$json_obj->{results}} ){ | |
next if $_->{types}->[0] eq 'route'; | |
return $_ if( grep /locality|political/, @{$_->{types}} ); | |
} | |
return undef; | |
} | |
sub address_components { | |
my ($json_obj) = @_; | |
my ( $city, $county, $state, $country ); | |
my $candidate = best_candidate( $json_obj ); | |
COMPONENT: for( @{$candidate->{address_components}} ){ | |
# Get the State? | |
if( $_->{types}->[0] eq 'administrative_area_level_1' ){ | |
$state = $_->{short_name}; | |
next COMPONENT; | |
} | |
if( $_->{types}->[0] eq 'administrative_area_level_2' && $_->{types}->[1] eq 'political' ){ | |
$county = $_->{short_name}; | |
next COMPONENT; | |
} | |
if( $_->{types}->[0] eq 'locality' && $_->{types}->[1] eq 'political' ){ | |
$city = $_->{short_name}; | |
next COMPONENT; | |
} | |
if( $_->{types}->[0] eq 'country' && $_->{types}->[1] eq 'political' ){ | |
$country = $_->{short_name}; | |
next COMPONENT; | |
} | |
} | |
return( $city, $county, $state, $country ); | |
} | |
sub get_lat_long { | |
my ($json_obj) = @_; | |
return ( $json_obj->{results}->[0]->{geometry}->{location}->{lat}, $json_obj->{results}->[0]->{geometry}->{location}->{lng} ); | |
} | |
sub get_json_obj { | |
my $_ = shift; | |
#print "Searching for [$_]\n"; | |
my @json_raw = `curl "http://maps.googleapis.com/maps/api/geocode/json?address=${_}&sensor=false&components=locality" 2>/dev/null`; | |
chomp for @json_raw; | |
# Get the components | |
my $s = join( "\n", @json_raw ); | |
my $o = decode_json( $s ); | |
} | |
my @entries = <>; | |
chomp for @entries; | |
LINE: for( @entries ){ | |
chomp; | |
my $orig = $_; | |
s/.*?,.*?,(.*)/$1/; | |
my $o = get_json_obj( $_ ); | |
if( $o->{status} eq 'OVER_QUERY_LIMIT' ){ | |
warn( "Over Query Limit - sleeping for 5 seconds..\n" ); | |
sleep 5; | |
redo LINE; | |
} | |
unless( $o->{status} eq 'OK' ){ | |
warn( "Failed to parse [$_], skipping...\n" ); | |
print "E:$_\n"; | |
next LINE; | |
} | |
my ($lat, $lng) = get_lat_long( $o ); | |
my ($city, $county, $state, $country) = address_components( $o ); | |
unless( $city ){ | |
$city = $county || $state; | |
} | |
unless( $country eq 'US' ){ | |
print Dumper( $o ),"\n"; | |
print "E:$orig\n"; | |
next LINE; | |
} | |
print "O:$city,$state,$country,$lat,$lng\n"; | |
unless( $lat ){ | |
print "E:$orig\n"; | |
next LINE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment