diff options
author | Yaroslav <contact@yaroslavps.com> | 2020-04-02 15:56:12 +0300 |
---|---|---|
committer | Yaroslav <contact@yaroslavps.com> | 2020-04-02 15:56:12 +0300 |
commit | 4a8d06f7ae982fd8d694c34b2383e6ae0a54d3e4 (patch) | |
tree | c80c8baaffc4faf0159d5867029fc6cc2eb630ca /dotfiles/.local/bin/barweather | |
parent | 07bcb021bcdf71cef93bb9c0dc09e30af6ad2f7d (diff) | |
download | swayrice-4a8d06f7ae982fd8d694c34b2383e6ae0a54d3e4.tar.gz swayrice-4a8d06f7ae982fd8d694c34b2383e6ae0a54d3e4.zip |
weather bar module improvements
Diffstat (limited to 'dotfiles/.local/bin/barweather')
-rwxr-xr-x | dotfiles/.local/bin/barweather | 35 |
1 files changed, 28 insertions, 7 deletions
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" |