]>
Commit | Line | Data |
---|---|---|
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 | |
23 | import sys | |
24 | import re | |
25 | ||
26 | if sys.version_info.major < 3: | |
27 | from urllib import urlopen, quote_plus | |
28 | else: | |
29 | from urllib.request import urlopen | |
30 | from urllib.parse import quote_plus | |
31 | ||
32 | def location(person, default=None): return lib.mod('userinfo').get(person, 'location', default=None) | |
33 | ||
34 | def _dayofweek(dayname): | |
35 | return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower()) | |
36 | ||
37 | def _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 | ||
45 | def _c2f(celsius): | |
46 | return round(celsius * 9.0/5 + 32, 2) | |
47 | ||
48 | def _kmh2mph(kmh): | |
49 | return round(kmh / 1.60934, 2) | |
50 | ||
51 | def _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 | url = 'http://api.weatherstack.com/current?access_key=%s&query=%s' % (lib.parent.cfg.get('weatherstack_weather', 'key'), quote_plus(place)) | |
57 | if sys.version_info.major < 3: | |
58 | url = url.encode('utf8') | |
59 | weather = json.load(urlopen(url)) | |
60 | if lib.parent.cfg.getboolean('debug', 'weather'): | |
61 | lib.parent.log('*', "?", repr(weather)) | |
62 | if 'error' in weather: | |
63 | return "Error from WeatherStack: (%d) %s" % (weather['error']['code'], weather['error']['info']) | |
64 | ||
65 | 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." % { | |
66 | 'location': weather['location']['name'], | |
67 | 'region': weather['location']['region'], | |
68 | 'country': weather['location']['country'], | |
69 | 'time': _time_adjust(weather), | |
70 | 'conditions': ', '.join(weather['current']['weather_descriptions']), | |
71 | 'cel': weather['current']['temperature'], 'far': _c2f(weather['current']['temperature']), | |
72 | 'flcel': weather['current']['feelslike'], 'flfar': _c2f(weather['current']['feelslike']), | |
73 | 'windk': weather['current']['wind_speed'], 'windm': _kmh2mph(weather['current']['wind_speed']), | |
74 | 'winddir': weather['current']['wind_dir'], | |
75 | } | |
76 | else: | |
77 | return "I don't know where to look! Try %ssetinfo location <your location>" % (lib.parent.trigger,) | |
78 | ||
79 | @lib.hook(('weather','w'), needchan=False, wantchan=True) | |
80 | @lib.help('[<location>]', 'show weather for your location') | |
81 | def weather(bot, user, chan, realtarget, *args): | |
82 | if chan is None: | |
83 | chan = user | |
84 | if len(args) == 0: | |
85 | place = location(user) | |
86 | else: | |
87 | place = ' '.join(args) | |
88 | bot.msg(chan, _weather(place)) | |
89 | ||
90 | @lib.hook(('weatheruser','wu')) | |
91 | @lib.help('<user>', 'show weather for <user>\'s location') | |
92 | def wu(bot, user, chan, realtarget, *args): | |
93 | if len(args) == 0: | |
94 | u = user | |
95 | else: | |
96 | u = ' '.join(args) | |
97 | bot.msg(chan, _weather(location(u))) |