From f9b756d317d6f92af2ab51265f4d9d40a4b14bd8 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Tue, 9 Jul 2024 10:43:14 -0700 Subject: [PATCH] alertsWx --- mesh_bot.py | 4 ++-- modules/locationdata.py | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/mesh_bot.py b/mesh_bot.py index d6e9ec5..dbbacf5 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -101,11 +101,11 @@ def auto_response(message, snr, rssi, hop, message_from_id): bot_response = moon elif "wxalert" in message.lower(): location = get_node_location(message_from_id) - weatherAlert = get_wx_alert_details(str(location[0]),str(location[1])) + weatherAlert = getActiveWeatherAlertsDetail(str(location[0]),str(location[1])) bot_response = weatherAlert[0] elif "wxa" in message.lower(): location = get_node_location(message_from_id) - weatherAlert = get_wx_alerts_list(str(location[0]),str(location[1])) + weatherAlert = getWeatherAlerts(str(location[0]),str(location[1])) bot_response = weatherAlert[0] elif "wxc" in message.lower(): location = get_node_location(message_from_id) diff --git a/modules/locationdata.py b/modules/locationdata.py index 388468b..2c765ec 100644 --- a/modules/locationdata.py +++ b/modules/locationdata.py @@ -84,6 +84,7 @@ def get_tide(lat=0, lon=0): return tide_string def get_weather(lat=0, lon=0, unit=0): + # get weather report from NOAA for forecast detailed weather = "" if float(lat) == 0 and float(lon) == 0: return NO_DATA_NOGPS @@ -112,7 +113,7 @@ def get_weather(lat=0, lon=0, unit=0): # extract data from rows for row in rows: # shrink the text - line = replace_weather(row.text) + line = abbreviate_weather(row.text) # only grab a few days of weather if len(weather.split("\n")) < DAYS_OF_WEATHER: weather += line + "\n" @@ -120,15 +121,15 @@ def get_weather(lat=0, lon=0, unit=0): weather = weather[:-1] # get any alerts and return the count - alert, alert_num = get_wx_alerts_list(lat, lon) + alert_num = getWeatherAlerts(lat, lon)[1] if alert_num > 0: # add the alert count warning to the weather - weather = str(alert_num) + " weather alerts!\n" + weather + weather = str(alert_num) + " local alerts!\n" + weather return weather -def replace_weather(row): - # replace long strings with shorter ones for display used in get_weather +def abbreviate_weather(row): + # replace long strings with shorter ones for display used in get_weather, get_wx_alerts_list, get_wx_alert_details replacements = { "Monday": "Mon ", "Tuesday": "Tue ", @@ -140,6 +141,7 @@ def replace_weather(row): "Night": "Night ", "Tonight": "Tonight ", "Tomorrow": "Tomorrow ", + "Day": "Day ", "This Afternoon": "Afternoon ", "Overnight": "Overnight ", "northwest": "NW", @@ -169,7 +171,7 @@ def replace_weather(row): return line -def get_wx_alerts_list(lat=0, lon=0): +def getWeatherAlerts(lat=0, lon=0): # get weather alerts from NOAA limited to ALERT_COUNT with the total number of alerts found alerts = "" if float(lat) == 0 and float(lon) == 0: @@ -190,7 +192,6 @@ def get_wx_alerts_list(lat=0, lon=0): for i in alertxml.getElementsByTagName("entry"): alerts += ( - #i.getElementsByTagName("updated")[0].childNodes[0].nodeValue + ": " + i.getElementsByTagName("title")[0].childNodes[0].nodeValue + "\n" ) @@ -205,10 +206,12 @@ def get_wx_alerts_list(lat=0, lon=0): alert_num = 0 alert_num = len(alerts.split("\n")) + alerts = abbreviate_weather(alerts) + # return the first ALERT_COUNT alerts return "\n".join(alerts.split("\n")[:ALERT_COUNT]), alert_num -def get_wx_alert_details(lat=0, lon=0): +def getActiveWeatherAlertsDetail(lat=0, lon=0): # get the latest details of weather alerts from NOAA alerts = "" if float(lat) == 0 and float(lon) == 0: @@ -239,6 +242,8 @@ def get_wx_alert_details(lat=0, lon=0): "\n***\n" ) + alerts = abbreviate_weather(alerts) + # trim the alerts to the first ALERT_COUNT alerts = alerts.split("\n***\n")[:ALERT_COUNT]