tfw: handle location errors properly
parent
3a82d3281b
commit
c702d7bc59
|
@ -5,6 +5,7 @@ author: mutantmonkey <mutantmonkey@mutantmonkey.in>
|
|||
|
||||
import re
|
||||
import unittest
|
||||
import tools
|
||||
from mock import MagicMock, Mock
|
||||
from modules.tfw import tfw
|
||||
|
||||
|
@ -13,11 +14,11 @@ class TestTfw(unittest.TestCase):
|
|||
def setUp(self):
|
||||
self.phenny = MagicMock()
|
||||
|
||||
#def test_badloc(self):
|
||||
# input = Mock(group=lambda x: 'tu3jgoajgoahghqog')
|
||||
# tfw(self.phenny, input)
|
||||
#
|
||||
# self.phenny.say.assert_called_once_with("UNKNOWN FUCKING LOCATION. Try another?")
|
||||
def test_badloc(self):
|
||||
input = Mock(group=lambda x: 'tu3jgoajgoahghqog')
|
||||
tfw(self.phenny, input)
|
||||
|
||||
self.phenny.say.assert_called_once_with("UNKNOWN FUCKING LOCATION. Try another?")
|
||||
|
||||
def test_celsius(self):
|
||||
input = Mock(group=lambda x: '24060')
|
||||
|
|
|
@ -20,33 +20,31 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
|
|||
# default to Blacksburg, VA
|
||||
where = "24060"
|
||||
|
||||
if fahrenheit:
|
||||
celsius_param = ""
|
||||
else:
|
||||
celsius_param = "&CELSIUS=yes"
|
||||
url = "http://thefuckingweather.com/?where=" + urlquote(where)
|
||||
if not fahrenheit:
|
||||
url += "&CELSIUS=yes"
|
||||
|
||||
try:
|
||||
req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param))
|
||||
req = web.get(url)
|
||||
except (HTTPError, IOError):
|
||||
# the fucking weather is fucking unstable, try again
|
||||
try:
|
||||
req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param))
|
||||
req = web.get(url)
|
||||
except (HTTPError, IOError):
|
||||
raise GrumbleError("THE INTERNET IS FUCKING BROKEN. Please try again later.")
|
||||
|
||||
doc = lxml.html.fromstring(req)
|
||||
|
||||
try:
|
||||
#location = doc.find_class('small')[0].text_content()
|
||||
location = doc.get_element_by_id('locationDisplaySpan').text_content()
|
||||
|
||||
temp_sel = lxml.cssselect.CSSSelector('span.temperature')
|
||||
temp = temp_sel(doc)[0].text_content()
|
||||
temp = int(temp)
|
||||
except (IndexError, KeyError):
|
||||
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
|
||||
return
|
||||
|
||||
temp_sel = lxml.cssselect.CSSSelector('span.temperature')
|
||||
temp = temp_sel(doc)[0].text_content()
|
||||
temp = int(temp)
|
||||
|
||||
# add units and convert if necessary
|
||||
if fahrenheit:
|
||||
temp = "{0:d}°F‽".format(temp)
|
||||
|
|
Loading…
Reference in New Issue