import time try: import meteofrance_api as mf DEPENDENCIES_FOUND = True except ImportError: DEPENDENCIES_FOUND = False from edmond.plugin import Plugin class MeteoFrancePlugin(Plugin): REQUIRED_CONFIGS = [ "commands", "result_message", "nearest_message", "temp_format", "rain_chance_format", "all_rain_format", "today_message", "minmax_temp_format", ] def __init__(self, bot): super().__init__(bot) self.client = None def on_welcome(self, event): self.client = mf.MeteoFranceClient() def on_pubmsg(self, event): if not self.should_handle_command(event.arguments[0]): return False if self.command.ident == self.config["commands"][0]: self.tell_weather(event) return True def tell_weather(self, event): places = self.client.search_places(self.command.content) if not places: self.signal_failure(event.target) return place = places[0] # Assume first result is the good one. try: forecast = self.client.get_forecast_for_place(place) except: # really unsure about that unofficial API self.signal_failure(event.target) return nearest_f = forecast.nearest_forecast today_f = forecast.today_forecast try: nearest_s = self.format_nearest_forecast(nearest_f, place) today_s = self.format_today_forecast(today_f) except KeyError: self.signal_failure(event.target) return result = self.config["result_message"].format( nearest_s=nearest_s, today_s=today_s ) self.bot.say(event.target, result) def format_nearest_forecast(self, nearest, place): city = place.name hour = format_ts_hour(nearest["dt"]) temp = self.format_temperature(nearest["T"]) weather = format_weather(nearest["weather"]) wind = self.format_wind(nearest["wind"]) rain = self.format_rain_perc(nearest["rain"]) return self.config["nearest_message"].format( city=city, hour=hour, weather=weather, temp=temp, wind=wind, rain=rain, ) def format_temperature(self, temperature): temp_c = temperature.get("value", "?") temp_wc = temperature.get("windchill", "?") return self.config["temp_format"].format( temp_c=temp_c, temp_wc=temp_wc ) def format_rain_perc(self, rain_percs): perc_fmt = self.config["rain_chance_format"] all_percs = ", ".join( perc_fmt.format(p=p, h=h) for h, p in rain_percs.items() ) return self.config["all_rain_format"].format(all_percs=all_percs) def format_wind(self, wind): speed = wind.get("speed") direction = wind.get("icon") if direction == "Variable": direction = self.config["unknown_direction"] return self.config["wind_format"].format( speed=speed, direction=direction ) def format_today_forecast(self, today_f): temp = today_f["T"] temp_minmax = self.config["minmax_temp_format"].format( t_max=temp["max"], t_min=temp["min"] ) rain = self.format_rain_perc(today_f["precipitation"]) sunset = format_ts_hour(today_f["sun"]["set"]) return self.config["today_message"].format( temp_minmax=temp_minmax, rain=rain, sunset=sunset ) def format_ts_hour(timestamp): return time.strftime("%H:%M", time.localtime(timestamp)) def format_weather(weather): return weather.get("desc", "?").lower()