fix tfw for new redesign

master
mutantmonkey 2012-04-24 14:28:56 -04:00
parent 5cb4a56cf7
commit 5ba500bc5c
1 changed files with 15 additions and 23 deletions

View File

@ -9,14 +9,15 @@ from urllib.parse import quote as urlquote
from urllib.error import HTTPError from urllib.error import HTTPError
import web import web
import lxml.html import lxml.html
import lxml.cssselect
def tfw(phenny, input, fahrenheit=False, celsius=False): def tfw(phenny, input, fahrenheit=False, celsius=False):
""".tfw <city/zip> - Show the fucking weather at the specified location.""" """.tfw <city/zip> - Show the fucking weather at the specified location."""
zipcode = input.group(2) where = input.group(2)
if not zipcode: if not where:
# default to Blacksburg, VA # default to Blacksburg, VA
zipcode = "24060" where = "24060"
if fahrenheit: if fahrenheit:
celsius_param = "" celsius_param = ""
@ -24,7 +25,7 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
celsius_param = "&CELSIUS=yes" celsius_param = "&CELSIUS=yes"
try: try:
req = web.get("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param)) req = web.get("http://thefuckingweather.com/?where={0}{1}".format(urlquote(where), celsius_param))
except (HTTPError, IOError): except (HTTPError, IOError):
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.") phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
return return
@ -32,20 +33,15 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
doc = lxml.html.fromstring(req) doc = lxml.html.fromstring(req)
try: try:
location = doc.find_class('small')[0].text_content() #location = doc.find_class('small')[0].text_content()
weather = doc.get_element_by_id('content') location = doc.get_element_by_id('locationDisplaySpan').text_content()
except (IndexError, KeyError): except (IndexError, KeyError):
phenny.say("UNKNOWN FUCKING LOCATION. Try another?") phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
return return
main = weather.find_class('large') temp_sel = lxml.cssselect.CSSSelector('span.temperature')
temp = temp_sel(doc)[0].text_content()
# temperature is everything up to first <br /> temp = int(temp)
tempt = ""
for c in main[0].text:
if c.isdigit() or c == '-':
tempt += c
temp = int(tempt)
# add units and convert if necessary # add units and convert if necessary
if fahrenheit: if fahrenheit:
@ -56,17 +52,13 @@ def tfw(phenny, input, fahrenheit=False, celsius=False):
tempev = (temp + 273.15) * 8.617343e-5 * 1000 tempev = (temp + 273.15) * 8.617343e-5 * 1000
temp = "%f meV‽" % tempev temp = "%f meV‽" % tempev
# parse comment (broken by <br />, so we have do it this way) remark_sel = lxml.cssselect.CSSSelector('p.remark')
comments = main[0].xpath('text()') remark = remark_sel(doc)[0].text_content()
if len(comments) > 2:
comment = "%s %s" % (comments[1], comments[2])
else :
comment = comments[1]
# remark is in its own div, so we have it easy flavor_sel = lxml.cssselect.CSSSelector('p.flavor')
remark = weather.get_element_by_id('remark').text_content() flavor = flavor_sel(doc)[0].text_content()
response = "%s %s - %s - %s" % (temp, comment, remark, location) response = "%s %s - %s - %s" % (temp, remark, flavor, location)
phenny.say(response) phenny.say(response)
tfw.rule = (['tfw'], r'(.*)') tfw.rule = (['tfw'], r'(.*)')