Make location information during weather reports less verbose.

This commit is contained in:
Storm Dragon 2025-04-04 04:29:35 -04:00
parent e869bc4add
commit 5db02d384b

View File

@ -210,6 +210,46 @@ class Weather(callbacks.Plugin):
return "N/A"
return f"{wind_speed:.1f} {unit}"
def _format_location(self, address):
"""
Format a location string to only show City, State for US locations
or City, Country for international locations.
Args:
address (str): The full address string from Nominatim
Usually in format: City, County, State, Zipcode, Country
Returns:
str: Formatted location string with just city and state/country
"""
if not address:
return "Unknown location"
# Split the address components
parts = [p.strip() for p in address.split(',')]
# Need at least two parts to extract meaningful location
if len(parts) < 2:
return address
# First part is usually the city/town
city = parts[0]
# Look for United States in the last part
if parts[-1].strip() == "United States":
# For US addresses, we want City, State (usually the 3rd part)
if len(parts) >= 3:
state_index = 2 # Third element (0-indexed)
state = parts[state_index].strip()
return f"{city}, {state}"
else:
# Not enough parts, return city and country
return f"{city}, United States"
else:
# For international addresses, return City, Country
country = parts[-1].strip()
return f"{city}, {country}"
def _geocode(self, location):
"""Geocode a location string to get latitude and longitude"""
cached_location = self.db.get_from_cache(location)
@ -348,10 +388,12 @@ class Weather(callbacks.Plugin):
if not location_data:
irc.error("Could not find that location. Please try a different search term.", Raise=True)
# Store the user's location
# Store the user's location (with the original full address)
self.db.set(username, location_data['address'], location_data['lat'], location_data['lon'])
irc.reply(f"Your location has been set to: {location_data['address']}")
# Format the location only for display
formatted_location = self._format_location(location_data['address'])
irc.reply(f"Your location has been set to: {formatted_location}")
setweather = wrap(setweather, ['text'])
@ -401,7 +443,9 @@ class Weather(callbacks.Plugin):
lat = location_data['lat']
lon = location_data['lon']
location_name = location_data['address']
# Format the location for display only
location_name = self._format_location(location_data['address'])
weather_for = location_name
else:
# Looking up the sender's weather from their saved location
@ -411,7 +455,9 @@ class Weather(callbacks.Plugin):
lat = user_data['lat']
lon = user_data['lon']
location_name = user_data['location']
# Format the location for display only
location_name = self._format_location(user_data['location'])
weather_for = f"your location ({location_name})"
# Get the weather data