diff --git a/notification_server/notification_saver.py b/notification_server/notification_saver.py new file mode 100644 index 0000000..964656f --- /dev/null +++ b/notification_server/notification_saver.py @@ -0,0 +1,26 @@ +from datetime import datetime +import os + + +class NotificationSaver: + @staticmethod + def create_folders_if_needed(path): + os.makedirs(path, exist_ok=True) + + @staticmethod + def get_date_folder(base_dir='notifications'): + date = str(datetime.now().date()) + return os.path.join(base_dir, date) + + @staticmethod + def save_notification(base_dir, title, message, timestamp) -> bool: + dirname = NotificationSaver.get_date_folder(base_dir) + filename = f'{title}_{timestamp}.md' + + full_file_name = os.path.join(dirname, filename) + full_folder_name = os.path.dirname(full_file_name) + NotificationSaver.create_folders_if_needed(full_folder_name) + + with open(full_file_name, 'w') as file: + file.write(message) + diff --git a/notification_server/server.py b/notification_server/server.py index 85909ed..74a5171 100644 --- a/notification_server/server.py +++ b/notification_server/server.py @@ -20,6 +20,7 @@ import struct from rich.console import Console import asyncio +from notification_server.notification_saver import NotificationSaver from notification_server.timestamp import Timestamp @@ -87,6 +88,9 @@ class Server: self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) finished.") break + now = Timestamp.now() + NotificationSaver.save_notification(self.NOTIFICATION_DIR, title, message, now) + await self.broadcast_message(title) await self.broadcast_message(message)