]> jfr.im git - erebus.git/blob - modules/weather.py
weather - workaround for python rfc822.parsedate silliness
[erebus.git] / modules / weather.py
1 # Erebus IRC bot - Author: Erebus Team
2 # weather module
3 # This file is released into the public domain; see http://unlicense.org/
4
5 # module info
6 modinfo = {
7 'author': 'Erebus Team',
8 'license': 'public domain',
9 'compatible': [0],
10 'depends': ['userinfo'],
11 'softdeps': ['help'],
12 }
13
14 # preamble
15 import modlib
16 lib = modlib.modlib(__name__)
17 modstart = lib.modstart
18 modstop = lib.modstop
19
20 # module code
21 import json, urllib, time, rfc822
22
23 def location(person, default=None): return lib.mod('userinfo')._get(person, 'location', default=None)
24
25 def _dayofweek(dayname):
26 return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower())
27
28 def _weather(place):
29 if place is not None:
30 weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)))
31 if lib.parent.cfg.getboolean('debug', 'weather'):
32 lib.parent.log('*', "?", repr(weather))
33 if 'response' in weather:
34 if 'error' in weather['response']:
35 return "Error from Wunderground: %s" % (weather['response']['error']['description'])
36 if 'results' in weather['response']:
37 return "That search term is ambiguous. Please be more specific."
38
39 current = weather['current_observation']
40 measuredat = list(rfc822.parsedate(current['observation_time_rfc822']))
41 measuredat[6] = _dayofweek(current['observation_time_rfc822'][0:3])
42 measuredatTZ = current['local_tz_short']
43 loc = current['observation_location']
44 if loc['city'] == "" or loc['state'] == "": loc = current['display_location']
45 return u"Weather in %(location)s: As of %(time)s %(tz)s, %(conditions)s, %(cel)s\u00B0C (%(far)s\u00B0F) (feels like %(flcel)s\u00B0C (%(flfar)s\u00B0F)). Wind %(wind)s. %(link)s" % {
46 'location': loc['full'],
47 'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ,
48 'conditions': current['weather'],
49 'cel': current['temp_c'], 'far': current['temp_f'],
50 'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'],
51 'wind': current['wind_string'],
52 'link': current['forecast_url'],
53 }
54 else:
55 return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (lib.parent.trigger)
56
57 @lib.hook(('weather','w'), needchan=False, wantchan=True)
58 @lib.help('[<location>]', 'show weather for your location')
59 def weather(bot, user, chan, realtarget, *args):
60 if chan is None:
61 chan = user
62 if len(args) == 0:
63 place = location(user)
64 else:
65 place = ' '.join(args)
66 bot.msg(chan, _weather(place))
67
68 @lib.hook(('weatheruser','wu'))
69 @lib.help('<user>', 'show weather for <user>\'s location')
70 def wu(bot, user, chan, realtarget, *args):
71 bot.msg(chan, _weather(location(' '.join(args))))