import logging from datetime import datetime, timedelta, timezone from libbot.pyrogram.classes import PyroClient from pytz import timezone as pytz_timezone from classes.enums import GarbageType from classes.location import Location from classes.pyrouser import PyroUser from modules.database import col_entries, col_users logger = logging.getLogger(__name__) async def remind(app: PyroClient) -> None: now = datetime.now() users = await col_users.find( {"time_hour": now.hour, "time_minute": now.minute} ).to_list() for user_db in users: user = PyroUser(**user_db) logger.info("Processing %s...", user.id) if not user.enabled or user.location is None: continue try: location: Location = await app.get_location(user.location.id) # type: ignore except ValueError: continue user_date = ( datetime.now(pytz_timezone(location.timezone)).replace( second=0, microsecond=0 ) + timedelta(days=user.offset) ).replace(tzinfo=timezone.utc) entries = await col_entries.find( { "location": {"$in": location.id}, "date": user_date.replace(hour=0, minute=0), } ).to_list() logger.info("Entries of %s for %s: %s", user.id, user_date, entries) for entry in entries: try: garbage_type = app._( str(GarbageType(entry["garbage_type"]).value), "garbage_types", locale=user.locale, ) garbage_date = datetime.strftime( entry["date"], app._("date", "formats", locale=user.locale) ) await app.send_message( user.id, "**Garbage Collection**\n\nType: {type}\nDate: {date}\n\nDon't forget to prepare your bin for collection!".format( type=garbage_type, date=garbage_date ), ) except Exception as exc: logger.warning( "Could not send a notification to %s due to %s", user.id, exc ) continue