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.
This commit is contained in:
dece 2020-10-09 22:32:36 +02:00
parent 828802fdbe
commit f4e884e9d7
5 changed files with 52 additions and 6 deletions

View file

@ -16,7 +16,7 @@ Missing features
- [ ] Handle compliments - [ ] Handle compliments
- [ ] Handle - [ ] Handle
- [x] Horoscope - [x] Horoscope
- [ ] "Journee mondiale" - [x] "Journee mondiale"
- [ ] Mug - [ ] Mug
- [ ] Music - [ ] Music
- [ ] Opinions - [ ] Opinions

View file

@ -22,6 +22,10 @@
"url": "http://zwergf.elynx.fr/bots/horobot/", "url": "http://zwergf.elynx.fr/bots/horobot/",
"delay": 2 "delay": 2
}, },
"journeemondiale": {
"commands": ["journée mondiale"],
"url": "https://www.journee-mondiale.com/"
},
"mood": { "mood": {
"questions": ["how are you?"], "questions": ["how are you?"],
"greetings": { "greetings": {

View file

@ -70,9 +70,9 @@ class Plugin:
return True return True
return False 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.""" """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", []) commands = self.config.get("commands", [])
if command and any(c == command.ident for c in commands): if command and any(c == command.ident for c in commands):
self.command = command self.command = command
@ -80,11 +80,15 @@ class Plugin:
return True return True
return False 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.""" """Return a command ID if this message is a command."""
words = message.split() words = message.split()
command_suffix = self.config["command_suffix"] command_suffix = self.config["command_suffix"]
if words[0].lower() in self.bot.names and words[-1] == command_suffix: if words[0].lower() in self.bot.names and words[-1] == command_suffix:
if no_content:
ident = " ".join(words[1:-1])
content = ""
else:
ident = words[1] ident = words[1]
content = " ".join(words[2:-1]) content = " ".join(words[2:-1])
return Command(ident, content) return Command(ident, content)

View file

@ -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

View file

@ -0,0 +1,5 @@
# Wikipedia
wikipedia
# Journée Mondiale
beautifulsoup4