Skip to content

Commit

Permalink
Merge pull request #289 from ikubetoomuzik/master
Browse files Browse the repository at this point in the history
weather.sh: added timeouts to curl requests
  • Loading branch information
ethancedwards8 authored Oct 18, 2024
2 parents 3f892b2 + 66e3b0a commit ec85f84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 14 additions & 2 deletions scripts/weather.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fetch_weather_information()
#get weather display
display_weather()
{
fahrenheit=$1
if $fahrenheit; then
display_weather='&u' # for USA system
else
Expand Down Expand Up @@ -73,13 +74,24 @@ forecast_unicode()
fi
}

export -f display_weather
export -f display_location
export -f forecast_unicode
export -f fetch_weather_information

main()
{
# process should be cancelled when session is killed
if timeout 1 bash -c "</dev/tcp/ipinfo.io/443" && timeout 1 bash -c "</dev/tcp/wttr.in/443"; then
echo "$(display_weather)$(display_location)"
if ! weather=$(timeout 3 bash -c "display_weather $fahrenheit"); then
echo "Weather Unavailable"
elif ! location=$(timeout 3 bash -c display_location); then
echo "Location Unavailable"
else
echo "${weather}${location}"
fi
else
echo "Weather Unavailable"
echo "Network Error"
fi
}

Expand Down
5 changes: 5 additions & 0 deletions scripts/weather_wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ fixedlocation=$3
DATAFILE=/tmp/.dracula-tmux-data
LAST_EXEC_FILE="/tmp/.dracula-tmux-weather-last-exec"
RUN_EACH=1200
RETRY_EACH=60
TIME_NOW=$(date +%s)
TIME_LAST=$(cat "${LAST_EXEC_FILE}" 2>/dev/null || echo "0")

Expand All @@ -22,6 +23,10 @@ main()
# Run weather script here
$current_dir/weather.sh $fahrenheit $location "$fixedlocation" > "${DATAFILE}"
echo "${TIME_NOW}" > "${LAST_EXEC_FILE}"
elif grep -q 'Unavailable\|Error' "${DATAFILE}" && [ "$(expr ${TIME_LAST} + ${RETRY_EACH})" -lt "${TIME_NOW}" ]; then
# Run weather script here
$current_dir/weather.sh $fahrenheit $location "$fixedlocation" > "${DATAFILE}"
echo "${TIME_NOW}" > "${LAST_EXEC_FILE}"
fi

cat "${DATAFILE}"
Expand Down

0 comments on commit ec85f84

Please sign in to comment.