-#! /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"
| 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)
echo "$ip_addresses"
echo "$bar"
echo ''
+echo "Fetched on: $(stat -c %y $FILE_WEATHER)"
echo "$weather"
echo ''