-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
872e041
commit cdaafea
Showing
5 changed files
with
52 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,3 +168,18 @@ Timeout = 30 | |
|
||
# Chunk size for translation | ||
ChunkSize = 500 | ||
|
||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
# U.S. National Weather Service (NWS) | ||
# (weather.gov) | ||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
[NWS] | ||
# Fetch NWS foreacsts and/or alerts? (true/false) | ||
# Note that the service can be slow and unreliable at times. | ||
# I recommand getting the alerts to supplement i.e. OpenWeatherMap. | ||
# The alerts usually work, but sadly their open API forecasts are often broken. | ||
FetchNWSForecast = false | ||
FetchNWSAlerts = true | ||
NWSUserAgent = ChatKekeWeather/1.0 ([email protected]) | ||
NWSRetries = 3 | ||
NWSRetryDelay = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,12 @@ | |
import asyncio | ||
import httpx | ||
import logging | ||
from config_paths import NWS_USER_AGENT, NWS_RETRIES, NWS_RETRY_DELAY, FETCH_NWS_FORECAST, FETCH_NWS_ALERTS | ||
|
||
# Base URL for NWS API | ||
NWS_BASE_URL = 'https://api.weather.gov' | ||
|
||
# Custom User-Agent as per NWS API requirements | ||
USER_AGENT = 'ChatKekeWeather/1.0 ([email protected])' | ||
|
||
# Default number of retries if not specified | ||
RETRIES = 0 | ||
|
||
async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2): | ||
async def get_nws_forecast(lat, lon, retries=NWS_RETRIES, delay=NWS_RETRY_DELAY): | ||
""" | ||
Fetches the forecast from the NWS API for the given latitude and longitude. | ||
|
@@ -32,6 +27,11 @@ async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2): | |
Returns: | ||
dict: Combined forecast data or None if fetching fails. | ||
""" | ||
|
||
if not FETCH_NWS_FORECAST: | ||
logging.info("Fetching NWS forecast is disabled in the config.") | ||
return None | ||
|
||
# Round coordinates to 4 decimal places | ||
lat = round(lat, 4) | ||
lon = round(lon, 4) | ||
|
@@ -41,7 +41,7 @@ async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2): | |
for attempt in range(retries + 1): # Ensure at least one attempt is made | ||
try: | ||
# Step 1: Retrieve metadata for the location | ||
response = await client.get(points_url, headers={'User-Agent': USER_AGENT}) | ||
response = await client.get(points_url, headers={'User-Agent': NWS_USER_AGENT}) | ||
response.raise_for_status() | ||
points_data = response.json() | ||
|
||
|
@@ -50,15 +50,15 @@ async def get_nws_forecast(lat, lon, retries=RETRIES, delay=2): | |
forecast_hourly_url = points_data['properties'].get('forecastHourly') | ||
|
||
# Step 2: Retrieve forecast data | ||
forecast_response = await client.get(forecast_url, headers={'User-Agent': USER_AGENT}) | ||
forecast_response = await client.get(forecast_url, headers={'User-Agent': NWS_USER_AGENT}) | ||
forecast_response.raise_for_status() | ||
forecast_data = forecast_response.json() | ||
|
||
# Step 3: Retrieve hourly forecast data | ||
forecast_hourly_data = None | ||
if forecast_hourly_url: | ||
try: | ||
forecast_hourly_response = await client.get(forecast_hourly_url, headers={'User-Agent': USER_AGENT}) | ||
forecast_hourly_response = await client.get(forecast_hourly_url, headers={'User-Agent': NWS_USER_AGENT}) | ||
forecast_hourly_response.raise_for_status() | ||
forecast_hourly_data = forecast_hourly_response.json() | ||
except httpx.HTTPStatusError as e: | ||
|
@@ -93,11 +93,16 @@ async def get_nws_alerts(lat, lon): | |
Returns: | ||
list: A list of active alerts or an empty list if none are found. | ||
""" | ||
|
||
if not FETCH_NWS_ALERTS: | ||
logging.info("Fetching NWS alerts is disabled in the config.") | ||
return [] | ||
|
||
alerts_url = f"{NWS_BASE_URL}/alerts/active?point={lat},{lon}" | ||
|
||
async with httpx.AsyncClient() as client: | ||
try: | ||
response = await client.get(alerts_url, headers={'User-Agent': USER_AGENT}) | ||
response = await client.get(alerts_url, headers={'User-Agent': NWS_USER_AGENT}) | ||
response.raise_for_status() | ||
alerts_data = response.json() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,11 @@ | |
ELASTICSEARCH_USERNAME = '' | ||
ELASTICSEARCH_PASSWORD = '' | ||
|
||
# Default NWS settings | ||
NWS_USER_AGENT = 'ChatKekeWeather/1.0 ([email protected])' | ||
NWS_RETRIES = 0 | ||
NWS_RETRY_DELAY = 2 | ||
|
||
# Attempt to read the configuration file | ||
if CONFIG_PATH.exists(): | ||
try: | ||
|
@@ -71,6 +76,18 @@ | |
ELASTICSEARCH_USERNAME = '' | ||
ELASTICSEARCH_PASSWORD = '' | ||
logger.warning("Elasticsearch section missing in config.ini. Using default Elasticsearch settings.") | ||
|
||
# NWS Configuration | ||
if 'NWS' in config: | ||
NWS_USER_AGENT = config['NWS'].get('NWSUserAgent', fallback='ChatKekeWeather/1.0 ([email protected])') | ||
NWS_RETRIES = config['NWS'].getint('NWSRetries', fallback=0) | ||
NWS_RETRY_DELAY = config['NWS'].getint('NWSRetryDelay', fallback=2) | ||
FETCH_NWS_FORECAST = config['NWS'].getboolean('FetchNWSForecast', fallback=True) | ||
FETCH_NWS_ALERTS = config['NWS'].getboolean('FetchNWSAlerts', fallback=True) | ||
logger.info(f"NWS Config: User-Agent={NWS_USER_AGENT}, Retries={NWS_RETRIES}, Retry Delay={NWS_RETRY_DELAY}, Fetch Forecast={FETCH_NWS_FORECAST}, Fetch Alerts={FETCH_NWS_ALERTS}") | ||
else: | ||
logger.warning("NWS section not found in config.ini. Using default NWS settings.") | ||
|
||
except Exception as e: | ||
# Handle exceptions during config parsing | ||
logger.error(f"Error reading configuration file: {e}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters