implemented functions to get human readable dates
This commit is contained in:
parent
420deb16cf
commit
6be934e374
@ -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())
|
||||
@ -67,6 +67,10 @@ class Routine:
|
||||
return Calendar(f.read())
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_event_string(event: Event) -> str:
|
||||
return f""
|
||||
|
||||
|
||||
def run(self):
|
||||
pass
|
||||
|
@ -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()
|
||||
|
@ -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",
|
||||
]
|
||||
|
@ -11,6 +11,8 @@ _config: dict = {
|
||||
"mastodon",
|
||||
"twitter",
|
||||
],
|
||||
"weekday_translations": {},
|
||||
"months_translations": {},
|
||||
"ics_url": "",
|
||||
"mastodon": {},
|
||||
"twitter": {},
|
||||
|
40
publish_meetups/utils/date.py
Normal file
40
publish_meetups/utils/date.py
Normal 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),
|
||||
}
|
Loading…
Reference in New Issue
Block a user