From 2516b8d56c5b311c7f7e3a71bb44f36b5849aa9c Mon Sep 17 00:00:00 2001 From: Alexander PapaTutuWawa Date: Thu, 16 Sep 2021 14:01:20 +0200 Subject: [PATCH] pollen: Improve (?) date handling --- papatutuwawa/mira/modules/pollen.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/papatutuwawa/mira/modules/pollen.py b/papatutuwawa/mira/modules/pollen.py index 4584e0a..ba835d0 100644 --- a/papatutuwawa/mira/modules/pollen.py +++ b/papatutuwawa/mira/modules/pollen.py @@ -46,7 +46,7 @@ class PollenModule(BaseModule): _subcommand_table = {} _pollen_data = {} # PLZ -> Date -> Type -> Severity - _dates_notified = {} # JID -> [Dates already sent out] + _oldest_date_notified = {} # JID -> Oldest date sent out _sleep_duration = 100 @staticmethod @@ -74,7 +74,7 @@ class PollenModule(BaseModule): # NOTE: Since we request the pollen_data at start anyway, we won't # need to save it. We only save dates_notified to prevent # sending one notification too many. - self._dates_notified = self._stm.get_data("dates_notified") + self._oldest_date_notified = self._stm.get_data("oldest_date_notified") loop = asyncio.get_event_loop() periodic = loop.create_task(self._request_pollen_data(True)) @@ -104,24 +104,20 @@ class PollenModule(BaseModule): Returns True, when the pollen data for the date of date has already been sent to jid. """ - if not jid in self._dates_notified: + if not jid in self._oldest_date_notified: return False - return date in self._dates_notified[jid] + return int(date.replace("-", "")) <= self._oldest_date_notified[jid] def _set_dates_notified(self, jid, new_dates): """ Marks a date as being sent to a user. Also ensures that there are at maximum 7 days in this list """ - if not jid in self._dates_notified: - self._dates_notified[jid] = new_dates - self._stm.set_data("dates_notified", self._dates_notified) - return - - self._dates_notified[jid] += new_dates - self._dates_notified[jid] = self._dates_notified[jid][-7:] - self._stm.set_data("dates_notified", self._dates_notified) + date_ints = [int(date.replace("-", "") for date in new_dates)] + oldest_date = max(date_ints) + self._oldest_date_notified[jid] = oldest_date + self._stm.set_data("oldest_date_notified", self._oldest_date_notified) def _broadcast_jid(self, jid, filter_allergies=[]): """