-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_formatter.py
More file actions
275 lines (237 loc) · 9.05 KB
/
event_formatter.py
File metadata and controls
275 lines (237 loc) · 9.05 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python3
import datetime
import pickle
import logging
logger = logging.getLogger(__name__)
GOOGLE_EVENT_RANGE = 11
def hash_event(e):
threeLetterName = (e.get("subject", e.get("name")).lower().replace("č", "c").replace("š", "s").replace("ž", "z") + "___")[:3]
if e["type"] == "school_hour":
return f'id:1{threeLetterName}{extract_HHMM(e["start"].get("dateTime", e["start"].get("date")))}{extract_HHMM(e["end"].get("dateTime", e["end"].get("date")))}'
elif e["type"] == "event":
return f'id:2{threeLetterName}{extract_HHMM(e["start"].get("dateTime", e["start"].get("date")))}{extract_HHMM(e["end"].get("dateTime", e["end"].get("date")))}'
else:
return f'id:3{threeLetterName}{extract_HHMM(e["start"].get("dateTime", e["start"].get("date")))}{extract_HHMM(e["end"].get("dateTime", e["end"].get("date")))}'
def str_to_colorId(input_str: str, range_n: int = GOOGLE_EVENT_RANGE, color_string=False) -> int:
if color_string:
return int(input_str[1]+input_str[3], 16) % range_n
sum_of_ascii = 0
for c in input_str:
sum_of_ascii += ord(c)
return int(sum_of_ascii * 101 + 37) % range_n + 1
class EventFormatter:
#TODO Create serializable format class
#TODO Do Format string for properties?
def __init__(self):
self.TIMEZONE = 'Europe/Belgrade'
self.COLORMAP = self.load_colormap()
def load_colormap(self) -> dict:
r = {}
try:
with open("event_manager/COLORMAP.pickle", "rb") as CM_file:
r = pickle.load(CM_file)
except (FileNotFoundError, EOFError):
with open("event_manager/COLORMAP.pickle", "wb") as CM_file:
pickle.dump(r, CM_file)
self.COLORMAP = r
return r
def write_colormap(self):
with open("event_manager/COLORMAP.pickle", "wb") as CM_file:
pickle.dump(self.COLORMAP, CM_file)
def google_event_body_from_parsed_event(self, e: dict) -> (str, dict):
specialty = e.get("special", None)
if e["type"] == "school_hour":
description = [f'Speciality: {e.get("special", "None")}']
addition = []
if "classroom" in e:
addition.append((e["classroom"]+" ")[:3])
description.append(f'Classroom: {e["classroom"]}')
if "teachers" in e:
addition.append(" ".join("".join([x[0].upper() for x in teacher.split(" ")]) for teacher in e["teachers"]))
description.append(f'Teachers: {", ".join(e["teachers"])}')
if addition:
addition = f'({":".join(addition)})'
else:
addition = ""
description.append(f'#school {e["type"]} {e["hash"]}')
summary = f'{(e.get("subject", e.get("name"))+" ")[:3]}' + addition
BODY = {
"summary": summary,
"start": e["start"],
"end": e["end"],
"description": ("\n".join(description)).strip(),
"colorId": self.COLORMAP[e["color"]]
}
elif e["type"] == "event":
description = [
f'Location: {e["location"]}'
]
addition = []
if "classroom" in e:
addition.append((e["classroom"] + " ")[:3])
if "teachers" in e:
addition.append(
" ".join("".join([x[0].upper() for x in teacher.split(" ")]) for teacher in e["teachers"]))
description.append(f'Teachers: {", ".join(e["teachers"])}')
if addition:
addition = f'({":".join(addition)})'
else:
addition = ""
description.append(f'#school {e["type"]} {e["hash"]}')
summary = f'{(e.get("subject", e.get("name"))+" ")[:3]}' + addition
BODY = {
"summary": summary,
"start": e["start"],
"end": e["end"],
"description": ("\n".join(description)).strip(),
"colorId": str_to_colorId(str(hash(e["start"].get("dateTime", e["start"].get("date"))))+e["type"])
}
else: # e["type"] == "all_day_event"
#TODO make sure to connect multiday-events
description = [
f'Location: {e["location"]}'
]
addition = []
if "classroom" in e:
addition.append((e["classroom"] + " ")[:3])
if "teachers" in e:
addition.append(
" ".join("".join([x[0].upper() for x in teacher.split(" ")]) for teacher in e["teachers"]))
description.append(f'Teachers: {", ".join(e["teachers"])}')
if addition:
addition = f'({":".join(addition)})'
else:
addition = ""
description.append(f'#school {e["type"]} {e["hash"]}')
summary = f'{(e.get("subject", e.get("name"))+" ")[:3]}' + addition
BODY = {
"summary": summary,
"start": e["start"],
"end": e["end"],
"description": ("\n".join(description)).strip(),
"colorId": str_to_colorId(str(hash(e["start"].get("dateTime", e["start"].get("date"))))+e["type"])
}
return specialty, BODY
def format_timetable_for_entry(self, table):
self.TIMEZONE = 'Europe/Belgrade'
all_the_colors = set()
def date_cmp(f, d1, d2, fmt="%Y-%m-%dT%H:%M:%S"):
if d1 is None and d2:
return d2
elif d2 is None and d1:
return d1
elif d2 and d1:
dt1 = datetime.datetime.strptime(d1, fmt)
dt2 = datetime.datetime.strptime(d2, fmt)
return f(dt1, dt2).isoformat()
return None
# instantiate translation dict (id -> data)
translation_dict = {"time": {}, "date": {}}
time_boundary = {"min": None, "max": None}
for e in table["time_table"]:
key = str(e["id"])
translation_dict["time"][key] = {
"name": e["name_short"],
"time": {
"from": datetime.datetime.strptime(e["time"]["from"], "%H:%M").time().isoformat(),
"to": datetime.datetime.strptime(e["time"]["to"], "%H:%M").time().isoformat()
}
}
for e in table["day_table"]:
dtime = datetime.datetime.strptime(e["date"], "%Y-%m-%d").date()
translation_dict["date"][dtime.isoformat()] = {
"name": e["name"],
"name_short": e["short_name"],
"date": dtime.isoformat()
}
events = []
for entry in table["school_hour_events"]:
time_from_e = translation_dict["time"][str(entry["time"]["from_id"])]
time_to_e = translation_dict["time"][str(entry["time"]["to_id"])]
date_e = translation_dict["date"][str(entry["time"]["date"])]
e = {
"start": {'timeZone': self.TIMEZONE},
"end": {'timeZone': self.TIMEZONE},
"names": {
"day_name": date_e["name"],
"hour_name_from": time_from_e["name"],
"hour_name_to": time_to_e["name"]
},
"type": "school_hour",
"completed": entry["completed"],
"subject": entry["subject"]["name"],
"special": entry["hour_special_type"],
"classroom": entry["classroom"]["name"],
"teachers": [x["name"] for x in entry["teachers"]],
"departments": [x["name"] for x in entry["departments"]],
"groups": entry["groups"],
"info": entry["info"],
"color": entry["color"]
}
e["start"]["dateTime"] = date_e["date"] + 'T' + time_from_e["time"]["from"]
e["end"]["dateTime"] = date_e["date"] + 'T' + time_to_e["time"]["to"]
time_boundary["min"] = date_cmp(min, time_boundary["min"], e["start"]["dateTime"])
time_boundary["max"] = date_cmp(max, time_boundary["max"], e["end"]["dateTime"])
e["hash"] = hash_event(e)
all_the_colors.add(e["color"]) # Add Color for color mapping to Google's 11 color combinations
events.append(e)
for entry in table["events"]:
time_from = datetime.datetime.strptime(entry["time"]["from"], "%H:%M").time().isoformat()
time_to = datetime.datetime.strptime(entry["time"]["to"], "%H:%M").time().isoformat()
date_e = translation_dict["date"][str(entry["date"])]
e = {
"start": {'timeZone': self.TIMEZONE},
"end": {'timeZone': self.TIMEZONE},
"names": {
"day_name": date_e["name"]
},
"time": {
"day_name": date_e["name"]
},
"type": "event",
"name": entry["name"],
"location": entry["location"]["name"],
"teachers": [x["name"] for x in entry["teachers"]]
}
e["start"]["dateTime"] = date_e["date"] + 'T' + time_from
e["end"]["dateTime"] = date_e["date"] + 'T' + time_to
time_boundary["min"] = date_cmp(min, time_boundary["min"], e["start"]["dateTime"])
time_boundary["max"] = date_cmp(max, time_boundary["max"], e["end"]["dateTime"])
e["hash"] = hash_event(e)
events.append(e)
for entry in table["all_day_events"]:
date_e = translation_dict["date"][str(entry["date"])]
e = {
"start": {'timeZone': self.TIMEZONE},
"end": {'timeZone': self.TIMEZONE},
"names": {
"day_name": date_e["name"]
},
"name": entry["name"],
"type": "all_day_event",
"event_type": entry["event_type"],
"location": entry["location"]["name"],
"teachers": [x["name"] for x in entry["teachers"]],
}
e["start"]["date"] = date_e["date"]
e["end"]["date"] = date_e["date"]
time_boundary["min"] = date_cmp(min, time_boundary["min"], e["start"]["date"] + "T00:00:00")
time_boundary["max"] = date_cmp(max, time_boundary["max"], e["end"]["date"] + "T00:00:00")
e["hash"] = hash_event(e)
events.append(e)
time_boundary['timeZone'] = self.TIMEZONE
if not self.COLORMAP:
# Translate all_the_colors to COLORMAP
for i, c in enumerate(all_the_colors, start=1):
self.COLORMAP[c] = i % GOOGLE_EVENT_RANGE + 1
self.write_colormap()
return {"translation": translation_dict, "time_boundary": time_boundary,
"events": sorted(events, key=lambda x: x["start"].get("dateTime", x["start"].get("date")))}
""" HASH : [1|2|3]XXXSSSSEEEE
1,2,3 event type
XXX subj
SSSS start HHMM
EEEE end HHMM
"""
def extract_HHMM(iso_formatted_string):
return iso_formatted_string[11:13] + iso_formatted_string[14:16]