2004-01-07 16:17:53 +01:00
|
|
|
###
|
2004-08-23 15:14:06 +02:00
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2004-01-07 16:17:53 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
2004-02-23 10:05:12 +01:00
|
|
|
This plugin does weather-related stuff. It can't change the weather, though,
|
|
|
|
so don't get your hopes up. We just report it.
|
2004-01-07 16:17:53 +01:00
|
|
|
"""
|
|
|
|
|
2004-09-19 23:14:43 +02:00
|
|
|
import supybot
|
2004-03-19 17:58:54 +01:00
|
|
|
|
2004-09-19 23:14:43 +02:00
|
|
|
__revision__ = "$Id$"
|
|
|
|
__contributors__ = {
|
|
|
|
supybot.authors.jamessan: ['cnn', 'wunder',
|
|
|
|
'temperatureUnit configuration variable',
|
|
|
|
'convert configuration variable'],
|
|
|
|
supybot.authors.jemfinch: ['weather'],
|
|
|
|
supybot.authors.bwp: ['ham'],
|
|
|
|
}
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
import re
|
|
|
|
import sets
|
2004-02-23 10:05:12 +01:00
|
|
|
import urllib
|
2004-01-07 16:17:53 +01:00
|
|
|
|
2004-09-19 23:14:43 +02:00
|
|
|
import BeautifulSoup
|
|
|
|
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
2004-09-23 00:10:16 +02:00
|
|
|
import supybot.commands as commands
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.privmsgs as privmsgs
|
|
|
|
import supybot.registry as registry
|
2004-09-23 00:10:16 +02:00
|
|
|
import supybot.webutils as webutils
|
2004-07-24 07:18:26 +02:00
|
|
|
import supybot.callbacks as callbacks
|
2004-01-07 16:17:53 +01:00
|
|
|
|
2004-02-23 12:08:22 +01:00
|
|
|
unitAbbrevs = utils.abbrev(['Fahrenheit', 'Celsius', 'Centigrade', 'Kelvin'])
|
|
|
|
unitAbbrevs['C'] = 'Celsius'
|
|
|
|
unitAbbrevs['Ce'] = 'Celsius'
|
2004-02-21 22:11:50 +01:00
|
|
|
|
2004-08-18 20:55:54 +02:00
|
|
|
noLocationError = 'No such location could be found.'
|
|
|
|
|
2004-02-21 22:11:50 +01:00
|
|
|
class WeatherUnit(registry.String):
|
2004-02-21 22:49:44 +01:00
|
|
|
def setValue(self, s):
|
2004-02-23 12:08:22 +01:00
|
|
|
#print '***', repr(s)
|
|
|
|
s = s.capitalize()
|
2004-02-21 22:11:50 +01:00
|
|
|
if s not in unitAbbrevs:
|
|
|
|
raise registry.InvalidRegistryValue,\
|
2004-03-30 10:32:17 +02:00
|
|
|
'Unit must be one of Fahrenheit, Celsius, or Kelvin.'
|
2004-02-23 12:08:22 +01:00
|
|
|
s = unitAbbrevs[s]
|
2004-02-21 22:49:44 +01:00
|
|
|
registry.String.setValue(self, s)
|
2004-02-21 22:11:50 +01:00
|
|
|
|
|
|
|
class WeatherCommand(registry.String):
|
2004-02-23 10:05:12 +01:00
|
|
|
def setValue(self, s):
|
|
|
|
m = Weather.weatherCommands
|
|
|
|
if s not in m:
|
2004-02-21 22:11:50 +01:00
|
|
|
raise registry.InvalidRegistryValue,\
|
2004-03-30 10:32:17 +02:00
|
|
|
'Command must be one of %s' % utils.commaAndify(m)
|
2004-02-23 10:05:12 +01:00
|
|
|
else:
|
|
|
|
method = getattr(Weather, s)
|
2004-02-23 10:49:47 +01:00
|
|
|
Weather.weather.im_func.__doc__ = method.__doc__
|
2004-02-23 10:05:12 +01:00
|
|
|
registry.String.setValue(self, s)
|
2004-02-21 22:11:50 +01:00
|
|
|
|
2004-03-30 10:32:17 +02:00
|
|
|
# Registry variables moved to the bottom to use Weather.weatherCommands.
|
|
|
|
|
2004-01-07 16:17:53 +01:00
|
|
|
class Weather(callbacks.Privmsg):
|
2004-07-20 07:26:52 +02:00
|
|
|
"""This should never be seen, because this plugin defines a command by
|
|
|
|
the name of 'weather' which should override this help."""
|
2004-06-04 21:49:08 +02:00
|
|
|
weatherCommands = ['ham', 'cnn', 'wunder']
|
2004-01-07 16:17:53 +01:00
|
|
|
threaded = True
|
2004-09-19 23:14:43 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.__parent = super(Weather, self)
|
|
|
|
self.__parent.__init__()
|
|
|
|
|
2004-09-09 01:34:48 +02:00
|
|
|
def callCommand(self, name, irc, msg, *L, **kwargs):
|
2004-01-07 16:17:53 +01:00
|
|
|
try:
|
2004-09-19 23:14:43 +02:00
|
|
|
self.__parent.callCommand(name, irc, msg, *L, **kwargs)
|
2004-01-07 16:17:53 +01:00
|
|
|
except webutils.WebError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2004-07-19 00:25:12 +02:00
|
|
|
|
2004-02-23 10:05:12 +01:00
|
|
|
def weather(self, irc, msg, args):
|
|
|
|
# This specifically does not have a docstring.
|
|
|
|
channel = None
|
|
|
|
if ircutils.isChannel(msg.args[0]):
|
|
|
|
channel = msg.args[0]
|
2004-04-30 20:36:16 +02:00
|
|
|
if not args:
|
|
|
|
s = self.userValue('lastLocation', msg.prefix)
|
|
|
|
if s:
|
|
|
|
args = [s]
|
2004-09-25 19:55:22 +02:00
|
|
|
location = privmsgs.getArgs(args)
|
|
|
|
self.setUserValue('lastLocation', msg.prefix,
|
|
|
|
location, ignoreNoUser=True)
|
2004-02-25 16:46:22 +01:00
|
|
|
realCommandName = self.registryValue('command', channel)
|
2004-02-23 10:05:12 +01:00
|
|
|
realCommand = getattr(self, realCommandName)
|
2004-09-25 19:55:22 +02:00
|
|
|
ret = realCommand(irc, msg, args)
|
2004-07-19 00:25:12 +02:00
|
|
|
|
2004-02-23 12:08:22 +01:00
|
|
|
def _toCelsius(self, temp, unit):
|
|
|
|
if unit == 'K':
|
|
|
|
return temp - 273.15
|
2004-02-23 12:35:06 +01:00
|
|
|
elif unit == 'F':
|
2004-02-23 12:08:22 +01:00
|
|
|
return (temp - 32) * 5 /9
|
2004-02-23 12:35:06 +01:00
|
|
|
else:
|
|
|
|
return temp
|
2004-02-23 12:08:22 +01:00
|
|
|
|
2004-07-19 00:25:12 +02:00
|
|
|
_temp = re.compile(r'(-?\d+)(.*?)(F|C)')
|
2004-02-21 22:11:50 +01:00
|
|
|
def _getTemp(self, temp, deg, unit, chan):
|
2004-02-23 12:08:22 +01:00
|
|
|
assert unit == unit.upper()
|
|
|
|
assert temp == int(temp)
|
|
|
|
default = self.registryValue('temperatureUnit', chan)
|
2004-02-21 22:11:50 +01:00
|
|
|
if unitAbbrevs[unit] == default:
|
2004-02-23 10:49:47 +01:00
|
|
|
# Short circuit if we're the same unit as the default.
|
2004-02-23 12:08:22 +01:00
|
|
|
return '%s%s%s' % (temp, deg, unit)
|
|
|
|
temp = self._toCelsius(temp, unit)
|
|
|
|
unit = 'C'
|
|
|
|
if default == 'Kelvin':
|
|
|
|
temp = temp + 273.15
|
|
|
|
unit = 'K'
|
|
|
|
deg = ' '
|
|
|
|
elif default == 'Fahrenheit':
|
|
|
|
temp = temp * 9 / 5 + 32
|
|
|
|
unit = 'F'
|
|
|
|
return '%s%s%s' % (temp, deg, unit)
|
2004-02-21 22:11:50 +01:00
|
|
|
|
2004-06-04 21:49:08 +02:00
|
|
|
_hamLoc = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'<td><font size="4" face="arial"><b>'
|
|
|
|
r'(.*?), (.*?),(.*?)</b></font></td>', re.I)
|
2004-01-07 16:17:53 +01:00
|
|
|
_interregex = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'<td><font size="4" face="arial"><b>'
|
|
|
|
r'([^,]+), ([^<]+)</b></font></td>', re.I)
|
2004-06-04 21:49:08 +02:00
|
|
|
_hamCond = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'<td width="100%" colspan="2" align="center"><strong>'
|
|
|
|
r'<font face="arial">([^<]+)</font></strong></td>', re.I)
|
2004-06-04 21:49:08 +02:00
|
|
|
_hamTemp = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'<td valign="top" align="right"><strong><font face="arial">'
|
2004-02-23 12:08:22 +01:00
|
|
|
r'(-?\d+)(.*?)(F|C)</font></strong></td>', re.I)
|
2004-06-04 21:49:08 +02:00
|
|
|
_hamChill = re.compile(
|
2004-02-23 12:08:22 +01:00
|
|
|
r'Wind Chill</font></strong>:</small></td>\s+<td align="right">'
|
2004-01-09 03:50:23 +01:00
|
|
|
r'<small><font face="arial">([^N][^<]+)</font></small></td>',
|
|
|
|
re.I | re.S)
|
2004-06-04 21:49:08 +02:00
|
|
|
_hamHeat = re.compile(
|
2004-02-23 12:08:22 +01:00
|
|
|
r'Heat Index</font></strong>:</small></td>\s+<td align="right">'
|
2004-01-09 03:50:23 +01:00
|
|
|
r'<small><font face="arial">([^N][^<]+)</font></small></td>',
|
|
|
|
re.I | re.S)
|
2004-01-07 16:17:53 +01:00
|
|
|
# States
|
2004-07-19 00:25:12 +02:00
|
|
|
_realStates = sets.Set(['ak', 'al', 'ar', 'az', 'ca', 'co', 'ct',
|
2004-01-11 15:47:44 +01:00
|
|
|
'dc', 'de', 'fl', 'ga', 'hi', 'ia', 'id',
|
|
|
|
'il', 'in', 'ks', 'ky', 'la', 'ma', 'md',
|
|
|
|
'me', 'mi', 'mn', 'mo', 'ms', 'mt', 'nc',
|
|
|
|
'nd', 'ne', 'nh', 'nj', 'nm', 'nv', 'ny',
|
|
|
|
'oh', 'ok', 'or', 'pa', 'ri', 'sc', 'sd',
|
|
|
|
'tn', 'tx', 'ut', 'va', 'vt', 'wa', 'wi',
|
|
|
|
'wv', 'wy'])
|
2004-01-07 16:17:53 +01:00
|
|
|
# Provinces. (Province being a metric state measurement mind you. :D)
|
|
|
|
_fakeStates = sets.Set(['ab', 'bc', 'mb', 'nb', 'nf', 'ns', 'nt',
|
|
|
|
'nu', 'on', 'pe', 'qc', 'sk', 'yk'])
|
|
|
|
# Certain countries are expected to use a standard abbreviation
|
|
|
|
# The weather we pull uses weird codes. Map obvious ones here.
|
2004-02-21 22:11:50 +01:00
|
|
|
_hamCountryMap = {'uk': 'gb'}
|
2004-09-23 00:10:16 +02:00
|
|
|
def ham(self, irc, msg, args, loc):
|
2004-02-17 07:29:19 +01:00
|
|
|
"""<US zip code | US/Canada city, state | Foreign city, country>
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
Returns the approximate weather conditions for a given city.
|
|
|
|
"""
|
2004-07-19 00:25:12 +02:00
|
|
|
|
2004-01-07 16:17:53 +01:00
|
|
|
#If we received more than one argument, then we have received
|
|
|
|
#a city and state argument that we need to process.
|
2004-09-23 00:10:16 +02:00
|
|
|
if ' ' in loc:
|
2004-01-07 16:17:53 +01:00
|
|
|
#If we received more than 1 argument, then we got a city with a
|
|
|
|
#multi-word name. ie ['Garden', 'City', 'KS'] instead of
|
|
|
|
#['Liberal', 'KS']. We join it together with a + to pass
|
|
|
|
#to our query
|
2004-09-23 00:10:16 +02:00
|
|
|
loc = rsplit(loc, None, 1)
|
|
|
|
state = loc.pop().lower()
|
|
|
|
city = '+'.join(loc)
|
|
|
|
city = city.rstrip(',').lower()
|
2004-01-07 16:17:53 +01:00
|
|
|
#We must break the States up into two sections. The US and
|
|
|
|
#Canada are the only countries that require a State argument.
|
2004-07-19 00:25:12 +02:00
|
|
|
|
2004-01-07 16:17:53 +01:00
|
|
|
if state in self._realStates:
|
|
|
|
country = 'us'
|
|
|
|
elif state in self._fakeStates:
|
|
|
|
country = 'ca'
|
|
|
|
else:
|
|
|
|
country = state
|
|
|
|
state = ''
|
2004-02-21 22:11:50 +01:00
|
|
|
if country in self._hamCountryMap.keys():
|
|
|
|
country = self._hamCountryMap[country]
|
2004-01-09 00:03:48 +01:00
|
|
|
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?' \
|
|
|
|
'pass=&dpp=&forecast=zandh&config=&' \
|
|
|
|
'place=%s&state=%s&country=%s' % (city, state, country)
|
2004-01-11 15:47:44 +01:00
|
|
|
html = webutils.getUrl(url)
|
|
|
|
if 'was not found' in html:
|
|
|
|
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?' \
|
|
|
|
'pass=&dpp=&forecast=zandh&config=&' \
|
|
|
|
'place=%s&state=&country=%s' % (city, state)
|
|
|
|
html = webutils.getUrl(url)
|
|
|
|
if 'was not found' in html: # Still.
|
2004-08-18 20:55:54 +02:00
|
|
|
irc.error(noLocationError, Raise=True)
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
#We received a single argument. Zipcode or station id.
|
|
|
|
else:
|
2004-09-23 00:10:16 +02:00
|
|
|
zip = loc.replace(',', '')
|
2004-02-21 22:11:50 +01:00
|
|
|
zip = zip.lower()
|
2004-01-09 00:03:48 +01:00
|
|
|
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?' \
|
2004-02-21 22:11:50 +01:00
|
|
|
'config=&forecast=zandh&pands=%s&Submit=GO' % zip
|
2004-01-07 16:17:53 +01:00
|
|
|
html = webutils.getUrl(url)
|
2004-01-11 15:47:44 +01:00
|
|
|
if 'was not found' in html:
|
2004-08-18 20:55:54 +02:00
|
|
|
irc.error(noLocationError, Raise=True)
|
2004-07-19 00:25:12 +02:00
|
|
|
|
2004-06-04 21:49:08 +02:00
|
|
|
headData = self._hamLoc.search(html)
|
2004-02-08 08:16:58 +01:00
|
|
|
if headData is not None:
|
2004-01-07 16:17:53 +01:00
|
|
|
(city, state, country) = headData.groups()
|
|
|
|
else:
|
|
|
|
headData = self._interregex.search(html)
|
2004-01-11 15:47:44 +01:00
|
|
|
if headData:
|
|
|
|
(city, state) = headData.groups()
|
|
|
|
else:
|
2004-08-18 20:55:54 +02:00
|
|
|
irc.error(noLocationError, Raise=True)
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
city = city.strip()
|
|
|
|
state = state.strip()
|
2004-06-04 21:49:08 +02:00
|
|
|
temp = self._hamTemp.search(html)
|
2004-04-14 02:26:08 +02:00
|
|
|
convert = self.registryValue('convert', msg.args[0])
|
2004-02-08 08:16:58 +01:00
|
|
|
if temp is not None:
|
2004-02-23 12:08:22 +01:00
|
|
|
(temp, deg, unit) = temp.groups()
|
2004-04-14 02:26:08 +02:00
|
|
|
if convert:
|
|
|
|
temp = self._getTemp(int(temp), deg, unit, msg.args[0])
|
|
|
|
else:
|
|
|
|
temp = deg.join((temp, unit))
|
2004-06-04 21:49:08 +02:00
|
|
|
conds = self._hamCond.search(html)
|
2004-02-08 08:16:58 +01:00
|
|
|
if conds is not None:
|
2004-01-07 16:17:53 +01:00
|
|
|
conds = conds.group(1)
|
2004-01-09 03:50:23 +01:00
|
|
|
index = ''
|
2004-06-04 21:49:08 +02:00
|
|
|
chill = self._hamChill.search(html)
|
2004-02-08 08:16:58 +01:00
|
|
|
if chill is not None:
|
2004-01-07 16:17:53 +01:00
|
|
|
chill = chill.group(1)
|
2004-02-23 12:08:22 +01:00
|
|
|
chill = utils.htmlToText(chill)
|
2004-04-14 02:26:08 +02:00
|
|
|
if convert:
|
|
|
|
tempsplit = self._temp.search(chill)
|
|
|
|
if tempsplit:
|
|
|
|
(chill, deg, unit) = tempsplit.groups()
|
|
|
|
chill = self._getTemp(int(chill), deg, unit,msg.args[0])
|
2004-03-26 01:28:51 +01:00
|
|
|
if float(chill[:-2]) < float(temp[:-2]):
|
2004-01-09 03:50:23 +01:00
|
|
|
index = ' (Wind Chill: %s)' % chill
|
2004-06-04 21:49:08 +02:00
|
|
|
heat = self._hamHeat.search(html)
|
2004-02-08 07:24:00 +01:00
|
|
|
if heat is not None:
|
2004-01-07 16:17:53 +01:00
|
|
|
heat = heat.group(1)
|
2004-02-23 12:08:22 +01:00
|
|
|
heat = utils.htmlToText(heat)
|
2004-04-14 02:26:08 +02:00
|
|
|
if convert:
|
|
|
|
tempsplit = self._temp.search(heat)
|
|
|
|
if tempsplit:
|
|
|
|
(heat, deg, unit) = tempsplit.groups()
|
|
|
|
if convert:
|
|
|
|
heat = self._getTemp(int(heat), deg, unit,msg.args[0])
|
2004-03-26 01:28:51 +01:00
|
|
|
if float(heat[:-2]) > float(temp[:-2]):
|
2004-01-09 03:50:23 +01:00
|
|
|
index = ' (Heat Index: %s)' % heat
|
2004-01-07 16:17:53 +01:00
|
|
|
if temp and conds and city and state:
|
2004-02-08 08:16:58 +01:00
|
|
|
conds = conds.replace('Tsra', 'Thunderstorms')
|
2004-06-06 23:21:15 +02:00
|
|
|
conds = conds.replace('Ts', 'Thunderstorms')
|
2004-07-29 20:36:45 +02:00
|
|
|
s = 'The current temperature in %s, %s is %s%s. Conditions: %s.'% \
|
2004-01-07 16:17:53 +01:00
|
|
|
(city, state, temp, index, conds)
|
2004-01-09 01:06:48 +01:00
|
|
|
irc.reply(s)
|
2004-01-07 16:17:53 +01:00
|
|
|
else:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error('The format of the page was odd.')
|
2004-09-23 00:23:54 +02:00
|
|
|
ham = commands.wrap(ham, ['something'])
|
2004-01-07 16:17:53 +01:00
|
|
|
|
2004-02-21 22:11:50 +01:00
|
|
|
_cnnUrl = 'http://weather.cnn.com/weather/search?wsearch='
|
2004-06-04 21:49:08 +02:00
|
|
|
_cnnFTemp = re.compile(r'(-?\d+)(°)(F)</span>', re.I | re.S)
|
|
|
|
_cnnCond = re.compile(r'align="center"><b>([^<]+)</b></div></td>',
|
|
|
|
re.I | re.S)
|
|
|
|
_cnnHumid = re.compile(r'Rel. Humidity: <b>(\d+%)</b>', re.I | re.S)
|
|
|
|
_cnnWind = re.compile(r'Wind: <b>([^<]+)</b>', re.I | re.S)
|
|
|
|
_cnnLoc = re.compile(r'<title>([^<]+)</title>', re.I | re.S)
|
2004-02-21 22:11:50 +01:00
|
|
|
# Certain countries are expected to use a standard abbreviation
|
|
|
|
# The weather we pull uses weird codes. Map obvious ones here.
|
|
|
|
_cnnCountryMap = {'uk': 'en', 'de': 'ge'}
|
2004-09-23 00:10:16 +02:00
|
|
|
def cnn(self, irc, msg, args, loc):
|
2004-02-21 22:11:50 +01:00
|
|
|
"""<US zip code | US/Canada city, state | Foreign city, country>
|
|
|
|
|
|
|
|
Returns the approximate weather conditions for a given city.
|
|
|
|
"""
|
2004-09-23 00:10:16 +02:00
|
|
|
if ' ' in loc:
|
2004-02-21 22:11:50 +01:00
|
|
|
#If we received more than 1 argument, then we got a city with a
|
|
|
|
#multi-word name. ie ['Garden', 'City', 'KS'] instead of
|
2004-09-23 00:10:16 +02:00
|
|
|
#['Liberal', 'KS'].
|
|
|
|
loc = rsplit(loc, None, 1)
|
|
|
|
state = loc.pop().lower()
|
|
|
|
city = ' '.join(loc)
|
2004-02-21 22:11:50 +01:00
|
|
|
city = city.rstrip(',').lower()
|
|
|
|
if state in self._cnnCountryMap:
|
|
|
|
state = self._cnnCountryMap[state]
|
|
|
|
loc = ' '.join([city, state])
|
|
|
|
else:
|
|
|
|
#We received a single argument. Zipcode or station id.
|
2004-09-23 00:10:16 +02:00
|
|
|
loc = loc.replace(',', '')
|
2004-02-21 22:11:50 +01:00
|
|
|
url = '%s%s' % (self._cnnUrl, urllib.quote(loc))
|
2004-08-18 20:55:54 +02:00
|
|
|
text = webutils.getUrl(url) # Errors caught in callCommand.
|
2004-02-21 22:11:50 +01:00
|
|
|
if "No search results" in text or "does not match a zip code" in text:
|
2004-08-18 20:55:54 +02:00
|
|
|
irc.error(noLocationError, Raise=True)
|
2004-06-04 21:49:08 +02:00
|
|
|
location = self._cnnLoc.search(text)
|
|
|
|
temp = self._cnnFTemp.search(text)
|
|
|
|
conds = self._cnnCond.search(text)
|
|
|
|
humidity = self._cnnHumid.search(text)
|
|
|
|
wind = self._cnnWind.search(text)
|
2004-04-14 02:26:08 +02:00
|
|
|
convert = self.registryValue('convert', msg.args[0])
|
2004-02-21 22:11:50 +01:00
|
|
|
if location and temp:
|
|
|
|
location = location.group(1)
|
|
|
|
location = location.split('-')[-1].strip()
|
2004-02-21 22:49:44 +01:00
|
|
|
(temp, deg, unit) = temp.groups()
|
2004-04-14 02:26:08 +02:00
|
|
|
if convert:
|
|
|
|
temp = self._getTemp(int(temp), deg, unit, msg.args[0])
|
|
|
|
else:
|
|
|
|
temp = deg.join((temp, unit))
|
2004-07-29 20:36:45 +02:00
|
|
|
resp = ['The current temperature in %s is %s.' % (location, temp)]
|
2004-02-21 22:11:50 +01:00
|
|
|
if conds is not None:
|
|
|
|
resp.append('Conditions: %s.' % conds.group(1))
|
|
|
|
if humidity is not None:
|
|
|
|
resp.append('Humidity: %s.' % humidity.group(1))
|
|
|
|
if wind is not None:
|
|
|
|
resp.append('Wind: %s.' % wind.group(1))
|
|
|
|
resp = map(utils.htmlToText, resp)
|
2004-06-04 21:49:08 +02:00
|
|
|
irc.reply(' '.join(resp))
|
|
|
|
else:
|
|
|
|
irc.error('Could not find weather information.')
|
2004-09-23 00:23:54 +02:00
|
|
|
cnn = commands.wrap(cnn, ['something'])
|
2004-06-04 21:49:08 +02:00
|
|
|
|
2004-09-19 23:14:43 +02:00
|
|
|
_wunderUrl = 'http://mobile.wunderground.com/cgi-bin/' \
|
|
|
|
'findweather/getForecast?query='
|
|
|
|
_wunderLoc = re.compile(r'Page (.+?) Forecast</title>', re.I | re.S)
|
2004-08-18 21:32:15 +02:00
|
|
|
_wunderMultiLoc = re.compile(r'<a href="([^"]+)', re.I | re.S)
|
2004-09-23 00:10:16 +02:00
|
|
|
def wunder(self, irc, msg, args, loc):
|
2004-06-04 21:49:08 +02:00
|
|
|
"""<US zip code | US/Canada city, state | Foreign city, country>
|
|
|
|
|
|
|
|
Returns the approximate weather conditions for a given city.
|
|
|
|
"""
|
|
|
|
url = '%s%s' % (self._wunderUrl, urllib.quote(loc))
|
2004-09-19 23:14:43 +02:00
|
|
|
text = webutils.getUrl(url)
|
2004-06-04 21:49:08 +02:00
|
|
|
if 'Search not found' in text:
|
2004-08-18 20:55:54 +02:00
|
|
|
irc.error(noLocationError, Raise=True)
|
2004-09-19 23:14:43 +02:00
|
|
|
if 'Click on a city name' in text:
|
|
|
|
soup = BeautifulSoup.BeautifulSoup()
|
|
|
|
soup.feed(text)
|
|
|
|
newloc = soup.first('a').get('href')
|
2004-08-18 21:32:15 +02:00
|
|
|
if newloc is None:
|
|
|
|
irc.error('Multiple locations found. '
|
|
|
|
'Please be more specific.', Raise=True)
|
2004-09-19 23:14:43 +02:00
|
|
|
url = 'http://mobile.wunderground.com%s' % newloc
|
|
|
|
text = webutils.getUrl(url)
|
|
|
|
soup.close()
|
|
|
|
soup = BeautifulSoup.BeautifulSoup()
|
|
|
|
soup.feed(text)
|
|
|
|
# Get the table with all the weather info
|
|
|
|
table = soup.first('table', {'border':'1'})
|
|
|
|
trs = table.fetch('tr')
|
|
|
|
try:
|
|
|
|
time = trs.pop(0).first('b').string
|
|
|
|
except AttributeError:
|
|
|
|
time = ''
|
|
|
|
info = {}
|
|
|
|
def isText(t):
|
|
|
|
return not isinstance(t,BeautifulSoup.NavigableText) and t.contents
|
|
|
|
def getText(t):
|
|
|
|
s = getattr(t, 'string', None)
|
|
|
|
if s is None:
|
|
|
|
t = t.contents
|
|
|
|
num = t[0].string
|
|
|
|
units = t[1].string
|
|
|
|
# htmlToText strips leading whitespace, so we have to handle
|
|
|
|
# strings with differently.
|
|
|
|
if units.startswith(' '):
|
|
|
|
units = utils.htmlToText(units)
|
|
|
|
s = ' '.join((num, units))
|
|
|
|
else:
|
|
|
|
units = utils.htmlToText(units)
|
|
|
|
s = ' '.join((num, units[0], units[1:]))
|
|
|
|
return s
|
|
|
|
for tr in trs:
|
|
|
|
k = tr.first('td').string
|
|
|
|
v = filter(isText, tr.fetch('td')[1].contents)
|
|
|
|
value = map(getText, v)
|
|
|
|
info[k] = ' '.join(value)
|
2004-06-04 21:49:08 +02:00
|
|
|
location = self._wunderLoc.search(text)
|
2004-09-19 23:14:43 +02:00
|
|
|
temp = info['Temperature']
|
2004-06-04 21:49:08 +02:00
|
|
|
convert = self.registryValue('convert', msg.args[0])
|
|
|
|
if location and temp:
|
2004-09-19 23:14:43 +02:00
|
|
|
(temp, deg, unit) = temp.split()
|
2004-06-04 21:49:08 +02:00
|
|
|
if convert:
|
|
|
|
temp = self._getTemp(int(temp), deg, unit, msg.args[0])
|
|
|
|
else:
|
|
|
|
temp = deg.join((temp, unit))
|
2004-09-19 23:14:43 +02:00
|
|
|
resp = ['The current temperature in %s is %s (%s).' %\
|
|
|
|
(location.group(1), temp, time)]
|
|
|
|
conds = info['Conditions']
|
|
|
|
resp.append('Conditions: %s.' % info['Conditions'])
|
|
|
|
humidity = info['Humidity']
|
|
|
|
resp.append('Humidity: %s.' % info['Humidity'])
|
|
|
|
(dew, deg, unit) = info['Dew Point'].split()
|
|
|
|
if convert:
|
|
|
|
dew = self._getTemp(int(dew), deg, unit, msg.args[0])
|
2004-06-04 21:49:08 +02:00
|
|
|
else:
|
2004-09-19 23:14:43 +02:00
|
|
|
dew = deg.join((dew, unit))
|
|
|
|
resp.append('Dew Point: %s.' % dew)
|
|
|
|
resp.append('Wind: %s at %s %s.' % tuple(info['Wind'].split()))
|
|
|
|
resp.append('Pressure: %s.' % info['Pressure'])
|
|
|
|
resp.append('Visibility: %s.' % info['Visibility'])
|
2004-06-04 21:49:08 +02:00
|
|
|
resp = map(utils.htmlToText, resp)
|
|
|
|
irc.reply(' '.join(resp))
|
2004-02-21 22:11:50 +01:00
|
|
|
else:
|
|
|
|
irc.error('Could not find weather information.')
|
2004-09-23 00:23:54 +02:00
|
|
|
wunder = commands.wrap(wunder, ['something'])
|
2004-01-07 16:17:53 +01:00
|
|
|
|
2004-02-23 10:49:47 +01:00
|
|
|
conf.registerPlugin('Weather')
|
2004-02-23 12:08:22 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Weather, 'temperatureUnit',
|
2004-02-23 10:49:47 +01:00
|
|
|
WeatherUnit('Fahrenheit', """Sets the default temperature unit to use when
|
|
|
|
reporting the weather."""))
|
2004-02-25 16:46:22 +01:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Weather, 'command',
|
2004-08-18 21:32:15 +02:00
|
|
|
WeatherCommand('wunder', """Sets the default command to use when retrieving
|
2004-03-30 10:32:17 +02:00
|
|
|
the weather. Command must be one of %s.""" %
|
|
|
|
utils.commaAndify(Weather.weatherCommands, And='or')))
|
2004-04-14 02:26:08 +02:00
|
|
|
conf.registerChannelValue(conf.supybot.plugins.Weather, 'convert',
|
|
|
|
registry.Boolean(True, """Determines whether the weather commands will
|
|
|
|
automatically convert weather units to the unit specified in
|
|
|
|
supybot.plugins.Weather.temperatureUnit."""))
|
2004-02-23 10:49:47 +01:00
|
|
|
|
2004-04-30 20:36:16 +02:00
|
|
|
conf.registerUserValue(conf.users.plugins.Weather, 'lastLocation',
|
|
|
|
registry.String('', ''))
|
|
|
|
|
2004-01-07 16:17:53 +01:00
|
|
|
Class = Weather
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|