mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-30 14:14:37 +01:00
Weather now works. This is gotenks2's commit.
This commit is contained in:
parent
5234c730c1
commit
5b2cc92917
102
plugins/Http.py
102
plugins/Http.py
@ -209,75 +209,64 @@ class Http(callbacks.Privmsg):
|
|||||||
_tempregex = re.compile(
|
_tempregex = re.compile(
|
||||||
r'<td valign="top" align="right"><strong><font face="arial">'\
|
r'<td valign="top" align="right"><strong><font face="arial">'\
|
||||||
r'(.*?)</font></strong></td>', re.IGNORECASE)
|
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>
|
"""<US zip code> <US/Canada city, state> <Foreign city, country>
|
||||||
|
|
||||||
Returns the approximate weather conditions for a given city.
|
Returns the approximate weather conditions for a given city.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
zip = privmsgs.getArgs(args)
|
zip = privmsgs.getArgs(args)
|
||||||
zip = zip.replace(',','')
|
zip = zip.replace(',','')
|
||||||
zip = zip.lower().split()
|
zip = zip.lower().split()
|
||||||
|
|
||||||
#If we received more than one argument, then we have received
|
#If we received more than one argument, then we have received
|
||||||
#a city and state argument that we need to process.
|
#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
|
#If we received more than 1 argument, then we got a city with a
|
||||||
#multi-word name. ie ['Garden', 'City', 'KS'] instead of
|
#multi-word name. ie ['Garden', 'City', 'KS'] instead of
|
||||||
#['Liberal', 'KS']. We join it together with a + to pass
|
#['Liberal', 'KS']. We join it together with a + to pass
|
||||||
#to our query
|
#to our query
|
||||||
if len(zip) > 2:
|
state = args.pop()
|
||||||
city = '+'.join(zip[:-1]).lower()
|
state = state.lower()
|
||||||
isState = zip[-1].lower()
|
city = '+'.join(args)
|
||||||
else:
|
city = city.rstrip(',')
|
||||||
city = zip[0].lower()
|
city = city.lower()
|
||||||
isState = zip[1].lower()
|
|
||||||
|
|
||||||
#We must break the States up into two sections. The US and
|
#We must break the States up into two sections. The US and
|
||||||
#Canada are the only countries that require a State argument.
|
#Canada are the only countries that require a State argument.
|
||||||
|
|
||||||
#United States
|
if state in self._realStates:
|
||||||
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
|
|
||||||
country = 'us'
|
country = 'us'
|
||||||
elif isState in fakeStates:
|
elif state in self._fakeStates:
|
||||||
state = isState
|
|
||||||
country = 'ca'
|
country = 'ca'
|
||||||
else:
|
else:
|
||||||
state = ''
|
state = ''
|
||||||
country = isState
|
country = state
|
||||||
|
|
||||||
#debug.printf('State: %s' % (state,))
|
|
||||||
#debug.printf('Country: %s' % (country,))
|
|
||||||
|
|
||||||
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?'\
|
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?'\
|
||||||
'pass=&dpp=&forecast=zandh&config=&'\
|
'pass=&dpp=&forecast=zandh&config=&'\
|
||||||
'place=%s&state=%s&country=%s' % \
|
'place=%s&state=%s&country=%s' % \
|
||||||
(city, state, country)
|
(city, state, country)
|
||||||
|
|
||||||
#We received a single argument. Zipcode or station id.
|
#We received a single argument. Zipcode or station id.
|
||||||
else:
|
else:
|
||||||
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?'\
|
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:
|
try:
|
||||||
fd = urllib2.urlopen(url)
|
fd = urllib2.urlopen(url)
|
||||||
html = fd.read()
|
html = fd.read()
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
headData = self._cityregex.search(html)
|
headData = self._cityregex.search(html)
|
||||||
if headData:
|
if headData:
|
||||||
(city, state, country) = headData.groups()
|
(city, state, country) = headData.groups()
|
||||||
@ -289,40 +278,17 @@ class Http(callbacks.Privmsg):
|
|||||||
conds = self._condregex.search(html).group(1)
|
conds = self._condregex.search(html).group(1)
|
||||||
|
|
||||||
if temp and conds and city and state:
|
if temp and conds and city and state:
|
||||||
irc.reply(msg, 'The current temperature in %s, %s is %s'\
|
s = 'The current temperature in %s, %s is %s. ' \
|
||||||
' with %s conditions.' % (city.strip(), state.strip(),
|
'Conditions are %s.' % \
|
||||||
temp, conds))
|
(city.strip(), state.strip(), temp, conds)
|
||||||
|
irc.reply(msg, s)
|
||||||
else:
|
else:
|
||||||
irc.error(msg, 'the format of the page was odd.')
|
irc.error(msg, 'The format of the page was odd.')
|
||||||
|
|
||||||
except urllib2.URLError:
|
except urllib2.URLError:
|
||||||
irc.error(msg, 'Couldn\'t open the search page.')
|
irc.error(msg, 'I couldn\'t open the search page.')
|
||||||
except:
|
except:
|
||||||
irc.error(msg, 'the format of the page was odd.')
|
irc.error(msg, 'The format of the page was odd.')
|
||||||
|
|
||||||
_tempregex = re.compile('CLASS=obsTempTextA>(\d+)°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.')
|
|
||||||
|
|
||||||
_geekquotere = re.compile('<p class="qt">(.*?)</p>')
|
_geekquotere = re.compile('<p class="qt">(.*?)</p>')
|
||||||
def geekquote(self, irc, msg, args):
|
def geekquote(self, irc, msg, args):
|
||||||
|
@ -74,6 +74,11 @@ class HttpTest(PluginTestCase):
|
|||||||
def testNetcraft(self):
|
def testNetcraft(self):
|
||||||
self.assertNotError('netcraft slashdot.org')
|
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:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user