-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.py
487 lines (457 loc) · 20.8 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
###
# Copyright (c) 2005,2009, James Vega
# 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.
###
import re
# Specifically use our local copy since later versions changed their interface
# and (depending on the version) don't work as well
from local import BeautifulSoup
import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
try:
feedparser = utils.python.universalImport('feedparser', 'local.feedparser')
except ImportError:
raise callbacks.Error, \
'You need the feedparser module installed to use this plugin. ' \
'Download the module at <http://www.feedparser.org/>.'
simplejson = None
try:
import json as simplejson
except ImportError:
pass
try:
# The 3rd party simplejson module was included in Python 2.6 and renamed to
# json. Unfortunately, this conflicts with the 3rd party json module.
# Luckily, the 3rd party json module has a different interface so we test
# to make sure we aren't using it.
if simplejson is None or hasattr(simplejson, 'read'):
simplejson = utils.python.universalImport('simplejson',
'local.simplejson')
except ImportError:
raise callbacks.Error, \
'You need Python2.6 or the simplejson module installed to use ' \
'this plugin. Download the module at ' \
'<http://undefined.org/python/#simplejson>.'
unitAbbrevs = utils.abbrev(['Fahrenheit', 'Celsius', 'Centigrade', 'Kelvin'])
unitAbbrevs['C'] = 'Celsius'
unitAbbrevs['Ce'] = 'Celsius'
noLocationError = 'No such location could be found.'
class NoLocation(callbacks.Error):
pass
class Weather(callbacks.Plugin):
weatherCommands = ('wunder', 'wunder rss', 'cnn', 'ham')
threaded = True
def callCommand(self, method, irc, msg, *args, **kwargs):
try:
super(Weather, self).callCommand(method, irc, msg, *args, **kwargs)
except utils.web.Error, e:
irc.error(str(e))
def _noLocation():
raise NoLocation, noLocationError
_noLocation = staticmethod(_noLocation)
def weather(self, irc, msg, args, location):
"""<US zip code | US/Canada city, state | Foreign city, country>
Returns the approximate weather conditions for a given city.
"""
channel = None
if irc.isChannel(msg.args[0]):
channel = msg.args[0]
if not location:
location = self.userValue('lastLocation', msg.prefix)
if not location:
raise callbacks.ArgumentError
self.setUserValue('lastLocation', msg.prefix,
location, ignoreNoUser=True)
args = [location]
commandName = self.registryValue('command', channel)
firstCommand = commandName
command = self.getCommandMethod(commandName.split())
try:
command(irc, msg, args[:])
except (NoLocation, utils.web.Error):
self.log.info('%s lookup failed, Trying others.', firstCommand)
for commandName in self.weatherCommands:
if commandName != firstCommand:
self.log.info('Trying %s.', commandName)
try:
command = self.getCommandMethod(commandName.split())
command(irc, msg, args[:])
self.log.info('%s lookup succeeded.', commandName)
return
except NoLocation:
self.log.info('%s lookup failed as backup.',
commandName)
irc.error(format('Could not retrieve weather for %q.', location))
weather = wrap(weather, [additional('text')])
def _toCelsius(temp, unit):
if unit == 'K':
return temp - 273.15
elif unit == 'F':
return (temp - 32) * 5 /9
else:
return temp
_toCelsius = staticmethod(_toCelsius)
_temp = re.compile(r'(-?\d+)(.*?)(F|C)')
def _getTemp(temp, deg, unit, chan):
assert unit == unit.upper()
assert temp == float(temp)
default = conf.get(conf.supybot.plugins.Weather.temperatureUnit, chan)
convert = conf.get(conf.supybot.plugins.Weather.convert, chan)
# Short circuit if we're the same unit as the default or no conversion
# has been requested
if unitAbbrevs[unit] == default or not convert:
return format('%0.1f%s%s', temp, deg, unit)
temp = Weather._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 '%0.1f%s%s' % (temp, deg, unit)
_getTemp = staticmethod(_getTemp)
_hamLoc = re.compile(
r'<span class="Place">([^,]+), ([^,\n]+),(.*?)</span>', re.I)
_interregex = re.compile(
r'<span class="Place">([^,]+), ([^,\n]+?)</span>', re.I)
_hamCond = re.compile(
r'<td width="100%" colspan="2" align="center" class="Wx">([^<]+)</td>',
re.I)
_hamTemp = re.compile(
r'<td valign="top" align="right" class="Temp">(-?\d+)(.*?)(F|C)</td>',
re.I)
_hamChill = re.compile(
r'Wind Chill:</td>\s+<td align="right" class="Value">([^N][^<]+)</td>',
re.I | re.S)
_hamHeat = re.compile(
r'Heat Index:</td>\s+<td align="right" class="Value">([^N][^<]+)</td>',
re.I | re.S)
_hamMultiLoc = re.compile(
r'Select from one of[^<]+</b></font></td></tr>\s*<tr><td><font[^>]+>'
r'\s*<a href="(/cgi-bin/hw3[^"]+)">', re.I | re.S)
def ham(self, irc, msg, args, loc):
"""<US zip code | US/Canada city, state | Foreign city, country>
Returns the approximate weather conditions for a given city.
"""
url = 'http://www.hamweather.net/cgi-bin/hw3/hw3.cgi?' \
'config=&forecast=zandh&pands=%s&Submit=GO' % \
utils.web.urlquote(loc.lower())
html = utils.web.getUrl(url)
if 'was not found' in html:
self._noLocation()
# ham seems to automatically return a location for duplicate names with
# no list of other possibilities anymore, so this code may not be
# needed
if 'Multiple Locations for' in html:
m = self._hamMultiLoc.search(html)
if m:
url = 'http://www.hamweather.net/%s' % m.group(1)
html = utils.web.getUrl(url)
else:
self._noLocation()
headData = self._hamLoc.search(html)
if headData is not None:
(city, state, country) = headData.groups()
else:
headData = self._interregex.search(html)
if headData:
(city, state) = headData.groups()
else:
self._noLocation()
city = utils.web.htmlToText(city.strip())
state = utils.web.htmlToText(state.strip())
temp = self._hamTemp.search(html)
if temp is not None:
(temp, deg, unit) = temp.groups()
deg = utils.web.htmlToText(deg)
temp = self._getTemp(float(temp), deg, unit, msg.args[0])
conds = self._hamCond.search(html)
if conds is not None:
conds = conds.group(1)
index = ''
chill = self._hamChill.search(html)
if chill is not None:
chill = chill.group(1)
chill = utils.web.htmlToText(chill)
tempsplit = self._temp.search(chill)
if tempsplit:
(chill, deg, unit) = tempsplit.groups()
chill = self._getTemp(float(chill), deg, unit,msg.args[0])
if float(chill[:-2]) < float(temp[:-2]):
index = format(' (Wind Chill: %s)', chill)
heat = self._hamHeat.search(html)
if heat is not None:
heat = heat.group(1)
heat = utils.web.htmlToText(heat)
tempsplit = self._temp.search(heat)
if tempsplit:
(heat, deg, unit) = tempsplit.groups()
heat= self._getTemp(float(heat), deg, unit,msg.args[0])
if float(heat[:-2]) > float(temp[:-2]):
index = format(' (Heat Index: %s)', heat)
if temp and conds and city and state:
conds = conds.replace('Tsra', 'Thunderstorms')
conds = conds.replace('Ts', 'Thunderstorms')
s = format('The current temperature in %s, %s is %s%s. '
'Conditions: %s.',
city, state, temp, index, conds)
irc.reply(s.decode('latin1').encode('utf-8'))
else:
irc.errorPossibleBug('The format of the page was odd.')
ham = wrap(ham, ['text'])
_cnnSearchUrl = 'http://weather.cnn.com/weather/citySearch?' \
'search_term=%s&mode=json&filter=true'
_cnnUrl='http://weather.cnn.com/weather/forecast.jsp?locCode=%s&zipCode=%s'
_cnnFTemp = re.compile(r'<div class="cnnWeatherTempCurrent">' \
r'(-?\d+)(°)</div>',
re.I | re.S)
_cnnCond = re.compile(r'<span class="cnnWeatherConditionCurrent">' \
r'([^<]+)</span>',
re.I | re.S)
_cnnHumid = re.compile(r'Humidity: </b>(\d+%)', re.I | re.S)
_cnnWind = re.compile(r'Wind: </b>([^<\n\r]+)', re.I | re.S)
# 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'}
def cnn(self, irc, msg, args, loc):
"""<US zip code | US/Canada city, state | Foreign city, country>
Returns the approximate weather conditions for a given city.
"""
if ' ' in loc:
#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'].
loc = utils.str.rsplit(loc, None, 1)
state = loc.pop().lower()
city = ' '.join(loc)
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.
loc = loc.replace(',', '')
url = self._cnnSearchUrl % (utils.web.urlquote(loc))
json = simplejson.loads(utils.web.getUrl(url))
if not json:
self._noLocation()
json = json[0]
url = self._cnnUrl % (json['locCode'], json['zip'])
text = utils.web.getUrl(url)
location = ', '.join([json['city'], json['stateOrCountry']])
temp = self._cnnFTemp.search(text)
conds = self._cnnCond.search(text)
humidity = self._cnnHumid.search(text)
wind = self._cnnWind.search(text)
if location and temp:
(temp, deg) = temp.groups()
unit = 'F'
temp = self._getTemp(float(temp), deg, unit, msg.args[0])
resp = [format('The current temperature in %s is %s.',
location, temp)]
if conds is not None:
resp.append(format('Conditions: %s.', conds.group(1)))
if humidity is not None:
resp.append(format('Humidity: %s.', humidity.group(1)))
if wind is not None:
resp.append(format('Wind: %s.', wind.group(1)))
resp = map(utils.web.htmlToText, resp)
irc.reply(' '.join(resp))
else:
irc.errorPossibleBug('Could not find weather information.')
cnn = wrap(cnn, ['text'])
class wunder(callbacks.Commands):
_backupUrl = re.compile(r'<a href="(/global/stations[^"]+)">')
_wunderUrl = 'http://mobile.wunderground.com/cgi-bin/' \
'findweather/getForecast?query='
_wunderSevere = re.compile(r'font color="?#ff0000"?>([^<]+)<', re.I)
_wunderMultiLoc = re.compile(r'<a href="([^"]+)', re.I | re.S)
def wunder(self, irc, msg, args, loc):
"""<US zip code | US/Canada city, state | Foreign city, country>
Returns the approximate weather conditions for a given city.
"""
url = '%s%s' % (self._wunderUrl, utils.web.urlquote(loc))
text = utils.web.getUrl(url)
if 'Search not found' in text or \
re.search(r'size="2"> Place </font>', text, re.I):
Weather._noLocation()
if 'Place: Temperature' in text:
m = self._backupUrl.search(text)
if m is not None:
url = 'http://mobile.wunderground.com' + m.group(1)
text = utils.web.getUrl(url)
severe = ''
m = self._wunderSevere.search(text)
if m:
severe = ircutils.bold(format(' %s', m.group(1)))
text = self._formatSymbols(text)
soup = BeautifulSoup.BeautifulSoup()
soup.feed(text)
# Get the table with all the weather info
table = soup.first('table', {'border':'1'})
if not table:
Weather._noLocation()
trs = table.fetch('tr')
(time, location) = trs.pop(0).fetch('b')
time = time.string
location = location.string
info = {}
def isText(t):
return not isinstance(t, BeautifulSoup.NavigableText) \
and t.contents
def getText(t):
s = t.string
if s is BeautifulSoup.Null:
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.web.htmlToText(units)
s = ' '.join((num, units))
else:
units = utils.web.htmlToText(units)
s = ' '.join((num, units[0], units[1:]))
return s
for tr in trs:
k = tr.td.string
v = filter(isText, tr.fetch('td')[1].contents)
value = map(getText, v)
info[k] = ' '.join(value)
temp = info['Temperature']
if location and temp:
(temp, deg, unit) = temp.split()[3:] # We only want temp format
temp = Weather._getTemp(float(temp), deg, unit, msg.args[0])
resp = ['The current temperature in %s is %s (%s).' %\
(location, temp, time)]
conds = info['Conditions']
resp.append('Conditions: %s.' % info['Conditions'])
humidity = info['Humidity']
resp.append('Humidity: %s.' % info['Humidity'])
# Apparently, the "Dew Point" and "Wind" categories are
# occasionally set to "-" instead of an actual reading. So,
# we'll just catch the ValueError from trying to unpack a tuple
# of the wrong size.
try:
(dew, deg, unit) = info['Dew Point'].split()[3:]
dew = Weather._getTemp(float(dew), deg, unit, msg.args[0])
resp.append('Dew Point: %s.' % dew)
except (ValueError, KeyError):
pass
try:
wind = 'Wind: %s at %s %s.' % tuple(info['Wind'].split())
resp.append(wind)
except (ValueError, TypeError):
pass
try:
(chill, deg, unit) = info['Windchill'].split()[3:]
chill = Weather._getTemp(float(chill), deg,
unit, msg.args[0])
resp.append('Windchill: %s.' % chill)
except (ValueError, KeyError):
pass
if info['Pressure']:
resp.append('Pressure: %s.' % info['Pressure'])
resp.append(severe)
resp = map(utils.web.htmlToText, resp)
irc.reply(' '.join(resp).decode('latin1').encode('utf-8'))
else:
Weather._noLocation()
wunder = wrap(wunder, ['text'])
_rsswunderUrl = 'http://www.wunderground.com/cgi-bin/findweather/' \
'getForecast?query=%s'
_rsswunderfeed = re.compile(
r'<link rel="alternate".*href="([^"]+)" */?>', re.I)
_rsswunderSevere = re.compile(
r'font color="?#ff0000"?><b>([^<]+)<', re.I)
_rsswunderLocation = re.compile(
r'<title>(?:(.*) Weather from Weather Underground|'
r'Weather Underground - (.*))</title>', re.I)
_rsswunderForecastDate = re.compile(r'Forecast for (.*) as of', re.I)
def rss(self, irc, msg, args, loc):
"""<US zip code | US/Canada city, state | Foreign city, country>
Returns the approximate weather conditions for a given city.
"""
url = self._rsswunderUrl % utils.web.urlquote(loc)
url = url.replace('%20', '+')
text = utils.web.getUrl(url)
if 'Search not found' in text or \
re.search(r'size="2"> Place </font>', text, re.I):
Weather._noLocation()
if 'Search Results' in text:
m = self._backupUrl.search(text)
if m is not None:
url = 'http://www.wunderground.com' + m.group(1)
text = utils.web.getUrl(url)
else:
Weather._noLocation()
self._rss(irc, text)
rss = wrap(rss, ['text'])
def _rss(self, irc, text):
severe = None
m = self._rsswunderSevere.search(text)
if m:
severe = ircutils.bold(m.group(1))
feed = self._rsswunderfeed.search(text)
if not feed:
Weather._noLocation()
feed = feed.group(1)
rss = utils.web.getUrl(feed)
rss = self._formatSymbols(rss)
rss = rss.replace(":", ": ")
rss = rss.replace(": ", ": ")
resp = []
location = self._rsswunderLocation.search(rss)
if location is not None:
title = filter(None, location.groups())
if title:
resp.append('Weather for %s' % title[0])
info = feedparser.parse(rss)
for e in info['entries']:
d = self._rsswunderForecastDate.search(e['title'])
if d is not None:
resp.append(d.group(1) + ' - Conditions: ' + e['summary'])
else:
resp.append(e['summary'])
resp = [s.encode('utf-8').rstrip('.') for s in resp]
if severe is not None:
resp.append(severe)
irc.reply(utils.web.htmlToText('; '.join(resp)))
def _formatSymbols(self, text):
text = text.replace("&", "&")
text = text.replace("°", "°")
text = text.replace(" ° ", "°")
text = text.replace("°", "\xb0")
return text
Class = Weather
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: