From ab37006b032d79e46be3ace24568f8465c9b5974 Mon Sep 17 00:00:00 2001 From: Max Gurela Date: Thu, 28 Jan 2016 18:27:57 -0800 Subject: [PATCH] [weather] Fix YQL woeid lookup Handles an edge case that neither of the PRs handled Fixes #1006 Closes #1007, #1012 --- sopel/modules/weather.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/sopel/modules/weather.py b/sopel/modules/weather.py index ca06089231..ea03b46135 100644 --- a/sopel/modules/weather.py +++ b/sopel/modules/weather.py @@ -16,16 +16,16 @@ def woeid_search(query): node for the result, so that location data can still be retrieved. Returns None if there is no result, or the woeid field is empty. """ - query = 'q=select * from geo.placefinder where text="%s"' % query + query = 'q=select * from geo.places where text="%s"' % query body = web.get('http://query.yahooapis.com/v1/public/yql?' + query, dont_decode=True) parsed = xmltodict.parse(body).get('query') results = parsed.get('results') - if results is None or results.get('Result') is None: + if results is None or results.get('place') is None: return None - if type(results.get('Result')) is list: - return results.get('Result')[0] - return results.get('Result') + if type(results.get('place')) is list: + return results.get('place')[0] + return results.get('place') def get_cover(parsed): @@ -166,12 +166,18 @@ def update_woeid(bot, trigger): bot.db.set_nick_value(trigger.nick, 'woeid', woeid) - neighborhood = first_result.get('neighborhood') or '' + neighborhood = first_result.get('locality2') or '' if neighborhood: - neighborhood += ',' - city = first_result.get('city') or '' - state = first_result.get('state') or '' - country = first_result.get('country') or '' - uzip = first_result.get('uzip') or '' - bot.reply('I now have you at WOEID %s (%s %s, %s, %s %s.)' % + neighborhood = neighborhood.get('#text') + ', ' + city = first_result.get('locality1') or '' + # This is to catch cases like 'Bawlf, Alberta' where the location is + # thought to be a "LocalAdmin" rather than a "Town" + if city: + city = city.get('#text') + else: + city = first_result.get('name') + state = first_result.get('admin1').get('#text') or '' + country = first_result.get('country').get('#text') or '' + uzip = first_result.get('postal').get('#text') or '' + bot.reply('I now have you at WOEID %s (%s%s, %s, %s %s)' % (woeid, neighborhood, city, state, country, uzip))