From 000337107b7fd3c2a546d3e00143da25a86c7c9e Mon Sep 17 00:00:00 2001 From: stijndcl Date: Tue, 21 Jun 2022 18:44:47 +0200 Subject: [PATCH] Parse publication time of notifications --- database/crud/ufora_announcements.py | 2 +- didier/cogs/tasks.py | 3 ++- didier/data/embeds/ufora/announcements.py | 17 +++++++++++++++-- didier/didier.py | 2 +- didier/utils/discord/__init__.py | 0 didier/utils/{ => discord}/prefix.py | 0 didier/utils/types/__init__.py | 0 didier/utils/types/datetime.py | 3 +++ didier/utils/types/string.py | 21 +++++++++++++++++++++ 9 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 didier/utils/discord/__init__.py rename didier/utils/{ => discord}/prefix.py (100%) create mode 100644 didier/utils/types/__init__.py create mode 100644 didier/utils/types/datetime.py create mode 100644 didier/utils/types/string.py diff --git a/database/crud/ufora_announcements.py b/database/crud/ufora_announcements.py index 0d8e15f..c4d67f6 100644 --- a/database/crud/ufora_announcements.py +++ b/database/crud/ufora_announcements.py @@ -13,7 +13,7 @@ async def get_courses_with_announcements(session: AsyncSession) -> list[UforaCou async def create_new_announcement( - session: AsyncSession, announcement_id: int, course: UforaCourse, publication_date: datetime + session: AsyncSession, announcement_id: int, course: UforaCourse, publication_date: datetime.datetime ) -> UforaAnnouncement: """Add a new announcement to the database""" new_announcement = UforaAnnouncement( diff --git a/didier/cogs/tasks.py b/didier/cogs/tasks.py index b56e426..8b366fa 100644 --- a/didier/cogs/tasks.py +++ b/didier/cogs/tasks.py @@ -13,7 +13,8 @@ class Tasks(commands.Cog): client: Didier - def __init__(self, client: Didier): # pylint: disable=no-member + def __init__(self, client: Didier): + # pylint: disable=no-member self.client = client # Only pull announcements if a token was provided diff --git a/didier/data/embeds/ufora/announcements.py b/didier/data/embeds/ufora/announcements.py index 71b3191..52d5849 100644 --- a/didier/data/embeds/ufora/announcements.py +++ b/didier/data/embeds/ufora/announcements.py @@ -12,6 +12,8 @@ from sqlalchemy.ext.asyncio import AsyncSession import settings from database.crud import ufora_announcements as crud from database.models import UforaCourse +from didier.utils.types.datetime import int_to_weekday +from didier.utils.types.string import leading @dataclass @@ -88,8 +90,19 @@ class UforaNotification: def _get_published(self) -> str: """Get a formatted string that represents when this announcement was published""" - # TODO - return "Placeholder :) TODO make the functions to format this" + return ( + f"{int_to_weekday(self.published_dt.weekday())} " + f"{leading('0', str(self.published_dt.day))}" + "/" + f"{leading('0', str(self.published_dt.month))}" + "/" + f"{self.published_dt.year} " + f"om {leading('0', str(self.published_dt.hour))}" + ":" + f"{leading('0', str(self.published_dt.minute))}" + ":" + f"{leading('0', str(self.published_dt.second))}" + ) def parse_ids(url: str) -> Optional[tuple[int, int]]: diff --git a/didier/didier.py b/didier/didier.py index 0b5b457..fa2eb24 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -8,7 +8,7 @@ from sqlalchemy.ext.asyncio import AsyncSession import settings from database.engine import DBSession -from didier.utils.prefix import get_prefix +from didier.utils.discord.prefix import get_prefix class Didier(commands.Bot): diff --git a/didier/utils/discord/__init__.py b/didier/utils/discord/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/didier/utils/prefix.py b/didier/utils/discord/prefix.py similarity index 100% rename from didier/utils/prefix.py rename to didier/utils/discord/prefix.py diff --git a/didier/utils/types/__init__.py b/didier/utils/types/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/didier/utils/types/datetime.py b/didier/utils/types/datetime.py new file mode 100644 index 0000000..6e5c88e --- /dev/null +++ b/didier/utils/types/datetime.py @@ -0,0 +1,3 @@ +def int_to_weekday(number: int) -> str: + """Get the Dutch name of a weekday from the number""" + return ["Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag", "Zondag"][number] diff --git a/didier/utils/types/string.py b/didier/utils/types/string.py new file mode 100644 index 0000000..773890a --- /dev/null +++ b/didier/utils/types/string.py @@ -0,0 +1,21 @@ +from typing import Optional + + +def leading(character: str, string: str, target_length: Optional[int] = 2) -> str: + """Add a leading [character] to [string] to make it length [target_length] + Pass None to target length to always do it, no matter the length + """ + # Cast to string just in case + string = str(string) + + # Add no matter what + if target_length is None: + return character + string + + # String is already long enough + if len(string) >= target_length: + return string + + frequency = (target_length - len(string)) // len(character) + + return (frequency * character) + string