From 0f0ab90062066d725f24cff2ea54e181959cdcea Mon Sep 17 00:00:00 2001 From: dece Date: Tue, 13 Sep 2022 18:31:49 +0200 Subject: [PATCH] journee_mondiale: plus option for jmcom --- config.json.example | 5 +---- edmond/plugins/journee_mondiale.py | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/config.json.example b/config.json.example index 607f4d2..c5efa58 100644 --- a/config.json.example +++ b/config.json.example @@ -152,10 +152,7 @@ "not_fresh_reply": "It's not from today but here it is: {url}" }, "plus": { - "commands": ["plus"], - "aliases": { - "plus": "more" - } + "commands": ["plus"] }, "randomchoice": { "commands": ["choose"], diff --git a/edmond/plugins/journee_mondiale.py b/edmond/plugins/journee_mondiale.py index 480d743..078cbe8 100644 --- a/edmond/plugins/journee_mondiale.py +++ b/edmond/plugins/journee_mondiale.py @@ -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 @@ -46,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) @@ -61,13 +63,14 @@ 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 days - def get_jmcom_days(self) -> str: + def get_jmcom_days(self, target) -> str: """Get international days from journee-mondiale.com.""" response = http_get(JourneeMondialePlugin.JMCOM_URL) if not response: @@ -80,14 +83,20 @@ class JourneeMondialePlugin(Plugin): for item in items: entries.append( { - "url": item.find("a").href, + "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")): + + 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