From e0d31887197c8e9438fcac26ed77ec206dbeb54b Mon Sep 17 00:00:00 2001 From: James Lu Date: Thu, 9 Mar 2017 19:40:03 -0800 Subject: [PATCH] Weather: add a locationsearch command to look up raw zmw codes --- Weather/plugin.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/Weather/plugin.py b/Weather/plugin.py index 1d63e67..46f6cc5 100644 --- a/Weather/plugin.py +++ b/Weather/plugin.py @@ -297,7 +297,7 @@ class Weather(callbacks.Plugin): # WUNDERGROUND API CALLS # ########################## - def _wuac(self, q): + def _wuac(self, q, returnFirstResult=True): """Internal helper to find locations via Wunderground's autocomplete API.""" if q.startswith('zmw:'): @@ -312,7 +312,16 @@ class Weather(callbacks.Plugin): irc.error("Failed to load location data for %r." % q, Raise=True) data = json.loads(page.decode('utf-8')) - return ["zmw:%s" % item['zmw'] for item in data['RESULTS'] if item['tz'] != 'MISSING'] + results = [] + for item in data['RESULTS']: + if item['tz'] != 'MISSING': + zmw = "zmw:%s" % item['zmw'] + if returnFirstResult: + return [zmw] + else: + results.append((zmw, item['name'])) + + return results def _wunderjson(self, url, location): """Fetch wunderground JSON and return.""" @@ -591,6 +600,30 @@ class Weather(callbacks.Plugin): output = "{0} {1}".format(self._bu('Forecast:'), " | ".join(outforecast)) irc.reply(output) + @wrap(['text']) + def locationsearch(self, irc, msg, args, text): + """ + + Returns a list of raw Wunderground (ZMW) codes given the search query . This can be + helpful if Wunderground's autocomplete is not picking up the right place, as you can directly + look up weather using any ZMW codes returned here. + + Warning: ZMW codes are not fixed and are thus prone to sudden changes! + """ + apikey = self.registryValue('apiKey') + if not apikey: + irc.error("No Wunderground API key was defined; set 'config plugins.Weather.apiKey'.", + Raise=True) + + results = self._wuac(text, returnFirstResult=False) + max_results = 10 # TODO: possibly make this configurable? + cut_results = results[:max_results] + # Output the list of results. + if not results: + irc.error("No results found.") + else: + irc.reply(format('%L', [': '.join(res) for res in cut_results])) + Class = Weather # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=250: