Brian S. Stephan
2aa369add7
more of a moving of the code, actually, it now exists in (an overridden) _handle_event, so that recursions happen against irc events directly, rather than an already partially interpreted object. with this change, modules don't need to implement do() nor do we have a need for the internal_bus, which was doing an additional walk of the modules after the irc event was already handled and turned into text. now the core event handler does the recursion scans. to support this, we bring back the old replypath trick and use it again, so we know when to send a privmsg reply and when to return text so that it may be chained in recursion. this feels old hat by now, but if you haven't been following along, you should really look at the diff. that's the meat of the change. the rest is updating modules to use self.reply() and reimplementing (un)register_handlers where appropriate
73 lines
3.2 KiB
Python
73 lines
3.2 KiB
Python
# coding: utf-8
|
|
"""
|
|
Weather - query various weather services for info
|
|
Copyright (C) 2010 Brian S. Stephan
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
import re
|
|
|
|
from extlib import irclib
|
|
from extlib import pywapi
|
|
|
|
from urllib import quote
|
|
from urllib2 import URLError
|
|
|
|
from Module import Module
|
|
|
|
class Weather(Module):
|
|
"""Provide weather lookup services to the bot."""
|
|
|
|
def do(self, connection, event, nick, userhost, what, admin_unlocked):
|
|
"""Query Google Weather for a location's weather."""
|
|
|
|
match = re.search('^!weather\s+(.*)$', what)
|
|
if match:
|
|
query = match.group(1)
|
|
try:
|
|
google_weather = pywapi.get_weather_from_google(quote(query))
|
|
city = google_weather['forecast_information']['city'].encode('utf-8')
|
|
condition = google_weather['current_conditions']['condition'].encode('utf-8')
|
|
temp_f = google_weather['current_conditions']['temp_f'].encode('utf-8')
|
|
temp_c = google_weather['current_conditions']['temp_c'].encode('utf-8')
|
|
wind = google_weather['current_conditions']['wind_condition'].encode('utf-8')
|
|
humidity = google_weather['current_conditions']['humidity'].encode('utf-8')
|
|
|
|
wind_speed_regex = re.compile("Wind: [NSEW]+ at ([0-9]+) mph")
|
|
matches = wind_speed_regex.search(wind)
|
|
|
|
if matches is not None and float(temp_f) < 50.0:
|
|
wind_speed = matches.group(1)
|
|
windchill = 35.74 + (0.6215 * float(temp_f)) - (35.75 * float(wind_speed)**0.16) + (0.4275 * float(temp_f) * float(wind_speed)**0.16)
|
|
else:
|
|
windchill = ""
|
|
|
|
weatherstr = "Current weather for " + city + ": " + condition + ". " + temp_f + "°F (" + temp_c + "°C), " + wind + ", " + humidity
|
|
if windchill is not "":
|
|
weatherstr += ", Wind chill: " + str(round(windchill, 2)) + "°F"
|
|
weatherstr += "."
|
|
|
|
for city in google_weather['forecasts']:
|
|
weatherstr += " " + city['day_of_week'].encode('utf-8') + ": " + city['condition'].encode('utf-8') + ". High " + city['high'].encode('utf-8') + "°F, Low " + city['low'].encode('utf-8') + "°F."
|
|
|
|
return self.reply(connection, event, weatherstr)
|
|
except URLError as e:
|
|
return self.reply(connection, event, "error connecting to google weather:" + str(e))
|
|
except IndexError as e:
|
|
return self.reply(connection, event, "error in pywapi: " + str(e))
|
|
|
|
# vi:tabstop=4:expandtab:autoindent
|
|
# kate: indent-mode python;indent-width 4;replace-tabs on;
|