Skip to content

Commit 8c9f07c

Browse files
committed
v0.75052 - detailed US NWS weather alerts
1 parent fe9b268 commit 8c9f07c

File tree

4 files changed

+65
-5
lines changed

4 files changed

+65
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ If you run into any issues, consult the logs or reach out on the repository's [I
236236
---
237237

238238
# Changelog
239+
- v0.75052 - include the details from U.S. National Weather Service on alerts
239240
- v0.75051 - updated `config.ini` for configuring NWS weather forecasts & alerts
240241
- suggested method is to supplement via NWS the additional weather data you need
241242
- leaving U.S. NWS's weather alerts on in `config.ini` is highly recommended, even if you have other fetching methods enabled (i.e. OpenWeatherMap), rather be safe than sorry

src/api_get_nws_weather.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ async def get_nws_forecast(lat, lon, retries=NWS_RETRIES, delay=NWS_RETRY_DELAY)
8282

8383
return None
8484

85+
# get alerts via NWS (weather.gov)
8586
async def get_nws_alerts(lat, lon):
8687
"""
8788
Fetches active alerts from the NWS API for the given latitude and longitude.
@@ -91,7 +92,7 @@ async def get_nws_alerts(lat, lon):
9192
lon (float): Longitude in decimal degrees.
9293
9394
Returns:
94-
list: A list of active alerts or an empty list if none are found.
95+
list: A list of active alerts with detailed information or an empty list if none are found.
9596
"""
9697

9798
if not FETCH_NWS_ALERTS:
@@ -106,8 +107,26 @@ async def get_nws_alerts(lat, lon):
106107
response.raise_for_status()
107108
alerts_data = response.json()
108109

109-
# Extract alerts from GeoJSON
110-
alerts = alerts_data.get('features', [])
110+
# Extracting the detailed alerts
111+
alerts = []
112+
for feature in alerts_data.get('features', []):
113+
properties = feature.get('properties', {})
114+
alert = {
115+
'headline': properties.get('headline'),
116+
'description': properties.get('description'),
117+
'instruction': properties.get('instruction'),
118+
'severity': properties.get('severity'),
119+
'event': properties.get('event'),
120+
'areaDesc': properties.get('areaDesc'),
121+
'certainty': properties.get('certainty'),
122+
'urgency': properties.get('urgency'),
123+
'effective': properties.get('effective'),
124+
'expires': properties.get('expires'),
125+
'senderName': properties.get('senderName'),
126+
'response': properties.get('response'),
127+
# Add more fields if needed
128+
}
129+
alerts.append(alert)
111130
return alerts
112131

113132
except httpx.HTTPStatusError as e:
@@ -116,3 +135,40 @@ async def get_nws_alerts(lat, lon):
116135
logging.error(f"Error fetching NWS alerts: {e}")
117136

118137
return []
138+
139+
# # // (old method)
140+
# # get alerts via NWS (weather.gov)
141+
# async def get_nws_alerts(lat, lon):
142+
# """
143+
# Fetches active alerts from the NWS API for the given latitude and longitude.
144+
145+
# Args:
146+
# lat (float): Latitude in decimal degrees.
147+
# lon (float): Longitude in decimal degrees.
148+
149+
# Returns:
150+
# list: A list of active alerts or an empty list if none are found.
151+
# """
152+
153+
# if not FETCH_NWS_ALERTS:
154+
# logging.info("Fetching NWS alerts is disabled in the config.")
155+
# return []
156+
157+
# alerts_url = f"{NWS_BASE_URL}/alerts/active?point={lat},{lon}"
158+
159+
# async with httpx.AsyncClient() as client:
160+
# try:
161+
# response = await client.get(alerts_url, headers={'User-Agent': NWS_USER_AGENT})
162+
# response.raise_for_status()
163+
# alerts_data = response.json()
164+
165+
# # Extract alerts from GeoJSON
166+
# alerts = alerts_data.get('features', [])
167+
# return alerts
168+
169+
# except httpx.HTTPStatusError as e:
170+
# logging.error(f"NWS Alerts API HTTP error: {e.response.status_code} - {e.response.text}")
171+
# except Exception as e:
172+
# logging.error(f"Error fetching NWS alerts: {e}")
173+
174+
# return []

src/api_get_openweathermap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
# Import the NWS data fetching function
1616
from api_get_nws_weather import get_nws_forecast, get_nws_alerts
17+
from config_paths import NWS_USER_AGENT, NWS_RETRIES, NWS_RETRY_DELAY, FETCH_NWS_FORECAST, FETCH_NWS_ALERTS
1718

1819
# date & time utils
1920
import datetime as dt
@@ -404,7 +405,7 @@ async def combine_weather_data(city_name, country, lat, lon, current_weather_dat
404405
lon_rounded = round(lon, 4)
405406
alerts_url = f"https://api.weather.gov/alerts/active?point={lat_rounded},{lon_rounded}"
406407
async with httpx.AsyncClient(follow_redirects=True) as client:
407-
alerts_response = await client.get(alerts_url, headers={'User-Agent': 'YourAppName ([email protected])'})
408+
alerts_response = await client.get(alerts_url, headers={'User-Agent': NWS_USER_AGENT})
408409
alerts_response.raise_for_status()
409410
alerts_data = alerts_response.json()
410411
except httpx.HTTPStatusError as e:
@@ -422,6 +423,7 @@ async def combine_weather_data(city_name, country, lat, lon, current_weather_dat
422423

423424
event = properties.get('event', 'EVENT').upper()
424425
headline = properties.get('headline', 'HEADLINE')
426+
description = properties.get('description', 'No further details available') # Fetching the detailed description
425427
instruction = properties.get('instruction', 'INSTRUCTION')
426428
severity = properties.get('severity', 'Unknown').capitalize()
427429
certainty = properties.get('certainty', 'Unknown').capitalize()
@@ -433,6 +435,7 @@ async def combine_weather_data(city_name, country, lat, lon, current_weather_dat
433435
alerts_info += (
434436
f"{idx}. ⚠️ <b>{event}</b>\n"
435437
f"<b>Vaara:</b> {headline}\n"
438+
f"<b>Kuvaus:</b> {description}\n" # Adding the detailed description
436439
f"<b>Ohjeet:</b> {instruction}\n"
437440
f"<b>Alue:</b> {area_desc}\n"
438441
f"<b>Vakavuus:</b> {severity}\n"

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# https://github.com/FlyingFathead/TelegramBot-OpenAI-API
66
#
77
# version of this program
8-
version_number = "0.75051"
8+
version_number = "0.75052"
99

1010
# Add the project root directory to Python's path
1111
import sys

0 commit comments

Comments
 (0)