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.
master
dece 4 years ago
parent 828802fdbe
commit f4e884e9d7

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

@ -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": {

@ -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,13 +80,17 @@ 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:
ident = words[1] if no_content:
content = " ".join(words[2:-1]) ident = " ".join(words[1:-1])
content = ""
else:
ident = words[1]
content = " ".join(words[2:-1])
return Command(ident, content) return Command(ident, content)

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

@ -0,0 +1,5 @@
# Wikipedia
wikipedia
# Journée Mondiale
beautifulsoup4
Loading…
Cancel
Save