2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
Copyright (C) 2021 Alexander "PapaTutuWawa"
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
# TODO: remove Alle sollte jede subscription entfernen
|
|
|
|
# TODO: Deal with weird casing and typos
|
|
|
|
# TODO: pollen hilfe <CMD> should print help per command
|
|
|
|
import asyncio
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
from mira.module import BaseModule
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import aioxmpp
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
NAME = "pollen"
|
|
|
|
|
|
|
|
API_ENDPOINT = "https://allergie.hexal.de/pollenflug/vorhersage/load_pollendaten.php"
|
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
def api_query(plz, date):
|
2021-06-15 18:33:24 +00:00
|
|
|
return API_ENDPOINT + "?datum={}&plz={}".format(date, plz)
|
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
def intensity_str(intensity):
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
Map the intensity value to a human readable string
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
|
|
|
return ("Kein Pollenflug", "Schwach", "Mittelschwer", "Stark")[intensity]
|
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
class PollenModule(BaseModule):
|
|
|
|
__instance = None
|
|
|
|
_subcommand_table = {}
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
_pollen_data = {} # PLZ -> Date -> Type -> Severity
|
2021-09-16 12:01:20 +00:00
|
|
|
_oldest_date_notified = {} # JID -> Oldest date sent out
|
2021-06-12 22:26:40 +00:00
|
|
|
_sleep_duration = 100
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_instance(base, **kwargs):
|
|
|
|
if PollenModule.__instance == None:
|
|
|
|
PollenModule(base, **kwargs)
|
|
|
|
|
|
|
|
return PollenModule.__instance
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
def __init__(self, base, **kwargs):
|
|
|
|
if PollenModule.__instance != None:
|
2021-06-15 18:33:24 +00:00
|
|
|
raise Exception("Trying to init singleton twice")
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
super().__init__(base, **kwargs)
|
|
|
|
PollenModule.__instance = self
|
|
|
|
self._subcommand_table = {
|
2021-06-15 18:33:24 +00:00
|
|
|
"subscribe": self._subscribe,
|
|
|
|
"unsubscribe": self._unsubscribe,
|
|
|
|
"hilfe": self._help,
|
|
|
|
"*": self._any,
|
2021-06-12 22:26:40 +00:00
|
|
|
}
|
2021-06-15 18:33:24 +00:00
|
|
|
self._sleep_duration = self.get_option("sleep_duration", 12 * 3600)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
# Load data
|
|
|
|
# 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.
|
2021-09-16 12:01:20 +00:00
|
|
|
self._oldest_date_notified = self._stm.get_data("oldest_date_notified")
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
periodic = loop.create_task(self._request_pollen_data(True))
|
|
|
|
|
|
|
|
def notification_body(self, plz, date, allergies):
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
Format a message that will be sent to the user
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
|
|
|
msg = "{} (*{}*)\n".format(date, plz)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
if "Alle" in [x["type"] for x in allergies]:
|
2021-06-12 22:26:40 +00:00
|
|
|
allergies = list(self._pollen_data[plz][date].keys)
|
|
|
|
|
2021-06-13 11:17:39 +00:00
|
|
|
count = 0
|
2021-06-12 22:26:40 +00:00
|
|
|
for allergy in allergies:
|
2021-06-15 18:33:24 +00:00
|
|
|
intensity = self._pollen_data[plz][date][allergy["type"]]
|
|
|
|
if intensity >= allergy["level"]:
|
|
|
|
msg += "{}: {}".format(allergy["type"], intensity_str(intensity))
|
2021-06-13 11:17:39 +00:00
|
|
|
count += 1
|
|
|
|
|
|
|
|
if count == 0:
|
2021-06-15 18:33:24 +00:00
|
|
|
return ""
|
2021-06-12 22:26:40 +00:00
|
|
|
return msg
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
def _is_notified(self, jid, date):
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
Returns True, when the pollen data for the date of date has
|
|
|
|
already been sent to jid.
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-09-16 12:01:20 +00:00
|
|
|
if not jid in self._oldest_date_notified:
|
2021-06-12 22:26:40 +00:00
|
|
|
return False
|
|
|
|
|
2021-09-16 12:01:20 +00:00
|
|
|
return int(date.replace("-", "")) <= self._oldest_date_notified[jid]
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
def _set_dates_notified(self, jid, new_dates):
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
Marks a date as being sent to a user. Also ensures that
|
|
|
|
there are at maximum 7 days in this list
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-09-16 12:01:20 +00:00
|
|
|
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)
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
def _broadcast_jid(self, jid, filter_allergies=[]):
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
Sends the pollen data to jid. If filter_allergies is set, then
|
|
|
|
only the data for the pollen types in filter_allergies will be sent.
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
|
|
|
for plz, data in self._sum.get_subscriptions_for_jid(jid).items():
|
2021-06-13 11:17:39 +00:00
|
|
|
if filter_allergies:
|
|
|
|
allergies = filter_allergies
|
|
|
|
else:
|
2021-06-15 18:33:24 +00:00
|
|
|
allergies = data["data"]
|
2021-06-12 22:26:40 +00:00
|
|
|
notified = []
|
|
|
|
|
|
|
|
for date in self._pollen_data[plz]:
|
|
|
|
if self._is_notified(jid, date) and not filter_allergies:
|
|
|
|
continue
|
|
|
|
notified.append(date)
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
msg = self.notification_body(plz, date, allergies)
|
2021-06-13 11:17:39 +00:00
|
|
|
if msg:
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(aioxmpp.JID.fromstr(jid), msg)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
if notified:
|
|
|
|
self._set_dates_notified(jid, notified)
|
|
|
|
|
|
|
|
def _broadcast_all(self):
|
|
|
|
for plz in self._pollen_data:
|
2021-06-15 18:33:24 +00:00
|
|
|
for jid, data in self._sum.get_subscriptions_for_keyword(plz).items():
|
2021-06-12 22:26:40 +00:00
|
|
|
notified = []
|
|
|
|
|
|
|
|
for date in self._pollen_data[plz]:
|
|
|
|
if self._is_notified(jid, date):
|
|
|
|
continue
|
|
|
|
notified.append(date)
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(
|
|
|
|
aioxmpp.JID.fromstr(jid),
|
|
|
|
self.notification_body(plz, date, data),
|
|
|
|
)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
if notified:
|
|
|
|
self._set_dates_notified(jid, notified)
|
|
|
|
|
|
|
|
async def _request_pollen_data(self, loop):
|
|
|
|
while True:
|
2021-06-15 18:33:24 +00:00
|
|
|
today = datetime.date.today().strftime("%Y-%m-%d")
|
2021-06-12 22:26:40 +00:00
|
|
|
for plz in self._sum.get_subscription_keywords():
|
|
|
|
req = requests.get(api_query(plz, today))
|
2021-06-15 18:33:24 +00:00
|
|
|
data = req.json()["content"]
|
|
|
|
pollen = data["pollen"]
|
|
|
|
for date in data["values"]:
|
2021-06-12 22:26:40 +00:00
|
|
|
if not plz in self._pollen_data:
|
|
|
|
self._pollen_data[plz] = {}
|
|
|
|
if not date in self._pollen_data[plz]:
|
|
|
|
self._pollen_data[plz][date] = {}
|
|
|
|
|
|
|
|
for i in range(len(pollen)):
|
|
|
|
type_ = pollen[i]
|
2021-06-15 18:33:24 +00:00
|
|
|
self._pollen_data[plz][date][type_] = int(
|
|
|
|
data["values"][date][i]
|
|
|
|
)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
# For when we want to just refresh data as someone just subscribed
|
|
|
|
if not loop:
|
|
|
|
break
|
|
|
|
|
|
|
|
self._broadcast_all()
|
|
|
|
await asyncio.sleep(self._sleep_duration)
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
async def _subscribe(self, cmd, msg):
|
2021-06-13 11:17:39 +00:00
|
|
|
if len(cmd) < 3 or len(cmd) > 4:
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(
|
|
|
|
msg.from_, "Verwendung: pollen add <PLZ> <Allergen> [<Level>]"
|
|
|
|
)
|
2021-06-12 22:26:40 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
plz = cmd[1]
|
|
|
|
allergy = cmd[2]
|
|
|
|
jid = str(msg.from_.bare())
|
2021-06-13 11:17:39 +00:00
|
|
|
level = int(cmd[3]) if len(cmd) == 4 else 0
|
2021-06-12 22:26:40 +00:00
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
if self._sum.is_subscribed_to_data(jid, plz, "Alle"):
|
|
|
|
self.send_message(msg.from_, "Du hast schon alle Allergene abonniert")
|
2021-06-12 22:26:40 +00:00
|
|
|
return
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-06-13 11:17:39 +00:00
|
|
|
if self._sum.is_subscribed_to_data(jid, plz, allergy):
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(msg.from_, "Du hast das schon abonniert")
|
2021-06-13 11:17:39 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if level < 0 or level > 3:
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(msg.from_, "Das Level muss zwischen 0 und 3 liegen")
|
2021-06-13 11:17:39 +00:00
|
|
|
return
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
self._sum.append_subscription_data(
|
|
|
|
jid, plz, {"type": allergy, "level": level}
|
|
|
|
)
|
|
|
|
self.send_message(msg.from_, "Du erhälst nun Pollenmeldungen zu %s" % allergy)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
# Just some bandwidth saving measure
|
|
|
|
if plz not in self._pollen_data:
|
|
|
|
await self._request_pollen_data(False)
|
2021-06-15 18:33:24 +00:00
|
|
|
self._broadcast_jid(jid, [{"type": allergy, "level": level}])
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
async def _unsubscribe(self, cmd, msg):
|
|
|
|
if len(cmd) != 3:
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(msg.from_, "Verwendung: pollen remove <PLZ> <Allergen>")
|
2021-06-12 22:26:40 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
plz = cmd[1]
|
|
|
|
allergy = cmd[2]
|
|
|
|
jid = str(msg.from_.bare())
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
if allergy == "Alle":
|
|
|
|
self._sum.filter_subscription_data_items(jid, plz, lambda x: False)
|
|
|
|
self.send_message(msg.from_, "Du erhälst keine Pollenmeldungen mehr")
|
2021-06-12 22:26:40 +00:00
|
|
|
return
|
2021-06-15 18:33:24 +00:00
|
|
|
|
2021-09-16 11:53:49 +00:00
|
|
|
if not self._sum.is_subscribed_to_data_func(
|
2021-06-15 18:33:24 +00:00
|
|
|
jid, plz, lambda x: x["type"] == allergy
|
|
|
|
):
|
|
|
|
self.send_message(msg.from_, "Du hast %s nicht abonniert" % (allergy))
|
2021-06-13 11:17:39 +00:00
|
|
|
return
|
|
|
|
|
2021-06-15 18:33:24 +00:00
|
|
|
self._sum.filter_subscription_data_items(
|
|
|
|
str(msg.from_.bare()), plz, lambda x: x["type"] != allergy
|
|
|
|
)
|
|
|
|
self.send_message(
|
|
|
|
msg.from_, "Du erhälst nun keine Pollenmeldungen zu %s mehr" % (allergy)
|
|
|
|
)
|
2021-06-12 22:26:40 +00:00
|
|
|
|
|
|
|
async def _help(self, cmd, msg):
|
2021-06-15 18:33:24 +00:00
|
|
|
body = """Verfügbare Befehle:
|
2021-06-13 11:17:39 +00:00
|
|
|
pollen subscribe <PLZ> <Typ> [<Min. Level>]
|
|
|
|
pollen unsubscribe <PLZ> <Typ>
|
2021-06-12 22:26:40 +00:00
|
|
|
pollen hilfe
|
|
|
|
|
2021-06-13 11:17:39 +00:00
|
|
|
Pollentypen: Ambrosia, Ampfer, Beifuß, Birke, Buche, Eiche, Erle, Esche, Gräser, Hasel, Pappel, Roggen, Ulme, Wegerich, Weide, Alle (Alias für alle Pollentypen)
|
|
|
|
Level: 0 (Kein Pollenflug) - 3 (Starker Pollenflug)
|
2021-06-15 18:33:24 +00:00
|
|
|
"""
|
2021-06-12 22:26:40 +00:00
|
|
|
self.send_message(msg.from_, body)
|
|
|
|
|
2021-06-13 17:42:45 +00:00
|
|
|
async def _any(self, cmd, msg):
|
2021-06-15 18:33:24 +00:00
|
|
|
self.send_message(msg.from_, "Unbekannter Befehl")
|
2021-06-13 17:42:45 +00:00
|
|
|
await self._help(cmd, msg)
|
2021-06-15 18:33:24 +00:00
|
|
|
|
|
|
|
|
2021-06-12 22:26:40 +00:00
|
|
|
def get_instance(base, **kwargs):
|
|
|
|
return PollenModule.get_instance(base, **kwargs)
|