2004-01-09 01:06:48 +01:00
|
|
|
#!/usr/bin/env python
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
###
|
|
|
|
# Copyright (c) 2002, Jeremiah Fincher
|
|
|
|
# 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.
|
|
|
|
###
|
|
|
|
|
|
|
|
"""
|
|
|
|
This plugin does weather-related stuff.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import plugins
|
|
|
|
|
|
|
|
import re
|
|
|
|
import sets
|
|
|
|
|
|
|
|
import utils
|
|
|
|
import webutils
|
|
|
|
import privmsgs
|
|
|
|
import callbacks
|
|
|
|
|
|
|
|
|
|
|
|
class Weather(callbacks.Privmsg):
|
|
|
|
threaded = True
|
|
|
|
def callCommand(self, method, irc, msg, *L):
|
|
|
|
try:
|
|
|
|
callbacks.Privmsg.callCommand(self, method, irc, msg, *L)
|
|
|
|
except webutils.WebError, e:
|
2004-01-08 04:12:14 +01:00
|
|
|
irc.error(str(e))
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
_cityregex = 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-01-07 16:17:53 +01:00
|
|
|
_condregex = 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-01-07 16:17:53 +01:00
|
|
|
_tempregex = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'<td valign="top" align="right"><strong><font face="arial">'
|
|
|
|
r'([^<]+)</font></strong></td>', re.I)
|
2004-01-07 16:17:53 +01:00
|
|
|
_chillregex = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'Wind Chill</font></strong>:</small></a></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
|
|
|
_heatregex = re.compile(
|
2004-01-09 00:03:48 +01:00
|
|
|
r'Heat Index</font></strong>:</small></a></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
|
|
|
|
_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.
|
|
|
|
_countryMap = {'uk': 'gb'}
|
|
|
|
def weather(self, irc, msg, args):
|
|
|
|
"""<US zip code> <US/Canada city, state> <Foreign city, country>
|
|
|
|
|
|
|
|
Returns the approximate weather conditions for a given city.
|
|
|
|
"""
|
|
|
|
|
|
|
|
#If we received more than one argument, then we have received
|
|
|
|
#a city and state argument that we need to process.
|
|
|
|
if len(args) > 1:
|
|
|
|
#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
|
|
|
|
state = args.pop()
|
|
|
|
state = state.lower()
|
|
|
|
city = '+'.join(args)
|
|
|
|
city = city.rstrip(',')
|
|
|
|
city = city.lower()
|
|
|
|
#We must break the States up into two sections. The US and
|
|
|
|
#Canada are the only countries that require a State argument.
|
|
|
|
|
|
|
|
if state in self._realStates:
|
|
|
|
country = 'us'
|
|
|
|
elif state in self._fakeStates:
|
|
|
|
country = 'ca'
|
|
|
|
else:
|
|
|
|
country = state
|
|
|
|
state = ''
|
2004-01-11 15:47:44 +01:00
|
|
|
if country in self._countryMap.keys():
|
|
|
|
country = self._countryMap[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.
|
|
|
|
irc.error('No such location could be found.')
|
|
|
|
return
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
#We received a single argument. Zipcode or station id.
|
|
|
|
else:
|
|
|
|
zip = privmsgs.getArgs(args)
|
|
|
|
zip = zip.replace(',', '')
|
|
|
|
zip = zip.lower().split()
|
2004-01-09 00:03:48 +01:00
|
|
|
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?' \
|
2004-01-07 16:17:53 +01:00
|
|
|
'config=&forecast=zandh&pands=%s&Submit=GO' % args[0]
|
|
|
|
html = webutils.getUrl(url)
|
2004-01-11 15:47:44 +01:00
|
|
|
if 'was not found' in html:
|
|
|
|
irc.error('No such location could be found.')
|
|
|
|
return
|
|
|
|
|
2004-01-07 16:17:53 +01:00
|
|
|
headData = self._cityregex.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:
|
|
|
|
irc.error('No such location could be found.')
|
|
|
|
return
|
2004-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
city = city.strip()
|
|
|
|
state = state.strip()
|
|
|
|
temp = self._tempregex.search(html)
|
2004-02-08 08:16:58 +01:00
|
|
|
if temp is not None:
|
2004-01-07 16:17:53 +01:00
|
|
|
temp = temp.group(1)
|
|
|
|
conds = self._condregex.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-01-09 00:45:22 +01:00
|
|
|
chill = self._chillregex.search(html)
|
2004-02-08 08:16:58 +01:00
|
|
|
if chill is not None:
|
2004-01-09 00:43:32 +01:00
|
|
|
#self.log.warning(chill.groups())
|
2004-01-07 16:17:53 +01:00
|
|
|
chill = chill.group(1)
|
2004-01-09 03:50:23 +01:00
|
|
|
if int(chill[:-2]) < int(temp[:-2]):
|
|
|
|
index = ' (Wind Chill: %s)' % chill
|
2004-01-07 16:17:53 +01:00
|
|
|
heat = self._heatregex.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-01-09 03:50:23 +01:00
|
|
|
if int(heat[:-2]) > int(temp[:-2]):
|
|
|
|
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')
|
|
|
|
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-01-07 16:17:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
Class = Weather
|
|
|
|
|
|
|
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|