]> jfr.im git - erebus.git/commitdiff
add WeatherStack weather module
authorzonidjan <redacted>
Mon, 8 Mar 2021 03:38:19 +0000 (21:38 -0600)
committerzonidjan <redacted>
Mon, 8 Mar 2021 03:38:28 +0000 (21:38 -0600)
needs a bot.config entry like:

[weatherstack_weather]
key = *your api key*

modules/weatherstack_weather.py [new file with mode: 0644]

diff --git a/modules/weatherstack_weather.py b/modules/weatherstack_weather.py
new file mode 100644 (file)
index 0000000..a79ff78
--- /dev/null
@@ -0,0 +1,94 @@
+# Erebus IRC bot - Author: Erebus Team
+# vim: fileencoding=utf-8
+# weather module
+# This file is released into the public domain; see http://unlicense.org/
+
+# module info
+modinfo = {
+       'author': 'Erebus Team',
+       'license': 'public domain',
+       'compatible': [0],
+       'depends': ['userinfo'],
+       'softdeps': ['help'],
+}
+
+# preamble
+import modlib
+lib = modlib.modlib(__name__)
+modstart = lib.modstart
+modstop = lib.modstop
+
+# module code
+import json
+import sys
+import re
+
+if sys.version_info.major < 3:
+       from urllib import urlopen, quote_plus
+else:
+       from urllib.request import urlopen
+       from urllib.parse import quote_plus
+
+def location(person, default=None): return lib.mod('userinfo').get(person, 'location', default=None)
+
+def _dayofweek(dayname):
+       return ['mon','tue','wed','thu','fri','sat','sun'].index(dayname.lower())
+
+def _time_adjust(d):
+       t = d['current']['observation_time']
+       #XXX
+       #mo = re.match(r"(\d\d):(\d\d) (AM|PM)", t)
+       #if mo:
+       #       return 
+       return t + ' UTC'
+
+def _c2f(celsius):
+       return round(celsius * 9.0/5 + 32, 2)
+
+def _kmh2mph(kmh):
+       return round(kmh / 1.60934, 2)
+
+def _weather(place):
+       if not lib.parent.cfg.get('weatherstack_weather', 'key'):
+               return "Weather is not enabled - please set the API key in the config file"
+
+       if place is not None:
+               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')))
+               if lib.parent.cfg.getboolean('debug', 'weather'):
+                       lib.parent.log('*', "?", repr(weather))
+               if 'error' in weather:
+                       return "Error from WeatherStack: (%d) %s" % (weather['error']['code'], weather['error']['info'])
+
+               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." % {
+                       'location': weather['location']['name'],
+                       'region': weather['location']['region'],
+                       'country': weather['location']['country'],
+                       'time': _time_adjust(weather),
+                       'conditions': ', '.join(weather['current']['weather_descriptions']),
+                       'cel': weather['current']['temperature'], 'far': _c2f(weather['current']['temperature']),
+                       'flcel': weather['current']['feelslike'], 'flfar': _c2f(weather['current']['feelslike']),
+                       'windk': weather['current']['wind_speed'], 'windm': _kmh2mph(weather['current']['wind_speed']),
+                       'winddir': weather['current']['wind_dir'],
+               }
+       else:
+               return "I don't know where to look! Try %ssetinfo location <your location>" % (lib.parent.trigger,)
+
+@lib.hook(('weather','w'), needchan=False, wantchan=True)
+@lib.help('[<location>]', 'show weather for your location')
+def weather(bot, user, chan, realtarget, *args):
+       if chan is None:
+               chan = user
+       if len(args) == 0:
+               place = location(user)
+       else:
+               place = ' '.join(args)
+       bot.msg(chan, _weather(place))
+
+@lib.hook(('weatheruser','wu'))
+@lib.help('<user>', 'show weather for <user>\'s location')
+def wu(bot, user, chan, realtarget, *args):
+       if len(args) == 0:
+               u = user
+       else:
+               u = ' '.join(args)
+       bot.msg(chan, _weather(location(u)))