stormbot/modules/weather/weather.sh
2020-08-10 20:23:56 -04:00

74 lines
1.9 KiB
Bash
Executable File

[ -f functions.sh ] && source functions.sh
set -f
name="$1"
channelName="$2"
shift
shift
if [[ $# -eq 0 ]]; then
msg "$channelName" "$name: Please provide a location or postcode."
return 0
fi
location="${*}"
# Convert spaces to +.
location="${location//[[:space:]]/+}"
# c Weather condition,
# C Weather condition textual name,
# h Humidity,
# t Temperature (Actual),
# f Temperature (Feels Like),
# w Wind,
# l Location,
# m Moonphase ðð,
# M Moonday,
# p precipitation (mm),
# o Probability of Precipitation,
# P pressure (hPa),
# D Dawn*,
# S Sunrise*,
# z Zenith*,
# s Sunset*,
# d Dusk*.
format="%c|%C|%h|%t|%f|%w|%l|%m|%M|%p|%o|%P|%D|%S|%z|%s|%d"
argList="${format//%/}"
argList="${argList//|/ }"
weatherString="$(curl -s https://wttr.in/${location}?format="${format}")"
i=1
declare -A weatherInfo
for j in $argList ; do
weatherInfo[$j]="$(echo "${weatherString}" | cut -d '|' -f $i)"
((i++))
done
# Format times to 12 hour format.
for i in S D z s d ; do
weatherInfo[$i]="$(date '+%r' --date="${weatherInfo[$i]}")"
done
message="${weatherInfo[l]}: ${weatherInfo[t]} and ${weatherInfo[C]}"
if [[ "${weatherInfo[t]}" == "${weatherInfo[f]}" ]]; then
message+=". "
else
message+=" with a real feel of ${weatherInfo[f]}. "
fi
message+="Wind: ${weatherInfo[w]} "
if [[ "${weatherInfo[p]}" != "0.0mm" ]]; then
message+=" Precipitation: ${weatherInfo[p]} "
fi
if [[ -n "${weatherInfo[o]}" ]]; then
message+="Chance of precipitation ${weatherInfo[o]} "
fi
message+="Humidity: ${weatherInfo[h]}. "
message+="Sunrise: ${weatherInfo[S]}, Sunset: ${weatherInfo[s]}."
msg "$channelName" "$name: ${message//+/ }"