aboutsummaryrefslogtreecommitdiff
path: root/dotfiles/.local/bin/barweather
blob: 1fd2c6cb2656a99ce6974ec3c7c96ea19dc0fd2f (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
#!/bin/sh

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

update_forecast() {
  # Check several times before giving up, useful when just waking up from sleep
  # since internet is usually not available right away
  wget -q --tries=5 --timeout=20 --spider 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"