2011-10-19 18:56:04 -04:00
|
|
|
#!/usr/bin/python3
|
2012-02-22 15:40:26 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-12-06 16:12:28 -05:00
|
|
|
"""
|
|
|
|
tfw.py - the fucking weather module
|
2012-01-03 14:09:34 -05:00
|
|
|
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
|
2010-12-06 16:12:28 -05:00
|
|
|
"""
|
|
|
|
|
2011-09-22 14:17:27 -04:00
|
|
|
from urllib.parse import quote as urlquote
|
|
|
|
from urllib.error import HTTPError
|
2011-09-21 20:43:05 -04:00
|
|
|
import web
|
2010-12-06 16:12:28 -05:00
|
|
|
import lxml.html
|
|
|
|
|
2011-04-04 16:37:27 -04:00
|
|
|
def tfw(phenny, input, fahrenheit=False, celsius=False):
|
2011-12-04 19:46:28 -05:00
|
|
|
""".tfw <city/zip> - Show the fucking weather at the specified location."""
|
2010-12-08 22:00:54 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
zipcode = input.group(2)
|
|
|
|
if not zipcode:
|
|
|
|
# default to Blacksburg, VA
|
|
|
|
zipcode = "24060"
|
2010-12-06 17:39:08 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
if fahrenheit:
|
|
|
|
celsius_param = ""
|
|
|
|
else:
|
|
|
|
celsius_param = "&CELSIUS=yes"
|
2010-12-08 22:00:54 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
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
|
2010-12-06 16:12:28 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
doc = lxml.html.fromstring(req)
|
2010-12-06 16:12:28 -05:00
|
|
|
|
2011-12-04 19:43:46 -05:00
|
|
|
try:
|
2011-12-04 19:46:28 -05:00
|
|
|
location = doc.find_class('small')[0].text_content()
|
|
|
|
weather = doc.get_element_by_id('content')
|
|
|
|
except (IndexError, KeyError):
|
|
|
|
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
|
|
|
|
return
|
2010-12-06 17:53:04 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
main = weather.find_class('large')
|
2010-12-06 16:12:28 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
# temperature is everything up to first <br />
|
|
|
|
tempt = ""
|
|
|
|
for c in main[0].text:
|
|
|
|
if c.isdigit() or c == '-':
|
|
|
|
tempt += c
|
|
|
|
temp = int(tempt)
|
|
|
|
|
|
|
|
# add units and convert if necessary
|
|
|
|
if fahrenheit:
|
2012-02-22 15:40:26 -05:00
|
|
|
temp = "{0:d}°F‽".format(temp)
|
2011-12-04 19:46:28 -05:00
|
|
|
elif celsius:
|
2012-02-22 15:40:26 -05:00
|
|
|
temp = "{0:d}°C‽".format(temp)
|
2011-12-04 19:46:28 -05:00
|
|
|
else:
|
|
|
|
tempev = (temp + 273.15) * 8.617343e-5 * 1000
|
2012-02-22 15:40:26 -05:00
|
|
|
temp = "%f meV‽" % tempev
|
2011-12-04 19:46:28 -05:00
|
|
|
|
|
|
|
# parse comment (broken by <br />, so we have do it this way)
|
|
|
|
comments = main[0].xpath('text()')
|
|
|
|
if len(comments) > 2:
|
|
|
|
comment = "%s %s" % (comments[1], comments[2])
|
|
|
|
else :
|
|
|
|
comment = comments[1]
|
2010-12-06 16:12:28 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
# remark is in its own div, so we have it easy
|
|
|
|
remark = weather.get_element_by_id('remark').text_content()
|
2010-12-06 16:12:28 -05:00
|
|
|
|
2011-12-04 19:46:28 -05:00
|
|
|
response = "%s %s - %s - %s" % (temp, comment, remark, location)
|
|
|
|
phenny.say(response)
|
2010-12-06 17:53:04 -05:00
|
|
|
tfw.rule = (['tfw'], r'(.*)')
|
2010-12-06 16:12:28 -05:00
|
|
|
|
2011-04-04 16:37:27 -04:00
|
|
|
def tfwf(phenny, input):
|
2011-12-04 19:46:28 -05:00
|
|
|
""".tfwf <city/zip> - The fucking weather, in fucking degrees Fahrenheit."""
|
|
|
|
return tfw(phenny, input, fahrenheit=True)
|
2011-04-04 16:37:27 -04:00
|
|
|
tfwf.rule = (['tfwf'], r'(.*)')
|
|
|
|
|
2010-12-08 22:00:54 -05:00
|
|
|
def tfwc(phenny, input):
|
2011-12-04 19:46:28 -05:00
|
|
|
""".tfwc <city/zip> - The fucking weather, in fucking degrees celsius."""
|
|
|
|
return tfw(phenny, input, celsius=True)
|
2010-12-08 22:00:54 -05:00
|
|
|
tfwc.rule = (['tfwc'], r'(.*)')
|
|
|
|
|
2010-12-06 16:12:28 -05:00
|
|
|
if __name__ == '__main__':
|
2011-12-04 19:46:28 -05:00
|
|
|
print(__doc__.strip())
|