@@ -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)
8586async 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 []
0 commit comments