mirror of
https://github.com/jlu5/SupyPlugins.git
synced 2025-04-30 07:21:12 -05:00
Update Weather snapshot (merge 'weather/devel')
This commit is contained in:
commit
fc0e0077fc
@ -292,31 +292,29 @@ class Weather(callbacks.Plugin):
|
|||||||
# WUNDERGROUND API CALLS #
|
# WUNDERGROUND API CALLS #
|
||||||
##########################
|
##########################
|
||||||
|
|
||||||
def _wuac(self, q):
|
def _wuac(self, irc, q):
|
||||||
"""Internal helper to find a location via Wunderground's autocomplete API."""
|
"""Internal helper to find a location via Wunderground's autocomplete API."""
|
||||||
|
|
||||||
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("Autocomplete URL: %s", url)
|
self.log.debug("Weather: Autocomplete URL: %s", url)
|
||||||
try:
|
try:
|
||||||
page = utils.web.getUrl(url)
|
page = utils.web.getUrl(url)
|
||||||
except Exception as e:
|
except utils.web.Error as e:
|
||||||
self.log.info("Weather: (_wuac) Error trying to open {0} message: {1}".format(url, e))
|
irc.error("Failed to load location data for %r." % q, Raise=True)
|
||||||
return None
|
data = json.loads(page.decode('utf-8'))
|
||||||
try:
|
loc = ''
|
||||||
data = json.loads(page.decode('utf-8'))
|
# ZMW is in some ways a lot like Wunderground's location ID Codes, for when locations
|
||||||
# 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
|
||||||
# are too ambiguous. (e.g. looking up "France", which is a country with many different
|
# locations!)
|
||||||
# locations!)
|
for item in data['RESULTS']:
|
||||||
for item in data['RESULTS']:
|
# Sometimes the autocomplete will lead us to more disambiguation pages...
|
||||||
# Sometimes the autocomplete will lead us to more disambiguation pages...
|
# which cause lots of errors in processing!
|
||||||
# which cause lots of errors in processing!
|
if item['tz'] != 'MISSING':
|
||||||
if item['tz'] != 'MISSING':
|
loc = "zmw:%s" % item['zmw']
|
||||||
loc = "zmw:%s" % item['zmw']
|
break
|
||||||
break
|
else:
|
||||||
return loc
|
irc.error("Failed to find a valid location for: %r" % q, Raise=True)
|
||||||
except Exception as e:
|
return loc
|
||||||
self.log.info("Weather: (_wuac) Error processing JSON in {0} :: {1}".format(url, e))
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _wunderjson(self, url, location):
|
def _wunderjson(self, url, location):
|
||||||
"""Fetch wunderground JSON and return."""
|
"""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.
|
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 <location>'.", Raise=True)
|
irc.error("I did not find a preset location for you. Set one via 'setweather <location>'.", Raise=True)
|
||||||
|
|
||||||
if optinput:
|
loc = self._wuac(irc, optinput or loc)
|
||||||
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)
|
|
||||||
|
|
||||||
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]:
|
||||||
@ -403,7 +393,7 @@ class Weather(callbacks.Plugin):
|
|||||||
if key in ("lang", "bestfct", "pws"): # rest added with key:value
|
if key in ("lang", "bestfct", "pws"): # rest added with key:value
|
||||||
url += "{0}:{1}/".format(key, value)
|
url += "{0}:{1}/".format(key, value)
|
||||||
|
|
||||||
page = self._wunderjson(url, wloc)
|
page = self._wunderjson(url, loc)
|
||||||
try:
|
try:
|
||||||
data = json.loads(page.decode('utf-8'))
|
data = json.loads(page.decode('utf-8'))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -501,7 +491,7 @@ class Weather(callbacks.Plugin):
|
|||||||
if args['alerts']: # only look for alerts if there.
|
if args['alerts']: # only look for alerts if there.
|
||||||
if data['alerts']: # alerts is a list. it can also be empty.
|
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'] = 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.
|
outdata['alerts'] = utils.str.normalizeWhitespace(outdata['alerts']) # fix pesky double whitespacing.
|
||||||
else: # no alerts found (empty).
|
else: # no alerts found (empty).
|
||||||
outdata['alerts'] = "No alerts."
|
outdata['alerts'] = "No alerts."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user