def comment(sentence, temp, condition): ''' Make a comment on what the weather will be like. ''' warm = 25 if paul.get_temp().lower() == "c" else 77 cold = 0 if paul.get_temp().lower() == "c" else 32 if sentence.has_one_of(["hot", "warm", "boiling", "roasting"]): if int(temp) < warm: return "Meh. " elif int(temp) < cold: return "Absolutely not hot! " elif int(temp) > warm: return "It will be warm. " elif sentence.has_one_of(["cold", "cool", "freezing"]): if int(temp) > warm: return "Definitely not cold, it'll be quite hot. " elif int(temp) > cold: return "Not particularly cold. " elif int(temp) < cold: return "It'll be cold, so wrap up warm! " return ""
def weather(day_index, sentence): ''' Simply get the weather for any day ''' if day_index > 5 or day_index < 0: return "I can't see that far ahead. Sorry!" try: page = urllib.request.urlopen("http://weather.yahooapis.com/" "forecastrss?u=" + paul.get_temp().lower() + "&w=" + paul.get_woeid()) except urllib.error.URLError: return ("I couldn't retrieve the weather. " + "Are you connected to the internet?") lines = page.readlines() conditions = get_conditions(lines, day_index) paul.log("WEATHER_RAW:", lines) paul.log("CONDITIONS:", conditions) paul.set_it("http://weather.yahoo.com/") if day_index == 0: condition = (", and {}".format(conditions['text'].lower()) if conditions['text'].lower() != "unknown" else "") temp = conditions['temp'] conditions2 = get_conditions(lines, 1) paul.log("CONDITIONS2:", conditions2) condition2 = conditions2['text'].lower() temp2 = "{}".format(conditions2['high']) com = comment(sentence, temp, conditions['text']) return "{0}It's {1}°{2}{3}. The high today is {4}°{2}, {5}.".format( com, temp, paul.get_temp(), condition, temp2, condition2) else: condition = conditions['text'].lower() com = comment(sentence, conditions['high'], conditions['text']) rep = ("{0}It will have a low of " "{1}°{2}, a high of {3}°{2}, and will be {4}.".format( com, conditions['low'], paul.get_temp(), conditions['high'], condition)) return rep