Modify weather script to pull hourly data and format in tables

master
Miles Alan 5 years ago
parent 95f171e7da
commit 9919f49d82
  1. 164
      scripts/appscripts/sxmo_weather.sh

@ -1,24 +1,142 @@
#!/usr/bin/env sh #!/usr/bin/env sh
[ -z "$SXMO_WEATHER_ZIPS" ] && SXMO_WEATHER_ZIPS=" [ -z "$SXMO_GPSLOCATIONSFILES" ] && SXMO_GPSLOCATIONSFILES="/usr/share/sxmo/places_for_gps.tsv"
10025 - NYC NHOURS=72
60007 - Chicago ROWHOURS=12
94016 - San Francisco WEATHERXML=""
97035 - Portland, OR
" downloadweatherxml() {
WEATHERXML="$(
pidof svkbd-sxmo || svkbd-sxmo & curl "https://forecast.weather.gov/MapClick.php?lat=$LAT&lon=$LON&FcstType=digitalDWML"
ZIP=$( )"
printf %b "$SXMO_WEATHER_ZIPS" | }
awk 'NF' |
awk '{$1=$1};1' | weatherdata() {
dmenu -fn Terminus-20 -i -c -l 10 -p "US Zipcode" | XPATH="$1"
awk -F " " '{print $1}' GREP="$2"
) echo "$WEATHERXML" |
pkill svkbd-sxmo xmllint --xpath "$XPATH" - |
grep "$GREP" |
LATLON="$(grep "^$ZIP" /usr/share/sxmo/zipcodes_for_weather.csv)" sed 's/<[^>]*>/ /g' |
LAT="$(echo "$LATLON" | cut -d, -f2 | tr -d ' ')" sed 's/ / /g' |
LON="$(echo "$LATLON" | cut -d, -f3 | tr -d ' ')" awk '{$1=$1};1'
URL='https://forecast.weather.gov/MapClick.php?lat='$LAT'&lon='$LON'&unit=0&lg=english&FcstType=text&TextType=1' }
st -f Monospace-20 -e w3m "$URL" printarow() {
LABEL="$1"
NITEMS="$2"
DATA="$3"
INDENT="$4"
printf %b "$LABEL: $INDENT"
for i in $(seq "$NITEMS"); do
NUM="$(echo "$DATA" | grep -Eo '[0-9]+' | head -n1 | bc)"
if [ "$NUM" -gt "15" ] && [ "$LABEL" = "Rain" ]; then
# Rain indicator
tput setaf 12; printf "%2d " "$NUM"; tput sgr0
else
printf "%2d " "$NUM"
fi
DATA="$(echo "$DATA" | grep -Eo '[0-9]+' | tail -n+2)"
done
printf "%b" "\n"
}
clearitemsfromrow() {
NITEMS="$1"
DATA="$2"
for i in $(seq "$NITEMS"); do
DATA="$(echo "$DATA" | grep -Eo '[0-9]+' | tail -n+2)"
done
echo "$DATA"
}
printtables() {
NOWDAY="$(date +%s)"
NOWHR="$(echo "$TIME" | cut -c 1-2)"
INDENTN="0"
if [ "$NOWHR" != "00" ] && [ "$NOWHOUR" != "12" ]; then
if [ "$NOWHR" -gt "12" ]; then
INDENTN="$(echo "$NOWHR - 12" | bc)"
else
INDENTN="$(echo "12 - $NOWHR" | bc)"
fi
fi
INDENT=""
# shellcheck disable=SC2034
for i in $(seq "$INDENTN"); do INDENT="$INDENT "; done
FULLROWHOURS="$ROWHOURS"
ROWHOURS="$(echo "$ROWHOURS - $INDENTN" | bc)"
LASTDAY=""
# E.g. each while loop iteration handles 1 row
while echo "$TIME" | grep -Eq "[0-9]+" && [ "$NHOURS" -gt 0 ]; do
if [ "$LASTDAY" != "$NOWDAY" ]; then
printf "%b" "\n"
tput setaf 14; date -d "@$NOWDAY" +'%a %b %d'; tput sgr0
fi
echo "-----------------------------------------------------"
LASTDAY="$NOWDAY"
printarow "Time" "$ROWHOURS" "$TIME" "$INDENT"
printarow "Temp" "$ROWHOURS" "$TEMP" "$INDENT"
printarow "Rain" "$ROWHOURS" "$RAIN" "$INDENT"
printarow "Wind" "$ROWHOURS" "$WIND" "$INDENT"
TIME="$(clearitemsfromrow "$ROWHOURS" "$TIME")"
TEMP="$(clearitemsfromrow "$ROWHOURS" "$TEMP")"
RAIN="$(clearitemsfromrow "$ROWHOURS" "$RAIN")"
WIND="$(clearitemsfromrow "$ROWHOURS" "$WIND")"
echo "$TIME" | tr -d " " | tr -d '\n' | grep -Eq "^0" &&
NOWDAY="$(echo "$NOWDAY + (60 * 60 * 24)" | bc)"
NHOURS="$(echo "$NHOURS - $ROWHOURS" | bc)"
ROWHOURS="$FULLROWHOURS"
INDENT=""
done
}
getweathertexttable() {
LAT="$1"
LON="$2"
PLACE="$3"
downloadweatherxml "$LAT" "$LON" 2>/dev/null
TEMP="$(weatherdata "//temperature" "hourly")"
RAIN="$(weatherdata "//probability-of-precipitation" ".")"
WIND="$(weatherdata "//wind-speed" ".")"
#LOCATION="$(weatherdata "//location/description" ".")"
TIME="$(
weatherdata "//start-valid-time" "." |
grep -oE 'T[0-9]{2}' | tr -d 'T' | tr '\n' ' '
)"
tput rev; echo "$PLACE"; tput sgr0
printtables
read -r
}
weathermenu() {
CHOICE="$(
printf %b "$(
echo "Close Menu";
echo "$SXMO_GPSLOCATIONSFILES" |
tr "," "\n" |
xargs cat |
grep "United States" # Note only US latlons work on weather.gov
)" |
grep -vE '^#' |
sed "s/\t/: /g" |
sxmo_dmenu_with_kb.sh -i -c -l 10 -fn Terminus-18 -p "Locations"
)"
if [ "$CHOICE" = "Close Menu" ]; then
exit 0
else
PLACE="$(printf %b "$CHOICE" | cut -d: -f1 | awk '{$1=$1};1')"
LAT="$(printf %b "$CHOICE" | cut -d: -f2- | awk '{$1=$1};1' | cut -d ' ' -f1)"
LON="$(printf %b "$CHOICE" | cut -d: -f2- | awk '{$1=$1};1' | cut -d ' ' -f2)"
st -e "$0" getweathertexttable "$LAT" "$LON" "$PLACE"
fi
}
if [ -z "$1" ]; then
weathermenu
else
"$@"
fi

Loading…
Cancel
Save