]> jfr.im git - erebus.git/commitdiff
weather module
authorzonidjan <redacted>
Wed, 11 Oct 2017 01:34:27 +0000 (20:34 -0500)
committerzonidjan <redacted>
Wed, 11 Oct 2017 01:34:27 +0000 (20:34 -0500)
modules/weather.py [new file with mode: 0644]

diff --git a/modules/weather.py b/modules/weather.py
new file mode 100644 (file)
index 0000000..5973350
--- /dev/null
@@ -0,0 +1,53 @@
+# Erebus IRC bot - Author: Erebus Team
+# weather module
+# This file is released into the public domain; see http://unlicense.org/
+
+# module info
+modinfo = {
+       'author': 'Erebus Team',
+       'license': 'public domain',
+       'compatible': [2],
+       'depends': ['userinfo'],
+       'softdeps': ['help'],
+}
+
+# preamble
+import modlib
+lib = modlib.modlib(__name__)
+modstart = lib.modstart
+modstop = lib.modstop
+
+# module code
+import json, urllib, time, rfc822
+
+def location(person, default=None): return lib.mod('userinfo')._get(person, 'location', default=None)
+
+@lib.hook(needchan=False)
+@lib.help('[<location>]', 'show weather for your location')
+def weather(bot, user, chan, realtarget, *args):
+       if len(args) == 0:
+               place = location(user)
+       else:
+               place = ' '.join(args)
+
+       if place is not None:
+               weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place)))
+               if 'error' in weather:
+                       bot.msg(chan, "Error from Wunderground: %s" % (weather['error']['description']))
+                       return
+
+               current = weather['current_observation']
+               measuredat = rfc822.parsedate_tz(current['observation_time_rfc822'])[:-1] # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT
+               measuredatTZ = current['local_tz_short']
+               output = u"Weather in %(location)s: As of %(time)s %(tz)s, %(conditions)s, %(cel)s\u00B0C (%(far)s\u00B0F) (feels like %(flcel)s\u00B0C (%(flfar)s\u00B0F)). Wind %(wind)s. %(link)s" % {
+                       'location': current['observation_location']['full'],
+                       'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ,
+                       'conditions': current['weather'],
+                       'cel': current['temp_c'], 'far': current['temp_f'],
+                       'flcel': current['feelslike_c'], 'flfar': current['feelslike_f'],
+                       'wind': current['wind_string'],
+                       'link': current['forecast_url'],
+               }
+               bot.msg(chan, output)
+       else:
+               bot.msg(chan, "I don't know where to look!")