-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathgyft.py
141 lines (117 loc) · 3.88 KB
/
gyft.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
import argparse
import iitkgp_erp_login.erp as erp
import requests
from timetable import delete_calendar, create_calendar, build_courses, generate_ics
from utils.dates import SEM_BEGIN
headers = {
"timeout": "20",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
}
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-D",
"--development",
action="store_true",
help="Automate erpcreds parsing while development (except OTP)",
)
parser.add_argument(
"-O",
"--otp",
action="store_true",
help="Automate erpcreds parsing while development (including OTP)",
)
parser.add_argument(
"-o", "--output", help="Output file containing timetable in .ics format"
)
parser.add_argument(
"-d",
"--del-events",
action="store_true",
help="Delete events automatically added by the script before adding new events",
)
args = parser.parse_args()
return args
def main():
args = parse_args()
if args.del_events:
delete_calendar()
if (
input("\nWould you like to generate a new timetable? (y/n): ").lower()
== "n"
):
return
output_filename = args.output if args.output else "timetable.ics"
session = requests.Session()
if args.otp and not args.development:
print('ERROR: -O [--otp] must be used with -D [--development]')
return
if args.development:
if args.otp:
import erpcreds
_, sso_token = erp.login(
headers,
session,
ERPCREDS=erpcreds,
OTP_CHECK_INTERVAL=2,
LOGGING=True,
)
else:
import erpcreds
_, sso_token = erp.login(
headers,
session,
ERPCREDS=erpcreds,
LOGGING=True,
)
else:
_, sso_token = erp.login(headers, session)
roll_number = erp.ROLL_NUMBER
courses = get_courses(session, sso_token, roll_number)
print("Timetable fetched.\n")
print("What would you like to do now?")
print("1. Add timetable directly to Google Calendar (requires client_secret.json)")
print("2. Generate an ICS file")
print("3. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
create_calendar(courses)
elif choice == 2:
generate_ics(courses, output_filename)
else:
exit()
def get_courses(session: requests.Session, sso_token: str, roll_number: str):
erp_timetable_url = "https://erp.iitkgp.ac.in/Acad/student/student_timetable.jsp"
courses_url: str = "https://erp.iitkgp.ac.in/Academic/student_performance_details_ug.htm?semno={}&rollno={}"
timetable_page = session.post(
headers=headers,
url=erp_timetable_url,
data={
"ssoToken": sso_token,
"module_id": "16",
"menu_id": "40",
},
)
sem_num = 1
if SEM_BEGIN.month > 6:
# autumn semester
sem_num = (int(SEM_BEGIN.strftime("%y")) - int(roll_number[:2])) * 2 + 1
else:
# spring semester - sem begin year is 1 more than autumn sem
sem_num = (int(SEM_BEGIN.strftime("%y")) - int(roll_number[:2])) * 2
r = session.post(
headers=headers,
url=courses_url.format(sem_num, roll_number),
data={
"ssoToken": sso_token,
"semno": sem_num,
"rollno": roll_number,
"order": "asc",
},
)
sub_dict = {item["subno"]: item["subname"] for item in r.json()}
course_names = {k: v.replace("&", "&") for k, v in sub_dict.items()}
courses = build_courses(timetable_page.text, course_names)
return courses
if __name__ == "__main__":
main()