-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweath.sh
More file actions
141 lines (123 loc) · 4.29 KB
/
weath.sh
File metadata and controls
141 lines (123 loc) · 4.29 KB
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
#!/bin/bash
echo Weather
echo
echo this can show multiple Zipcodes and it sounds a beep when a weather warning was issued.
echo
echo k to kill alarm
echo "(may not be as accurate and quick as models from the National Weather Service, ONLY use
echo this as a logging system or in a situation where it is the only thing available for weather updates)"
# Folders
ROOT="$HOME/.weather_alert"
ZIP_FILE="$ROOT/locations.txt"
LOG_DIR="$ROOT/logs"
mkdir -p "$ROOT" "$LOG_DIR"
# Log file
LOG="$LOG_DIR/weather_$(date +%Y%m%d_%H%M%S).log"
touch "$LOG"
# Get forecast + zone from coords
get_zone_from_coords() {
local LAT=$1
local LON=$2
local POINT_INFO=$(curl -s "https://api.weather.gov/points/$LAT,$LON")
FORECAST_URL=$(echo "$POINT_INFO" | jq -r '.properties.forecast')
ZONE_URL=$(echo "$POINT_INFO" | jq -r '.properties.forecastZone')
CITY=$(echo "$POINT_INFO" | jq -r '.properties.relativeLocation.properties.city')
STATE=$(echo "$POINT_INFO" | jq -r '.properties.relativeLocation.properties.state')
}
# Convert ZIP to coordinates
coords_from_zip() {
local ZIP=$1
local RESPONSE=$(curl -s "http://api.zippopotam.us/us/$ZIP")
LAT=$(echo "$RESPONSE" | jq -r '.places[0].latitude')
LON=$(echo "$RESPONSE" | jq -r '.places[0].longitude')
}
# Sound alarm with escape
alert_user() {
echo -e "\n🔔 ALERT: $1"
echo "Press 'q' to silence."
while true; do
espeak "$1 Alert! Stay safe!"
echo -e "\a"
read -t 2 -n 1 key
[[ "$key" == "q" ]] && break
done
}
# Add location (ZIP or coords)
add_location() {
echo -n "Enter ZIP or lat,lon: "
read LOC
echo "$LOC" >> "$ZIP_FILE"
echo "📍 Added: $LOC"
}
# Remove location
remove_location() {
echo -n "Enter exact ZIP or lat,lon to remove: "
read LOC
sed -i "/^$LOC$/d" "$ZIP_FILE"
echo "❌ Removed: $LOC"
}
# List saved
list_locations() {
echo "📌 Saved locations:"
cat "$ZIP_FILE"
}
# Parse weather for one location
check_weather_for() {
local LOC=$1
if [[ "$LOC" =~ ^[0-9]{5}$ ]]; then
coords_from_zip "$LOC"
else
LAT=$(echo "$LOC" | cut -d',' -f1)
LON=$(echo "$LOC" | cut -d',' -f2)
fi
get_zone_from_coords "$LAT" "$LON"
ZONE_ID=$(basename "$ZONE_URL")
echo -e "\n🌐 [$CITY, $STATE] ($LAT,$LON)" | tee -a "$LOG"
echo "Forecast:" | tee -a "$LOG"
curl -s "$FORECAST_URL" | jq -r '.properties.periods[0] | "\(.name): \(.detailedForecast)"' | tee -a "$LOG"
echo -e "\n🌀 Alerts:" | tee -a "$LOG"
ALERT_JSON=$(curl -s "https://api.weather.gov/alerts/active/zone/$ZONE_ID")
ALERT_COUNT=$(echo "$ALERT_JSON" | jq '.features | length')
if [[ "$ALERT_COUNT" -gt 0 ]]; then
echo "$ALERT_JSON" | jq -r '.features[] | "🔺 \(.properties.event) | Severity: \(.properties.severity)\n\(.properties.description)\nInstructions: \(.properties.instruction // "None")\n"' | tee -a "$LOG"
for TYPE in $(echo "$ALERT_JSON" | jq -r '.features[].properties.event'); do
[[ "$TYPE" =~ (Tornado|Hurricane|Severe|Storm|Flood) ]] && alert_user "$TYPE in $CITY"
done
else
echo "✅ No alerts." | tee -a "$LOG"
fi
# Extended weather info
echo -e "\n📊 Detailed data:" | tee -a "$LOG"
curl -s "$FORECAST_URL" | jq -r '.properties.periods[0] | "Wind: \(.windSpeed) \(.windDirection)\nTemp: \(.temperature)°\nPrecip Chance: \(.probabilityOfPrecipitation.value // "?" )%\nConditions: \(.shortForecast)\n"' | tee -a "$LOG"
}
# Monitor loop
monitor_loop() {
while true; do
echo -e "\n==== WEATHER UPDATE [$(date)] ====" | tee -a "$LOG"
while IFS= read -r LOC; do
check_weather_for "$LOC"
echo "------------------------" | tee -a "$LOG"
done < "$ZIP_FILE"
echo -e "\n🔄 Refreshing in 60 seconds..."
sleep 60
done
}
# Main menu
while true; do
echo -e "\n🌤️ Bash Weather Alert System"
echo "1. Add location (ZIP or lat,lon)"
echo "2. Remove location"
echo "3. List saved locations"
echo "4. Start monitoring"
echo "5. Exit"
echo -n "Choose: "
read CHOICE
case $CHOICE in
1) add_location ;;
2) remove_location ;;
3) list_locations ;;
4) monitor_loop ;;
5) echo "🛑 Exiting."; exit ;;
*) echo "❓ Invalid choice." ;;
esac
done