dr.botzo/weather/ircplugin.py

61 lines
3.0 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Report on the weather via wttr.in."""
import logging
from ircbot.lib import Plugin
from weather.lib import weather_summary
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)
super(Weather, self).start()
def stop(self):
"""Tear down handlers."""
self.connection.reactor.remove_global_regex_handler(['pubmsg', 'privmsg'], self.handle_weather)
super(Weather, self).stop()
def handle_weather(self, connection, event, match):
"""Make the weather query and format it for IRC."""
query = match.group(1)
queryitems = query.split(" ")
if len(queryitems) <= 0:
return
weather = weather_summary(query)
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']}. "
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"
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']}.")
return self.bot.reply(event, weather_output)
plugin = Weather