From 56a6299d4f0d93069544eadcaadd2d53f4f4dc6f Mon Sep 17 00:00:00 2001 From: John Mertz Date: Wed, 20 Jul 2022 19:45:18 -0400 Subject: [PATCH] Simple weather bar applet --- waybar/waybar-weather.pl | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 waybar/waybar-weather.pl diff --git a/waybar/waybar-weather.pl b/waybar/waybar-weather.pl new file mode 100755 index 0000000..2f5efcd --- /dev/null +++ b/waybar/waybar-weather.pl @@ -0,0 +1,38 @@ +#!/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 = ( + '04d' => "☁", +); +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("%s%.1f%s", $icons{$icon}, ${temp}, "°C"); +} elsif ($cmd eq 'notify') { + `notify-send weather TODO`; +} else { + die "Invalid command\n"; +}