-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_data.py
85 lines (66 loc) Β· 2.56 KB
/
parse_data.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
import datetime
import os
import icalendar
def get_dirfiles(path) -> list:
dirfiles = []
if not path:
return []
for _, _, files in os.walk(path):
for file in files:
_, extension = os.path.splitext(file)
if extension == ".ics":
i = open(f"{path}/{file}", "rb")
dirfiles.append(i)
return dirfiles
def files_to_calendar(files: list) -> list:
converted = []
for file in files:
gcal = icalendar.Calendar.from_ical(file.read())
converted.append({"name": file.name, "data": gcal})
file.close()
return converted
def parse_description(description: str) -> dict:
details = {}
key_value = description.split("\n")
for i in key_value:
aux = i.split(": ")
details[aux[0]] = aux[1]
return details
def same_week(d1, d2):
return d1.isocalendar()[1] == d2.isocalendar()[1] and d1.year == d2.year
def parse_files(calendars) -> list:
people = []
for calendar in calendars:
weekly_lectures = []
for component in calendar["data"].walk():
lecture = {}
same = False
if component.get("dtstart"):
component_date = component.get("dtstart").dt
same = same_week(component_date, datetime.date.today())
if component.name == "VEVENT" and same:
lecture["Names"] = calendar["name"]
lecture["Day"] = component.get("dtstart").dt.strftime("%A")
lecture["Start"] = component.get("dtstart").dt.time()
lecture["End"] = component.get("dtend").dt.time()
parsed = parse_description(component.get("description"))
lecture = dict(list(lecture.items()) + list(parsed.items()))
weekly_lectures.append(lecture)
people.append(weekly_lectures)
return people
def pretty_lecture(lecture) -> list:
output = (
"\n"
"**Module**: " + lecture.Unit + "\n"
"**Type**: " + lecture.Type + "\n"
"**Lecturer**: " + lecture.Lecturer + "\n"
"**Start**: " + lecture.Start.strftime("%H:%M") + "\n"
"**End**: " + lecture.End.strftime("%H:%M") + "\n"
"**Group**: " + lecture.Group + "\n"
"**Building**: " + (lecture.Building if lecture.Building else "N/A") + "\n"
"**Room**: " + (lecture.Room if lecture.Room else "N/A") + "\n"
"**People**: `" + format_names(lecture.Names) + "`\n"
)
return output
def format_names(names):
return ", ".join(map(lambda name: name.capitalize(), names.split(" ")))