]> jfr.im git - erebus.git/blob - modules/weather.py
weather module
[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': [2],
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 @lib.hook(needchan=False)
26 @lib.help('[<location>]', 'show weather for your location')
27 def weather(bot, user, chan, realtarget, *args):
28 if len(args) == 0:
29 place = location(user)
30 else:
31 place = ' '.join(args)
32
33 if place is not None:
34 weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)))
35 if 'error' in weather:
36 bot.msg(chan, "Error from Wunderground: %s" % (weather['error']['description']))
37 return
38
39 current = weather['current_observation']
40 measuredat = rfc822.parsedate_tz(current['observation_time_rfc822'])[:-1] # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT
41 measuredatTZ = current['local_tz_short']
42 output = 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" % {
43 'location': current['observation_location']['full'],
44 'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ,
45 'conditions': current['weather'],
46 'cel': current['temp_c'], 'far': current['temp_f'],
47 'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'],
48 'wind': current['wind_string'],
49 'link': current['forecast_url'],
50 }
51 bot.msg(chan, output)
52 else:
53 bot.msg(chan, "I don't know where to look!")