2010-12-06 16:12:28 -05:00
|
|
|
#!/usr/bin/python2
|
|
|
|
"""
|
|
|
|
tfw.py - the fucking weather module
|
|
|
|
author: mutantmonkey <mutantmonkey@gmail.com>
|
|
|
|
"""
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
2010-12-06 17:53:04 -05:00
|
|
|
from urllib import quote as urlquote
|
|
|
|
from urllib2 import urlopen, HTTPError
|
2010-12-06 16:12:28 -05:00
|
|
|
import lxml.html
|
|
|
|
|
|
|
|
def tfw(phenny, input):
|
|
|
|
zipcode = input.group(2)
|
2010-12-07 18:36:31 -05:00
|
|
|
if not zipcode:
|
|
|
|
# default to Blacksburg, VA
|
|
|
|
zipcode = "24060"
|
2010-12-06 17:39:08 -05:00
|
|
|
|
|
|
|
try:
|
2010-12-06 17:53:04 -05:00
|
|
|
req = urlopen("http://thefuckingweather.com/?zipcode=%s" % urlquote(zipcode))
|
|
|
|
except HTTPError:
|
|
|
|
phenny.say("THE INTERNET IS FUCKING BROKEN. Please try again later.")
|
2010-12-06 17:39:08 -05:00
|
|
|
return
|
2010-12-06 16:12:28 -05:00
|
|
|
|
|
|
|
doc = lxml.html.parse(req)
|
|
|
|
|
|
|
|
location = doc.getroot().find_class('small')[0].text_content()
|
|
|
|
|
2010-12-06 17:53:04 -05:00
|
|
|
try:
|
|
|
|
weather = doc.getroot().get_element_by_id('content')
|
|
|
|
except KeyError:
|
|
|
|
phenny.say("Unknown location")
|
|
|
|
return
|
|
|
|
|
2010-12-06 16:12:28 -05:00
|
|
|
main = weather.find_class('large')
|
|
|
|
|
|
|
|
# temperature is everything up to first <br />
|
|
|
|
temp = main[0].text
|
|
|
|
|
|
|
|
# parse comment (broken by <br />, so we have do it this way)
|
|
|
|
comments = main[0].xpath('text()')
|
2010-12-06 17:08:43 -05:00
|
|
|
if len(comments) > 2:
|
|
|
|
comment = "%s %s" % (comments[1], comments[2])
|
|
|
|
else :
|
|
|
|
comment = comments[1]
|
2010-12-06 16:12:28 -05:00
|
|
|
|
|
|
|
# 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)
|
2010-12-06 17:53:04 -05:00
|
|
|
tfw.rule = (['tfw'], r'(.*)')
|
2010-12-06 16:12:28 -05:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print __doc__.strip()
|
|
|
|
|