scripts/waybar/waybar-weather.pl

48 lines
974 B
Perl
Raw Normal View History

2022-07-20 23:45:18 +00:00
#!/usr/bin/perl
use strict;
use warnings;
my $cmd = 'bar';
if (defined($ARGV[0])) {
2023-01-06 19:41:31 +00:00
$cmd = $ARGV[0];
2022-07-20 23:45:18 +00:00
}
my $url = "https://john.me.tz/weather/weather.json";
my %icons = (
2023-01-06 19:41:31 +00:00
'01d' => "☀",
'01n' => "☾",
'02d' => "☁",
'02n' => "☁",
'03d' => "☁",
'03n' => "☁",
'04d' => "☁",
'04n' => "☁",
'10d' => "🌧",
'10n' => "🌧",
2022-07-20 23:45:18 +00:00
);
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $ret = $ua->get($url);
unless (defined($ret->{_rc}) && $ret->{_rc} == 200) {
2023-01-06 19:41:31 +00:00
die "Failed to fetch $url";
2022-07-20 23:45:18 +00:00
}
use JSON::XS;
my $json = JSON::XS->new();
my $ref = $json->decode($ret->{_content});
if ($cmd eq 'bar') {
2023-01-06 19:41:31 +00:00
my $temp = $ref->{current}->{temp} - 273.15;
my $icon = $ref->{current}->{weather}->[0]->{icon} //= '';
2022-07-20 23:45:18 +00:00
printf("<span font='Anonymice Nerd Font 18'>%s</span>%.1f%s", $icons{$icon}, ${temp}, "°C");
2022-07-20 23:45:18 +00:00
} elsif ($cmd eq 'notify') {
2023-01-06 19:41:31 +00:00
`notify-send weather TODO`;
2022-07-20 23:45:18 +00:00
} else {
2023-01-06 19:41:31 +00:00
die "Invalid command\n";
2022-07-20 23:45:18 +00:00
}