Add saving of notifications to the directory.
This commit is contained in:
@@ -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)
|
||||||
|
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
import struct
|
import struct
|
||||||
from rich.console import Console
|
from rich.console import Console
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from notification_server.notification_saver import NotificationSaver
|
||||||
from notification_server.timestamp import Timestamp
|
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.")
|
self.console.print(f"[yellow b][INFO][/][#8abeb7 b][{now}][/] The producer ({addr[0]}:{addr[1]}) finished.")
|
||||||
break
|
break
|
||||||
|
|
||||||
|
now = Timestamp.now()
|
||||||
|
NotificationSaver.save_notification(self.NOTIFICATION_DIR, title, message, now)
|
||||||
|
|
||||||
await self.broadcast_message(title)
|
await self.broadcast_message(title)
|
||||||
await self.broadcast_message(message)
|
await self.broadcast_message(message)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user