From edbc35aa190015843ddb158ce59f578e37340549 Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 7 Jun 2022 18:11:41 +0200 Subject: [PATCH] journee_mondiale: ditch the site for a static list --- config.json.example | 5 +-- edmond/plugins/journee_mondiale.py | 54 +++++++++++++++++------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/config.json.example b/config.json.example index fb135c8..4a05023 100644 --- a/config.json.example +++ b/config.json.example @@ -77,8 +77,9 @@ "deny_message": "No." }, "journeemondiale": { - "commands": ["journée mondiale"], - "url": "https://www.journee-mondiale.com/" + "commands": ["international day"], + "dates": ["01-01 NYE"], + "no_entry_reply": "Nothing special today." }, "meteofrance": { "commands": ["météo"], diff --git a/edmond/plugins/journee_mondiale.py b/edmond/plugins/journee_mondiale.py index ede6a72..fde4c3b 100644 --- a/edmond/plugins/journee_mondiale.py +++ b/edmond/plugins/journee_mondiale.py @@ -1,17 +1,26 @@ -try: - from bs4 import BeautifulSoup - DEPENDENCIES_FOUND = True -except ImportError: - DEPENDENCIES_FOUND = False +import datetime from edmond.plugin import Plugin -from edmond.utils import http_get class JourneeMondialePlugin(Plugin): - """This plugin gets today's international observance (french).""" + """This plugin shows today's international observance. - REQUIRED_CONFIGS = ["commands", "url"] + It used to fetch data from the website journee-mondiale.com but it is + regularly broken so it is now loading a static list as a resource. This + list uses the format "MM-DD Name", one per line, e.g.: + + ``` + 01-01 NYE + 08-03 Something the 3rd of August + 12-25 Christmas + ``` + + A list can be found on the UN website but it has to be converted by hand: + https://www.un.org/en/observances/list-days-weeks + """ + + REQUIRED_CONFIGS = ["commands", "dates", "no_entry_reply"] def __init__(self, bot): super().__init__(bot) @@ -20,20 +29,17 @@ class JourneeMondialePlugin(Plugin): if not self.should_handle_command(event.arguments[0], no_content=True): return False - response = http_get(self.config["url"]) - if not response: - self.signal_failure(event.target) - return True - - soup = BeautifulSoup(response, "html.parser") - try: - jm = soup.find("div", id="journeesDuJour").find("h2").string - except (ValueError, KeyError): - self.signal_failure(event.target) - return True - - if jm: - self.bot.say(event.target, jm) - else: - self.signal_failure(event.target) + now = datetime.datetime.now() + date_tag = f"{now.month:02}-{now.day:02}" + today_obs = map( + lambda line: line.split(maxsplit=1)[1], + filter( + lambda line: line.startswith(date_tag), + self.config["dates"] + ) + ) + reply = ", ".join(today_obs) + if not reply: + reply = self.config["no_entry_reply"] + self.bot.say(event.target, reply) return True