From 0dc3e4f2c0b6e90c2b67fab5aaf7c9c3d2b1f971 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Fri, 23 Jan 2015 14:35:00 +0000 Subject: Expire cached geolocations after a week. And refactor geoloction caching code into one function (except for Zurich which has its own SOAP based system). --- perllib/FixMyStreet/Geocode.pm | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'perllib/FixMyStreet/Geocode.pm') diff --git a/perllib/FixMyStreet/Geocode.pm b/perllib/FixMyStreet/Geocode.pm index b5be152a8..616df87b0 100644 --- a/perllib/FixMyStreet/Geocode.pm +++ b/perllib/FixMyStreet/Geocode.pm @@ -7,6 +7,11 @@ package FixMyStreet::Geocode; use strict; +use Digest::MD5 qw(md5_hex); +use Encode; +use File::Slurp; +use File::Path (); +use LWP::Simple qw($ua); use URI::Escape; use FixMyStreet::Geocode::Bing; use FixMyStreet::Geocode::Google; @@ -55,4 +60,25 @@ sub escape { return $s; } +sub cache { + my ($type, $url, $args, $re) = @_; + my $cache_dir = FixMyStreet->config('GEO_CACHE') . $type . '/'; + my $cache_file = $cache_dir . md5_hex($url); + my $js; + if (-s $cache_file && -M $cache_file <= 7) { + $js = File::Slurp::read_file($cache_file); + } else { + $url .= '&' . $args if $args; + $ua->timeout(15); + $js = LWP::Simple::get($url); + $js = encode_utf8($js) if utf8::is_utf8($js); + File::Path::mkpath($cache_dir); + if ($js && (!$re || $js !~ $re)) { + File::Slurp::write_file($cache_file, $js); + } + } + $js = JSON->new->utf8->allow_nonref->decode($js) if $js; + return $js; +} + 1; -- cgit v1.2.3 From da63f72c27e16d491f9103b9a8cbdb2fd96d2b59 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 22 Jan 2015 15:57:45 +0000 Subject: Make sure all co-ordinates are stringified. This includes MapIt postcode lookups, geocoding, query parameters, tile clicks. Stringifying truncates them to six decimal places, which means we no longer need any "short" versions anywhere, and the JSON response will always uses a decimal point regardless of locale. --- perllib/FixMyStreet/Geocode.pm | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'perllib/FixMyStreet/Geocode.pm') diff --git a/perllib/FixMyStreet/Geocode.pm b/perllib/FixMyStreet/Geocode.pm index b5be152a8..bf1681b70 100644 --- a/perllib/FixMyStreet/Geocode.pm +++ b/perllib/FixMyStreet/Geocode.pm @@ -12,6 +12,7 @@ use FixMyStreet::Geocode::Bing; use FixMyStreet::Geocode::Google; use FixMyStreet::Geocode::OSM; use FixMyStreet::Geocode::Zurich; +use Utils; # lookup STRING CONTEXT # Given a user-inputted string, try and convert it into co-ordinates using either @@ -21,6 +22,11 @@ use FixMyStreet::Geocode::Zurich; sub lookup { my ($s, $c) = @_; my $data = $c->cobrand->geocode_postcode($s); + if (defined $data->{latitude}) { + ( $data->{latitude}, $data->{longitude} ) = + map { Utils::truncate_coordinate($_) } + ( $data->{latitude}, $data->{longitude} ); + } $data = string($s, $c) unless $data->{error} || defined $data->{latitude}; $data->{error} = _('Sorry, we could not find that location.') -- cgit v1.2.3