Cache weather report
authorSiraaj Khandkar <siraaj@khandkar.net>
Mon, 25 Mar 2019 17:41:42 +0000 (13:41 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Mon, 25 Mar 2019 17:41:42 +0000 (13:41 -0400)
bin/today

index 0c7192a..74640e0 100755 (executable)
--- 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,21 @@ 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_get_or_fetch() {
+    weather_file_age=$(( $(date +%s) - $(stat -c '%Y' "$FILE_WEATHER") ))
+    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"
+}
 
 clear
 #LANG="$ts_lang" ncal -M $(date +%Y)
@@ -61,5 +76,6 @@ echo "$bar"
 echo "$ip_addresses"
 echo "$bar"
 echo ''
-echo "$weather"
+echo "Fetched on: $(stat -c %y $FILE_WEATHER)"
+echo "$(weather_get_or_fetch)"
 echo ''
This page took 0.02763 seconds and 4 git commands to generate.