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