aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/barweather
blob: da8f25f5fdaa3bb0a5e0389a1773cdff80d6e1a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/sh

cachefile="$HOME/.cache/weather"
location="Saint Petersburg"
update_freq=1800
obsolete_after=10800

if [ -n "$location" ]; then
    location="~${location// /+}"
fi

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 [ -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

cat "$cachefile"