Skip to content

Instantly share code, notes, and snippets.

@major
Created July 29, 2020 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save major/e067f062d58b6eb4e7f625a77450de65 to your computer and use it in GitHub Desktop.
Save major/e067f062d58b6eb4e7f625a77450de65 to your computer and use it in GitHub Desktop.
Weather + alerts lookup for i3 status bar
#!/bin/bash
WTTR_URL="http://wttr.in/KRND?format=j1"
WTTR_JSON=/tmp/weather-wttr.json
ALERTS_URL=https://api.weather.gov/alerts/active/zone/TXZ207
ALERTS_JSON=/tmp/weather-alerts.json
UPDATE_JSON=yes
if [[ -f $WTTR_JSON ]]; then
CURRENT_TIME=$(date +%s)
LAST_UPDATED=$(stat -c %Y $WTTR_JSON)
AGE=$(expr $CURRENT_TIME - $LAST_UPDATED)
if [[ $AGE -lt 900 ]]; then
UPDATE_JSON=no
fi
fi
# Update weather JSON file.
if [[ $UPDATE_JSON == yes ]]; then
curl --retry 5 --output "${WTTR_JSON}" -s "${WTTR_URL}"
curl --retry 5 --output "${ALERTS_JSON}" -s "${ALERTS_URL}"
fi
c_cond () {
jq -r ".current_condition[].${1}" $WTTR_JSON
}
alerts () {
jq -r '.features[].properties.event' /tmp/weather-alerts.json 2>&1 | sed -z 's/\n/, /' | sed 's/, $//'
}
TEMP_C=$(c_cond temp_C)
TEMP_F=$(c_cond temp_F)
FEEL_C=$(c_cond FeelsLikeC)
FEEL_F=$(c_cond FeelsLikeF)
HUMIDITY=$(c_cond humidity)
PRESSURE=$(c_cond pressure)
DESCRIPTION=$(c_cond weatherDesc[].value)
WIND_DIR=$(c_cond winddir16Point)
WIND_SPEED=$(c_cond windspeedMiles)
echo -n "${DESCRIPTION}  ${TEMP_C}°C/${TEMP_F}°F (${FEEL_C}°C/${FEEL_F}°F) "
echo -n " ${HUMIDITY}%  ${WIND_DIR} @ ${WIND_SPEED} ${PRESSURE}mb "
ALERTS=$(alerts)
if [[ ${ALERTS} != "" ]]; then
echo -n " $(alerts)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment