def send_home_forecast(self) -> None:
def __periodically_send_morning_forecast__() -> None:
__send_home_forecast__("sending periodic morning weather forecast...",
lambda weathers: list(filter(lambda w: w.date == datetime.date.today(), weathers)))
def __periodically_send_evening_forecast__() -> None:
__send_home_forecast__("sending periodic evening weather forecast...",
lambda weathers: list(filter(lambda w: w.date != datetime.date.today(), weathers)))
def __send_home_forecast__(logging_header: str, weathers_predicate) -> None:
logging.debug(logging_header)
forecast = self._weather_service.get_forecast(self._home_settings.full_name, self._measurement_system)
forecast.weathers = weathers_predicate(forecast.weathers)
for channel in self._discord_client.get_all_channels():
if self.__should_send_forecast__(channel):
try:
asyncio.get_event_loop().run_until_complete(
SendForecastDiscordCommand(self._discord_client, channel, forecast).execute())
except discord.HTTPException:
msg_template = "failed to send home weather forecast (@{}) to channel {}.{}"
msg = msg_template.format(self._home_settings.full_name, channel.server.name, channel.name)
logging.exception(msg)
if self._home_settings.morning_forecast_time is not None:
schedule.every().day.at(self._home_settings.morning_forecast_time)\
.do(__periodically_send_morning_forecast__)
if self._home_settings.evening_forecast_time is not None:
schedule.every().day.at(self._home_settings.evening_forecast_time)\
.do(__periodically_send_evening_forecast__)
while True:
schedule.run_pending()
await asyncio.sleep(1)
评论列表
文章目录