aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dotfiles/.config/waybar/config2
-rwxr-xr-xdotfiles/.local/bin/barweather35
2 files changed, 29 insertions, 8 deletions
diff --git a/dotfiles/.config/waybar/config b/dotfiles/.config/waybar/config
index 5e5b800..7ec4d73 100644
--- a/dotfiles/.config/waybar/config
+++ b/dotfiles/.config/waybar/config
@@ -176,7 +176,7 @@
},
"custom/weather": {
"format": "{}",
- "interval": 900,
+ "interval": 180,
"exec": "barweather"
},
"custom/swaykbd": {
diff --git a/dotfiles/.local/bin/barweather b/dotfiles/.local/bin/barweather
index 0ae6d3c..da8f25f 100755
--- a/dotfiles/.local/bin/barweather
+++ b/dotfiles/.local/bin/barweather
@@ -1,18 +1,39 @@
-#!/bin/bash
+#!/bin/sh
+cachefile="$HOME/.cache/weather"
location="Saint Petersburg"
+update_freq=1800
+obsolete_after=10800
-if [[ "$location" != "" ]]
-then
+if [ -n "$location" ]; then
location="~${location// /+}"
fi
-forecast=$(curl -s wttr.in/$location?format=1)
+update_forecast() {
+ ping -q -c 1 1.1.1.1 >/dev/null || exit
+ forecast=$(curl -s wttr.in/"$location"?format=1)
+ if [ ${#forecast} -lt 30 ]; then
+ printf "%s\n" "$forecast" > "$cachefile"
+ fi
+}
-if [[ ${#forecast} -gt 30 ]]
-then
+if [ -f "$cachefile" ]; then
+ last_modif=$(date +%s -r "$cachefile")
+ current_secs=$(date +%s)
+ elapsed=$((current_secs - last_modif))
+ if [ $elapsed -gt $obsolete_after ]; then
+ rm "$cachefile"
+ fi
+ if [ $elapsed -gt $update_freq ]; then
+ update_forecast
+ fi
+else
+ update_forecast
+fi
+
+if [ ! -f "$cachefile" ]; then
exit
fi
-echo $forecast
+cat "$cachefile"