Repository: sopel-irc/sopel

Issue:
.setlocation gives an error and fails: AttributeError: 'NoneType' object has no attribute 'get'
yoosi: `.setlocation london`

sopel: `AttributeError: 'NoneType' object has no attribute 'get' (file "/home/sopel/sopel/sopel/modules/weather.py", line 181, in update_woeid)`



Discussion:
Here's stderr from Sopel when user does a `.setlocation london`

> Function get is deprecated.
>   File "/usr/lib/python3.4/threading.py", line 888, in _bootstrap
>     self._bootstrap_inner()
>   File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
>     self.run()
>   File "/usr/lib/python3.4/threading.py", line 868, in run
>     self._target(_self._args, *_self._kwargs)
>   File "/home/carter/sopel/sopel/bot.py", line 441, in call
>     exit_code = func(sopel, trigger)
>   File "/home/carter/sopel/sopel/modules/weather.py", line 161, in update_woeid
>     first_result = woeid_search(trigger.group(2))
>   File "/home/carter/sopel/sopel/modules/weather.py", line 21, in woeid_search
>     dont_decode=True)
> Traceback (most recent call last):
>   File "/home/carter/sopel/sopel/bot.py", line 441, in call
>     exit_code = func(sopel, trigger)
>   File "/home/carter/sopel/sopel/modules/weather.py", line 181, in update_woeid
>     uzip = first_result.get('postal').get('#text') or ''
> AttributeError: 'NoneType' object has no attribute 'get'

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20geo.places%20where%20text=%22London%22

first_result has a postal tag, but nothing else

want to try:

``` python
    uzip = first_result.get('postal') and first_result.get('postal').get('#text') or ''
```

or do something more sane?

```
15:33 yoosi .weather
15:33 Sopel AttributeError: 'NoneType' object has no attribute 'get' (file "/opt/rh/python33/root/usr/lib/python3.3/site-packages/sopel/modules/weather.py", line 144, in weather)
```

Now I'm getting errors after location is properly set when I try to get the weather. Last tried in a PM with Sopel on freenode as well as my personal bot after running a git pull today.


Resolving patch:
diff --git a/sopel/modules/weather.py b/sopel/modules/weather.py
--- a/sopel/modules/weather.py
+++ b/sopel/modules/weather.py
@@ -137,16 +137,16 @@ def weather(bot, trigger):
     if not woeid:
         return bot.reply("I don't know where that is.")
 
-    query = web.urlencode({'w': woeid, 'u': 'c'})
-    raw = web.get('http://weather.yahooapis.com/forecastrss?' + query,
+    query = 'q=select * from weather.forecast where woeid="%s" and u=\'c\'' % woeid
+    body = web.get('http://query.yahooapis.com/v1/public/yql?' + query,
                   dont_decode=True)
-    parsed = xmltodict.parse(raw).get('rss')
-    location = parsed.get('channel').get('title')
-
-    cover = get_cover(parsed)
-    temp = get_temp(parsed)
-    humidity = get_humidity(parsed)
-    wind = get_wind(parsed)
+    parsed = xmltodict.parse(body).get('query')
+    results = parsed.get('results')
+    location = results.get('channel').get('title')
+    cover = get_cover(results)
+    temp = get_temp(results)
+    humidity = get_humidity(results)
+    wind = get_wind(results)
     bot.say(u'%s: %s, %s, %s, %s' % (location, cover, temp, humidity, wind))
 
 
@@ -178,6 +178,5 @@ def update_woeid(bot, trigger):
         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))
+    bot.reply('I now have you at WOEID %s (%s%s, %s, %s)' %
+              (woeid, neighborhood, city, state, country))


Test patch: