X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/cdd662d80c867821a9f31da59f00da6ef534f01e..71ef8273778252519ee9a14a37017a7b233ffab6:/modules/weather.py diff --git a/modules/weather.py b/modules/weather.py index 0a3ce3b..27fc901 100644 --- a/modules/weather.py +++ b/modules/weather.py @@ -18,10 +18,14 @@ modstart = lib.modstart modstop = lib.modstop # module code -import json, urllib, time, rfc822 +import json, urllib, time +from email.utils import parsedate 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 _weather(place): if place is not None: weather = json.load(urllib.urlopen('http://api.wunderground.com/api/8670e6d2e69ff3c7/conditions/q/%s.json' % (place))) @@ -34,10 +38,11 @@ def _weather(place): return "That search term is ambiguous. Please be more specific." current = weather['current_observation'] - measuredat = rfc822.parsedate(current['observation_time_rfc822']) # parsedate_tz returns a 10-tuple which strftime DOESN'T ACCEPT + measuredat = list(parsedate(current['observation_time_rfc822'])) + measuredat[6] = _dayofweek(current['observation_time_rfc822'][0:3]) measuredatTZ = current['local_tz_short'] loc = current['observation_location'] - if loc['city'] == "": loc = current['display_location'] + if loc['city'] == "" or loc['state'] == "": loc = current['display_location'] return 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': loc['full'], 'time': time.strftime("%a %H:%M", measuredat), 'tz': measuredatTZ, @@ -48,7 +53,7 @@ def _weather(place): 'link': current['forecast_url'], } else: - return "I don't know where to look! Try %sSETINFO LOCATION " % (bot.parent.trigger) + return "I don't know where to look! Try %sSETINFO LOCATION " % (lib.parent.trigger) @lib.hook(('weather','w'), needchan=False, wantchan=True) @lib.help('[]', 'show weather for your location')