-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_attendance.py
116 lines (88 loc) · 3.74 KB
/
auto_attendance.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
import os
import sys
from requests import Session
from datetime import datetime
from apscheduler.schedulers.blocking import BlockingScheduler as Scheduler
BASE_URL = "https://erp.iith.ac.in/MobileAPI/"
TIMETABLE_PATH = "GetStudentTimeTableForAttendance"
MARK_ATTENDANCE_PATH = "UpSertStudentAttendanceDetails"
if len(sys.argv) > 1:
CONFIG_PATH = sys.argv[1]
else:
CONFIG_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config.txt")
def load_config():
if os.path.exists(CONFIG_PATH):
with open(CONFIG_PATH, "r") as f:
return f.read().splitlines()
else:
return None
def timetable_req(webIdentifier):
body = { "WebIdentifier": webIdentifier }
try:
res = req.post(BASE_URL + TIMETABLE_PATH, json=body)
except Exception as e:
return False, f"{e}\nProbably a server-side error, IDK\n"
if res.status_code == 200:
return True, res.json()["table"]
else:
return False, f"Http Status {res.status_code}\n{res.text}"
def mark_attendance_req(webIdentifier, timeTableId):
body = {
"Webidentifier": webIdentifier,
"TimeTableId": timeTableId
}
try:
res = req.post(BASE_URL + MARK_ATTENDANCE_PATH, json=body)
except Exception as e:
return False, f"{e}\nProbably a server-side error, IDK\n"
if res.status_code == 200:
data = res.json()["table"][0]
if data["errorid"] == 0:
return True, None
else:
return False, data["errormessage"] + "\n"
else:
return False, f"Http Status {res.status_code}\n{res.text}"
def check_and_mark(webIdentifier, name):
success1, data = timetable_req(webIdentifier)
if not success1:
print("\nUnable to fetch timetable for", name, "\nError:", data)
else:
for course in data:
if course["classGroup"] == "Ongoing" and not course["attendanceMarked"]:
success2, err_msg = mark_attendance_req(webIdentifier, course["timeTableId"])
if success2:
print(f"Attendance marked for {name} for {course['courseCode']}.")
else:
print("Unable to mark attendance for", name, "\nError:", err_msg)
break
else: # no break
print(f"No attendance to mark for {name}.")
def main():
global req
req = Session()
print(datetime.now())
if config := load_config():
print(f"Config loaded, found {len(config)} entries.\n")
for entry in config:
check_and_mark( *map(str.strip, entry.split(",")[:2]) )
else:
print(f"No config found at {os.path.abspath(CONFIG_PATH)}")
print(f"\n{'-'*50}\n")
def start_scheduler():
scheduler = Scheduler()
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour= 9, minute=10, misfire_grace_time = 120)
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour=10, minute=10, misfire_grace_time = 120)
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour=11, minute=10, misfire_grace_time = 120)
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour=12, minute=10, misfire_grace_time = 120)
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour=14, minute=40, misfire_grace_time = 120)
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour=16, minute=10, misfire_grace_time = 120)
scheduler.add_job(main, "cron", day_of_week="mon-fri", hour=17, minute=40, misfire_grace_time = 120)
print(datetime.now(), "Scheduler started", f"Config path: {os.path.abspath(CONFIG_PATH)}", "=" * 50, "\n", sep="\n")
scheduler.start()
if __name__ == "__main__":
try:
start_scheduler()
except Exception as e:
print("\nError: ", e)
exit(1)