implemented functions to get human readable dates

This commit is contained in:
Hellow 2024-02-14 23:37:43 +01:00
parent 420deb16cf
commit 6be934e374
5 changed files with 71 additions and 5 deletions

View File

@ -3,7 +3,7 @@ import os.path
from pathlib import Path from pathlib import Path
import requests import requests
from ics import Calendar from ics import Calendar, Event
from ..utils import config, error, prompt, ICS_FILE from ..utils import config, error, prompt, ICS_FILE
from ..feeds import * from ..feeds import *
@ -42,7 +42,7 @@ class Routine:
raise error.InvalidCredential(f"Invalid credentials for {feed.__name__}.") raise error.InvalidCredential(f"Invalid credentials for {feed.__name__}.")
@staticmethod @staticmethod
def get_ics(use_cached: bool = False) -> Path: def get_ics(use_cached: bool = False) -> Calendar:
if use_cached and ICS_FILE.exists(): if use_cached and ICS_FILE.exists():
with ICS_FILE.open("r") as f: with ICS_FILE.open("r") as f:
return Calendar(f.read()) return Calendar(f.read())
@ -66,6 +66,10 @@ class Routine:
with ICS_FILE.open("r") as f: with ICS_FILE.open("r") as f:
return Calendar(f.read()) return Calendar(f.read())
@staticmethod
def get_event_string(event: Event) -> str:
return f""
def run(self): def run(self):

View File

@ -1,7 +1,19 @@
from . import Routine from . import Routine
from ..feeds import Feed from ..feeds import Feed, TwitterFeed
from ..utils import date
from ics import Calendar
class Development(Routine): class Development(Routine):
def run(self): 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()

View File

@ -12,4 +12,12 @@ PROGRAM_DATA_DIR.mkdir(parents=True, exist_ok=True)
ICS_FILE = Path(PROGRAM_DATA_DIR / "meetup.ics") 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",
]

View File

@ -11,6 +11,8 @@ _config: dict = {
"mastodon", "mastodon",
"twitter", "twitter",
], ],
"weekday_translations": {},
"months_translations": {},
"ics_url": "", "ics_url": "",
"mastodon": {}, "mastodon": {},
"twitter": {}, "twitter": {},

View File

@ -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),
}