diff --git a/waybar/waybar-weather.pl b/waybar/waybar-weather.pl deleted file mode 100755 index a7d39d4..0000000 --- a/waybar/waybar-weather.pl +++ /dev/null @@ -1,47 +0,0 @@ -#!/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("%s%.1f%s", $icons{$icon}, ${temp}, "°C"); -} elsif ($cmd eq 'notify') { - `notify-send weather TODO`; -} else { - die "Invalid command\n"; -} diff --git a/waybar/waybar-weather.sh b/waybar/waybar-weather.sh new file mode 100755 index 0000000..d8e33f5 --- /dev/null +++ b/waybar/waybar-weather.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash + +URL="https://john.me.tz/weather/weather.json" + +curl $URL -o ${HOME}/.local/state/latest_weather 2>/dev/null +RAW="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .weather[0]')" +MAIN="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .weather[0] | .main')" +DESC="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .weather[0] | .description')" +ICON="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .weather[0] | .icon')" +WIND="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .wind_speed')" +WDEG="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .wind_deg')" +TEMP="$(cat ${HOME}/.local/state/latest_weather | jq -r '.current | .feels_like')" +TEMP=$(bc <<<"$TEMP-273.15" | cut -d '.' -f 1) + +if [[ $WDEG -gt 337 ]]; then + WDIR=North +elif [[ $WDEG -gt 292 ]]; then + WDIR=North-West +elif [[ $WDEG -gt 247 ]]; then + WDIR=West +elif [[ $WDEG -gt 202 ]]; then + WDIR=South-West +elif [[ $WDEG -gt 157 ]]; then + WDIR=South +elif [[ $WDEG -gt 112 ]]; then + WDIR=South-East +elif [[ $WDEG -gt 67 ]]; then + WDIR=East +elif [[ $WDEG -gt 22 ]]; then + WDIR=North-East +else + WDIR=North +fi + +if [[ $ICON == '01d' ]]; then + ICON="☀" +elif [[ $ICON == '01n' ]]; then + ICON="☾" +elif [[ $ICON == '02d' ]]; then + ICON="☁" +elif [[ $ICON == '02n' ]]; then + ICON="☁" +elif [[ $ICON == '03d' ]]; then + ICON="☁" +elif [[ $ICON == '03n' ]]; then + ICON="☁" +elif [[ $ICON == '04d' ]]; then + ICON="☁" +elif [[ $ICON == '04n' ]]; then + ICON="☁" +elif [[ $ICON == '10d' ]]; then + ICON="🌧" +elif [[ $ICON == '10n' ]]; then + ICON="🌧" +else + ICON="?" +fi + +echo '{"text": "'$ICON'  '$TEMP'°C", "tooltip": "Condition: '$DESC', Wind: '$WIND'km/h '$WDIR'", "class": "'$MAIN'"}' +