From 76219a22e663344176f4dd8ea944ed16b81e0e24 Mon Sep 17 00:00:00 2001 From: incognico Date: Thu, 9 Dec 2021 16:30:19 +0100 Subject: [PATCH] Cache legends.json locally instead of always fetching it --- README.md | 2 +- cpanfile | 1 + lib/Weather/METNO.pm | 25 ++++++++++++++++--------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d09438a..ab7f214 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,6 @@ Example result of usage in a Discord embed: A unique string in the user agent is required per met.no TOS, it should preferably be your domain or your email address, in case you need to be contacted. `uid` will get appeneded to LWP's UA string. -The legend json for the symbols is currently set to `https://distfiles.lifeisabug.com/metno/legends.json` (a cached version I keep) instead of `https://api.met.no/weatherapi/weathericon/2.0/legends` because those are probably not updated very often. The API 2.0 symbol icons are meant to be self-hosted instead of being API provided, if you want to use those feel free to embed them from `https://distfiles.lifeisabug.com/metno/` +The API 2.0 symbol icons are meant to be self-hosted instead of being API provided, if you want to use those feel free to embed them from `https://distfiles.lifeisabug.com/metno/` Feel free to work on and improve this. Pull-requests are more than welcome. diff --git a/cpanfile b/cpanfile index 7cee5f0..dc925cd 100644 --- a/cpanfile +++ b/cpanfile @@ -1,6 +1,7 @@ # cpanm --installdeps . requires 'DateTime::Format::ISO8601'; +requires 'File::Slurp'; requires 'JSON::MaybeXS'; requires 'LWP::UserAgent'; recommends 'JSON::XS'; diff --git a/lib/Weather/METNO.pm b/lib/Weather/METNO.pm index b893f90..9f39149 100644 --- a/lib/Weather/METNO.pm +++ b/lib/Weather/METNO.pm @@ -11,6 +11,7 @@ no warnings qw(experimental::signatures experimental::smartmatch); use Carp; use DateTime::Format::ISO8601; +use File::Slurp; use JSON::MaybeXS; use LWP::UserAgent; use POSIX 'floor'; @@ -23,7 +24,9 @@ my ($data, @times, $weather, $closest, $symbols); my $api_ver = '2.0'; my $api_url = 'https://api.met.no/weatherapi/locationforecast/' . $api_ver . '/complete'; -my $sym_url = 'https://distfiles.lifeisabug.com/metno/legends.json'; +my $sym_url = 'https://api.met.no/weatherapi/weathericon/2.0/legends'; +my $sym_tmp = '/tmp/lwp-metno-legends.json'; +my $sym_sec = 604800; # seconds to $sym_tmp expiry sub new ($class, %args) { @@ -36,7 +39,7 @@ sub new ($class, %args) $self->{alt} = $args{alt}; $self->{lang} = defined $args{lang} ? $args{lang} : 'en'; - $self->{timeout} = defined $args{timeout} ? $args{timeout} : 3; + $self->{timeout} = defined $args{timeout} ? $args{timeout} : 5; $self->fetch_weather; @@ -45,10 +48,18 @@ sub new ($class, %args) sub fetch_weather ($self) { - my $ua = LWP::UserAgent->new(timeout => $self->{timeout}); - $ua->default_header('Accept' => 'application/json'); + my $ua = LWP::UserAgent->new(timeout => $self->{timeout}, agent => 'p5-Weather-METNO '.$self->{uid}.' '); + $ua->default_header('Accept' => 'application/json'); + + unless (-f $sym_tmp && (time - (stat($sym_tmp))[9]) > $sym_sec) + { + my $r = $ua->mirror($sym_url, $sym_tmp); + croak $r->status_line unless ($r->is_success); + } + + $symbols = decode_json(read_file($sym_tmp)); + $ua->default_header('Accept-Encoding' => HTTP::Message::decodable); - $ua->agent('p5-Weather-METNO '.$self->{uid}.' '); my $url = $api_url . '?lat=' . sprintf('%.2f', $self->{lat}) . '&lon=' . sprintf('%.2f', $self->{lon}) . (defined $self->{alt} ? ('&altitude=' . int($self->{alt})) : ''); @@ -76,10 +87,6 @@ sub fetch_weather ($self) last; } - $r = $ua->get($sym_url); - croak $r->status_line unless ($r->is_success); - $symbols = decode_json($r->decoded_content); - return; }