1
1
#!/usr/bin/env python
2
+ from __future__ import annotations
3
+
2
4
from collections import defaultdict
3
- from datetime import date , datetime , timedelta
5
+ from typing import TYPE_CHECKING
6
+
7
+ from _common_tasks_stuff import _next_weekday
4
8
5
- from smartschool import Courses , FutureTasks , PathCredentials , Periods , Smartschool , SmartschoolHours , SmartschoolLessons , SmartschoolMomentInfos
9
+ from smartschool import FutureTasks , PathCredentials , Smartschool , SmartschoolHours
6
10
from smartschool .common import IsSaved , save , send_email
7
- from smartschool .objects import AgendaHour , FutureTaskOneTask
8
- from smartschool .session import session
11
+
12
+ if TYPE_CHECKING :
13
+ from datetime import date
14
+
15
+ from smartschool .objects import AgendaHour , FutureTaskOneTask
9
16
10
17
11
18
def _is_equal_task (a : FutureTaskOneTask , b : FutureTaskOneTask ) -> bool :
12
- return (a .course , a .hourID , a .label , a .description , a .date ) == (b .course , b .hourID , b .label , b .description , b .date )
19
+ return (a .course , a .hourID , a .label , a .description , a .date ) == (b .course , b .hourID , b .label , b .description , b .date )
13
20
14
21
15
- def _email_text (task : FutureTaskOneTask ) -> str :
16
- hour : AgendaHour = SmartschoolHours ().search_by_hourId (task .hourID )
22
+ def _email_text (session : Smartschool , task : FutureTaskOneTask ) -> str :
23
+ hour : AgendaHour = SmartschoolHours (session ).search_by_hourId (task .hourID )
17
24
18
- return (
19
- f"date: { task .date } \n "
20
- f"lesuur: { hour .title } e lesuur\n "
21
- f"time: { hour .start } -> { hour .end } \n "
22
- f"les: { task .course } \n "
23
- "\n "
24
- f"{ task .label } :\n "
25
- f" { task .description } \n "
26
- )
25
+ return f"date: { task .date } \n lesuur: { hour .title } e lesuur\n time: { hour .start } -> { hour .end } \n les: { task .course } \n \n { task .label } :\n { task .description } \n "
27
26
28
27
29
- def process_task (task : FutureTaskOneTask , all_tasks : dict ) -> None :
28
+ def process_task (session : Smartschool , task : FutureTaskOneTask , all_tasks : dict ) -> None :
30
29
"""Stores in an array + report if it's a new/updated task."""
31
-
32
30
course_name = task .course
33
- hour : AgendaHour = SmartschoolHours ().search_by_hourId (task .hourID )
31
+ hour : AgendaHour = SmartschoolHours (session ).search_by_hourId (task .hourID )
34
32
when = task .date
35
33
36
34
all_tasks [when ][hour .title ].append (task )
37
35
38
- status = save (type_ = "todo" , course_name = course_name , id_ = task .assignmentID , data = task , is_eq = _is_equal_task )
36
+ status = save (session , type_ = "todo" , course_name = course_name , id_ = task .assignmentID , data = task , is_eq = _is_equal_task )
39
37
if status == IsSaved .SAME :
40
38
return
41
39
42
40
subject = f"📑 [{ when } { hour .title } e lesuur] { course_name } ({ task .label } )"
43
41
if status != IsSaved .NEW :
44
42
subject = f"⚠⚠ { subject } " # There is an update...
45
43
46
- text = _email_text (task )
47
-
48
- send_email (subject = subject , text = text , email_from = session .creds .other_info ['email_from' ], email_to = session .creds .other_info ['email_to' ])
44
+ text = _email_text (session , task )
49
45
46
+ send_email (subject = subject , text = text , email_from = session .creds .other_info ["email_from" ], email_to = session .creds .other_info ["email_to" ])
50
47
51
- def _next_weekday () -> date | None :
52
- now = datetime .now ()
53
48
54
- if now .hour in [12 , 20 ] and now .isoweekday () == 3 : # Wednesday
55
- ... # Ok
56
- elif now .hour in [17 , 20 ] and now .isoweekday () != 3 : # !Wednesday
57
- ... # Ok
58
- else :
59
- return None
60
-
61
- next_weekday = now + timedelta (days = 1 )
62
- while next_weekday .isoweekday () in [6 , 7 ]: # Saturday, Sunday
63
- next_weekday += timedelta (days = 1 )
64
-
65
- return next_weekday .date ()
66
-
67
-
68
- def report_tasks_for_next_day (all_tasks : dict ):
49
+ def report_tasks_for_next_day (session : Smartschool , all_tasks : dict ):
69
50
next_weekday = _next_weekday ()
70
51
if not next_weekday :
71
52
return
@@ -76,40 +57,38 @@ def report_tasks_for_next_day(all_tasks: dict):
76
57
tasks = all_tasks [next_weekday ]
77
58
78
59
text = []
79
- for uur , tasks in sorted (tasks .items (), key = lambda x : x [0 ]): # type: str, list[FutureTaskOneTask]
80
- for task in tasks :
81
- text .append (_email_text (task ))
60
+ for _uur , subtasks in sorted (tasks .items (), key = lambda x : x [0 ]): # type: str, list[FutureTaskOneTask]
61
+ for task in subtasks :
62
+ text .append (_email_text (session , task ))
82
63
83
64
text = "\n ---------------------------------------\n \n " .join (text )
84
65
85
- status = save (' todo' , ' .email' , str (next_weekday ), data = text , extension = ' txt' )
66
+ status = save (session , " todo" , " .email" , str (next_weekday ), data = text , extension = " txt" )
86
67
if status == IsSaved .SAME :
87
68
return # Don't re-send if nothing changed
88
69
89
- subject = f"🍕 Todo list { next_weekday } "
70
+ subject = f"🍕 Todo list { next_weekday } "
90
71
if status != IsSaved .NEW :
91
72
subject = f"⚠⚠ { subject } " # There is an update...
92
- send_email (
93
- subject = subject ,
94
- text = text , email_from = session .creds .other_info ['email_from' ], email_to = session .creds .other_info ['email_to' ])
73
+ send_email (subject = subject , text = text , email_from = session .creds .other_info ["email_from" ], email_to = session .creds .other_info ["email_to" ])
95
74
96
75
97
76
def main ():
98
77
all_tasks : dict [date , dict [str , list ]] = defaultdict (lambda : defaultdict (list ))
99
78
100
- Smartschool . start (PathCredentials ())
101
- assert ' email_from' in session .creds .other_info
102
- assert ' email_to' in session .creds .other_info
79
+ session = Smartschool (PathCredentials ())
80
+ assert " email_from" in session .creds .other_info
81
+ assert " email_to" in session .creds .other_info
103
82
104
- for day in FutureTasks (). days :
83
+ for day in FutureTasks (session ) :
105
84
for course in day .courses :
106
85
assert not course .items .materials , "Please correct the model to include this information."
107
86
108
87
for task in course .items .tasks :
109
- process_task (task , all_tasks )
110
-
111
- report_tasks_for_next_day (all_tasks )
88
+ process_task (session , task , all_tasks )
89
+
90
+ report_tasks_for_next_day (session , all_tasks )
112
91
113
92
114
- if __name__ == ' __main__' :
93
+ if __name__ == " __main__" :
115
94
main ()
0 commit comments