journee_mondiale: ditch the site for a static list

master
dece 2 years ago
parent 7a2e011272
commit edbc35aa19

@ -77,8 +77,9 @@
"deny_message": "No."
},
"journeemondiale": {
"commands": ["journée mondiale"],
"url": "https://www.journee-mondiale.com/"
"commands": ["international day"],
"dates": ["01-01 NYE"],
"no_entry_reply": "Nothing special today."
},
"meteofrance": {
"commands": ["météo"],

@ -1,17 +1,26 @@
try:
from bs4 import BeautifulSoup
DEPENDENCIES_FOUND = True
except ImportError:
DEPENDENCIES_FOUND = False
import datetime
from edmond.plugin import Plugin
from edmond.utils import http_get
class JourneeMondialePlugin(Plugin):
"""This plugin gets today's international observance (french)."""
"""This plugin shows today's international observance.
REQUIRED_CONFIGS = ["commands", "url"]
It used to fetch data from the website journee-mondiale.com but it is
regularly broken so it is now loading a static list as a resource. This
list uses the format "MM-DD Name", one per line, e.g.:
```
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"]
def __init__(self, bot):
super().__init__(bot)
@ -20,20 +29,17 @@ class JourneeMondialePlugin(Plugin):
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(event.target)
return True
soup = BeautifulSoup(response, "html.parser")
try:
jm = soup.find("div", id="journeesDuJour").find("h2").string
except (ValueError, KeyError):
self.signal_failure(event.target)
return True
if jm:
self.bot.say(event.target, jm)
else:
self.signal_failure(event.target)
now = datetime.datetime.now()
date_tag = f"{now.month:02}-{now.day:02}"
today_obs = map(
lambda line: line.split(maxsplit=1)[1],
filter(
lambda line: line.startswith(date_tag),
self.config["dates"]
)
)
reply = ", ".join(today_obs)
if not reply:
reply = self.config["no_entry_reply"]
self.bot.say(event.target, reply)
return True

Loading…
Cancel
Save