Convert weather script to bash to avoid Perl dependencies

This commit is contained in:
John Mertz 2024-10-15 18:48:23 -06:00
parent 16671eaec1
commit cf75de8ee9
2 changed files with 60 additions and 47 deletions

View File

@ -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("<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";
}

60
waybar/waybar-weather.sh Executable file
View File

@ -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'"}'