scripts/waybar/waybar-weather.pl

48 lines
974 B
Perl
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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