tfw: handle location errors properly

master
mutantmonkey 2012-06-04 21:42:56 -07:00
parent 3a82d3281b
commit c702d7bc59
2 changed files with 15 additions and 16 deletions

View File

@ -5,6 +5,7 @@ author: mutantmonkey <mutantmonkey@mutantmonkey.in>
import re import re
import unittest import unittest
import tools
from mock import MagicMock, Mock from mock import MagicMock, Mock
from modules.tfw import tfw from modules.tfw import tfw
@ -13,11 +14,11 @@ class TestTfw(unittest.TestCase):
def setUp(self): def setUp(self):
self.phenny = MagicMock() self.phenny = MagicMock()
#def test_badloc(self): def test_badloc(self):
# input = Mock(group=lambda x: 'tu3jgoajgoahghqog') input = Mock(group=lambda x: 'tu3jgoajgoahghqog')
# tfw(self.phenny, input) tfw(self.phenny, input)
#
# self.phenny.say.assert_called_once_with("UNKNOWN FUCKING LOCATION. Try another?") self.phenny.say.assert_called_once_with("UNKNOWN FUCKING LOCATION. Try another?")
def test_celsius(self): def test_celsius(self):
input = Mock(group=lambda x: '24060') input = Mock(group=lambda x: '24060')

View File

@ -20,32 +20,30 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
# default to Blacksburg, VA # default to Blacksburg, VA
where = "24060" where = "24060"
if fahrenheit: url = "http://thefuckingweather.com/?where=" + urlquote(where)
celsius_param = "" if not fahrenheit:
else: url += "&CELSIUS=yes"
celsius_param = "&CELSIUS=yes"
try: try:
req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param)) req = web.get(url)
except (HTTPError, IOError): except (HTTPError, IOError):
# the fucking weather is fucking unstable, try again # the fucking weather is fucking unstable, try again
try: try:
req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param)) req = web.get(url)
except (HTTPError, IOError): except (HTTPError, IOError):
raise GrumbleError("THE INTERNET IS FUCKING BROKEN. Please try again later.") raise GrumbleError("THE INTERNET IS FUCKING BROKEN. Please try again later.")
doc = lxml.html.fromstring(req) doc = lxml.html.fromstring(req)
try: try:
#location = doc.find_class('small')[0].text_content()
location = doc.get_element_by_id('locationDisplaySpan').text_content() location = doc.get_element_by_id('locationDisplaySpan').text_content()
except (IndexError, KeyError):
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
return
temp_sel = lxml.cssselect.CSSSelector('span.temperature') temp_sel = lxml.cssselect.CSSSelector('span.temperature')
temp = temp_sel(doc)[0].text_content() temp = temp_sel(doc)[0].text_content()
temp = int(temp) temp = int(temp)
except (IndexError, KeyError):
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
return
# add units and convert if necessary # add units and convert if necessary
if fahrenheit: if fahrenheit: