tfw: add meV unit support

master
mutantmonkey 2011-04-04 16:37:27 -04:00
parent 08e8c8c6c6
commit c866b75f88
1 changed files with 26 additions and 9 deletions

View File

@ -4,13 +4,11 @@ tfw.py - the fucking weather module
author: mutantmonkey <mutantmonkey@gmail.com> author: mutantmonkey <mutantmonkey@gmail.com>
""" """
import random
from urllib import quote as urlquote from urllib import quote as urlquote
from urllib2 import urlopen, HTTPError from urllib2 import urlopen, HTTPError
import lxml.html import lxml.html
def tfw(phenny, input, 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)
@ -18,10 +16,10 @@ def tfw(phenny, input, celsius=False):
# default to Blacksburg, VA # default to Blacksburg, VA
zipcode = "24060" zipcode = "24060"
if celsius: if fahrenheit:
celsius_param = "&CELSIUS=yes"
else:
celsius_param = "" celsius_param = ""
else:
celsius_param = "&CELSIUS=yes"
try: try:
req = urlopen("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param)) req = urlopen("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param))
@ -42,7 +40,21 @@ def tfw(phenny, input, celsius=False):
main = weather.find_class('large') main = weather.find_class('large')
# temperature is everything up to first <br /> # temperature is everything up to first <br />
temp = main[0].text tempt = ""
for c in main[0].text:
if c.isdigit():
tempt += c
temp = int(tempt)
deg = unichr(176).encode('latin-1')
# add units and convert if necessary
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) # parse comment (broken by <br />, so we have do it this way)
comments = main[0].xpath('text()') comments = main[0].xpath('text()')
@ -58,9 +70,14 @@ def tfw(phenny, input, celsius=False):
phenny.say(response) phenny.say(response)
tfw.rule = (['tfw'], r'(.*)') tfw.rule = (['tfw'], r'(.*)')
def tfwf(phenny, input):
""".tfwf <city/zip> - The fucking weather, in fucking degrees Fahrenheit."""
return tfw(phenny, input, fahrenheit=True)
tfwf.rule = (['tfwf'], r'(.*)')
def tfwc(phenny, input): def tfwc(phenny, input):
""".tfwc <city/zip> - The fucking weather, in fucking celsius.""" """.tfwc <city/zip> - The fucking weather, in fucking degrees celsius."""
return tfw(phenny, input, True) return tfw(phenny, input, celsius=True)
tfwc.rule = (['tfwc'], r'(.*)') tfwc.rule = (['tfwc'], r'(.*)')
if __name__ == '__main__': if __name__ == '__main__':