tmux-kanagawa/scripts/weather.sh
David Dunham 504098ed74 Fix multi word fixed location
If you set the @dracula-fixed-location to "San Diego, California" it
ends up looking at "San" which is somewhere in South Korea. This occurs
for any fixed-location that has a space. Something like
"Portland, Oregon" can be modified to "Portland,Oregon" and still work
but we can't remove the space from "San Diego". The reason that spaces
in the fixed location break the backend request is that dracula.sh loses
everything after the space.

Q: Why does the fixed-location end up as "San"?
A: In dracula.sh the fixed-location is not wrapped in quotes when
embedded in the status-right string.

A: In weather_wrapper.sh the call to weather.sh does not wrap the
fixed-location argument with quotes.

Q: When these are fixed why do we get a broken link?
A: Since we are calling a web service using a url, we need to escape
the spaces by changing them to %20
2024-01-03 17:59:07 -08:00

75 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
fahrenheit=$1
location=$2
fixedlocation=$3
display_location()
{
if $location && [[ ! -z "$fixedlocation" ]]; then
echo " $fixedlocation"
elif $location; then
city=$(curl -s https://ipinfo.io/city 2> /dev/null)
region=$(curl -s https://ipinfo.io/region 2> /dev/null)
echo " $city, $region"
else
echo ''
fi
}
fetch_weather_information()
{
display_weather=$1
# it gets the weather condition textual name (%C), and the temperature (%t)
curl -sL wttr.in/${fixedlocation// /%20}\?format="%C+%t$display_weather"
}
#get weather display
display_weather()
{
if $fahrenheit; then
display_weather='&u' # for USA system
else
display_weather='&m' # for metric system
fi
weather_information=$(fetch_weather_information $display_weather)
weather_condition=$(echo $weather_information | rev | cut -d ' ' -f2- | rev) # Sunny, Snow, etc
temperature=$(echo $weather_information | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc
unicode=$(forecast_unicode $weather_condition)
echo "$unicode${temperature/+/}" # remove the plus sign to the temperature
}
forecast_unicode()
{
weather_condition=$(echo $weather_condition | awk '{print tolower($0)}')
if [[ $weather_condition =~ 'snow' ]]; then
echo '❄ '
elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then
echo '☂ '
elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then
echo '☁ '
elif [[ $weather_condition = 'NA' ]]; then
echo ''
else
echo '☀ '
fi
}
main()
{
# process should be cancelled when session is killed
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
echo "$(display_weather)$(display_location)"
else
echo "Location Unavailable"
fi
}
#run main driver program
main