mirror of
https://github.com/pragma-/pbot.git
synced 2024-11-20 02:49:49 +01:00
Plugins/Wttr: minor improvements
This commit is contained in:
parent
3251cbdc58
commit
f099abb9e7
@ -21,6 +21,7 @@ no if $] >= 5.018, warnings => "experimental::smartmatch";
|
|||||||
use LWP::UserAgent::WithCache;
|
use LWP::UserAgent::WithCache;
|
||||||
use JSON;
|
use JSON;
|
||||||
use Getopt::Long qw(GetOptionsFromString);
|
use Getopt::Long qw(GetOptionsFromString);
|
||||||
|
use URI::Escape qw/uri_escape_utf8/;
|
||||||
use Carp ();
|
use Carp ();
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
@ -107,10 +108,10 @@ sub wttrcmd {
|
|||||||
delete $options{default};
|
delete $options{default};
|
||||||
}
|
}
|
||||||
|
|
||||||
return $self->get_weather($arguments, %options);
|
return $self->get_wttr($arguments, %options);
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_weather {
|
sub get_wttr {
|
||||||
my ($self, $location, %options) = @_;
|
my ($self, $location, %options) = @_;
|
||||||
|
|
||||||
my %cache_opt = (
|
my %cache_opt = (
|
||||||
@ -118,10 +119,10 @@ sub get_weather {
|
|||||||
'default_expires_in' => 3600
|
'default_expires_in' => 3600
|
||||||
);
|
);
|
||||||
|
|
||||||
$location = lc $location;
|
my $location_uri = uri_escape_utf8 $location;
|
||||||
|
|
||||||
my $ua = LWP::UserAgent::WithCache->new(\%cache_opt, timeout => 10);
|
my $ua = LWP::UserAgent::WithCache->new(\%cache_opt, timeout => 10);
|
||||||
my $response = $ua->get("http://wttr.in/$location?format=j1&m");
|
my $response = $ua->get("http://wttr.in/$location_uri?format=j1&m");
|
||||||
|
|
||||||
my $json;
|
my $json;
|
||||||
if ($response->is_success) {
|
if ($response->is_success) {
|
||||||
@ -246,7 +247,7 @@ sub get_weather {
|
|||||||
|
|
||||||
when ('moon') {
|
when ('moon') {
|
||||||
my $a = $w->{'astronomy'}->[0];
|
my $a = $w->{'astronomy'}->[0];
|
||||||
$result .= "Moon: phase: $a->{'moon_phase'}, illumination: $a->{'moon_illumination'}, rise: $a->{'moonrise'}, set: $a->{'moonset'}; ";
|
$result .= "Moon: phase: $a->{'moon_phase'}, illumination: $a->{'moon_illumination'}%, rise: $a->{'moonrise'}, set: $a->{'moonset'}; ";
|
||||||
}
|
}
|
||||||
|
|
||||||
when ('sunrise') {
|
when ('sunrise') {
|
||||||
|
Loading…
Reference in New Issue
Block a user