-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
180 lines (147 loc) · 6.13 KB
/
example.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
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
import asyncio
import os
from datetime import timedelta, date
from aiocvv.client import ClassevivaClient as Client
from aiocvv.enums import SchoolDayStatus, AbsenceCode, EventCode, NoteType
from aiocvv.dataclasses import Grade, Note
async def main():
# Log into the account
client = await Client(os.getenv("CVV_USERNAME"), os.getenv("CVV_PASSWORD"))
begin = date.today() - timedelta(days=7)
end = date.today()
final = [p for p in await client.me.calendar.get_periods() if p.final][-1]
print(f"Hello, {client.me.name}. ", end="")
if final.end < date.today():
print("This school year is over! Let's see how you did this year.\n")
total_grades = 0
total_absences = 0
total_notes = 0
grades_sum = 0
for period in await client.me.calendar.get_periods():
grades = [g for g in await period.get_grades() if g.value is not None]
notes = await period.get_notes()
absences = await period.get_absences()
total_grades += len(grades)
total_absences += len(absences)
total_notes += len(notes)
grades_sum += sum(g.value for g in grades)
avg = sum(g.value for g in grades) / len(grades)
print(f"====== {period.description} ======")
print(
f"You got {len(grades)} grades, {len(notes)} notes and {len(absences)} absences."
)
print(f"Your average grade was {avg:.2f}.\n")
print("Grades:")
for grade in grades:
print_grade(grade)
print("\nAbsences:")
for absence in absences:
print(f"\t- {absence.date}: ", end="")
if absence.type == AbsenceCode.absence:
print("Absence", end="")
elif absence.type == AbsenceCode.delay:
print(f"Delay, you entered at position {absence.position}", end="")
elif absence.type == AbsenceCode.short_delay:
print("Short delay", end="")
elif absence.type == AbsenceCode.exit:
print(f"Exit, you left at position {absence.position}", end="")
if not absence.justified:
print(" (not justified)", end="")
print()
print("\nNotes:")
for note in notes:
print_note(note)
print()
print()
print()
print(
f"In total, you got {total_grades} grades, "
f"{total_notes} notes and {total_absences} absences."
)
print(f"Your total average grade was {grades_sum / total_grades:.2f}.\n")
else:
print("Showing information for the last 7 days.\n")
# Request information for each day using the iterator
async for day in client.me.calendar(begin, end):
if day.status != SchoolDayStatus.school:
print(f"{day.date} isn't a school day.\n")
continue
print(f"====== What happened on {day.date}? ======")
for event in day.events:
if event.type == AbsenceCode.absence:
print("You were absent!", end="")
elif event.type == AbsenceCode.delay:
print(
f"You arrived late! You entered at position {event.position}.",
end="",
)
elif event.type == AbsenceCode.short_delay:
print("You had a small delay.", end="")
elif event.type == AbsenceCode.exit:
print(
f"You left early! You left at position {event.position}.",
end="",
)
print(" This was justified." if event.justified else "")
if day.lessons:
print("\nLessons:")
for lesson in day.lessons:
print(
f"\t- {lesson.subject.description} at {lesson.position} hour"
f" for {lesson.duration} hour{'s' if lesson.duration != 1 else ''}"
)
if day.agenda:
print("\nAgenda:")
for event in day.agenda:
msg = "\t- "
if event.type == EventCode.homework:
msg += "Homework "
elif event.type == EventCode.note:
msg += "Note "
elif event.type == EventCode.reservation:
msg += "Classroom reservation "
msg += f"by {event.author}"
if not event.full_day:
msg += f" from {event.start} to {event.end}"
msg += f": {event.notes}"
print(msg)
if day.grades:
print("\nGrades:")
for grade in day.grades:
print_grade(grade)
if day.notes:
print("\nNotes:")
for note in day.notes:
print_note(note)
print()
print()
print()
def print_grade(grade: Grade):
real_val = (
f" ({grade.value})"
if grade.value is not None and str(grade.value) != grade.display_value
else ""
)
print(
"\t- "
+ (grade.component_description + " " if grade.component_description else "")
+ f"{grade.subject}: {grade.display_value}{real_val}. {grade.family_notes}"
)
def print_note(note: Note):
msg = "\t- "
if note.type == NoteType.registry:
msg += "Disciplinary note"
elif note.type == NoteType.sanction:
msg += "Disciplinary sanction"
elif note.type == NoteType.teacher:
msg += "Annotation"
elif note.type == NoteType.warning:
msg += "Warning"
msg += f" by {note.author_name} on {note.date}: {note.text}"
print(msg)
if __name__ == "__main__":
if not os.getenv("CVV_USERNAME") or not os.getenv("CVV_PASSWORD"):
raise ValueError(
"Set CVV_USERNAME and CVV_PASSWORD environment variables first."
)
asyncio.run(main())