]> jfr.im git - erebus.git/blame - modules/weather.py
userinfo - remove _ from keys/has/get/set/delete
[erebus.git] / modules / weather.py
CommitLineData
2b2ae281 1# Erebus IRC bot - Author: Erebus Team
4477123d 2# vim: fileencoding=utf-8
2b2ae281 3# weather module
4# This file is released into the public domain; see http://unlicense.org/
5
6# module info
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
fa93b933 10 'compatible': [0],
2b2ae281 11 'depends': ['userinfo'],
12 'softdeps': ['help'],
13}
14
15# preamble
16import modlib
17lib = modlib.modlib(__name__)
18modstart = lib.modstart
19modstop = lib.modstop
20
21# module code
9221e6f6 22import json, time, sys
a28e2ae9 23from email.utils import parsedate
2b2ae281 24
9221e6f6 25if sys.version_info.major < 3:
26 from urllib import urlopen
27else:
28 from urllib.request import urlopen
29
aa582dd7 30def location(person, default=None): return lib.mod('userinfo').get(person, 'location', default=None)
2b2ae281 31
6e8f06d6 32def _dayofweek(dayname):
33 return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower())
34
cdd662d8 35def _weather(place):
2b2ae281 36 if place is not None:
4477123d 37 weather = json.load(urlopen(('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)).encode('utf8')))
cdd662d8 38 if lib.parent.cfg.getboolean('debug', 'weather'):
39 lib.parent.log('*', "?", repr(weather))
8b6595e1 40 if 'response' in weather:
41 if 'error' in weather['response']:
cdd662d8 42 return "Error from Wunderground: %s" % (weather['response']['error']['description'])
8b6595e1 43 if 'results' in weather['response']:
cdd662d8 44 return "That search term is ambiguous. Please be more specific."
2b2ae281 45
46 current = weather['current_observation']
b4dee2f6 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)'
6112c0cd 54 loc = current['observation_location']
cc38d6f4 55 if loc['city'] == "" or loc['state'] == "": loc = current['display_location']
4477123d 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" % {
6112c0cd 57 'location': loc['full'],
9221e6f6 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,
2b2ae281 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 }
2b2ae281 66 else:
b8fdeddf 67 return "I don't know where to look! Try %sSETINFO LOCATION <your location>" % (lib.parent.trigger,)
cdd662d8 68
69@lib.hook(('weather','w'), needchan=False, wantchan=True)
70@lib.help('[<location>]', 'show weather for your location')
71def 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')
4477123d 82@lib.argsEQ(1)
cdd662d8 83def wu(bot, user, chan, realtarget, *args):
84 bot.msg(chan, _weather(location(' '.join(args))))