Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
657090b5f7 | ||
|
|
a0fd6faafc | ||
|
|
fa5910218e | ||
|
|
079ea8098e | ||
|
|
8aea873746 | ||
|
|
77435364e8 | ||
|
|
d3c2857af8 | ||
|
|
5c23f41229 |
@@ -132,3 +132,5 @@ lib_sections = NAME | VERSION | DESCRIPTION | AUTHOR | LICENSE AND COPYRIGHT
|
|||||||
|
|
||||||
[-TooMuchCode::ProhibitLargeBlock]
|
[-TooMuchCode::ProhibitLargeBlock]
|
||||||
# read my lips: fuck off
|
# read my lips: fuck off
|
||||||
|
|
||||||
|
[-RegularExpressions::ProhibitComplexRegexes]
|
||||||
|
|||||||
7
README
7
README
@@ -72,3 +72,10 @@ service aleesa-webapp start
|
|||||||
|
|
||||||
в автозагрузку его можно прописать через команду:
|
в автозагрузку его можно прописать через команду:
|
||||||
rc-update add aleesa-webapp default
|
rc-update add aleesa-webapp default
|
||||||
|
|
||||||
|
|
||||||
|
На MacOS вылазит ошибка BerkeleyDB.xs:82:2: error: db.h is from Berkeley DB 1.x - need at least Berkeley DB 2.6.4
|
||||||
|
------------------------------------------------------------------------------------------------------------------------
|
||||||
|
На момент 04.02.2023 в Mac OS Ventura (13.1), это чинится установкой через brew пакета berkeley-db4:
|
||||||
|
brew install berkeley-db4
|
||||||
|
brew link berkeley-db4 --force
|
||||||
|
|||||||
@@ -43,7 +43,6 @@ sub __fork;
|
|||||||
sub __signal_handler;
|
sub __signal_handler;
|
||||||
|
|
||||||
local $OUTPUT_AUTOFLUSH = 1;
|
local $OUTPUT_AUTOFLUSH = 1;
|
||||||
my $runAsUid = $UID;
|
|
||||||
|
|
||||||
my $piddir = '/var/run/' . basename (abs_path $PROGRAM_NAME);
|
my $piddir = '/var/run/' . basename (abs_path $PROGRAM_NAME);
|
||||||
my $C = LoadConf ();
|
my $C = LoadConf ();
|
||||||
|
|||||||
30
cpanfile
30
cpanfile
@@ -2,34 +2,36 @@
|
|||||||
requires 'Carp', '==1.50';
|
requires 'Carp', '==1.50';
|
||||||
requires 'CHI', '==0.61';
|
requires 'CHI', '==0.61';
|
||||||
requires 'CHI::Driver::BerkeleyDB', '==0.05';
|
requires 'CHI::Driver::BerkeleyDB', '==0.05';
|
||||||
requires 'Clone', '==0.45';
|
requires 'Clone', '==0.46';
|
||||||
requires 'Cwd', '==3.75';
|
requires 'Cwd', '==3.75';
|
||||||
requires 'Cwd::utf8', '==0.011';
|
requires 'Cwd::utf8', '==0.011';
|
||||||
requires 'DateTime', '==1.55';
|
requires 'DateTime', '==1.59';
|
||||||
requires 'Digest::HMAC_SHA1', '==1.04';
|
requires 'Digest::HMAC_SHA1', '==1.04';
|
||||||
requires 'Encode', '==3.16';
|
requires 'Encode', '==3.19';
|
||||||
requires 'English', '0';
|
requires 'English', '0';
|
||||||
requires 'EV', '==4.33';
|
requires 'EV', '==4.33';
|
||||||
requires 'File::Basename', '==2.85';
|
requires 'File::Basename', '0';
|
||||||
requires 'File::Path', '==2.18';
|
requires 'File::Path', '==2.18';
|
||||||
requires 'File::Spec', '==3.75';
|
requires 'File::Spec', '==3.75';
|
||||||
requires 'HTML::TokeParser', '==3.76';
|
requires 'HTML::TokeParser', '==3.81';
|
||||||
requires 'IO::Socket::SSL', '==2.073';
|
requires 'IO::Socket::SSL', '==2.081';
|
||||||
requires 'JSON', '==4.04';
|
requires 'JSON', '==4.10';
|
||||||
requires 'JSON::XS', '==4.03';
|
requires 'JSON::XS', '==4.03';
|
||||||
requires 'local::lib', '==2.000028';
|
requires 'local::lib', '==2.000029';
|
||||||
requires 'Log::Any', '==1.710';
|
requires 'Log::Any', '==1.714';
|
||||||
requires 'Log::Any::Adapter', '==1.710';
|
requires 'Log::Any::Adapter', '==1.714';
|
||||||
requires 'Log::Any::For::Std', '==0.05';
|
requires 'Log::Any::For::Std', '==0.05';
|
||||||
requires 'Math::Random::Secure', '==0.080001';
|
requires 'Math::Random::Secure', '==0.080001';
|
||||||
requires 'MIME::Base64', '==3.16';
|
requires 'MIME::Base64', '==3.16';
|
||||||
requires 'Mojolicious', '==9.26';
|
requires 'Mojolicious', '==9.31';
|
||||||
requires 'Mojo::IOLoop::Signal', '==0.003';
|
requires 'Mojo::IOLoop::Signal', '==0.003';
|
||||||
# Чтобы заткнуть Mojo::UserAgent::Cached
|
# Чтобы заткнуть Mojo::UserAgent::Cached
|
||||||
requires 'Mojo::Log', '0';
|
requires 'Mojo::Log', '0';
|
||||||
requires 'Mojo::Redis', '==3.27';
|
requires 'Mojo::Redis', '==3.29';
|
||||||
requires 'Mojo::UserAgent', '0';
|
requires 'Mojo::UserAgent', '0';
|
||||||
requires 'Mojo::UserAgent::Cached', '==1.18';
|
requires 'Mojo::UserAgent::Cached', '==1.25';
|
||||||
requires 'POSIX', '0';
|
requires 'POSIX', '0';
|
||||||
|
# Рекомендованный модуль для DateTime
|
||||||
|
requires 'Sub::Util', '==1.63';
|
||||||
requires 'URI::Encode::XS', '==0.11';
|
requires 'URI::Encode::XS', '==0.11';
|
||||||
requires 'URI::URL', '==5.10';
|
requires 'URI::URL', '==5.19';
|
||||||
|
|||||||
@@ -26,6 +26,8 @@
|
|||||||
"forwards_max" : 5,
|
"forwards_max" : 5,
|
||||||
|
|
||||||
"openweathermap" : {
|
"openweathermap" : {
|
||||||
|
# (For shitty political reasons) 0 - use coordinates instead of coutry code, 1 - use country codes
|
||||||
|
"country" : 0,
|
||||||
"appid" : "get_it_on_openweathermap.org"
|
"appid" : "get_it_on_openweathermap.org"
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ use version; our $VERSION = qw (1.0);
|
|||||||
use Exporter qw (import);
|
use Exporter qw (import);
|
||||||
our @EXPORT_OK = qw (Anek Buni Drink Monkeyuser Kitty Fox Oboobs Obutts Rabbit Owl Frog Horse Snail Xkcd Weather);
|
our @EXPORT_OK = qw (Anek Buni Drink Monkeyuser Kitty Fox Oboobs Obutts Rabbit Owl Frog Horse Snail Xkcd Weather);
|
||||||
|
|
||||||
my @MONTH = qw (yanvar fevral mart aprel may iyun iyul avgust sentyabr oktyabr noyabr dekabr);
|
|
||||||
my $c = LoadConf ();
|
my $c = LoadConf ();
|
||||||
my $cachedir = $c->{cachedir};
|
my $cachedir = $c->{cachedir};
|
||||||
|
|
||||||
@@ -131,7 +130,12 @@ sub Anek {
|
|||||||
$got_anek = 1;
|
$got_anek = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$log->warn (sprintf '[WARN] anekdot.ru server returns incorrect json, full response text message: %s', $response_text);
|
$log->warn (
|
||||||
|
sprintf (
|
||||||
|
'[WARN] anekdot.ru server returns incorrect json, full response text message: %s',
|
||||||
|
$response_text,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$log->warn (sprintf '[WARN] anekdot.ru server returns unexpected response text: %s', $r->body);
|
$log->warn (sprintf '[WARN] anekdot.ru server returns unexpected response text: %s', $r->body);
|
||||||
@@ -192,7 +196,6 @@ sub Buni {
|
|||||||
sub Drink {
|
sub Drink {
|
||||||
my $r;
|
my $r;
|
||||||
my $ret = 'Не знаю праздников - вджобываю весь день на шахтах, как проклятая.';
|
my $ret = 'Не знаю праздников - вджобываю весь день на шахтах, как проклятая.';
|
||||||
my ($dayNum, $monthNum) = (localtime ())[3, 4];
|
|
||||||
my $url = 'https://prazdniki-segodnya.ru/';
|
my $url = 'https://prazdniki-segodnya.ru/';
|
||||||
|
|
||||||
# Those POSIX assholes just forgot to add unix timestamps without TZ offset, so...
|
# Those POSIX assholes just forgot to add unix timestamps without TZ offset, so...
|
||||||
@@ -266,7 +269,7 @@ sub Drink {
|
|||||||
if (ref $item) {
|
if (ref $item) {
|
||||||
foreach my $item1 (@{$item}) {
|
foreach my $item1 (@{$item}) {
|
||||||
my %h = eval { %{$item1} };
|
my %h = eval { %{$item1} };
|
||||||
|
|
||||||
if (defined ($h{class}) && $h{class} eq 'list-group-item text-monospace') {
|
if (defined ($h{class}) && $h{class} eq 'list-group-item text-monospace') {
|
||||||
$holiday {'* ' . decode ('UTF-8', $p->get_trimmed_text ('/div'))} = 1;
|
$holiday {'* ' . decode ('UTF-8', $p->get_trimmed_text ('/div'))} = 1;
|
||||||
}
|
}
|
||||||
@@ -280,7 +283,13 @@ sub Drink {
|
|||||||
$ret = join "\n", sort (keys (%holiday));
|
$ret = join "\n", sort (keys (%holiday));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$log->warn (sprintf '[WARN] prazdniki-segodnya.ru server return status %s with message: %s', $r->code, $r->message);
|
$log->warn (
|
||||||
|
sprintf (
|
||||||
|
'[WARN] prazdniki-segodnya.ru server return status %s with message: %s',
|
||||||
|
$r->code,
|
||||||
|
$r->message,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $ret;
|
return $ret;
|
||||||
@@ -564,70 +573,128 @@ sub Weather {
|
|||||||
$city = 'Екатеринбург';
|
$city = 'Екатеринбург';
|
||||||
}
|
}
|
||||||
|
|
||||||
my $w = __weather ($city);
|
|
||||||
my $reply;
|
my $reply;
|
||||||
|
|
||||||
if ($w) {
|
# Get coordinates
|
||||||
if ($w->{temperature_min} == $w->{temperature_max}) {
|
my $g = __weather_geo ($city);
|
||||||
$reply = sprintf (
|
|
||||||
"Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
unless (defined $g) {
|
||||||
$w->{name},
|
|
||||||
$w->{country},
|
|
||||||
ucfirst $w->{description},
|
|
||||||
$w->{wind_direction},
|
|
||||||
$w->{wind_speed},
|
|
||||||
$w->{temperature_min},
|
|
||||||
$w->{temperature_feelslike},
|
|
||||||
$w->{humidity},
|
|
||||||
$w->{pressure},
|
|
||||||
);
|
|
||||||
} elsif ($w->{temperature_min} < 0 && $w->{temperature_max} <= 0) {
|
|
||||||
$reply = sprintf (
|
|
||||||
"Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура от %s до %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
|
||||||
$w->{name},
|
|
||||||
$w->{country},
|
|
||||||
ucfirst $w->{description},
|
|
||||||
$w->{wind_direction},
|
|
||||||
$w->{wind_speed},
|
|
||||||
$w->{temperature_max},
|
|
||||||
$w->{temperature_min},
|
|
||||||
$w->{temperature_feelslike},
|
|
||||||
$w->{humidity},
|
|
||||||
$w->{pressure},
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
$reply = sprintf (
|
|
||||||
"Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура от %s до %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
|
||||||
$w->{name},
|
|
||||||
$w->{country},
|
|
||||||
ucfirst $w->{description},
|
|
||||||
$w->{wind_direction},
|
|
||||||
$w->{wind_speed},
|
|
||||||
$w->{temperature_min},
|
|
||||||
$w->{temperature_max},
|
|
||||||
$w->{temperature_feelslike},
|
|
||||||
$w->{humidity},
|
|
||||||
$w->{pressure},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$reply = "Я не знаю, какая погода в $city";
|
$reply = "Я не знаю, какая погода в $city";
|
||||||
|
} else {
|
||||||
|
my $w = __weather ($g->{lat}, $g->{lon});
|
||||||
|
|
||||||
|
if ($w) {
|
||||||
|
if ($w->{temperature_min} == $w->{temperature_max}) {
|
||||||
|
if ($c->{openweathermap}->{country}) {
|
||||||
|
$reply = sprintf (
|
||||||
|
"Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
||||||
|
$city,
|
||||||
|
$w->{country},
|
||||||
|
ucfirst $w->{description},
|
||||||
|
$w->{wind_direction},
|
||||||
|
$w->{wind_speed},
|
||||||
|
$w->{temperature_min},
|
||||||
|
$w->{temperature_feelslike},
|
||||||
|
$w->{humidity},
|
||||||
|
$w->{pressure},
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$reply = sprintf (
|
||||||
|
"Погода в городе %s, (ш:%s, д:%s):\n%s, ветер %s %s м/c, температура %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
||||||
|
$city,
|
||||||
|
$g->{lat},
|
||||||
|
$g->{lon},
|
||||||
|
ucfirst $w->{description},
|
||||||
|
$w->{wind_direction},
|
||||||
|
$w->{wind_speed},
|
||||||
|
$w->{temperature_min},
|
||||||
|
$w->{temperature_feelslike},
|
||||||
|
$w->{humidity},
|
||||||
|
$w->{pressure},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} elsif ($w->{temperature_min} < 0 && $w->{temperature_max} <= 0) {
|
||||||
|
if ($c->{openweathermap}->{country}) {
|
||||||
|
$reply = sprintf (
|
||||||
|
"Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура от %s до %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
||||||
|
$city,
|
||||||
|
$w->{country},
|
||||||
|
ucfirst $w->{description},
|
||||||
|
$w->{wind_direction},
|
||||||
|
$w->{wind_speed},
|
||||||
|
$w->{temperature_max},
|
||||||
|
$w->{temperature_min},
|
||||||
|
$w->{temperature_feelslike},
|
||||||
|
$w->{humidity},
|
||||||
|
$w->{pressure},
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$reply = sprintf (
|
||||||
|
"Погода в городе %s, (ш:%s, д:%s):\n%s, ветер %s %s м/c, температура от %s до %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
||||||
|
$city,
|
||||||
|
$g->{lat},
|
||||||
|
$g->{lon},
|
||||||
|
ucfirst $w->{description},
|
||||||
|
$w->{wind_direction},
|
||||||
|
$w->{wind_speed},
|
||||||
|
$w->{temperature_max},
|
||||||
|
$w->{temperature_min},
|
||||||
|
$w->{temperature_feelslike},
|
||||||
|
$w->{humidity},
|
||||||
|
$w->{pressure},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($c->{openweathermap}->{country}) {
|
||||||
|
$reply = sprintf (
|
||||||
|
"Погода в городе %s, %s:\n%s, ветер %s %s м/c, температура от %s до %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
||||||
|
$city,
|
||||||
|
$w->{country},
|
||||||
|
ucfirst $w->{description},
|
||||||
|
$w->{wind_direction},
|
||||||
|
$w->{wind_speed},
|
||||||
|
$w->{temperature_min},
|
||||||
|
$w->{temperature_max},
|
||||||
|
$w->{temperature_feelslike},
|
||||||
|
$w->{humidity},
|
||||||
|
$w->{pressure},
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$reply = sprintf (
|
||||||
|
"Погода в городе %s, (ш:%s, д:%s):\n%s, ветер %s %s м/c, температура от %s до %s°C, ощущается как %s°C, относительная влажность %s%%, давление %s мм.рт.ст",
|
||||||
|
$city,
|
||||||
|
$g->{lat},
|
||||||
|
$g->{lon},
|
||||||
|
ucfirst $w->{description},
|
||||||
|
$w->{wind_direction},
|
||||||
|
$w->{wind_speed},
|
||||||
|
$w->{temperature_min},
|
||||||
|
$w->{temperature_max},
|
||||||
|
$w->{temperature_feelslike},
|
||||||
|
$w->{humidity},
|
||||||
|
$w->{pressure},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$reply = "Я не знаю, какая погода в $city";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $reply;
|
return $reply;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub __weather {
|
sub __weather {
|
||||||
my $city = shift;
|
my $lat = shift;
|
||||||
$city = urlencode $city;
|
my $lon = shift;
|
||||||
|
|
||||||
my $appid = $c->{openweathermap}->{appid};
|
my $appid = $c->{openweathermap}->{appid};
|
||||||
my $now = time ();
|
|
||||||
my $fc;
|
my $fc;
|
||||||
my $w;
|
my $w;
|
||||||
|
|
||||||
my $r;
|
my $r;
|
||||||
|
|
||||||
# try 3 times and giveup
|
# Try 3 times and giveup
|
||||||
for (1..3) {
|
for (1..3) {
|
||||||
my $ua = Mojo::UserAgent::Cached->new;
|
my $ua = Mojo::UserAgent::Cached->new;
|
||||||
$ua->local_dir ($cachedir);
|
$ua->local_dir ($cachedir);
|
||||||
@@ -642,7 +709,14 @@ sub __weather {
|
|||||||
);
|
);
|
||||||
# just to make Mojo::UserAgent::Cached happy
|
# just to make Mojo::UserAgent::Cached happy
|
||||||
$ua->logger (Mojo::Log->new (path => File::Spec->devnull, level => 'error'));
|
$ua->logger (Mojo::Log->new (path => File::Spec->devnull, level => 'error'));
|
||||||
$r = $ua->get (sprintf ('http://api.openweathermap.org/data/2.5/weather?q=%s&lang=ru&APPID=%s', $city, $appid))->result;
|
$r = $ua->get (
|
||||||
|
sprintf (
|
||||||
|
'http://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&lang=ru&APPID=%s',
|
||||||
|
$lat,
|
||||||
|
$lon,
|
||||||
|
$appid,
|
||||||
|
),
|
||||||
|
)->result;
|
||||||
|
|
||||||
if ($r->is_success) {
|
if ($r->is_success) {
|
||||||
last;
|
last;
|
||||||
@@ -667,19 +741,19 @@ sub __weather {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# TODO: check all of this for existence
|
# TODO: check all of this for existence
|
||||||
$w->{'name'} = $fc->{name};
|
$w->{'name'} = $fc->{name};
|
||||||
$w->{'state'} = $fc->{state};
|
$w->{'state'} = $fc->{state};
|
||||||
$w->{'country'} = $fc->{sys}->{country};
|
$w->{'country'} = $fc->{sys}->{country};
|
||||||
$w->{'longitude'} = $fc->{coord}->{lon};
|
$w->{'longitude'} = $fc->{coord}->{lon};
|
||||||
$w->{'latitude'} = $fc->{coord}->{lat};
|
$w->{'latitude'} = $fc->{coord}->{lat};
|
||||||
$w->{'temperature_min'} = int ($fc->{main}->{temp_min} - 273.15);
|
$w->{'temperature_min'} = int ($fc->{main}->{temp_min} - 273.15);
|
||||||
$w->{'temperature_max'} = int ($fc->{main}->{temp_max} - 273.15);
|
$w->{'temperature_max'} = int ($fc->{main}->{temp_max} - 273.15);
|
||||||
$w->{'temperature_feelslike'} = int ($fc->{main}->{feels_like} - 273.15);
|
$w->{'temperature_feelslike'} = int ($fc->{main}->{feels_like} - 273.15);
|
||||||
$w->{'humidity'} = $fc->{main}->{humidity};
|
$w->{'humidity'} = $fc->{main}->{humidity};
|
||||||
$w->{'pressure'} = int ($fc->{main}->{pressure} * 0.75006375541921);
|
$w->{'pressure'} = int ($fc->{main}->{pressure} * 0.75006375541921);
|
||||||
$w->{'description'} = $fc->{weather}->[0]->{description};
|
$w->{'description'} = $fc->{weather}->[0]->{description};
|
||||||
$w->{'wind_speed'} = $fc->{wind}->{speed};
|
$w->{'wind_speed'} = $fc->{wind}->{speed};
|
||||||
$w->{'wind_direction'} = 'разный';
|
$w->{'wind_direction'} = 'разный';
|
||||||
my $dir = int ($fc->{wind}->{deg} + 0);
|
my $dir = int ($fc->{wind}->{deg} + 0);
|
||||||
|
|
||||||
if ($dir == 0) {
|
if ($dir == 0) {
|
||||||
@@ -721,6 +795,77 @@ sub __weather {
|
|||||||
return $w;
|
return $w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub __weather_geo {
|
||||||
|
my $city = shift;
|
||||||
|
$city = urlencode $city;
|
||||||
|
my $appid = $c->{openweathermap}->{appid};
|
||||||
|
my $geo;
|
||||||
|
|
||||||
|
my $r;
|
||||||
|
|
||||||
|
# Try 3 times and giveup
|
||||||
|
for (1..3) {
|
||||||
|
my $ua = Mojo::UserAgent::Cached->new;
|
||||||
|
$ua->local_dir ($cachedir);
|
||||||
|
$ua->cache_agent (
|
||||||
|
CHI->new (
|
||||||
|
driver => 'BerkeleyDB',
|
||||||
|
root_dir => $cachedir,
|
||||||
|
namespace => __PACKAGE__,
|
||||||
|
expires_in => '2 months',
|
||||||
|
expires_on_backend => 1,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
# Just to make Mojo::UserAgent::Cached happy
|
||||||
|
$ua->logger (Mojo::Log->new (path => File::Spec->devnull, level => 'error'));
|
||||||
|
|
||||||
|
$r = $ua->get (
|
||||||
|
sprintf (
|
||||||
|
'http://api.openweathermap.org/geo/1.0/direct?q=%s&limit=1&APPID=%s',
|
||||||
|
$city,
|
||||||
|
$appid,
|
||||||
|
),
|
||||||
|
)->result;
|
||||||
|
|
||||||
|
if ($r->is_success) {
|
||||||
|
last;
|
||||||
|
}
|
||||||
|
|
||||||
|
sleep 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
# All 3 times it can give us error, so check this case here
|
||||||
|
if ($r->is_success) {
|
||||||
|
$geo = eval {
|
||||||
|
return $r->json;
|
||||||
|
};
|
||||||
|
|
||||||
|
unless ($geo) {
|
||||||
|
$log->warn ("[WARN] openweathermap geo api returns corrupted json: $EVAL_ERROR");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$log->warn (
|
||||||
|
sprintf (
|
||||||
|
'[WARN] Server api.openweathermap.org return status %s with message: %s in request to geo',
|
||||||
|
$r->code,
|
||||||
|
$r->message,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (defined ($geo->[0]->{lat}) && defined ($geo->[0]->{lon})) {
|
||||||
|
# Success
|
||||||
|
return $geo->[0];
|
||||||
|
} else {
|
||||||
|
# Error, no coordinates
|
||||||
|
$log->warn ('[WARN] Server api.openweathermap.org does not return coordinates in response to geo request');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
||||||
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
# vim: set ft=perl noet ai ts=4 sw=4 sts=4:
|
||||||
|
|||||||
Reference in New Issue
Block a user