From ff9a299f68c343a1e7a8a5a465309860c957103c Mon Sep 17 00:00:00 2001 From: Tony Blyler Date: Wed, 7 Sep 2022 08:57:27 -0400 Subject: [PATCH] Update weather.zsh to honor timeouts and try again later --- chezmoi/dot_zshrc.d/executable_weather.zsh | 29 ++++++++++++++-------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/chezmoi/dot_zshrc.d/executable_weather.zsh b/chezmoi/dot_zshrc.d/executable_weather.zsh index 185b832..fc2c4de 100644 --- a/chezmoi/dot_zshrc.d/executable_weather.zsh +++ b/chezmoi/dot_zshrc.d/executable_weather.zsh @@ -35,7 +35,7 @@ function weather_short_status() { WEATHER_LOCATION_KEY="$(< "${HOME_LOCATION_KEY_PATH}")" - _weather_current_conditions | jq -r "$(<<'EOF' + _weather_current_conditions | jq -er "$(<<'EOF' first | [ "RF:"+(.RealFeelTemperature.Imperial.Value | tostring)+"°", @@ -49,7 +49,7 @@ first | join(" ") | rtrimstr(" ") EOF - )" + )" 2>/dev/null ) } @@ -151,15 +151,22 @@ function _weather_current_conditions() { TEMP_OUTPUT="$(mktemp)" trap 'rm -f "${TEMP_OUTPUT}"' EXIT - curl -LSsf \ - --max-time "${WEATHER_CURL_TIMEOUT}" \ - --get \ - -X GET \ - --data-urlencode "apikey=${WEATHER_API_KEY}" \ - --data-urlencode "language=en-us" \ - --data-urlencode "details=true" \ - "https://dataservice.accuweather.com/currentconditions/v1/${WEATHER_LOCATION_KEY}" \ - -o "${TEMP_OUTPUT}" + ( + set +e + curl -LSsf \ + --max-time "${WEATHER_CURL_TIMEOUT}" \ + --get \ + -X GET \ + --data-urlencode "apikey=${WEATHER_API_KEY}" \ + --data-urlencode "language=en-us" \ + --data-urlencode "details=true" \ + "https://dataservice.accuweather.com/currentconditions/v1/${WEATHER_LOCATION_KEY}" \ + -o "${TEMP_OUTPUT}" + if [ $? -eq 28 ]; then + # timeout reached, just store empty data + echo '{}' > "${TEMP_OUTPUT}" + fi + ) mv -f "${TEMP_OUTPUT}" "${CURRENT_WEATHER_CACHE_FILE}" > /dev/stdout < "${CURRENT_WEATHER_CACHE_FILE}"