From 2cb0e38703ca05a176e70cc34c86cafbe5aeedde Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 25 Mar 2015 20:28:20 -0700 Subject: [PATCH 1/2] Weather: bugfixes, refactor _wuac() --- plugin.py | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/plugin.py b/plugin.py index d362105..214d354 100644 --- a/plugin.py +++ b/plugin.py @@ -292,31 +292,29 @@ class Weather(callbacks.Plugin): # WUNDERGROUND API CALLS # ########################## - def _wuac(self, q): + def _wuac(self, irc, q): """Internal helper to find a location via Wunderground's autocomplete API.""" url = 'http://autocomplete.wunderground.com/aq?query=%s' % utils.web.urlquote(q) - self.log.debug("Autocomplete URL: %s", url) + self.log.debug("Weather: Autocomplete URL: %s", url) try: page = utils.web.getUrl(url) - except Exception as e: - self.log.info("Weather: (_wuac) Error trying to open {0} message: {1}".format(url, e)) - return None - try: - data = json.loads(page.decode('utf-8')) - # ZMW is in some ways a lot like Wunderground's location ID Codes, for when locations - # are too ambiguous. (e.g. looking up "France", which is a country with many different - # locations!) - for item in data['RESULTS']: - # Sometimes the autocomplete will lead us to more disambiguation pages... - # which cause lots of errors in processing! - if item['tz'] != 'MISSING': - loc = "zmw:%s" % item['zmw'] - break - return loc - except Exception as e: - self.log.info("Weather: (_wuac) Error processing JSON in {0} :: {1}".format(url, e)) - return None + except utils.web.Error as e: + irc.error("Failed to load location data for %r." % q, Raise=True) + data = json.loads(page.decode('utf-8')) + loc = '' + # ZMW is in some ways a lot like Wunderground's location ID Codes, for when locations + # are too ambiguous. (e.g. looking up "France", which is a country with many different + # locations!) + for item in data['RESULTS']: + # Sometimes the autocomplete will lead us to more disambiguation pages... + # which cause lots of errors in processing! + if item['tz'] != 'MISSING': + loc = "zmw:%s" % item['zmw'] + break + else: + irc.error("Failed to find a valid location for: %r" % q, Raise=True) + return loc def _wunderjson(self, url, location): """Fetch wunderground JSON and return.""" @@ -383,15 +381,7 @@ class Weather(callbacks.Plugin): if not optinput: # location was also not specified, so we must bail. irc.error("I did not find a preset location for you. Set one via 'setweather '.", Raise=True) - if optinput: - wloc = self._wuac(optinput) - if not wloc: - irc.error("I could not find a valid location for: {0}".format(optinput), Raise=True) - elif loc: # user is known. location is set. no optinput. - wloc = loc - else: # no optinput. no location. error out. this should happen above but lets be redundant. - irc.error("You must specify a city to search for weather.", Raise=True) - + loc = self._wuac(irc, optinput or loc) url = 'http://api.wunderground.com/api/%s/' % (apikey) for check in ['alerts', 'almanac', 'astronomy']: if args[check]: @@ -403,7 +393,7 @@ class Weather(callbacks.Plugin): if key in ("lang", "bestfct", "pws"): # rest added with key:value url += "{0}:{1}/".format(key, value) - page = self._wunderjson(url, wloc) + page = self._wunderjson(url, loc) try: data = json.loads(page.decode('utf-8')) except Exception as e: From ae4df05926e04a6cc77186220e63130370ef7897 Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 25 Mar 2015 20:52:25 -0700 Subject: [PATCH 2/2] Weather: don't limit alerts output to 300 characters (it will be handled automatically via mores) From upstream commit reticulatingspline/Weather@5ed7db7a8e. --- plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.py b/plugin.py index 214d354..d9396c7 100644 --- a/plugin.py +++ b/plugin.py @@ -491,7 +491,7 @@ class Weather(callbacks.Plugin): if args['alerts']: # only look for alerts if there. if data['alerts']: # alerts is a list. it can also be empty. outdata['alerts'] = data['alerts'][0]['message'] # need to do some formatting below. - outdata['alerts'] = outdata['alerts'].replace('\n', ' ')[:300] # \n->' ' and max 300 chars. + outdata['alerts'] = outdata['alerts'].replace('\n', ' ') outdata['alerts'] = utils.str.normalizeWhitespace(outdata['alerts']) # fix pesky double whitespacing. else: # no alerts found (empty). outdata['alerts'] = "No alerts."