summaryrefslogtreecommitdiffstats
path: root/bin/weather
blob: a0a9d14370e3955b5c3d4e605d6802efadf7625e (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

use open ':encoding(UTF-8)', ':std';

use DateTime;
use Path::Class;
use WWW::Wunderground::API;

chomp(my $api_key = file("$ENV{HOME}/.wunderground")->slurp);

my %weather_pics = (
    'Thunderstorm' => "\x{26a1}",
    'Rain'         => "\x{2614}",
    'Drizzle'      => "\x{2614}",
    'Showers'      => "\x{2614}",
    'Cloudy'       => "\x{2601}",
    'Clouds'       => "\x{2601}",
    'Overcast'     => "\x{2601}",
    'Fog'          => "\x{2592}",
    'Mist'         => "\x{2592}",
    'Haze'         => "\x{2592}",
    'Clear'        => "\x{2600}",
    'Pellets'      => "\x{2744}",
    'Snow'         => "\x{2744}",
);

my $wu = WWW::Wunderground::API->new(
    location => '10012',
    auto_api => 1,
    api_key  => $api_key,
);

my $forecast   = $wu->forecast->simpleforecast->forecastday->[0];
my $conditions = $wu->conditions;

my $temp      = int($conditions->temp_f);
my $high      = $forecast->{high}{fahrenheit};
my $low       = $forecast->{low}{fahrenheit};
my $feelslike = int($conditions->feelslike_f);
my $precip    = $forecast->{pop};

(my $description = $conditions->weather) =~ s/.* //;
my $weather = $weather_pics{$description} // $description;

my $time = DateTime->now(time_zone => 'local');

say join(' ',
    "$precip%",
    $weather,
    "${temp}F",
    (($feelslike != $temp)
        ? ("(\x{2245}${feelslike}F)")
        : ()),
    (($time->hour > 16 || $time->hour <= 4)
        ? ("\x{2193}${low}F")
        : ("\x{2191}${high}F")),
);