From f4e884e9d7913452e30de21038f5128f4e72ea5b Mon Sep 17 00:00:00 2001 From: dece Date: Fri, 9 Oct 2020 22:32:36 +0200 Subject: [PATCH] journee_mondiale: add plugin Add support for commands in multiple words by letting a plugin decide if the message content should be not split between ident/content. --- README.md | 2 +- config.json.example | 4 ++++ edmond/plugin.py | 14 ++++++++----- edmond/plugins/journee_mondiale.py | 33 ++++++++++++++++++++++++++++++ edmond/plugins/requirements.txt | 5 +++++ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 edmond/plugins/journee_mondiale.py create mode 100644 edmond/plugins/requirements.txt diff --git a/README.md b/README.md index 8864907..b01e875 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Missing features - [ ] Handle compliments - [ ] Handle - [x] Horoscope -- [ ] "Journee mondiale" +- [x] "Journee mondiale" - [ ] Mug - [ ] Music - [ ] Opinions diff --git a/config.json.example b/config.json.example index 46d7318..84045b1 100644 --- a/config.json.example +++ b/config.json.example @@ -22,6 +22,10 @@ "url": "http://zwergf.elynx.fr/bots/horobot/", "delay": 2 }, + "journeemondiale": { + "commands": ["journée mondiale"], + "url": "https://www.journee-mondiale.com/" + }, "mood": { "questions": ["how are you?"], "greetings": { diff --git a/edmond/plugin.py b/edmond/plugin.py index c61d1ee..269d2e7 100644 --- a/edmond/plugin.py +++ b/edmond/plugin.py @@ -70,9 +70,9 @@ class Plugin: return True return False - def should_handle_command(self, message): + def should_handle_command(self, message, no_content=False): """Store Command in object and return True if it should handle it.""" - command = self.parse_command(message) + command = self.parse_command(message, no_content=no_content) commands = self.config.get("commands", []) if command and any(c == command.ident for c in commands): self.command = command @@ -80,13 +80,17 @@ class Plugin: return True return False - def parse_command(self, message): + def parse_command(self, message, no_content=False): """Return a command ID if this message is a command.""" words = message.split() command_suffix = self.config["command_suffix"] if words[0].lower() in self.bot.names and words[-1] == command_suffix: - ident = words[1] - content = " ".join(words[2:-1]) + if no_content: + ident = " ".join(words[1:-1]) + content = "" + else: + ident = words[1] + content = " ".join(words[2:-1]) return Command(ident, content) diff --git a/edmond/plugins/journee_mondiale.py b/edmond/plugins/journee_mondiale.py new file mode 100644 index 0000000..6872dfa --- /dev/null +++ b/edmond/plugins/journee_mondiale.py @@ -0,0 +1,33 @@ +try: + from bs4 import BeautifulSoup + DEPENDENCIES_FOUND = True +except ImportError: + DEPENDENCIES_FOUND = False + +from edmond.plugin import Plugin +from edmond.utils import http_get + + +class JourneeMondialePlugin(Plugin): + """This plugin gets today's international observance (french).""" + + REQUIRED_CONFIGS = ["commands", "url"] + + def __init__(self, bot): + super().__init__(bot) + + def on_pubmsg(self, event): + 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() + return True + soup = BeautifulSoup(response, "html.parser") + try: + jm = soup.find("div", id="journeesDuJour").find("h2").string + except (ValueError, KeyError): + self.signal_failure() + return True + self.bot.say(event.target, jm) + return True diff --git a/edmond/plugins/requirements.txt b/edmond/plugins/requirements.txt new file mode 100644 index 0000000..f8d0495 --- /dev/null +++ b/edmond/plugins/requirements.txt @@ -0,0 +1,5 @@ +# Wikipedia +wikipedia + +# Journée Mondiale +beautifulsoup4