From 6be934e374e5a05e886c21e6c4591e75c1633818 Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Wed, 14 Feb 2024 23:37:43 +0100 Subject: [PATCH] implemented functions to get human readable dates --- publish_meetups/routines/__init__.py | 8 +++-- publish_meetups/routines/development.py | 16 ++++++++-- publish_meetups/utils/__init__.py | 10 ++++++- publish_meetups/utils/config.py | 2 ++ publish_meetups/utils/date.py | 40 +++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 publish_meetups/utils/date.py diff --git a/publish_meetups/routines/__init__.py b/publish_meetups/routines/__init__.py index 263ad67..64cba3d 100644 --- a/publish_meetups/routines/__init__.py +++ b/publish_meetups/routines/__init__.py @@ -3,7 +3,7 @@ import os.path from pathlib import Path import requests -from ics import Calendar +from ics import Calendar, Event from ..utils import config, error, prompt, ICS_FILE from ..feeds import * @@ -42,7 +42,7 @@ class Routine: raise error.InvalidCredential(f"Invalid credentials for {feed.__name__}.") @staticmethod - def get_ics(use_cached: bool = False) -> Path: + def get_ics(use_cached: bool = False) -> Calendar: if use_cached and ICS_FILE.exists(): with ICS_FILE.open("r") as f: return Calendar(f.read()) @@ -66,6 +66,10 @@ class Routine: with ICS_FILE.open("r") as f: return Calendar(f.read()) + + @staticmethod + def get_event_string(event: Event) -> str: + return f"" def run(self): diff --git a/publish_meetups/routines/development.py b/publish_meetups/routines/development.py index 234eb33..c1da829 100644 --- a/publish_meetups/routines/development.py +++ b/publish_meetups/routines/development.py @@ -1,7 +1,19 @@ from . import Routine -from ..feeds import Feed +from ..feeds import Feed, TwitterFeed +from ..utils import date + +from ics import Calendar class Development(Routine): def run(self): - print(self.get_ics()) + calendar = self.get_ics() + + for event in calendar.events: + print(event.name, date.event_formatting_values(event, "de")) + break + + event_description = f""" + Wir treffen uns {event.begin.humanize(locale="de")} zum {event.name}.""" + print(event_description.strip()) + print() diff --git a/publish_meetups/utils/__init__.py b/publish_meetups/utils/__init__.py index 9cca2d1..79c09eb 100644 --- a/publish_meetups/utils/__init__.py +++ b/publish_meetups/utils/__init__.py @@ -12,4 +12,12 @@ PROGRAM_DATA_DIR.mkdir(parents=True, exist_ok=True) ICS_FILE = Path(PROGRAM_DATA_DIR / "meetup.ics") -__all__ = ["prompt", "PROGRAM_DATA_DIR", "PROGRAM_NAME", "errors", "config", "ICS_FILE"] +__all__ = [ + "prompt", + "PROGRAM_DATA_DIR", + "PROGRAM_NAME", + "errors", + "config", + "ICS_FILE", + "date", +] diff --git a/publish_meetups/utils/config.py b/publish_meetups/utils/config.py index ae45121..efa4d4c 100644 --- a/publish_meetups/utils/config.py +++ b/publish_meetups/utils/config.py @@ -11,6 +11,8 @@ _config: dict = { "mastodon", "twitter", ], + "weekday_translations": {}, + "months_translations": {}, "ics_url": "", "mastodon": {}, "twitter": {}, diff --git a/publish_meetups/utils/date.py b/publish_meetups/utils/date.py new file mode 100644 index 0000000..b567600 --- /dev/null +++ b/publish_meetups/utils/date.py @@ -0,0 +1,40 @@ +from datetime import datetime +from ics import Calendar, Event + +from . import config + + +def to_date_string(date_time: datetime) -> str: + date_format = config.get_field("date_format", "%Y-%m-%d %H:%M:%S") + + weekday_translations = config.get_field("weekday_translations", {}) + months_translations = config.get_field("months_translations", {}) + + parsed = date_time.strftime(date_format) + + for key, value in weekday_translations.items(): + parsed = parsed.replace(key, value) + + for key, value in months_translations.items(): + parsed = parsed.replace(key, value) + + return parsed.strip() + + +def to_time_string(date_time: datetime) -> str: + time_format = config.get_field("time_format", "%H:%M") + time_format_full_hour = config.get_field("time_format_full_hour", time_format) + + parsed = date_time.strftime(time_format) + if int(date_time.minute) == 0: + parsed = date_time.strftime(time_format_full_hour) + + return parsed.strip() + + +def event_formatting_values(event: Event, locale="en") -> dict: + return { + "time": to_time_string(event.begin), + "date": to_date_string(event.begin), + "date_humanized": event.begin.humanize(locale=locale), + }