phenny/modules/tfw.py

79 lines
2.3 KiB
Python
Raw Normal View History

2011-10-19 18:56:04 -04:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
tfw.py - the fucking weather module
author: mutantmonkey <mutantmonkey@mutantmonkey.in>
"""
2011-09-22 14:17:27 -04:00
from urllib.parse import quote as urlquote
from urllib.error import HTTPError
from tools import GrumbleError
import web
import lxml.html
2012-04-24 14:28:56 -04:00
import lxml.cssselect
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."""
2012-04-24 14:28:56 -04:00
where = input.group(2)
if not where:
2011-12-04 19:46:28 -05:00
# default to Blacksburg, VA
2012-04-24 14:28:56 -04:00
where = "24060"
2010-12-06 17:39:08 -05:00
2012-06-05 00:42:56 -04:00
url = "http://thefuckingweather.com/?where=" + urlquote(where)
if not fahrenheit:
url += "&CELSIUS=yes"
2011-12-04 19:46:28 -05:00
try:
2012-06-05 00:42:56 -04:00
req = web.get(url)
2011-12-04 19:46:28 -05:00
except (HTTPError, IOError):
2012-05-31 03:39:40 -04:00
# the fucking weather is fucking unstable, try again
try:
2012-06-05 00:42:56 -04:00
req = web.get(url)
2012-05-31 03:39:40 -04:00
except (HTTPError, IOError):
raise GrumbleError("THE INTERNET IS FUCKING BROKEN. Please try again later.")
2011-12-04 19:46:28 -05:00
doc = lxml.html.fromstring(req)
2011-12-04 19:43:46 -05:00
try:
2012-04-24 14:28:56 -04:00
location = doc.get_element_by_id('locationDisplaySpan').text_content()
2012-06-05 00:42:56 -04:00
temp_sel = lxml.cssselect.CSSSelector('span.temperature')
temp = temp_sel(doc)[0].text_content()
temp = int(temp)
2011-12-04 19:46:28 -05:00
except (IndexError, KeyError):
phenny.say("UNKNOWN FUCKING LOCATION. Try another?")
return
2011-12-04 19:46:28 -05:00
# add units and convert if necessary
if fahrenheit:
temp = "{0:d}°F‽".format(temp)
2011-12-04 19:46:28 -05:00
elif celsius:
temp = "{0:d}°C‽".format(temp)
2011-12-04 19:46:28 -05:00
else:
tempev = (temp + 273.15) * 8.617343e-5 * 1000
temp = "%f meV‽" % tempev
2011-12-04 19:46:28 -05:00
2012-04-24 14:28:56 -04:00
remark_sel = lxml.cssselect.CSSSelector('p.remark')
remark = remark_sel(doc)[0].text_content()
2012-04-24 14:28:56 -04:00
flavor_sel = lxml.cssselect.CSSSelector('p.flavor')
flavor = flavor_sel(doc)[0].text_content()
2012-04-24 14:28:56 -04:00
response = "%s %s - %s - %s" % (temp, remark, flavor, location)
2011-12-04 19:46:28 -05:00
phenny.say(response)
tfw.rule = (['tfw'], r'(.*)')
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'(.*)')
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)
tfwc.rule = (['tfwc'], r'(.*)')
if __name__ == '__main__':
2011-12-04 19:46:28 -05:00
print(__doc__.strip())