Weather now works. This is gotenks2's commit.

This commit is contained in:
Jeremy Fincher 2003-08-23 13:00:24 +00:00
parent 5234c730c1
commit 5b2cc92917
2 changed files with 39 additions and 68 deletions

View File

@ -209,75 +209,64 @@ class Http(callbacks.Privmsg):
_tempregex = re.compile(
r'<td valign="top" align="right"><strong><font face="arial">'\
r'(.*?)</font></strong></td>', re.IGNORECASE)
def weather2(self, irc, msg, args):
# States
_realStates = sets.Set(['ak', 'al', 'ar', 'ca', 'co', 'ct', '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'])
# 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'])
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.
"""
zip = privmsgs.getArgs(args)
zip = zip.replace(',','')
zip = zip.lower().split()
#If we received more than one argument, then we have received
#a city and state argument that we need to process.
if len(zip) > 1:
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
if len(zip) > 2:
city = '+'.join(zip[:-1]).lower()
isState = zip[-1].lower()
else:
city = zip[0].lower()
isState = zip[1].lower()
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.
#United States
realStates = sets.Set(['ak', 'al', 'ar', 'ca', 'co', 'ct', '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'])
#Canadian 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'])
if isState in realStates:
state = isState
if state in self._realStates:
country = 'us'
elif isState in fakeStates:
state = isState
elif state in self._fakeStates:
country = 'ca'
else:
state = ''
country = isState
#debug.printf('State: %s' % (state,))
#debug.printf('Country: %s' % (country,))
country = state
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?'\
'pass=&dpp=&forecast=zandh&config=&'\
'place=%s&state=%s&country=%s' % \
(city, state, country)
'pass=&dpp=&forecast=zandh&config=&'\
'place=%s&state=%s&country=%s' % \
(city, state, country)
#We received a single argument. Zipcode or station id.
else:
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?'\
'config=&forecast=zandh&pands=%s&Submit=GO' % (zip[0],)
'config=&forecast=zandh&pands=%s&Submit=GO' % args[0]
#debug.printf(url)
debug.printf(url)
try:
fd = urllib2.urlopen(url)
html = fd.read()
fd.close()
headData = self._cityregex.search(html)
if headData:
(city, state, country) = headData.groups()
@ -289,40 +278,17 @@ class Http(callbacks.Privmsg):
conds = self._condregex.search(html).group(1)
if temp and conds and city and state:
irc.reply(msg, 'The current temperature in %s, %s is %s'\
' with %s conditions.' % (city.strip(), state.strip(),
temp, conds))
s = 'The current temperature in %s, %s is %s. ' \
'Conditions are %s.' % \
(city.strip(), state.strip(), temp, conds)
irc.reply(msg, s)
else:
irc.error(msg, 'the format of the page was odd.')
irc.error(msg, 'The format of the page was odd.')
except urllib2.URLError:
irc.error(msg, 'Couldn\'t open the search page.')
irc.error(msg, 'I couldn\'t open the search page.')
except:
irc.error(msg, 'the format of the page was odd.')
_tempregex = re.compile('CLASS=obsTempTextA>(\d+)&deg;F</b></td>',\
re.IGNORECASE)
_cityregex = re.compile(r'Local Forecast for (.*), (.*?) ')
_condregex = re.compile('CLASS=obsInfo2><b CLASS=obsTextA>(.*)</b></td>',\
re.IGNORECASE)
def weather(self, irc, msg, args):
"""<US zip code>
Returns the approximate weather conditions at a given US Zip code.
"""
zip = privmsgs.getArgs(args)
url = "http://www.weather.com/weather/local/%s?lswe=%s" % (zip, zip)
try:
html = urllib2.urlopen(url).read()
city, state = self._cityregex.search(html).groups()
temp = self._tempregex.search(html).group(1)
conds = self._condregex.search(html).group(1)
irc.reply(msg, 'The current temperature in %s, %s is %dF with %s'\
' conditions' % (city, state, int(temp), conds))
except AttributeError:
irc.error(msg, 'the format of the page was odd.')
except urllib2.URLError:
irc.error(msg, 'Couldn\'t open the search page.')
irc.error(msg, 'The format of the page was odd.')
_geekquotere = re.compile('<p class="qt">(.*?)</p>')
def geekquote(self, irc, msg, args):

View File

@ -74,6 +74,11 @@ class HttpTest(PluginTestCase):
def testNetcraft(self):
self.assertNotError('netcraft slashdot.org')
def testWeather(self):
self.assertNotError('weather Columbus, OH')
self.assertNotError('weather 43221')
self.assertNotError('weather Paris FR')
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: