Skip to content

Commit

Permalink
Cache legends.json locally instead of always fetching it
Browse files Browse the repository at this point in the history
  • Loading branch information
incognico committed Dec 9, 2021
1 parent 170a23f commit 76219a2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# cpanm --installdeps .

requires 'DateTime::Format::ISO8601';
requires 'File::Slurp';
requires 'JSON::MaybeXS';
requires 'LWP::UserAgent';
recommends 'JSON::XS';
25 changes: 16 additions & 9 deletions lib/Weather/METNO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)
{
Expand All @@ -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;

Expand All @@ -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})) : '');

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 76219a2

Please sign in to comment.