2022-06-07 18:11:41 +02:00
|
|
|
import datetime
|
2022-09-13 18:31:49 +02:00
|
|
|
from typing import cast
|
2020-10-09 22:32:36 +02:00
|
|
|
|
2022-11-28 23:29:22 +01:00
|
|
|
from bs4 import BeautifulSoup
|
2022-09-13 18:15:57 +02:00
|
|
|
|
2020-10-09 22:32:36 +02:00
|
|
|
from edmond.plugin import Plugin
|
2022-09-13 18:31:49 +02:00
|
|
|
from edmond.plugins.plus import PlusPlugin
|
2022-09-13 18:15:57 +02:00
|
|
|
from edmond.utils import http_get
|
2020-10-09 22:32:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class JourneeMondialePlugin(Plugin):
|
2022-06-07 18:11:41 +02:00
|
|
|
"""This plugin shows today's international observance.
|
2020-10-09 22:32:36 +02:00
|
|
|
|
2022-06-07 18:11:41 +02:00
|
|
|
It used to fetch data from the website journee-mondiale.com but it is
|
2022-09-13 18:15:57 +02:00
|
|
|
regularly broken so it is now loading a static list as a resource, and the
|
|
|
|
website is fetched only if the user adds the "jmcom" config value to True.
|
|
|
|
|
|
|
|
The local list uses the format "MM-DD Name", one per line, e.g.:
|
2022-06-07 18:11:41 +02:00
|
|
|
|
|
|
|
```
|
|
|
|
01-01 NYE
|
|
|
|
08-03 Something the 3rd of August
|
|
|
|
12-25 Christmas
|
|
|
|
```
|
|
|
|
|
|
|
|
A list can be found on the UN website but it has to be converted by hand:
|
|
|
|
https://www.un.org/en/observances/list-days-weeks
|
|
|
|
"""
|
|
|
|
|
|
|
|
REQUIRED_CONFIGS = ["commands", "dates", "no_entry_reply"]
|
2022-09-13 18:23:54 +02:00
|
|
|
JMCOM_URL = "https://www.journee-mondiale.com"
|
2020-10-09 22:32:36 +02:00
|
|
|
|
|
|
|
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
|
2021-12-02 11:36:30 +01:00
|
|
|
|
2022-11-29 12:59:37 +01:00
|
|
|
reg_days = self.get_registered_days()
|
|
|
|
if reg_days:
|
|
|
|
self.bot.say(event.target, ", ".join(reg_days))
|
2022-09-13 18:15:57 +02:00
|
|
|
|
2022-11-29 12:59:37 +01:00
|
|
|
jmcom_days = []
|
2022-09-13 18:15:57 +02:00
|
|
|
if self.config.get("jmcom", False) is True:
|
2022-11-29 12:59:37 +01:00
|
|
|
jmcom_days = self.get_jmcom_days(event.target)
|
|
|
|
if jmcom_days:
|
|
|
|
# Remove days already in reg_days.
|
|
|
|
lower_reg_days = [day.lower() for day in reg_days]
|
|
|
|
jmcom_days_filtered = [
|
|
|
|
day for day in jmcom_days
|
|
|
|
if day.lower() not in lower_reg_days
|
|
|
|
]
|
|
|
|
self.bot.say(event.target, ", ".join(jmcom_days_filtered))
|
|
|
|
|
|
|
|
if not (reg_days or jmcom_days):
|
2022-09-13 18:15:57 +02:00
|
|
|
self.bot.say(event.target, self.config["no_entry_reply"])
|
|
|
|
return True
|
|
|
|
|
2022-11-29 12:59:37 +01:00
|
|
|
def get_registered_days(self) -> list[str]:
|
2022-09-13 18:15:57 +02:00
|
|
|
"""Get international days for the local list."""
|
2022-06-07 18:11:41 +02:00
|
|
|
now = datetime.datetime.now()
|
|
|
|
date_tag = f"{now.month:02}-{now.day:02}"
|
2022-11-29 12:59:37 +01:00
|
|
|
today_items = map(
|
2022-06-07 18:11:41 +02:00
|
|
|
lambda line: line.split(maxsplit=1)[1],
|
|
|
|
filter(
|
2022-09-13 18:31:49 +02:00
|
|
|
lambda line: line.startswith(date_tag),
|
|
|
|
self.config["dates"],
|
2022-08-09 23:47:28 +02:00
|
|
|
),
|
2022-06-07 18:11:41 +02:00
|
|
|
)
|
2022-11-29 12:59:37 +01:00
|
|
|
return list(today_items)
|
2022-09-13 18:15:57 +02:00
|
|
|
|
2022-11-29 12:59:37 +01:00
|
|
|
def get_jmcom_days(self, target) -> list[str]:
|
2022-09-13 18:15:57 +02:00
|
|
|
"""Get international days from journee-mondiale.com."""
|
2022-09-13 18:23:54 +02:00
|
|
|
response = http_get(JourneeMondialePlugin.JMCOM_URL)
|
2022-09-13 18:15:57 +02:00
|
|
|
if not response:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
soup = BeautifulSoup(response, "html.parser")
|
|
|
|
entries = []
|
|
|
|
try:
|
|
|
|
items = soup.find("div", id="journeesDuJour").find_all("article")
|
|
|
|
for item in items:
|
2022-09-14 10:54:02 +02:00
|
|
|
if (first_link := item.find("a")) is None:
|
|
|
|
continue
|
|
|
|
if (first_title := item.find("h2")) is None:
|
|
|
|
continue
|
|
|
|
link = first_link["href"]
|
|
|
|
title = first_title.string
|
|
|
|
entries.append({"url": link, "title": title})
|
2022-09-13 18:15:57 +02:00
|
|
|
except (ValueError, KeyError):
|
|
|
|
return ""
|
|
|
|
|
2022-09-13 18:31:49 +02:00
|
|
|
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)
|
2022-09-13 18:15:57 +02:00
|
|
|
|
2022-11-29 12:59:37 +01:00
|
|
|
days = map(lambda i: i["title"], entries)
|
|
|
|
return list(days)
|