From 4edf618ef2817af8cde34618a22d30cc1ec56057 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Mon, 30 Sep 2013 22:40:03 -0700 Subject: [PATCH] =?UTF-8?q?tfw:=20fix=20"sexy=20time"=20for=2069=C2=B0F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/test/test_tfw.py | 34 ++++++++++++++++++++++++---------- modules/tfw.py | 38 +++++++++++++++++++++++--------------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/modules/test/test_tfw.py b/modules/test/test_tfw.py index fc303fa..20d7d74 100644 --- a/modules/test/test_tfw.py +++ b/modules/test/test_tfw.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ test_tfw.py - tests for the fucking weather module author: mutantmonkey @@ -7,7 +8,7 @@ import re import unittest import tools from mock import MagicMock, Mock -from modules.tfw import tfw +from modules import tfw class TestTfw(unittest.TestCase): @@ -16,34 +17,47 @@ class TestTfw(unittest.TestCase): def test_badloc(self): input = Mock(group=lambda x: 'tu3jgoajgoahghqog') - tfw(self.phenny, input) - + tfw.tfw(self.phenny, input) + self.phenny.say.assert_called_once_with( - "WHERE THE FUCK IS THAT? Try another location.") + "WHERE THE FUCK IS THAT? Try another location.") def test_celsius(self): input = Mock(group=lambda x: '24060') - tfw(self.phenny, input, celsius=True) + tfw.tfw(self.phenny, input, celsius=True) out = self.phenny.say.call_args[0][0] m = re.match('^\d+°C‽ .* \- .* \- [A-Z]{4} \d{2}:\d{2}Z$', out, - flags=re.UNICODE) + flags=re.UNICODE) self.assertTrue(m) def test_fahrenheit(self): input = Mock(group=lambda x: '24060') - tfw(self.phenny, input, fahrenheit=True) + tfw.tfw(self.phenny, input, fahrenheit=True) out = self.phenny.say.call_args[0][0] m = re.match('^\d+°F‽ .* \- .* \- [A-Z]{4} \d{2}:\d{2}Z$', out, - flags=re.UNICODE) + flags=re.UNICODE) self.assertTrue(m) def test_mev(self): input = Mock(group=lambda x: '24060') - tfw(self.phenny, input) + tfw.tfw(self.phenny, input) out = self.phenny.say.call_args[0][0] m = re.match('^[\d\.]+ meV‽ .* \- .* \- [A-Z]{4} \d{2}:\d{2}Z$', out, - flags=re.UNICODE) + flags=re.UNICODE) + self.assertTrue(m) + + def test_sexy_time(self): + input = Mock(group=lambda x: 'KBCB') + tfw.web = MagicMock() + tfw.metar.parse = lambda x: Mock(temperature=21) + tfw.tfwf(self.phenny, input) + + out = self.phenny.say.call_args[0][0] + m = re.match( + r'^69°F‽ IT\'S FUCKING SEXY TIME \- .*', + out, + flags=re.UNICODE) self.assertTrue(m) diff --git a/modules/tfw.py b/modules/tfw.py index 10bd04d..3e2d39b 100644 --- a/modules/tfw.py +++ b/modules/tfw.py @@ -11,6 +11,7 @@ import random import metar import web + def tfw(phenny, input, fahrenheit=False, celsius=False): """.tfw - Show the fucking weather at the specified location.""" @@ -21,20 +22,20 @@ def tfw(phenny, input, fahrenheit=False, celsius=False): else: icao_code = weather.code(phenny, where) - if not icao_code: + if not icao_code: phenny.say("WHERE THE FUCK IS THAT? Try another location.") return uri = 'http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' try: bytes = web.get(uri % icao_code) - except AttributeError: + except AttributeError: raise GrumbleError("THE INTERNET IS FUCKING BROKEN. Please try again later.") except web.HTTPError: phenny.say("WHERE THE FUCK IS THAT? Try another location.") return - if 'Not Found' in bytes: + if 'Not Found' in bytes: phenny.say("WHERE THE FUCK IS THAT? Try another location.") return @@ -52,7 +53,8 @@ def tfw(phenny, input, fahrenheit=False, celsius=False): if w.temperature < 6: remark = "IT'S FUCKING COLD" - flavors = ["Where's the cat? Oh shit. Fluffy's frozen.", + flavors = [ + "Where's the cat? Oh shit. Fluffy's frozen.", "Nothing a few shots couldn't fix", "Should have gone south", "You think this is cold? Have you been to upstate New York?", @@ -85,7 +87,8 @@ def tfw(phenny, input, fahrenheit=False, celsius=False): "It's bone chilling cold out. Sorry ladies."] elif w.temperature < 20: remark = "IT'S FUCKING...ALRIGHT" - flavors = ["Might as well rain, I'm not going out in that.", + flavors = [ + "Might as well rain, I'm not going out in that.", "Better than a sharp stick in the eye.", "Everything's nice butter weather!", "At least you aren't living in a small town in Alaska", @@ -114,7 +117,8 @@ def tfw(phenny, input, fahrenheit=False, celsius=False): "Maybe Jersey Shore is on tonight."] else: remark = "IT'S FUCKING NICE" - flavors = ["I made today breakfast in bed.", "FUCKING SWEET", + flavors = [ + "I made today breakfast in bed.", "FUCKING SWEET", "Quit your bitching", "Enjoy.", "IT'S ABOUT FUCKING TIME", "READ A FUCKIN' BOOK", "LETS HAVE A FUCKING PICNIC", "It is safe to take your ball-mittens off.", "More please.", @@ -135,7 +139,7 @@ def tfw(phenny, input, fahrenheit=False, celsius=False): "The geese are on their way back! Unless you live where they migrate to for the winter.", "FUCKING AFFABLE AS SHIT", "Give the sun a raise!", "Today is better than an original holographic charizard. Loser!"] - + if w.descriptor == "thunderstorm": remark += " AND THUNDERING" elif w.precipitation in ("snow", "snow grains"): @@ -147,31 +151,35 @@ def tfw(phenny, input, fahrenheit=False, celsius=False): elif w.precipitation in ("hail", "small hail"): remark += " AND HAILING" - if tempf == 69: + if int(tempf) == 69: remark = "IT'S FUCKING SEXY TIME" - flavors = ["Why is 77 better than 69? You get eight more.", - "What comes after 69? Mouthwash.", - "If you are given two contradictory orders, obey them both.", - "a good fuckin' time! ;)", - "What's the square root of 69? Eight something."] + flavors = [ + "Why is 77 better than 69? You get eight more.", + "What comes after 69? Mouthwash.", + "If you are given two contradictory orders, obey them both.", + "a good fuckin' time! ;)", + "What's the square root of 69? Eight something."] flavor = random.choice(flavors) response = "{temp} {remark} - {flavor} - {location} {time}Z".format( - temp=temp, remark=remark, flavor=flavor, location=w.station, - time=w.time.strftime("%H:%M")) + temp=temp, remark=remark, flavor=flavor, location=w.station, + time=w.time.strftime("%H:%M")) 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 degrees celsius.""" return tfw(phenny, input, celsius=True) tfwc.rule = (['tfwc'], r'(.*)') + if __name__ == '__main__': print(__doc__.strip())