X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=bin%2Ftoday;h=f9c2d289193680f400ce7b78061d5ded6c888a70;hb=0136ca23d40181f1fad3dd145586ee2d84bd6a22;hp=0c7192a997ef5c3736ba97b3f3d33b7f9aac71d4;hpb=4af9eedb66a0b7f3ae4201fe1b8260f265784f80;p=khome.git diff --git a/bin/today b/bin/today index 0c7192a..f9c2d28 100755 --- a/bin/today +++ b/bin/today @@ -1,7 +1,12 @@ -#! /bin/sh +#! /bin/bash set -e +DIR_DATA="$HOME/var/run/today" +FILE_WEATHER="$DIR_DATA/weather" + +mkdir -p "$DIR_DATA" + repeat() { x="$1" n="$2" @@ -40,11 +45,26 @@ ip_addresses=$( | column -t ) -weather=$( - curl "http://wttr.in/${ZIP_CODE}?1" 2> /dev/null \ - || \ - echo "Weather update failed" \ -) +weather_fetch() { + curl "http://wttr.in/${ZIP_CODE}?1" \ + 1> "$FILE_WEATHER" \ + 2> /dev/null +} + +weather_file_age() { + echo $(( $(date +%s) - $(stat -c '%Y' "$FILE_WEATHER") )) +} + +weather_get_or_fetch() { + weather_file_age_limit=$(( 3 * 60 * 60 )) + if [[ ! -e $FILE_WEATHER || weather_file_age -ge $weather_file_age_limit ]] + then + weather_fetch + fi + cat "$FILE_WEATHER" +} + +weather="$(weather_get_or_fetch)" clear #LANG="$ts_lang" ncal -M $(date +%Y) @@ -61,5 +81,6 @@ echo "$bar" echo "$ip_addresses" echo "$bar" echo '' +echo "Fetched on: $(stat -c %y $FILE_WEATHER)" echo "$weather" echo ''