Skip to content

Commit ec85f84

Browse files
Merge pull request #289 from ikubetoomuzik/master
weather.sh: added timeouts to curl requests
2 parents 3f892b2 + 66e3b0a commit ec85f84

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

scripts/weather.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ fetch_weather_information()
4242
#get weather display
4343
display_weather()
4444
{
45+
fahrenheit=$1
4546
if $fahrenheit; then
4647
display_weather='&u' # for USA system
4748
else
@@ -73,13 +74,24 @@ forecast_unicode()
7374
fi
7475
}
7576

77+
export -f display_weather
78+
export -f display_location
79+
export -f forecast_unicode
80+
export -f fetch_weather_information
81+
7682
main()
7783
{
7884
# process should be cancelled when session is killed
7985
if timeout 1 bash -c "</dev/tcp/ipinfo.io/443" && timeout 1 bash -c "</dev/tcp/wttr.in/443"; then
80-
echo "$(display_weather)$(display_location)"
86+
if ! weather=$(timeout 3 bash -c "display_weather $fahrenheit"); then
87+
echo "Weather Unavailable"
88+
elif ! location=$(timeout 3 bash -c display_location); then
89+
echo "Location Unavailable"
90+
else
91+
echo "${weather}${location}"
92+
fi
8193
else
82-
echo "Weather Unavailable"
94+
echo "Network Error"
8395
fi
8496
}
8597

scripts/weather_wrapper.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fixedlocation=$3
1111
DATAFILE=/tmp/.dracula-tmux-data
1212
LAST_EXEC_FILE="/tmp/.dracula-tmux-weather-last-exec"
1313
RUN_EACH=1200
14+
RETRY_EACH=60
1415
TIME_NOW=$(date +%s)
1516
TIME_LAST=$(cat "${LAST_EXEC_FILE}" 2>/dev/null || echo "0")
1617

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

2732
cat "${DATAFILE}"

0 commit comments

Comments
 (0)