]> gitweb.fluxo.info Git - scripts.git/commitdiff
Cache support at weather-query
authorSilvio Rhatto <rhatto@riseup.net>
Wed, 3 Jun 2015 14:57:13 +0000 (11:57 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Wed, 3 Jun 2015 14:57:13 +0000 (11:57 -0300)
weather-query

index 774bb739e7af4e6fa48f3e38f19478f126aeb694..e925342576bd53bcb23259e8df15343d23133f2d 100755 (executable)
@@ -3,11 +3,38 @@
 # Simple wrapper around brweather
 #
 
+# Parameters
+BASENAME="`basename $0`"
 PROGRAM="$1"
+CACHE="$HOME/.local/share/weather"
+
+# Command line args
 shift
 
+# Weather service query
+function weather_query {
+  # Make sure everything we need exists
+  mkdir -p $CACHE
+  touch $CACHE/weather.cur $CACHE/weather.prev
+
+  # Save the previous forecast
+  cp $CACHE/weather.cur $CACHE/weather.prev
+  weather $* > $CACHE/weather.cur
+
+  # Test if current forecast is empty, meaning
+  # that we're probably ofline. In that case
+  # we provide the previous output.
+  if [ ! -s "$CACHE/weather.cur" ]; then
+    cp $CACHE/weather.pre $CACHE/weather.cur
+  fi
+
+  # Display output
+  cat $CACHE/weather.cur
+}
+
+# Main
 if [ "$PROGRAM" == 'brweather' ]; then
   brweather $* | grep -v 'Erro.'
 else
-  weather $*
+  weather_query
 fi