-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler.py
88 lines (75 loc) · 2.51 KB
/
eventhandler.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from datetime import datetime
from calendar import timegm
from operator import itemgetter
class EventHandler(object):
def __init__(self, event_list):
self.event_list = self.sort_events(event_list)
@staticmethod
def sort_events(list_to_sort):
sorted_list = sorted(list_to_sort, key=itemgetter('time'),
reverse=True)
return sorted_list
@staticmethod
def parse_string(text):
words_list = text.split()
# defaults
person = "all"
category = "update"
for i in words_list:
if i.startswith("#"):
category = i.strip("#")
elif i.startswith("@"):
person = i.strip("@")
return category, person
@staticmethod
def make_time():
# the method truncates milliseconds
# to fit nicely in the URL
time = datetime.utcnow()
time = timegm(time.timetuple())
return time
def find_element_by_id(self, element_id):
for i in self.event_list:
if i['id'] == element_id:
return i
return None
def select_events(self, field=None, value=None, count=10):
selected_list = []
if field is None:
return self.event_list[0:count]
elif field == "time":
value = int(value)
for i in self.event_list:
if i[field] <= value:
selected_list.append(i)
if len(selected_list) == count:
break
else:
for i in self.event_list:
if i[field] == value:
selected_list.append(i)
if len(selected_list) == count:
break
return selected_list
def get_all_events(self):
return self.event_list
def get_last_ten(self):
return self.select_events()
def get_last_by_field(self, event_field, event_value):
return self.select_events(event_field, event_value)
def add_event(self, feed):
category, person = self.parse_string(feed)
time = self.make_time()
event = {
'id': len(self.event_list) + 1,
'text': feed,
'category': category,
'person': person,
'time': time
}
self.event_list.append(event)
self.event_list = self.sort_events(self.event_list)
return event
def get_event_by_id(self, event_id):
event = self.find_element_by_id(event_id)
return event