Weather: rewrite autocomplete code to support multiple results internally

This commit is contained in:
James Lu 2017-03-09 18:53:00 -08:00
parent a8ebadc82e
commit ba1eb6474b

View File

@ -297,12 +297,12 @@ class Weather(callbacks.Plugin):
# WUNDERGROUND API CALLS # # WUNDERGROUND API CALLS #
########################## ##########################
def _wuac(self, irc, q): def _wuac(self, q):
"""Internal helper to find a location via Wunderground's autocomplete API.""" """Internal helper to find locations via Wunderground's autocomplete API."""
if q.startswith('zmw:'): if q.startswith('zmw:'):
# If it's a raw Wunderground ZMW code, return it. # If it's a raw Wunderground ZMW code, return it.
return q return [q]
url = 'http://autocomplete.wunderground.com/aq?query=%s' % utils.web.urlquote(q) url = 'http://autocomplete.wunderground.com/aq?query=%s' % utils.web.urlquote(q)
self.log.debug("Weather: Autocomplete URL: %s", url) self.log.debug("Weather: Autocomplete URL: %s", url)
@ -311,16 +311,8 @@ class Weather(callbacks.Plugin):
except utils.web.Error as e: except utils.web.Error as e:
irc.error("Failed to load location data for %r." % q, Raise=True) irc.error("Failed to load location data for %r." % q, Raise=True)
data = json.loads(page.decode('utf-8')) data = json.loads(page.decode('utf-8'))
loc = ''
for item in data['RESULTS']: return ["zmw:%s" % item['zmw'] for item in data['RESULTS'] if item['tz'] != 'MISSING']
# 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): def _wunderjson(self, url, location):
"""Fetch wunderground JSON and return.""" """Fetch wunderground JSON and return."""
@ -389,16 +381,23 @@ class Weather(callbacks.Plugin):
if usersetting: if usersetting:
for (k, v) in usersetting.items(): for (k, v) in usersetting.items():
args[k] = v args[k] = v
loc = usersetting["location"] # Prefer the location given in the command, falling back to the one stored in the DB if not given.
location = location or usersetting["location"]
args['imperial'] = (not usersetting["metric"]) args['imperial'] = (not usersetting["metric"])
else: # If both command line and DB locations aren't given, bail.
if not location: # location was also not specified, so we must bail. if not location:
if nick != msg.nick: if nick != msg.nick:
irc.error("I did not find a preset location for %s." % nick, Raise=True) irc.error("I did not find a preset location for %s." % nick, Raise=True)
else: else:
irc.error("I did not find a preset location for you. Set one via 'setweather <location>'.", Raise=True) irc.error("I did not find a preset location for you. Set one via 'setweather <location>'.", Raise=True)
loc = self._wuac(location)
if not loc:
irc.error("Failed to find a valid location for: %r" % location, Raise=True)
else:
# Use the first location. XXX: maybe make this more configurable?
loc = loc[0]
loc = self._wuac(irc, location or loc)
url = 'http://api.wunderground.com/api/%s/' % (apikey) url = 'http://api.wunderground.com/api/%s/' % (apikey)
for check in ['alerts', 'almanac', 'astronomy']: for check in ['alerts', 'almanac', 'astronomy']:
if args[check]: if args[check]: