Updated weather so that w will work for weather as well as typing the full word.
This commit is contained in:
parent
5db02d384b
commit
07dbcd7a61
BIN
Weather/__pycache__/plugin.cpython-313.pyc
Normal file
BIN
Weather/__pycache__/plugin.cpython-313.pyc
Normal file
Binary file not shown.
@ -14,6 +14,8 @@ import supybot.conf as conf
|
|||||||
import supybot.registry as registry
|
import supybot.registry as registry
|
||||||
import supybot.ircdb as ircdb
|
import supybot.ircdb as ircdb
|
||||||
import supybot.log as log
|
import supybot.log as log
|
||||||
|
# Import the config to ensure proper registration
|
||||||
|
from . import config
|
||||||
import requests
|
import requests
|
||||||
import datetime
|
import datetime
|
||||||
import geopy.geocoders
|
import geopy.geocoders
|
||||||
@ -107,34 +109,6 @@ class WeatherDB:
|
|||||||
return self.location_cache.get(query_key, None)
|
return self.location_cache.get(query_key, None)
|
||||||
|
|
||||||
|
|
||||||
class WeatherConfig(registry.OnlySomeStrings):
|
|
||||||
validStrings = ('C', 'F')
|
|
||||||
|
|
||||||
class Configure(registry.Group):
|
|
||||||
def configure(self, advanced):
|
|
||||||
from supybot.questions import expect, anything, something, yn
|
|
||||||
conf.registerPlugin('Weather', True)
|
|
||||||
|
|
||||||
if advanced:
|
|
||||||
# User agent for Nominatim
|
|
||||||
user_agent = something("used for Nominatim?",
|
|
||||||
default=f"stormux-limnoria-weather-plugin/{__version__}")
|
|
||||||
self.userAgent.setValue(user_agent)
|
|
||||||
|
|
||||||
|
|
||||||
Config = conf.registerPlugin('Weather')
|
|
||||||
conf.registerGlobalValue(Config, 'userAgent',
|
|
||||||
registry.String(f'stormux-limnoria-weather-plugin/{__version__}', _("""User agent for Nominatim""")))
|
|
||||||
conf.registerChannelValue(Config, 'defaultUnit',
|
|
||||||
WeatherConfig('F', _("""Default temperature unit (C or F)""")))
|
|
||||||
conf.registerChannelValue(Config, 'showHumidity',
|
|
||||||
registry.Boolean(True, _("""Show humidity in weather reports""")))
|
|
||||||
conf.registerChannelValue(Config, 'showWind',
|
|
||||||
registry.Boolean(True, _("""Show wind speed in weather reports""")))
|
|
||||||
conf.registerChannelValue(Config, 'showForecast',
|
|
||||||
registry.Boolean(True, _("""Show forecast in weather reports""")))
|
|
||||||
conf.registerChannelValue(Config, 'forecastDays',
|
|
||||||
registry.PositiveInteger(3, _("""Number of forecast days to display""")))
|
|
||||||
|
|
||||||
|
|
||||||
class Weather(callbacks.Plugin):
|
class Weather(callbacks.Plugin):
|
||||||
@ -494,6 +468,82 @@ class Weather(callbacks.Plugin):
|
|||||||
|
|
||||||
weather = wrap(weather, [optional('text')])
|
weather = wrap(weather, [optional('text')])
|
||||||
|
|
||||||
|
def w(self, irc, msg, args, location):
|
||||||
|
"""[<location>]
|
||||||
|
|
||||||
|
Alias for weather command. Show weather for your saved location, or for the specified location.
|
||||||
|
"""
|
||||||
|
# Duplicate the weather command logic here since we can't call wrapped methods
|
||||||
|
channel = msg.args[0] if irc.isChannel(msg.args[0]) else None
|
||||||
|
unit = self.registryValue('defaultUnit', channel)
|
||||||
|
show_humidity = self.registryValue('showHumidity', channel)
|
||||||
|
show_wind = self.registryValue('showWind', channel)
|
||||||
|
show_forecast = self.registryValue('showForecast', channel)
|
||||||
|
forecast_days = self.registryValue('forecastDays', channel)
|
||||||
|
|
||||||
|
# Get the user's name
|
||||||
|
user = msg.prefix.split('!')[0]
|
||||||
|
|
||||||
|
# Determine if showing weather for a location or the user's saved location
|
||||||
|
if location:
|
||||||
|
# Looking up weather for the specified location
|
||||||
|
location_data = self._geocode(location)
|
||||||
|
if not location_data:
|
||||||
|
irc.error("Could not find that location. Please try a different search term.", Raise=True)
|
||||||
|
|
||||||
|
lat = location_data['lat']
|
||||||
|
lon = location_data['lon']
|
||||||
|
|
||||||
|
# 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
|
||||||
|
user_data = self.db.get(user)
|
||||||
|
if not user_data:
|
||||||
|
irc.error("You have not set a location. Use '.weather <location>' to check the weather for a specific location, or '.setweather <location>' to save your location if you're registered.", Raise=True)
|
||||||
|
|
||||||
|
lat = user_data['lat']
|
||||||
|
lon = user_data['lon']
|
||||||
|
|
||||||
|
# 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
|
||||||
|
weather_data = self._get_weather(lat, lon, unit, forecast_days)
|
||||||
|
if not weather_data:
|
||||||
|
irc.error("Could not retrieve weather data. Please try again later.", Raise=True)
|
||||||
|
|
||||||
|
# Format the current weather
|
||||||
|
current = weather_data['current']
|
||||||
|
temp = self._format_temp(current['temp'], unit)
|
||||||
|
conditions = current['conditions']
|
||||||
|
|
||||||
|
reply = f"Weather for {weather_for}: {temp}, {conditions}"
|
||||||
|
|
||||||
|
if show_humidity and current['humidity'] is not None:
|
||||||
|
reply += f", Humidity: {current['humidity']}%"
|
||||||
|
|
||||||
|
if show_wind and current['wind'] is not None:
|
||||||
|
reply += f", Wind: {self._format_wind(current['wind'], current['wind_unit'])}"
|
||||||
|
|
||||||
|
# Send the current weather
|
||||||
|
irc.reply(reply)
|
||||||
|
|
||||||
|
# Format and send the forecast if requested
|
||||||
|
if show_forecast and weather_data['forecast']:
|
||||||
|
forecast_parts = []
|
||||||
|
for day in weather_data['forecast']:
|
||||||
|
min_temp = self._format_temp(day['min_temp'], unit)
|
||||||
|
max_temp = self._format_temp(day['max_temp'], unit)
|
||||||
|
forecast_parts.append(f"{day['day']}: {min_temp} to {max_temp}, {day['conditions']}")
|
||||||
|
|
||||||
|
forecast_msg = f"Forecast: {' | '.join(forecast_parts)}"
|
||||||
|
irc.reply(forecast_msg)
|
||||||
|
|
||||||
|
w = wrap(w, [optional('text')])
|
||||||
|
|
||||||
|
|
||||||
Class = Weather
|
Class = Weather
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user