99import asyncio
1010import httpx
1111import logging
12+ from config_paths import NWS_USER_AGENT , NWS_RETRIES , NWS_RETRY_DELAY , FETCH_NWS_FORECAST , FETCH_NWS_ALERTS
1213
1314# Base URL for NWS API
1415NWS_BASE_URL = 'https://api.weather.gov'
1516
16- # Custom User-Agent as per NWS API requirements
17- USER_AGENT = 'ChatKekeWeather/1.0 ([email protected] )' 18-
19- # Default number of retries if not specified
20- RETRIES = 0
21-
22- async def get_nws_forecast (lat , lon , retries = RETRIES , delay = 2 ):
17+ async def get_nws_forecast (lat , lon , retries = NWS_RETRIES , delay = NWS_RETRY_DELAY ):
2318 """
2419 Fetches the forecast from the NWS API for the given latitude and longitude.
2520
@@ -32,6 +27,11 @@ async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2):
3227 Returns:
3328 dict: Combined forecast data or None if fetching fails.
3429 """
30+
31+ if not FETCH_NWS_FORECAST :
32+ logging .info ("Fetching NWS forecast is disabled in the config." )
33+ return None
34+
3535 # Round coordinates to 4 decimal places
3636 lat = round (lat , 4 )
3737 lon = round (lon , 4 )
@@ -41,7 +41,7 @@ async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2):
4141 for attempt in range (retries + 1 ): # Ensure at least one attempt is made
4242 try :
4343 # Step 1: Retrieve metadata for the location
44- response = await client .get (points_url , headers = {'User-Agent' : USER_AGENT })
44+ response = await client .get (points_url , headers = {'User-Agent' : NWS_USER_AGENT })
4545 response .raise_for_status ()
4646 points_data = response .json ()
4747
@@ -50,15 +50,15 @@ async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2):
5050 forecast_hourly_url = points_data ['properties' ].get ('forecastHourly' )
5151
5252 # Step 2: Retrieve forecast data
53- forecast_response = await client .get (forecast_url , headers = {'User-Agent' : USER_AGENT })
53+ forecast_response = await client .get (forecast_url , headers = {'User-Agent' : NWS_USER_AGENT })
5454 forecast_response .raise_for_status ()
5555 forecast_data = forecast_response .json ()
5656
5757 # Step 3: Retrieve hourly forecast data
5858 forecast_hourly_data = None
5959 if forecast_hourly_url :
6060 try :
61- forecast_hourly_response = await client .get (forecast_hourly_url , headers = {'User-Agent' : USER_AGENT })
61+ forecast_hourly_response = await client .get (forecast_hourly_url , headers = {'User-Agent' : NWS_USER_AGENT })
6262 forecast_hourly_response .raise_for_status ()
6363 forecast_hourly_data = forecast_hourly_response .json ()
6464 except httpx .HTTPStatusError as e :
@@ -93,11 +93,16 @@ async def get_nws_alerts(lat, lon):
9393 Returns:
9494 list: A list of active alerts or an empty list if none are found.
9595 """
96+
97+ if not FETCH_NWS_ALERTS :
98+ logging .info ("Fetching NWS alerts is disabled in the config." )
99+ return []
100+
96101 alerts_url = f"{ NWS_BASE_URL } /alerts/active?point={ lat } ,{ lon } "
97102
98103 async with httpx .AsyncClient () as client :
99104 try :
100- response = await client .get (alerts_url , headers = {'User-Agent' : USER_AGENT })
105+ response = await client .get (alerts_url , headers = {'User-Agent' : NWS_USER_AGENT })
101106 response .raise_for_status ()
102107 alerts_data = response .json ()
103108
0 commit comments