-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex4_cli.pl
More file actions
43 lines (36 loc) · 982 Bytes
/
Copy pathex4_cli.pl
File metadata and controls
43 lines (36 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!perl
use Mojo::Base -strict;
use Mojo::IOLoop;
use Mojo::UserAgent;
my $ua = Mojo::UserAgent->new;
my $addr = shift // die("Please pass in an IP");
# Non-blocking requests (synchronized with a delay)
Mojo::IOLoop->delay(
sub {
my $delay = shift;
my $ip_query = sprintf("http://ip-api.com/json/%s", $addr);
$ua->get($ip_query => $delay->begin);
},
sub {
my ($delay, $ip) = @_;
# Setup weather query
my $query = sprintf(
"lat=%s&lon=%s&unit=0&lg=english&FcstType=json",
$ip->res->json->{lat},
$ip->res->json->{lon}
);
my $url = sprintf("http://forecast.weather.gov/MapClick.php?%s", $query);
$ua->get($url => $delay->begin);
},
sub {
my ($delay, $weather) = @_;
# Talk about the weather
my $j = $weather->res->json;
say(sprintf(
"$addr: %s: %s: %s",
$j->{location}{areaDescription},
$j->{time}{startPeriodName}[0],
$j->{data}{text}[0]
));
}
)->wait;