Skip to content

Commit

Permalink
weather.sh: Introduce fallback when wttr.in reaches call limit
Browse files Browse the repository at this point in the history
When wttr.in reaches call limit returns the following:

Sorry, we are running out of queries to the weather service at the moment.
Here is the weather report for the default city (just to show you what it looks like).
We will get new queries as soon as possible.
You can follow https://twitter.com/igor_chubin for the updates.

Which makes weather.sh display a random error.

Make the error predictable.
  • Loading branch information
fvincenzo committed Oct 21, 2024
1 parent ea1a45d commit 6580062
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions scripts/weather.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ display_weather()
temperature=$(echo $weather_information | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc
unicode=$(forecast_unicode $weather_condition)

echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature
if [[ "${temperature/+/}" == "error" ]]; then
# Propagate Error
echo "${temperature/+/}"
else
echo "$unicode ${temperature/+/}" # remove the plus sign to the temperature
fi
}

forecast_unicode()
Expand All @@ -76,7 +81,7 @@ forecast_unicode()
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
if timeout 1 bash -c "</dev/tcp/ipinfo.io/443" && timeout 1 bash -c "</dev/tcp/wttr.in/443" && [[ "$(display_weather)" != "error" ]]; then
echo "$(display_weather)$(display_location)"
else
echo "Weather Unavailable"
Expand Down

0 comments on commit 6580062

Please # to comment.