From c866b75f884f23ec0b762dfe475c84be09d28e62 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Mon, 4 Apr 2011 16:37:27 -0400 Subject: [PATCH] tfw: add meV unit support --- modules/tfw.py | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/modules/tfw.py b/modules/tfw.py index 0c956e2..0080618 100755 --- a/modules/tfw.py +++ b/modules/tfw.py @@ -4,13 +4,11 @@ tfw.py - the fucking weather module author: mutantmonkey """ -import random - from urllib import quote as urlquote from urllib2 import urlopen, HTTPError import lxml.html -def tfw(phenny, input, celsius=False): +def tfw(phenny, input, fahrenheit=False, celsius=False): """.tfw - Show the fucking weather at the specified location.""" zipcode = input.group(2) @@ -18,10 +16,10 @@ def tfw(phenny, input, celsius=False): # default to Blacksburg, VA zipcode = "24060" - if celsius: - celsius_param = "&CELSIUS=yes" - else: + if fahrenheit: celsius_param = "" + else: + celsius_param = "&CELSIUS=yes" try: 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') # temperature is everything up to first
- 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
, so we have do it this way) comments = main[0].xpath('text()') @@ -58,9 +70,14 @@ def tfw(phenny, input, celsius=False): phenny.say(response) tfw.rule = (['tfw'], r'(.*)') +def tfwf(phenny, input): + """.tfwf - The fucking weather, in fucking degrees Fahrenheit.""" + return tfw(phenny, input, fahrenheit=True) +tfwf.rule = (['tfwf'], r'(.*)') + def tfwc(phenny, input): - """.tfwc - The fucking weather, in fucking celsius.""" - return tfw(phenny, input, True) + """.tfwc - The fucking weather, in fucking degrees celsius.""" + return tfw(phenny, input, celsius=True) tfwc.rule = (['tfwc'], r'(.*)') if __name__ == '__main__':