-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.py
73 lines (59 loc) · 2.79 KB
/
event.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
from datetime import datetime
from dateutil import tz
from dateutil.parser import parse
class EventTypes:
BLOCK="block"
UNBLOCK="unblock"
TWEET="tweet"
class event:
DELIM = ","
def __init__(self,site,op,body,truetime,name,timestamp=-1):
self.site = site
self.op = op
self.data = body
self.timestamp = timestamp
#when constructing this, assume the tweet's time is in UTC, and should be converted in the __str__ "view"
self.truetime = str(truetime)
self.name = name
def get_tweet(self):
if(self.op == EventTypes.TWEET):
return self.data
else:
return None
def get_blocker(self):
if(self.op == EventTypes.BLOCK or self.op == EventTypes.UNBLOCK):
return int(self.data.split(event.DELIM)[0])
else:
return None
def get_blocked(self):
if(self.op == EventTypes.BLOCK or self.op == EventTypes.UNBLOCK):
return int(self.data.split(event.DELIM)[1])
else:
return None
def superceding_unblock_exists(self, event_list):
linked_unblocks = list(filter(lambda e: e.site == self.site \
and e.data == self.data \
and e.op == EventTypes.UNBLOCK \
and e.timestamp > self.timestamp \
and self.op == EventTypes.BLOCK, event_list))
return len(linked_unblocks) > 0
def superceding_block_exists(self, event_list):
linked_unblocks = list(filter(lambda e: e.site == self.site \
and e.data == self.data \
and e.op == EventTypes.BLOCK \
and e.timestamp > self.timestamp \
and self.op == EventTypes.UNBLOCK, event_list))
return len(linked_unblocks) > 0
def __json__(self):
localtime = parse(str(self.truetime)).replace(tzinfo=tz.tzutc()).astimezone(tz.tzlocal())
return {"site":self.site,"op":self.op,"data":self.data,"timestamp":self.timestamp,"truetime":localtime,"name":self.name}
#problem here: when generated by SQL, self.truetime is a string, but when it is created it is a datetime object
def __str__(self):
localtime = parse(str(self.truetime)).replace(tzinfo=tz.tzutc()).astimezone(tz.tzlocal())
return "{} by {} at {}:\n {}".format(self.op.title(),self.name,localtime.strftime('%Y-%m-%d %H:%M:%S'),self.data)
def string2(self):
localtime = parse(str(self.truetime)).replace(tzinfo=tz.tzutc()).astimezone(tz.tzlocal())
return "{} by {} at {}: '{}'".format(self.op.title(),self.name,localtime.strftime('%Y-%m-%d %H:%M:%S'),self.data)
def prefix(self):
localtime = parse(str(self.truetime)).replace(tzinfo=tz.tzutc()).astimezone(tz.tzlocal())
return "{} by {} at {}: ".format(self.op.title(),self.name,localtime.strftime('%Y-%m-%d %H:%M:%S'))