You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.9 KiB
51 lines
1.9 KiB
from time import gmtime, localtime, strftime
|
|
import os
|
|
from ..utilities.messages import Message
|
|
|
|
class ChatLog(object):
|
|
|
|
def __init__(self, name):
|
|
self.name = name
|
|
self.chat_history = {}
|
|
|
|
def enable_logging(self, directory):
|
|
self.directory = directory
|
|
|
|
def save(self, message, is_new=True):
|
|
if not message.room_id in self.chat_history:
|
|
self.chat_history[message.room_id] = {}
|
|
self.chat_history[message.room_id][message.event_id] = message
|
|
self.chat_history[message.room_id] = dict(list(self.chat_history[message.room_id].items())[-100 :])
|
|
|
|
if hasattr(self, 'directory') and is_new:
|
|
keepcharacters = (' ','.','_','-')
|
|
room_id_sanitized = "".join(c for c in message.room_id if c.isalnum() or c in keepcharacters).strip()
|
|
time_suffix = strftime("%Y-%m", localtime())
|
|
time = strftime("%a, %d %b %Y %H:%M:%S", localtime(message.timestamp))
|
|
with open(os.path.join(self.directory, f"{message.room_name}_{room_id_sanitized}_{time_suffix}.txt"), "a") as f:
|
|
f.write("{} | {}: {}\n".format(time, message.user_name, message.message))
|
|
|
|
|
|
|
|
def remove_message_by_id(self, event_id):
|
|
for room_id in self.chat_history:
|
|
if event_id in self.chat_history[room_id]:
|
|
del self.chat_history[room_id][event_id]
|
|
|
|
def remove_message_in_room(self, room_id, num_items=1):
|
|
for i in range(num_items):
|
|
event_id, message = self.chat_history[room_id].popitem()
|
|
return message
|
|
|
|
def get_last_message(self, room_id):
|
|
key, message = list(self.chat_history[room_id].items())[-1]
|
|
return message
|
|
|
|
def clear_all(self):
|
|
self.chat_history = {}
|
|
|
|
def clear(self, room_id):
|
|
self.chat_history[room_id] = {}
|
|
|
|
def chat_history_len(self, room_id):
|
|
return len(self.chat_history[room_id])
|
|
|