dr.botzo/weather/ircplugin.py

61 lines
3.0 KiB
Python
Raw Permalink Normal View History

2023-03-27 16:14:11 -05:00
"""Report on the weather via wttr.in."""
2015-05-15 21:22:14 -05:00
import logging
from ircbot.lib import Plugin
from weather.lib import weather_summary
2015-05-15 21:22:14 -05:00
log = logging.getLogger('weather.ircplugin')
class Weather(Plugin):
"""Have IRC commands to do IRC things (join channels, quit, etc.)."""
def start(self):
"""Set up the handlers."""
self.connection.reactor.add_global_regex_handler(['pubmsg', 'privmsg'], r'^!weather\s+(.*)$',
self.handle_weather, -20)
2015-05-15 21:22:14 -05:00
super(Weather, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_weather)
2015-05-15 21:22:14 -05:00
super(Weather, self).stop()
def handle_weather(self, connection, event, match):
2023-03-27 16:14:11 -05:00
"""Make the weather query and format it for IRC."""
2015-05-15 21:22:14 -05:00
query = match.group(1)
queryitems = query.split(" ")
if len(queryitems) <= 0:
return
2021-04-24 12:59:12 -05:00
weather = weather_summary(query)
2019-10-06 09:13:51 -05:00
weather_output = (f"Weather in {weather['location']}: {weather['current']['description']}. "
f"{weather['current']['temp_F']}/{weather['current']['temp_C']}, "
f"feels like {weather['current']['feels_like_temp_F']}/"
f"{weather['current']['feels_like_temp_C']}. "
f"Wind {weather['current']['wind_speed']} "
f"from the {weather['current']['wind_direction']}. "
2019-10-06 09:13:51 -05:00
f"Visibility {weather['current']['visibility']}. "
f"Precipitation {weather['current']['precipitation']}. "
f"Pressure {weather['current']['pressure']}. "
f"Cloud cover {weather['current']['cloud_cover']}. "
f"UV index {weather['current']['uv_index']}.\n"
2019-10-06 09:13:51 -05:00
f"Today: {weather['today_forecast']['noteworthy']}, "
f"High {weather['today_forecast']['high_F']}/{weather['today_forecast']['high_C']}, "
f"Low {weather['today_forecast']['low_F']}/{weather['today_forecast']['low_C']}. "
f"Tomorrow: {weather['tomorrow_forecast']['noteworthy']}, "
f"High {weather['tomorrow_forecast']['high_F']}/{weather['tomorrow_forecast']['high_C']}, "
f"Low {weather['tomorrow_forecast']['low_F']}/{weather['tomorrow_forecast']['low_C']}. "
f"Day after tomorrow: {weather['day_after_tomorrow_forecast']['noteworthy']}, "
f"High {weather['day_after_tomorrow_forecast']['high_F']}/"
f"{weather['day_after_tomorrow_forecast']['high_C']}, "
f"Low {weather['day_after_tomorrow_forecast']['low_F']}/"
f"{weather['day_after_tomorrow_forecast']['low_C']}.")
2019-10-06 09:13:51 -05:00
return self.bot.reply(event, weather_output)
2015-05-15 21:22:14 -05:00
plugin = Weather