aboutsummaryrefslogtreecommitdiff
path: root/challenge-011/dave-jacoby/perl5/ch-3.pl
blob: cebe2559e800ee9cf05991aca5d67f34258da35f (plain)
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
44
45
46
#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use feature qw{ postderef say signatures state switch fc };
no warnings
    qw{ experimental::postderef experimental::smartmatch experimental::signatures };

# Using Open Weather Map API, write a script to fetch
# the current weather for an arbitrary city.
# Note that you will need to sign up for Open Weather Map's
# free tier and then wait a couple hours before your API key
# will be valid. This challenge was proposed by Joelle Maslak.
# The API challenge is optional but would love to see your solution.

# usage: ./pc011c3.pl london,uk washington,us paris

use JSON;
use Mojo::UserAgent;
use YAML qw{LoadFile};

my $json = JSON->new->pretty->canonical;
my $mojo = Mojo::UserAgent->new;

my $conf = LoadFile join '/', $ENV{HOME}, '.openweather.yml';
my $key  = $conf->{key};

for my $location (@ARGV) {
    my $query    = {
        q     => $location,
        appid => $key,
    };

    my $url = 'https://api.openweathermap.org/data/2.5/weather';
    my $res = $mojo->get( $url => form => $query )->result;
    if ( $res->is_success ) {
        my $obj = $json->decode( $res->body );
        say $json->encode($obj);
    } else {
        say $res->{code};
    }
}

# OK, verbose in JSON and not quite readable, but it fulfills the 
# letter of the challenge.