]> jfr.im git - erebus.git/blame - modules/weatherstack_weather.py
add WeatherStack weather module
[erebus.git] / modules / weatherstack_weather.py
CommitLineData
4d842745 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
7modinfo = {
8 'author': 'Erebus Team',
9 'license': 'public domain',
10 'compatible': [0],
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
22import json
23import sys
24import re
25
26if sys.version_info.major < 3:
27 from urllib import urlopen, quote_plus
28else:
29 from urllib.request import urlopen
30 from urllib.parse import quote_plus
31
32def location(person, default=None): return lib.mod('userinfo').get(person, 'location', default=None)
33
34def _dayofweek(dayname):
35 return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower())
36
37def _time_adjust(d):
38 t = d['current']['observation_time']
39 #XXX
40 #mo = re.match(r"(\d\d):(\d\d) (AM|PM)", t)
41 #if mo:
42 # return
43 return t + ' UTC'
44
45def _c2f(celsius):
46 return round(celsius * 9.0/5 + 32, 2)
47
48def _kmh2mph(kmh):
49 return round(kmh / 1.60934, 2)
50
51def _weather(place):
52 if not lib.parent.cfg.get('weatherstack_weather', 'key'):
53 return "Weather is not enabled - please set the API key in the config file"
54
55 if place is not None:
56 weather = json.load(urlopen(('http://api.weatherstack.com/current?access_key=%s&query=%s' % (lib.parent.cfg.get('weatherstack_weather', 'key'), quote_plus(place))).encode('utf8')))
57 if lib.parent.cfg.getboolean('debug', 'weather'):
58 lib.parent.log('*', "?", repr(weather))
59 if 'error' in weather:
60 return "Error from WeatherStack: (%d) %s" % (weather['error']['code'], weather['error']['info'])
61
62 return u"Weather in %(location)s, %(region)s, %(country)s: As of %(time)s, %(conditions)s, %(cel)s°C (%(far)s°F) (feels like %(flcel)s°C (%(flfar)s°F)). Wind %(windk)skm/h (%(windm)smph) %(winddir)s." % {
63 'location': weather['location']['name'],
64 'region': weather['location']['region'],
65 'country': weather['location']['country'],
66 'time': _time_adjust(weather),
67 'conditions': ', '.join(weather['current']['weather_descriptions']),
68 'cel': weather['current']['temperature'], 'far': _c2f(weather['current']['temperature']),
69 'flcel': weather['current']['feelslike'], 'flfar': _c2f(weather['current']['feelslike']),
70 'windk': weather['current']['wind_speed'], 'windm': _kmh2mph(weather['current']['wind_speed']),
71 'winddir': weather['current']['wind_dir'],
72 }
73 else:
74 return "I don't know where to look! Try %ssetinfo location <your location>" % (lib.parent.trigger,)
75
76@lib.hook(('weather','w'), needchan=False, wantchan=True)
77@lib.help('[<location>]', 'show weather for your location')
78def weather(bot, user, chan, realtarget, *args):
79 if chan is None:
80 chan = user
81 if len(args) == 0:
82 place = location(user)
83 else:
84 place = ' '.join(args)
85 bot.msg(chan, _weather(place))
86
87@lib.hook(('weatheruser','wu'))
88@lib.help('<user>', 'show weather for <user>\'s location')
89def wu(bot, user, chan, realtarget, *args):
90 if len(args) == 0:
91 u = user
92 else:
93 u = ' '.join(args)
94 bot.msg(chan, _weather(location(u)))