From fdf37cddc2718b6ff429ab51872fe7b7971a0edc Mon Sep 17 00:00:00 2001 From: Mindiell Date: Sat, 6 Apr 2024 22:27:24 +0200 Subject: [PATCH] =?UTF-8?q?Reprise=20de=20la=20gestion=20des=20=C3=A9v?= =?UTF-8?q?=C3=A9nements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- events/__init__.py | 7 +++++-- events/event.py | 43 +++++++++++++++++++++--------------------- events/mastodon.py | 22 +++++++++++++++++++++ events/notification.py | 27 ++++++++++++++++++++++++++ hebdobot.py | 42 +++++++++++++++++++++++------------------ settings.py | 9 +++++---- 6 files changed, 105 insertions(+), 45 deletions(-) create mode 100644 events/mastodon.py create mode 100644 events/notification.py diff --git a/events/__init__.py b/events/__init__.py index 005f15d..522ea55 100644 --- a/events/__init__.py +++ b/events/__init__.py @@ -1,5 +1,8 @@ -from .event import Event +from .notification import Notification +from .mastodon import Mastodon + events = ( - Event(), + Notification(), + Mastodon(), ) diff --git a/events/event.py b/events/event.py index bdea709..45f3fdf 100644 --- a/events/event.py +++ b/events/event.py @@ -1,31 +1,32 @@ """ -Script used to... +Script used to send notifications for review on IRC. """ +from dataclasses import dataclass import time -import settings - -SCHEDULER_DELAY = 60 +EXECUTE_EVERY = 1 +EXECUTE_AT = 2 +EXECUTE_AFTER = 3 +@dataclass class Event: - def __init__(self): - self.scheduler = None - self.callback = None - self.event_id = 1 - self.last_update = time.gmtime() + callback : str = "" + scheduler_type : int = 1 + scheduler_delay : int = 60 - def init_scheduler(self, scheduler, callback): + def init(self, scheduler, callback_func): self.scheduler = scheduler - self.callback = callback - self.scheduler.execute_every(SCHEDULER_DELAY, self.do_something) + self.callback_func = callback_func + if self.scheduler_type == EXECUTE_EVERY: + self.scheduler.execute_every(self.scheduler_delay, self._run) + elif self.scheduler_type == EXECUTE_AT: + self.scheduler.execute_at(self.scheduler_delay, self._run) + elif self.scheduler_type == EXECUTE_AFTER: + self.scheduler.execute_after(self.scheduler_delay, self._run) - def do_something(self): - now = time.localtime() - # Gestion du rappel avant la revue hebdomadaire - for reminder in settings.CRON_REVIEW_REMINDERS: - if now.tm_hour == reminder[0] and now.tm_min == reminder[1]: - # On calcule le nombre de minutes avant la revue hebdomadaire - minutes = (settings.REVIEW_HOUR[0] - now.tm_hour) * 60 - minutes += settings.REVIEW_HOUR[1] - now.tm_min - self.callback(minutes) + def _run(self): + self.run(time.localtime()) + + def run(self, now): + pass diff --git a/events/mastodon.py b/events/mastodon.py new file mode 100644 index 0000000..9a8d9a2 --- /dev/null +++ b/events/mastodon.py @@ -0,0 +1,22 @@ +""" +Script used to send message for review on Mastodon. +""" +from dataclasses import dataclass + +from events import event +import settings + + +@dataclass +class Mastodon(event.Event): + callback : str = "mastodon" + + def run(self, now): + """ + Gestion du message avant la revue hebdomadaire + """ + if ( + now.tm_hour == settings.REVIEW_MASTODON[0] + and now.tm_min == settings.REVIEW_MASTODON[1] + ): + self.callback_func() diff --git a/events/notification.py b/events/notification.py new file mode 100644 index 0000000..04b8a91 --- /dev/null +++ b/events/notification.py @@ -0,0 +1,27 @@ +""" +Script used to send notifications for review on IRC. +""" +from dataclasses import dataclass + +from events import event +import settings + +SCHEDULER_DELAY = 60 + + +@dataclass +class Notification(event.Event): + callback : str = "notifications" + scheduler_type : int = event.EXECUTE_EVERY + scheduler_delay : int = SCHEDULER_DELAY + + def run(self, now): + """ + Gestion du rappel avant la revue hebdomadaire + """ + for reminder in settings.REVIEW_REMINDERS: + if now.tm_hour == reminder[0] and now.tm_min == reminder[1]: + # On renvoie le nombre de minutes avant la revue hebdomadaire + minutes = (settings.REVIEW_HOUR[0] - now.tm_hour) * 60 + minutes += settings.REVIEW_HOUR[1] - now.tm_min + self.callback_func(minutes) diff --git a/hebdobot.py b/hebdobot.py index aab19a3..f9ee5f9 100644 --- a/hebdobot.py +++ b/hebdobot.py @@ -19,7 +19,7 @@ class HebdoBot(IrcBot): self.review = Review() for event in events: - event.init_scheduler(self.reactor.scheduler, self.callback) + event.init(self.reactor.scheduler, getattr(self, event.callback)) logger.info( "--==============================INIT====" @@ -28,25 +28,25 @@ class HebdoBot(IrcBot): logger.info(f"Hebdobot {self.VERSION}") - def callback(self, minutes): + def notifications(self, minutes): """ - Envoi des notification sur IRC et les réseaux sociaux. + Envoi des notifications sur IRC. """ - if minutes == 15: - # Envoi sur IRC - self.send( - self.channel, - f"Plus que {minutes} minutes avant le début de la revue hebdomdaire.", - ) - # Envoi sur Mastodon - # TODO : Gérer mastodon - # ~ mastodon.send( - # ~ "Revue hebdomadaire April à 12h sur notre salon : " - # ~ "http://april.org/salon-irc-de-lapril\nCette revue ne dure qu'une " - # ~ "quinzaine de minutes et permet d'avoir une idée des actions en cours " - # ~ "et à venir.\nVous pouvez aussi juste nous faire un coucou. Et ça met " - # ~ "en appétit :)", - # ~ ) + self.send(self.channel, f"Revue hebdomadaire dans {minutes} minutes.") + + def mastodon(self): + """ + Envoi d'une notification sur Mastodon. + """ + # TODO : Gérer mastodon + # ~ mastodon.send( + # ~ "Revue hebdomadaire April à 12h sur notre salon : " + # ~ "http://april.org/salon-irc-de-lapril\nCette revue ne dure qu'une " + # ~ "quinzaine de minutes et permet d'avoir une idée des actions en cours " + # ~ "et à venir.\nVous pouvez aussi juste nous faire un coucou. Et ça met " + # ~ "en appétit :)", + # ~ ) + pass def on_privmsg(self, connection, event): """ @@ -73,6 +73,12 @@ class HebdoBot(IrcBot): if hook.process(self, channel, sender, message): break + def on_join(self, connection, event): + """ + Lorsqu'il se connecte, le bot dit bonjour. + """ + self.send(event.target, "Bonjour tout le monde, je suis là :)") + def send(self, target, message): if self.review.is_started and target == self.channel: self.review.add_message(self.nickname, message) diff --git a/settings.py b/settings.py index 6759e82..e4fb647 100644 --- a/settings.py +++ b/settings.py @@ -22,7 +22,7 @@ IRC_DELAY = float(os.environ.get("IRC_DELAY", 0.5)) # Délai entre plusieurs mes USER_ALIASES = os.environ.get("USER_ALIASES", os.path.join(BASE_DIR, "users.conf")) # Review -REVIEW_HOUR = (16, 30) +REVIEW_HOUR = (12, 00) REVIEW_DIRECTORY = os.environ.get("REVIEW_DIRECTORY", os.path.join(BASE_DIR, "reviews")) REVIEW_STATS = os.environ.get("REVIEW_STATS", os.path.join(REVIEW_DIRECTORY, "reviewstats.csv")) @@ -31,9 +31,10 @@ PASTEBIN_URL = "https://paste.chapril.org/" # Values : 5min, 10min, 1hour, 1day, 1week, 1month, 1year, never PASTEBIN_EXPIRATION = "1week" -# Cron -CRON_REVIEW_REMINDERS = [(11, 45)] -CRON_STOP_BOT = [(16, 15)] +# Scheduler +REVIEW_REMINDERS = [(11, 30), (11, 45), (11, 55)] +REVIEW_MASTODON = (11, 45) +STOP_BOT = (14, 00) # Mastodon MASTODON_SERVER = ""