diff --git a/dagelijks_overzicht.py b/dagelijks_overzicht.py index 66fe0e1..cb8dda7 100644 --- a/dagelijks_overzicht.py +++ b/dagelijks_overzicht.py @@ -27,6 +27,9 @@ from collections import defaultdict from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from imap_utils import list_folders, quote_mailbox +from babel.dates import format_date +import babel +import random # Mappen die NIET in het overzicht komen (systeem + inbox zelf) UITSLUITINGEN = { @@ -117,6 +120,16 @@ def fetch_body(data) -> bytes | None: return item[1] return None +def invalid_locale(code): + return babel.Locale(code).language_name is None + +def get_random_locale() -> (str, str, str): + """Return the locale code, the local name and the english name of a language.""" + all_ids = babel.localedata.locale_identifiers() + code = all_ids[random.randrange(0, len(all_ids))] + while (invalid_locale(code)): + code = all_ids[random.randrange(0, len(all_ids))] + return (code, babel.Locale(code).language_name, babel.Locale(code).english_name) # ── IMAP ophalen ────────────────────────────────────────────────────────────── @@ -253,9 +266,11 @@ def bouw_html(berichten: dict[str, list[dict]], zoekdatum: date) -> str: # ── e-mail versturen ────────────────────────────────────────────────────────── def stuur_overzicht(html: str, zoekdatum: date, config: dict): - datum_nl = zoekdatum.strftime("%-d %B %Y") + datum_nl = format_date(zoekdatum, "EEEE d MMMM yyyy", locale="nl") # zoekdatum.strftime("%-d %B %Y") + random_locale, random_lang, random_elang = get_random_locale() + datum_bl = format_date(zoekdatum, locale=random_locale) msg = MIMEMultipart("alternative") - msg["Subject"] = f"Mailbak {datum_nl}" + msg["Subject"] = f"Mailoverzicht van {datum_bl} ({random_lang} - {random_elang}) of {datum_nl}" msg["From"] = config["from"] msg["To"] = config["to"] msg.attach(MIMEText(html, "html", "utf-8"))