tfw: fix indentation
parent
76c0feddb5
commit
6e69bc1ba3
106
modules/tfw.py
106
modules/tfw.py
|
@ -10,76 +10,76 @@ import web
|
||||||
import lxml.html
|
import lxml.html
|
||||||
|
|
||||||
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)
|
zipcode = input.group(2)
|
||||||
if not zipcode:
|
if not zipcode:
|
||||||
# default to Blacksburg, VA
|
# default to Blacksburg, VA
|
||||||
zipcode = "24060"
|
zipcode = "24060"
|
||||||
|
|
||||||
if fahrenheit:
|
if fahrenheit:
|
||||||
celsius_param = ""
|
celsius_param = ""
|
||||||
else:
|
else:
|
||||||
celsius_param = "&CELSIUS=yes"
|
celsius_param = "&CELSIUS=yes"
|
||||||
|
|
||||||
try:
|
|
||||||
req = web.get("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param))
|
|
||||||
except (HTTPError, IOError):
|
|
||||||
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
|
|
||||||
return
|
|
||||||
|
|
||||||
doc = lxml.html.fromstring(req)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
location = doc.find_class('small')[0].text_content()
|
req = web.get("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param))
|
||||||
weather = doc.get_element_by_id('content')
|
except (HTTPError, IOError):
|
||||||
except (IndexError, KeyError):
|
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
|
||||||
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
|
return
|
||||||
return
|
|
||||||
|
|
||||||
main = weather.find_class('large')
|
doc = lxml.html.fromstring(req)
|
||||||
|
|
||||||
# temperature is everything up to first <br />
|
try:
|
||||||
tempt = ""
|
location = doc.find_class('small')[0].text_content()
|
||||||
for c in main[0].text:
|
weather = doc.get_element_by_id('content')
|
||||||
if c.isdigit() or c == '-':
|
except (IndexError, KeyError):
|
||||||
tempt += c
|
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
|
||||||
temp = int(tempt)
|
return
|
||||||
deg = chr(176)
|
|
||||||
|
|
||||||
# add units and convert if necessary
|
main = weather.find_class('large')
|
||||||
if fahrenheit:
|
|
||||||
temp = "%d%cF?!" % (temp, deg)
|
|
||||||
elif celsius:
|
|
||||||
temp = "%d%cC?!" % (temp, deg)
|
|
||||||
else:
|
|
||||||
tempev = (temp + 273.15) * 8.617343e-5 * 1000
|
|
||||||
temp = "%f meV?!" % tempev
|
|
||||||
|
|
||||||
# parse comment (broken by <br />, so we have do it this way)
|
# temperature is everything up to first <br />
|
||||||
comments = main[0].xpath('text()')
|
tempt = ""
|
||||||
if len(comments) > 2:
|
for c in main[0].text:
|
||||||
comment = "%s %s" % (comments[1], comments[2])
|
if c.isdigit() or c == '-':
|
||||||
else :
|
tempt += c
|
||||||
comment = comments[1]
|
temp = int(tempt)
|
||||||
|
deg = chr(176)
|
||||||
|
|
||||||
# remark is in its own div, so we have it easy
|
# add units and convert if necessary
|
||||||
remark = weather.get_element_by_id('remark').text_content()
|
if fahrenheit:
|
||||||
|
temp = "%d%cF?!" % (temp, deg)
|
||||||
|
elif celsius:
|
||||||
|
temp = "%d%cC?!" % (temp, deg)
|
||||||
|
else:
|
||||||
|
tempev = (temp + 273.15) * 8.617343e-5 * 1000
|
||||||
|
temp = "%f meV?!" % tempev
|
||||||
|
|
||||||
response = "%s %s - %s - %s" % (temp, comment, remark, location)
|
# parse comment (broken by <br />, so we have do it this way)
|
||||||
phenny.say(response)
|
comments = main[0].xpath('text()')
|
||||||
|
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
|
||||||
|
remark = weather.get_element_by_id('remark').text_content()
|
||||||
|
|
||||||
|
response = "%s %s - %s - %s" % (temp, comment, remark, location)
|
||||||
|
phenny.say(response)
|
||||||
tfw.rule = (['tfw'], r'(.*)')
|
tfw.rule = (['tfw'], r'(.*)')
|
||||||
|
|
||||||
def tfwf(phenny, input):
|
def tfwf(phenny, input):
|
||||||
""".tfwf <city/zip> - The fucking weather, in fucking degrees Fahrenheit."""
|
""".tfwf <city/zip> - The fucking weather, in fucking degrees Fahrenheit."""
|
||||||
return tfw(phenny, input, fahrenheit=True)
|
return tfw(phenny, input, fahrenheit=True)
|
||||||
tfwf.rule = (['tfwf'], r'(.*)')
|
tfwf.rule = (['tfwf'], r'(.*)')
|
||||||
|
|
||||||
def tfwc(phenny, input):
|
def tfwc(phenny, input):
|
||||||
""".tfwc <city/zip> - The fucking weather, in fucking degrees celsius."""
|
""".tfwc <city/zip> - The fucking weather, in fucking degrees celsius."""
|
||||||
return tfw(phenny, input, celsius=True)
|
return tfw(phenny, input, celsius=True)
|
||||||
tfwc.rule = (['tfwc'], r'(.*)')
|
tfwc.rule = (['tfwc'], r'(.*)')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
print(__doc__.strip())
|
print(__doc__.strip())
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue