diff --git a/modules/weather/weather.sh b/modules/weather/weather.sh index 225dd53..dd69223 100755 --- a/modules/weather/weather.sh +++ b/modules/weather/weather.sh @@ -7,30 +7,67 @@ channelName="$2" shift shift -# Get the current weather information into a variable and stip all blank lines. -args="$@" -weather="$(curl -s http://wttr.in/${args//[[:space:]]/+}?T0 | sed '/^$/d')" +if [[ $# -eq 0 ]]; then + msg "$channelName" "$name: Please provide a location or postcode." + return 0 +fi -# Get the location -location="$(echo "$weather" | head -n1 | cut -f2 -d: | sed 's/^[[:space:]]//')" -# Strip location info from the weather variable -weather="${weather#*$'\n'}" +location="${*}" -# Extract current conditions from the weather variable -conditions="${weather%* }" -conditions="${conditions%%$'\n'*}" -conditions="${conditions##* }" -# Remove conditions info from weather variable. -# Also remove everything after the next line because we only need the temperature info. -weather="${weather#*$'\n'}" -weather="${weather%%$'\n'*}" +# Convert spaces to +. +location="${location//[[:space:]]/+}" -# Get and format the temperature information from the weather variable. -temperature="${weather##*. }" -temperature="${temperature##*-}" -# Remove leading and trailing spaces. -temperature="$(echo "$temperature" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" -# Show the weather information. -msg "$channelName" "$name: weather for ${location} is ${conditions} and ${temperature}." -set +f +# 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//+/ }"