-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage.py
More file actions
29 lines (23 loc) · 977 Bytes
/
message.py
File metadata and controls
29 lines (23 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from json import loads, dumps
from datetime import datetime
class Message:
"""Represents a single message sent from a client"""
def __init__(self, msg, client):
self.IP = client[0]
self.nickName = client[1]
msg = loads(msg)
try:
self.type = msg["type"]
self.text = msg["text"].strip()
self.date = datetime.now()
except (TypeError, KeyError, AttributeError) as e:
print("[E] Malformed message; possible json parse error?")
self.type = "error"
self.text = str(e)
self.date = "Now-ish"
def str(self):
return "Sent from "+self.IP+" ("+self.nickName+"):\n{\n\ttype: "+self.type+"\n\ttext: '"+self.text+"'\n\tdate: "+str(self.date)+"\n}"
def json(self):
return dumps({"type": self.type, "text": self.text, "date": str(self.date.timestamp()), "sender": self.nickName})
def jsonify(names, values):
return dumps({names[i]: values[i] for i in range(len(names))}, default=lambda bad: bad.timestamp() if isinstance(bad, datetime) else None)