diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2015-02-13 12:24:34 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2015-02-13 12:24:34 +0000 |
commit | 0db002a977d00d0a6e11333e190de4746e009bfb (patch) | |
tree | e754e071d99d3eed7618fe32c004196a8a8f3bd3 /perllib/FixMyStreet/Geocode/Google.pm | |
parent | 3571f96251df08dd8a9dda3ec4e1fadf30d6c63d (diff) | |
parent | 0dc3e4f2c0b6e90c2b67fab5aaf7c9c3d2b1f971 (diff) |
Merge branch 'issues/684-expire-geocache'
Diffstat (limited to 'perllib/FixMyStreet/Geocode/Google.pm')
-rw-r--r-- | perllib/FixMyStreet/Geocode/Google.pm | 43 |
1 files changed, 9 insertions, 34 deletions
diff --git a/perllib/FixMyStreet/Geocode/Google.pm b/perllib/FixMyStreet/Geocode/Google.pm index 11ff8ef80..8d3280ae7 100644 --- a/perllib/FixMyStreet/Geocode/Google.pm +++ b/perllib/FixMyStreet/Geocode/Google.pm @@ -7,11 +7,6 @@ package FixMyStreet::Geocode::Google; use strict; -use Encode; -use File::Slurp; -use File::Path (); -use LWP::Simple; -use Digest::MD5 qw(md5_hex); use mySociety::Locale; # string STRING CONTEXT @@ -24,6 +19,13 @@ sub string { my $params = $c->cobrand->disambiguate_location($s); + # For some reason adding gl=uk is no longer sufficient to make google + # think we are in the UK for some locations so we explictly add UK to + # the address. + if ($c->cobrand->country eq 'GB' && $s !~ /, *UK/ && $s !~ /united *kingdom$/) { + $s .= ', UK'; + } + $s = FixMyStreet::Geocode::escape($s); my $url = 'http://maps.google.com/maps/geo?q=' . $s; @@ -36,38 +38,11 @@ sub string { } $url .= '&hl=' . $params->{lang} if $params->{lang}; - my $cache_dir = FixMyStreet->config('GEO_CACHE') . 'google/'; - my $cache_file = $cache_dir . md5_hex($url); - my $js; - if (-s $cache_file) { - $js = File::Slurp::read_file($cache_file); - } else { - # For some reason adding gl=uk is no longer sufficient to make google - # think we are in the UK for some locations so we explictly add UK to - # the address. We do it here so as not to invalidate existing cache - # entries - if ( $c->cobrand->country eq 'GB' - && $url !~ /,\+UK/ - && $url !~ /united\++kingdom$/ ) - { - if ( $url =~ /&/ ) { - $url =~ s/&/,+UK&/; - } else { - $url .= ',+UK'; - } - } - $url .= '&sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY'); - $js = LWP::Simple::get($url); - $js = encode_utf8($js) if utf8::is_utf8($js); - File::Path::mkpath($cache_dir); - File::Slurp::write_file($cache_file, $js) if $js && $js !~ /"code":6[12]0/; - } - + my $args = 'sensor=false&key=' . FixMyStreet->config('GOOGLE_MAPS_API_KEY'); + my $js = FixMyStreet::Geocode::cache('google', $url, $args, qr/"code":6[12]0/); if (!$js) { return { error => _('Sorry, we could not parse that location. Please try again.') }; } - - $js = JSON->new->utf8->allow_nonref->decode($js); if ($js->{Status}->{code} ne '200') { return { error => _('Sorry, we could not find that location.') }; } |