Compare commits

...

3 commits

Author SHA1 Message Date
dece 0f0ab90062 journee_mondiale: plus option for jmcom 2022-09-13 18:31:49 +02:00
dece 725aa7d681 journee_mondiale: fix various issues 2022-09-13 18:23:54 +02:00
dece 37a75f051d plus: remove shortcut option 2022-09-13 18:23:39 +02:00
3 changed files with 29 additions and 24 deletions

View file

@ -152,11 +152,7 @@
"not_fresh_reply": "It's not from today but here it is: {url}"
},
"plus": {
"commands": ["plus"],
"aliases": {
"plus": "more"
},
"shortcut": "+"
"commands": ["plus"]
},
"randomchoice": {
"commands": ["choose"],

View file

@ -1,4 +1,5 @@
import datetime
from typing import cast
# BS is optional and only for scrapping journee-mondiale.com, thus why we do not
# mark the dependencies flag here.
@ -8,6 +9,7 @@ except ImportError:
BeautifulSoup = None
from edmond.plugin import Plugin
from edmond.plugins.plus import PlusPlugin
from edmond.utils import http_get
@ -31,6 +33,7 @@ class JourneeMondialePlugin(Plugin):
"""
REQUIRED_CONFIGS = ["commands", "dates", "no_entry_reply"]
JMCOM_URL = "https://www.journee-mondiale.com"
def __init__(self, bot):
super().__init__(bot)
@ -45,7 +48,7 @@ class JourneeMondialePlugin(Plugin):
jmcom_reply = ""
if self.config.get("jmcom", False) is True:
jmcom_reply = self.get_jmcom_days()
jmcom_reply = self.get_jmcom_days(event.target)
if jmcom_reply:
self.bot.say(event.target, jmcom_reply)
@ -60,15 +63,16 @@ class JourneeMondialePlugin(Plugin):
today_obs = map(
lambda line: line.split(maxsplit=1)[1],
filter(
lambda line: line.startswith(date_tag), self.config["dates"]
lambda line: line.startswith(date_tag),
self.config["dates"],
),
)
days = ", ".join(today_obs)
return reply
return days
def get_jmcom_days(self) -> str:
def get_jmcom_days(self, target) -> str:
"""Get international days from journee-mondiale.com."""
response = http_get(self.config["url"])
response = http_get(JourneeMondialePlugin.JMCOM_URL)
if not response:
return ""
@ -77,13 +81,22 @@ class JourneeMondialePlugin(Plugin):
try:
items = soup.find("div", id="journeesDuJour").find_all("article")
for item in items:
entries.append({
"url": item.find("a").href,
entries.append(
{
"url": item.find("a")["href"],
"title": item.find("h2").string,
})
}
)
except (ValueError, KeyError):
return ""
# TODO add plus plugin support
if plus_plugin := cast(PlusPlugin, self.bot.get_plugin("plus")):
return ", ".join(map(lambda i: i["title"], entries))
def handler(plus_event):
urls = map(lambda i: i["url"], entries)
self.bot.say(plus_event.target, "".join(urls))
plus_plugin.add_handler(target, handler)
days = ", ".join(map(lambda i: i["title"], entries))
return days

View file

@ -20,16 +20,12 @@ class PlusPlugin(Plugin):
def __init__(self, bot):
super().__init__(bot)
self.handlers = {}
self.shortcut = self.config.get("shortcut")
def on_pubmsg(self, event):
if (
(self.shortcut and event.arguments[0].strip() == self.shortcut)
or self.should_handle_command(event.arguments[0], no_content=True)
):
if not self.should_handle_command(event.arguments[0], no_content=True):
return False
self.process_handler(event)
return True
return False
def process_handler(self, event):
if handler := self.handlers.pop(event.target, None):