blob: dfaad4bef297774b4e7a4f4b913985c9b748310c (
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#!/bin/sh
arg=$1
report=~/.cache/weather_report.json
ifinstalled jq
# Get a weather report from 'wttr.in' and save it locally.
getforecast() { { rg -q -m1 '^up$' /sys/class/net/w*/operstate || rg -q -m1 '^up$' /sys/class/net/e*/operstate; } &&
curl -sf "wttr.in/?format=j1" --output "$report" && touch "$report"
}
# Forecast should be updated only once a day.
checkforecast() {
[ "$(stat -c %y "$report" 2>/dev/null |
cut -d' ' -f1)" = "$(date '+%Y-%m-%d')" ]
}
# Function to get weather emoji from code
get_weather_emoji() {
local code="$1"
grep "^$code " ~/.local/share/extras/weather_codes | awk '{print $2}'
}
showweather(){
temp=$(jq -r '.current_condition[0].temp_C' $report)
code=$(jq -r '.current_condition[0].weatherCode' $report)
rainchance=$(jq '.weather[].hourly[].chanceofrain | tonumber' $report | sort -n | tail -n 1)
emoji=$(get_weather_emoji "$code")
case "$arg" in
weather)
case 1 in
$((temp >= 30)) ) class="very_hot" ;;
$((temp >= 26)) ) class="hot" ;;
$((temp >= 21)) ) class="warm" ;;
$((temp >= 16)) ) class="mild" ;;
$((temp >= 11)) ) class="cool" ;;
$((temp >= 6)) ) class="chilly" ;;
$((temp >= 0)) ) class="cold" ;;
$((temp < 0)) ) class="below_zero" ;;
* ) echo unavailable && exit ;;
esac
output="$emoji$temp°C"
;;
rain)
case 1 in
$((rainchance >= 70)) ) class="high" ;;
$((rainchance >= 45)) ) class="mid" ;;
$((rainchance > 0)) ) class="low" ;;
$((rainchance <= 0)) ) printf '' && exit ;;
* ) echo unavailable && exit ;;
esac
output="$rainchance%"
;;
*)
echo "Usage: $0 {weather|rain}"
exit 1
;;
esac
printf '{"text": "%s", "class": "%s" }' "$output" "$class"
}
case $BLOCK_BUTTON in
2) getforecast && showweather ;;
3) notify-send " Weather module" "\- Middle click to update forecast.
: Chance of rain/snow
: Current temperature" ;;
8) setsid -f "$TERMINAL" -e "$EDITOR" "$0" >/dev/null 2>&1 ;;
esac
# shellcheck disable=SC2015
checkforecast && showweather ||
( flock -n 9 &&
( tries=0; while [ $tries -ne 100 ]; do
getforecast && break ||
{ tries=$((tries+1)); sleep .1; }
done
! checkforecast &&
until getforecast; do sleep 60; done
pkill -RTMIN+5 "${STATUSBAR:-waybar}"
) &
echo ) 9>"${XDG_RUNTIME_DIR}/sb-forecast.lock"
|