generated from me/template-mit
Enhance the usage, output, latlong handling, and error reporting. Feels much closer to done.
This commit is contained in:
1 parent
e1995dff80
commit
9cc194fcb4
1 file changed
+42
-5
+42
-5
@@ -18,7 +18,27 @@ PROMPT="sunset-alarm"
|
|||||||
# Usage
|
# Usage
|
||||||
###############################################################################
|
###############################################################################
|
||||||
usage() {
|
usage() {
|
||||||
echo "Usage: sunset-alarm (-l LAT:LONG | -t HH24:MI) (-m MINUTES) [-h]" >&2
|
cat <<EOF
|
||||||
|
Usage: sunset-alarm (-l LAT:LONG | -t HH24:MI) (-m MINUTES) [-h]
|
||||||
|
|
||||||
|
Display a notification when a certain time related to sunset has been reached.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
-l) Takes a LAT:LONG parameter to check Open Meteo for the sunset time if
|
||||||
|
there is an Internet connection. Otherwise may fail to notify.
|
||||||
|
-t) Takes a hard-coded 24-hour formatted time for guessing when sunset is.
|
||||||
|
-m) Takes the number of minutes that the alert should appear before sunset.
|
||||||
|
Using a negative time is allowed if you prefer having the alert after.
|
||||||
|
|
||||||
|
Example: Show the alert 90 minutes before sunset in Colorado City AZ.
|
||||||
|
sunset-alarm -l 37:-113 -m 90
|
||||||
|
|
||||||
|
Example: Show the alert at exactly 8:30 PM.
|
||||||
|
sunset-alarm -t 20:30 -m 0
|
||||||
|
|
||||||
|
Example: Show the alert 30 minutes after sunset in Flagstaff AZ.
|
||||||
|
sunset-alarm -l 35.1987671:-111.651813 -m -30
|
||||||
|
EOF
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +67,11 @@ if [ -z "$LATLONG" ] && [ -z "$SUNSET_TIME" ]; then
|
|||||||
usage
|
usage
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
LATLONG=${LATLONG//\,/:}
|
||||||
|
LATLONG=${LATLONG//\ /:}
|
||||||
|
LATLONG=${LATLONG//\_/:}
|
||||||
|
LATLONG=${LATLONG//\&/:}
|
||||||
|
LATLONG=${LATLONG//\?/:}
|
||||||
if [[ -n "$LATLONG" && "$LATLONG" != *":"* ]]; then
|
if [[ -n "$LATLONG" && "$LATLONG" != *":"* ]]; then
|
||||||
echo "ERROR: LAT:LONG is missing the colon." >&2
|
echo "ERROR: LAT:LONG is missing the colon." >&2
|
||||||
usage
|
usage
|
||||||
@@ -56,12 +81,15 @@ fi
|
|||||||
# Internet connectivity check
|
# Internet connectivity check
|
||||||
###############################################################################
|
###############################################################################
|
||||||
check_internet() {
|
check_internet() {
|
||||||
if curl -s --max-time 5 -o /dev/null https://hyperling,com; then
|
if curl -s --max-time 5 -o /dev/null https://hyperling.com; then
|
||||||
|
echo "Confirmed connectivity with Hyperling."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
if curl -s --max-time 5 -o /dev/null https://mullvad.net; then
|
if curl -s --max-time 5 -o /dev/null https://mullvad.net; then
|
||||||
|
echo "Confirmed connectivity with Mullvad."
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
echo "ERROR: Connectivity checks failed." >&2
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,12 +97,15 @@ check_internet() {
|
|||||||
# Get sunset from Open Meteo API
|
# Get sunset from Open Meteo API
|
||||||
###############################################################################
|
###############################################################################
|
||||||
fetch_sunset_time() {
|
fetch_sunset_time() {
|
||||||
|
echo "Checking for sunset time of lat:long ${LATLONG}."
|
||||||
LAT=$(echo "$LATLONG" | cut -d: -f1)
|
LAT=$(echo "$LATLONG" | cut -d: -f1)
|
||||||
LON=$(echo "$LATLONG" | cut -d: -f2)
|
LON=$(echo "$LATLONG" | cut -d: -f2)
|
||||||
|
|
||||||
RESPONSE=$(curl -s "https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&date=today")
|
REQUEST="https://api.open-meteo.com/v1/forecast?latitude=${LAT}&longitude=${LON}&timezone=auto&daily=sunset&date=today"
|
||||||
|
RESPONSE=$(curl -s "$REQUEST")
|
||||||
|
|
||||||
if [ -z "$RESPONSE" ]; then
|
if [ -z "$RESPONSE" ]; then
|
||||||
|
echo "ERROR: Did not receive response. $REQUEST" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -82,6 +113,7 @@ fetch_sunset_time() {
|
|||||||
SUNSET_RAW=$(echo "$RESPONSE" | jq -r '.daily.sunset[0]')
|
SUNSET_RAW=$(echo "$RESPONSE" | jq -r '.daily.sunset[0]')
|
||||||
|
|
||||||
if [ -z "$SUNSET_RAW" ]; then
|
if [ -z "$SUNSET_RAW" ]; then
|
||||||
|
echo "ERROR: Could not parse time. $RESPONSE" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -89,9 +121,11 @@ fetch_sunset_time() {
|
|||||||
SUNSET_EPOCH=$(date -d "$(echo "$SUNSET_RAW" | cut -dT -f1) $(echo "$SUNSET_RAW" | cut -dT -f2)" +%s 2>/dev/null)
|
SUNSET_EPOCH=$(date -d "$(echo "$SUNSET_RAW" | cut -dT -f1) $(echo "$SUNSET_RAW" | cut -dT -f2)" +%s 2>/dev/null)
|
||||||
|
|
||||||
if [ -z "$SUNSET_EPOCH" ]; then
|
if [ -z "$SUNSET_EPOCH" ]; then
|
||||||
|
echo "ERROR: Could not calculate epoch. $SUNSET_RAW" >&2
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
echo "Got ${SUNSET_RAW} and converted it to epoch ${SUNSET_EPOCH}."
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,13 +170,13 @@ while true; do
|
|||||||
if [ -n "$LATLONG" ]; then
|
if [ -n "$LATLONG" ]; then
|
||||||
# Need internet to get sunset time
|
# Need internet to get sunset time
|
||||||
if ! check_internet; then
|
if ! check_internet; then
|
||||||
echo "No internet connection. Retrying in 5 minutes..." >&2
|
echo "No internet connection. Retrying in 5 minutes." >&2
|
||||||
sleep 300
|
sleep 300
|
||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
|
|
||||||
fetch_sunset_time || {
|
fetch_sunset_time || {
|
||||||
echo "Error fetching sunset time. Retrying in 5 minutes..." >&2
|
echo "Failed fetching sunset time. Retrying in 5 minutes." >&2
|
||||||
sleep 300
|
sleep 300
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -167,8 +201,11 @@ while true; do
|
|||||||
# Alert while current time is within the window (from window start until midnight)
|
# Alert while current time is within the window (from window start until midnight)
|
||||||
if [ "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" ] && \
|
if [ "$CURRENT_TIME" -ge "$ALERT_WINDOW_START" ] && \
|
||||||
[ "$CURRENT_TIME" -lt "$MIDNIGHT" ]; then
|
[ "$CURRENT_TIME" -lt "$MIDNIGHT" ]; then
|
||||||
|
echo "Alert time has been reached, show message."
|
||||||
show_alert
|
show_alert
|
||||||
|
echo "Alert has been dismissed, sleeping for 5 minutes."
|
||||||
sleep 290
|
sleep 290
|
||||||
|
echo "Waking back up from notification dismissal."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Reset when we've passed midnight so we fetch new time tomorrow
|
# Reset when we've passed midnight so we fetch new time tomorrow
|
||||||
|
|||||||
Reference in new issue
Block a user