mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-05-06 11:33:41 -05:00
Move the httpget and json stuff into its own function. Create relookup for ambig locations so it returns the first result instead of matching cities.
This commit is contained in:
parent
54a79b7ab0
commit
d8d3bdd55b
48
plugin.py
48
plugin.py
@ -253,6 +253,23 @@ class Weather(callbacks.Plugin):
|
|||||||
# return.
|
# return.
|
||||||
return direction_names[index]
|
return direction_names[index]
|
||||||
|
|
||||||
|
def _wunderjson(self, url, location):
|
||||||
|
"""Fetch wunderground JSON and return."""
|
||||||
|
|
||||||
|
# first, construct the url properly.
|
||||||
|
if url.endswith('/'): # cheap way to strip the tailing /
|
||||||
|
url = '%sq/%s.json' % (url, utils.web.urlquote(location))
|
||||||
|
else:
|
||||||
|
url = '%s/q/%s.json' % (url, utils.web.urlquote(location))
|
||||||
|
# now actually fetch the url.
|
||||||
|
try:
|
||||||
|
page = utils.web.getUrl(url)
|
||||||
|
return page
|
||||||
|
except utils.web.Error as e: # something didn't work.
|
||||||
|
self.log.error("ERROR: Trying to open {0} message: {1}".format(url, e))
|
||||||
|
irc.reply("ERROR: Failed to load Weather Underground API: {0}".format(e))
|
||||||
|
return None
|
||||||
|
|
||||||
##############################################
|
##############################################
|
||||||
# PUBLIC FUNCTIONS TO WORK WITH THE DATABASE #
|
# PUBLIC FUNCTIONS TO WORK WITH THE DATABASE #
|
||||||
##############################################
|
##############################################
|
||||||
@ -415,15 +432,11 @@ class Weather(callbacks.Plugin):
|
|||||||
url += "".join([item + '/' for item in value]) # listcmp the features/
|
url += "".join([item + '/' for item in value]) # listcmp the features/
|
||||||
if key == "lang" or key == "bestfct" or key == "pws": # rest added with key:value
|
if key == "lang" or key == "bestfct" or key == "pws": # rest added with key:value
|
||||||
url += "{0}:{1}/".format(key, value)
|
url += "{0}:{1}/".format(key, value)
|
||||||
# finally, attach the q/input. url is now done.
|
|
||||||
url += 'q/%s.json' % utils.web.urlquote(optinput)
|
|
||||||
|
|
||||||
# try and query url.
|
# now that we're done, lets finally make our API call.
|
||||||
try:
|
page = self._wunderjson(url, optinput)
|
||||||
page = utils.web.getUrl(url)
|
if not page:
|
||||||
except utils.web.Error as e:
|
irc.reply("ERROR: Failed to load Wunderground API.")
|
||||||
self.log.error("ERROR: Trying to open {0} message: {1}".format(url, e))
|
|
||||||
irc.reply("ERROR: Failed to load Weather Underground API: {0}".format(e))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# process json.
|
# process json.
|
||||||
@ -435,15 +448,16 @@ class Weather(callbacks.Plugin):
|
|||||||
errordesc = data['response']['error'].get('description', 'no description')
|
errordesc = data['response']['error'].get('description', 'no description')
|
||||||
irc.reply("ERROR: I got an error searching for {0}. ({1}: {2})".format(optinput, errortype, errordesc))
|
irc.reply("ERROR: I got an error searching for {0}. ({1}: {2})".format(optinput, errortype, errordesc))
|
||||||
return
|
return
|
||||||
# if there is more than one city matching.
|
# if there is more than one city matching (Ambiguous Results). we now go with the first (best?) match.
|
||||||
if 'results' in data['response']: # results only comes when location matches more than one.
|
if 'results' in data['response']: # we grab the first location's "ZMW" which then gets constructed as location.
|
||||||
output = [i['city'] + ", " + i['state'] + " (" + i['country_name'] + ")" for i in data['response']['results']]
|
first = 'zmw:%s' % data['response']['results'][0]['zmw'] # grab the "first" location and create the
|
||||||
irc.reply("ERROR: More than 1 city matched your query, try being more specific: {0}".format(" | ".join(output)))
|
# grab this first location and search again.
|
||||||
return
|
page = self._wunderjson(url, first)
|
||||||
# last sanity check
|
if not page:
|
||||||
if not data.has_key('current_observation'):
|
irc.reply("ERROR: Failed to load Wunderground API.")
|
||||||
irc.reply("ERROR: something went horribly wrong looking up weather for {0}. Contact the plugin owner.".format(optinput))
|
return
|
||||||
return
|
# we're here if we got the second search (best?) now lets reload the json and continue.
|
||||||
|
data = json.loads(page.decode('utf-8'))
|
||||||
|
|
||||||
# no errors so we start the main part of processing.
|
# no errors so we start the main part of processing.
|
||||||
outdata = {}
|
outdata = {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user