-
Notifications
You must be signed in to change notification settings - Fork 1
/
Message.py
53 lines (44 loc) · 1.45 KB
/
Message.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''
Message class
'''
from typing import Tuple
import hashlib
class Message():
'''
Message class for easier parsing and storing
'''
@staticmethod
def parse(message: str) -> Tuple[str, str, str, str]:
'''parses the message and returns the date, time, the number and the text'''
try:
splitted = message.split(":")
if len(splitted) < 3:
return "", "", "", message
date = splitted[0][:-3].replace(",", "")
time = splitted[0][-2:] + ":" + splitted[1][:2]
num = "-".join(splitted[1].split("-")[1:]).strip()
text = ":".join(splitted[2:])
return date, time, num, text
except IndexError:
return "", "", "", message
def __init__(self, message: str):
self.date, self.time, self.sender, self.text = Message.parse(message)
@property
def is_copypasta(self) -> bool:
'''returns true if the message is a copypasta'''
return len(self) > 100 or len(self.text) > 400
@property
def is_readable(self) -> bool:
'''returns true if the message is readable'''
return self.sender and self.text
@property
def hash(self) -> str:
"""
Returns the sha1 of the text
"""
return hashlib.sha1(self.text.encode()).hexdigest()
def __len__(self) -> int:
'''
returns the num of words
'''
return len(self.text.split())