pollen: Improve (?) date handling

This commit is contained in:
PapaTutuWawa 2021-09-16 14:01:20 +02:00
parent bf077a5af0
commit 2516b8d56c

View File

@ -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=[]):
"""