1
0
Fork 0
dotfiles/screen/weather

80 lines
2.2 KiB
Bash
Executable File

#!/bin/bash
#
# Retrieves and formats current weather
#
# Copyright (C) 2013 Mike Gerwitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# In order to prevent the screen session from hanging due to network latency
# or a slow weather server, it is highly recommended that a cron job or some
# other task be used to populate /tmp/.weather with the output of the
# weather command. Should a file be empty or non-existant, the script will
# fall back to querying realtime.
##
export -n HTTP_PROXY http_proxy
# fall back to realtime weather data if no cache is available
wdata=/tmp/.weather
data="$(
if [ -s "$wdata" ]; then
cat /tmp/.weather
else
weather -i "${WEATHER_METAR:-kbuf}"
fi \
| sed 's/^ \+//g'
)"
weather_temp="$( echo "$data" \
| grep ^Temp \
)"
weather_f="$( echo "$weather_temp" \
| grep -oP '[0-9\.-]+ F' \
| cut -d' ' -f1 \
)"
weather_c="$( echo "$weather_temp" \
| grep -oP '[0-9\.-]+ C' \
| cut -d' ' -f1 \
)"
wind="$( grep -o '[0-9]\+ MPH' <<< "$data" \
| tr '\n' '-' \
| sed 's/-$//;s/ \?MPH-/-/' \
)"
# remove decimal
chk="$( echo "$weather_f" | cut -d. -f1 )"
# determine color based on temperature
color='.'
if [ $chk -gt 89 ]; then
color=r
elif [ $chk -gt 69 ]; then
color=y
elif [ $chk -gt 39 ]; then
color=d
elif [ $chk -gt 9 ]; then
color=b
else
color=m # purple with my color scheme
fi
# if it's sunny, make the status brighter
echo "$data" | grep -qP 'sunny|(mostly )?clear|partly cloudy' && {
color=$( echo $color | tr '[:lower:]' '[:upper:]' )
}
echo -e "\005{+ .$color}${weather_f}F/${weather_c}C $wind\005{-}"