-
Notifications
You must be signed in to change notification settings - Fork 0
/
geonet-earthquake-monitor.sh
executable file
·154 lines (130 loc) · 3.35 KB
/
geonet-earthquake-monitor.sh
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
set -uo pipefail
SCRIPT_NAME=$(basename "${0}")
VERSION="1.0.1"
CACHE_FILE="${HOME}/.local/state/geonet-earthquake-monitor/notified.cache"
mkdir -p "$(dirname "${CACHE_FILE}")"
touch "${CACHE_FILE}"
usage() {
cat <<EOF
Usage: ${SCRIPT_NAME} -m MMI -p PRIORITY
Monitor NZ earthquakes using the GeoNet API and send notifications via ntfy.
Required arguments:
-m, --mmi MMI Minimum MMI intensity (0-8)
-p, --priority PRI Notification priority (1-5)
Optional arguments:
-h, --help Show this help message and exit
-v, --version Show version information and exit
Environment variables required:
NTFY_TOKEN_DEVICES Authentication token for ntfy
NTFY_GEONET_URL The ntfy URL to publish to
Example:
${SCRIPT_NAME} -m 4 -p 4
EOF
}
check_dependencies() {
local DEPS=(curl jq)
for DEP in "${DEPS[@]}"; do
if ! command -v "${DEP}" >/dev/null 2>&1; then
echo "Error: Required dependency '${DEP}' is not installed." >&2
exit 1
fi
done
}
check_env_vars() {
if [[ -z "${NTFY_TOKEN_DEVICES:-}" ]]; then
echo "Error: NTFY_TOKEN_DEVICES environment variable is not set." >&2
exit 1
fi
if [[ -z "${NTFY_GEONET_URL:-}" ]]; then
echo "Error: NTFY_GEONET_URL environment variable is not set." >&2
exit 1
fi
}
validate_args() {
if [[ ! "${MMI}" =~ ^[0-8]$ ]]; then
echo "Error: MMI must be between 0 and 8." >&2
exit 1
fi
if [[ ! "${PRIORITY}" =~ ^[1-5]$ ]]; then
echo "Error: Priority must be between 1 and 5." >&2
exit 1
fi
}
send_notification() {
local PUBLIC_ID="${1}"
local MAGNITUDE="${2}"
local LOCALITY="${3}"
local DEPTH="${4}"
local TIME="${5}"
local DISPLAY_TIME
DISPLAY_TIME=$(date -d "${TIME}" "+%a %b %-d %Y %H:%M")
if ! curl -s \
-H "Authorization: Bearer ${NTFY_TOKEN_DEVICES}" \
-H "Title: ${MAGNITUDE}M earthquake ${LOCALITY}" \
-H "Tags: chart_with_upwards_trend" \
-H "Priority: ${PRIORITY}" \
-H "Click: https://www.geonet.org.nz/earthquake/${PUBLIC_ID}" \
-d "Depth: ${DEPTH}km at ${DISPLAY_TIME}" \
"${NTFY_GEONET_URL}"; then
echo "Error: curl failed to send message"
exit 1
fi
echo "${PUBLIC_ID}" >>"${CACHE_FILE}"
}
process_earthquakes() {
local DATA
DATA=$(curl -s "https://api.geonet.org.nz/quake?MMI=${MMI}")
if ! echo "${DATA}" | jq empty >/dev/null 2>&1; then
echo "Error: Invalid JSON response from GeoNet API" >&2
exit 1
fi
echo "${DATA}" | jq -r '.features | sort_by(.properties.time) | .[] | select(.properties.quality != "deleted") |
[.properties.publicID,
(.properties.magnitude | . * 10 | round / 10),
.properties.locality,
(.properties.depth | round),
.properties.time] | @tsv' |
while IFS=$'\t' read -r PUBLIC_ID MAGNITUDE LOCALITY DEPTH TIME; do
if ! grep -q "^${PUBLIC_ID}$" "${CACHE_FILE}"; then
send_notification "${PUBLIC_ID}" "${MAGNITUDE}" "${LOCALITY}" "${DEPTH}" "${TIME}"
sleep 1
fi
done
}
MMI=""
PRIORITY=""
while [[ $# -gt 0 ]]; do
case "${1}" in
-h | --help)
usage
exit 0
;;
-v | --version)
echo "${VERSION}"
exit 0
;;
-m | --mmi)
MMI="${2}"
shift 2
;;
-p | --priority)
PRIORITY="${2}"
shift 2
;;
*)
echo "Error: Unknown argument '${1}'" >&2
usage
exit 1
;;
esac
done
if [[ -z "${MMI}" || -z "${PRIORITY}" ]]; then
echo "Error: Missing required arguments" >&2
usage
exit 1
fi
check_dependencies
check_env_vars
validate_args
process_earthquakes