#!/usr/bin/env perl
binmode
(STDOUT,
':utf8'
);
if
(
scalar
(
@ARGV
) <= 1) {
die
"Usage: $0 YYYY-MM-DD location"
;
}
my
$date
=
shift
@ARGV
;
my
$location
=
join
(
' '
,
@ARGV
);
my
$coordinates
= Geo::Coder::Free::Local->new()->geocode(
$location
) //
Geo::Coder::Free->new()->geocode(
$location
);
if
((!
defined
(
$coordinates
)) &&
$ENV
{
'OPENADDR_HOME'
}) {
$coordinates
= Geo::Coder::Free::OpenAddresses->new(
openaddr
=>
$ENV
{
'OPENADDR_HOME'
})->geocode(
location
=>
$location
);
}
if
(!
defined
(
$coordinates
)) {
die
"$0: unknown location '$location'"
;
}
my
$cache_dir
=
$ENV
{
'CACHE_DIR'
};
if
(
$cache_dir
) {
mkdir
$cache_dir
, 0700
if
(!-d
$cache_dir
);
$cache_dir
= File::Spec->catfile(
$cache_dir
,
'lwp-cache'
);
}
else
{
$cache_dir
= File::Spec->catfile(File::HomeDir->my_home(),
'.cache'
,
'lwp-cache'
)
}
if
(!-d
$cache_dir
) {
mkdir
$cache_dir
, 0700 ||
die
"$cache_dir: $!"
;
}
my
$cached_browser
= LWP::UserAgent::Cached->new(
cache_dir
=>
$cache_dir
,
agent
=> basename($0),
keep_alive
=> 1,
timeout
=> 30,
);
$cached_browser
->env_proxy(1);
$cached_browser
->conn_cache->total_capacity(
undef
);
my
$weather
= Weather::Meteo->new(
ua
=>
$cached_browser
)->weather(
$coordinates
,
$date
);
print
'Maximum temperature: '
,
$weather
->{
'daily'
}->{
'temperature_2m_max'
}[0],
$weather
->{
'daily_units'
}->{
'temperature_2m_max'
},
"\n"
;
print
'Maximum wind speed '
,
$weather
->{
'daily'
}->{
'windspeed_10m_max'
}[0],
$weather
->{
'daily_units'
}->{
'windspeed_10m_max'
},
"\n"
;
print
'Maximum wind gust speed '
,
$weather
->{
'daily'
}->{
'windgusts_10m_max'
}[0],
$weather
->{
'daily_units'
}->{
'windgusts_10m_max'
},
"\n"
;