forked from metakgp/gyft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gyft.py
163 lines (137 loc) · 5.28 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from bs4 import BeautifulSoup as bs
import re
import json
import getpass
#### Parsing from commmand line
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--user", help="ERP Username/Login ID")
args = parser.parse_args()
if args.user is None:
args.user = input("Enter you Roll Number: ")
erp_password = getpass.getpass("Enter your ERP password: ")
#### Parsing ends
ERP_HOMEPAGE_URL = 'https://erp.iitkgp.ac.in/IIT_ERP3/'
ERP_LOGIN_URL = 'https://erp.iitkgp.ac.in/SSOAdministration/auth.htm'
ERP_SECRET_QUESTION_URL = 'https://erp.iitkgp.ac.in/SSOAdministration/getSecurityQues.htm'
headers = {
'timeout': '20',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36',
}
s = requests.Session()
r = s.get(ERP_HOMEPAGE_URL)
soup = bs(r.text, 'html.parser')
sessionToken = soup.find_all(id='sessionToken')[0].attrs['value']
r = s.post(ERP_SECRET_QUESTION_URL, data={'user_id': args.user},
headers = headers)
secret_question = r.text
print ("Your secret question: " + secret_question)
secret_answer = getpass.getpass("Enter the answer to the security question: ")
login_details = {
'user_id': args.user,
'password': erp_password,
'answer': secret_answer,
'sessionToken': sessionToken,
'requestedUrl': 'https://erp.iitkgp.ac.in/IIT_ERP3',
}
r = s.post(ERP_LOGIN_URL, data=login_details,
headers = headers)
try:
ssoToken = re.search(r'\?ssoToken=(.+)$',
r.history[1].headers['Location']).group(1)
except IndexError:
print("Error: Please make sure the entered credentials are correct!")
ERP_TIMETABLE_URL = "https://erp.iitkgp.ac.in/Acad/student/view_stud_time_table.jsp"
timetable_details = {
'ssoToken': ssoToken,
'module_id': '16',
'menu_id': '40',
}
# This is just a hack to get cookies. TODO: do the standard thing here
abc = s.post('https://erp.iitkgp.ac.in/Acad/student/view_stud_time_table.jsp', headers=headers, data=timetable_details)
cookie_val = None
for a in s.cookies:
if (a.path == "/Acad/"):
cookie_val = a.value
cookie = {
'JSESSIONID': cookie_val,
}
r = s.post('https://erp.iitkgp.ac.in/Acad/student/view_stud_time_table.jsp',cookies = cookie, headers=headers, data=timetable_details)
soup = bs(r.text, 'html.parser')
rows_head = soup.findAll('table')[2]
rows = rows_head.findAll('tr')
times = []
# Delete the rows that doesn't have tableheader, basically without a weekday
del_rows = []
for i in range(1, len(rows)):
HeaderRows = rows[i].findAll("td", {"class": "tableheader"})
# print(HeaderRows)
if len(HeaderRows) is 0:
del_rows.append(i)
for index_del in sorted(del_rows, reverse=True):
del rows[index_del]
##### For timings
for a in rows[0].findAll('td'):
if ('AM' in a.text or 'PM' in a.text):
times.append(a.text)
#### For timings end
days = {}
#### For day
days[1] = "Monday"
days[2] = "Tuesday"
days[3] = "Wednesday"
days[4] = "Thursday"
days[5] = "Friday"
days[6] = "Saturday"
#### For day end
timetable_dict = {}
for i in range(1, len(rows)):
timetable_dict[days[i]] = {}
tds = rows[i].findAll('td')
time = 0
for a in range(1, len(tds)):
if not tds[a].find('b'):
continue
txt = tds[a].find('b').text.strip()
if (len(txt) >= 7):
timetable_dict[days[i]][times[time]] = list(
(
tds[a].find('b').text[:7],
tds[a].find('b').text[7:],
int(tds[a].attrs['colspan'])
)
)
time = time + int(tds[a].attrs['colspan'])
def merge_slots(in_dict):
for a in in_dict:
in_dict[a] = sorted(in_dict[a])
for i in range(len(in_dict[a]) - 1, 0, -1):
if (in_dict[a][i][0] == in_dict[a][i-1][0] + in_dict[a][i-1][1]):
in_dict[a][i-1][1] = in_dict[a][i][1] + in_dict[a][i-1][1]
in_dict[a].remove(in_dict[a][i])
in_dict[a] = in_dict[a][0]
return (in_dict)
for day in timetable_dict.keys():
subject_timings = {}
for time in timetable_dict[day]:
flattened_time = int(time[:time.find(':')])
if (flattened_time < 6):
flattened_time += 12
if (not timetable_dict[day][time][0] in subject_timings.keys()):
subject_timings[timetable_dict[day][time][0]] = []
subject_timings[timetable_dict[day][time][0]].append([flattened_time, timetable_dict[day][time][2]])
subject_timings = merge_slots(subject_timings)
for time in list(timetable_dict[day].keys()):
flattened_time = int(time[:time.find(':')])
if (flattened_time < 6):
flattened_time += 12
if (not flattened_time == subject_timings[timetable_dict[day][time][0]][0]):
del (timetable_dict[day][time])
else:
timetable_dict[day][time][2] = subject_timings[timetable_dict[day][time][0]][1]
with open('data.txt', 'w') as outfile:
json.dump(timetable_dict, outfile, indent = 4, ensure_ascii=False)
print ("\n\nTimetable saved to data.txt file. Be sure to edit this file to have desired names of subjects rather than subject codes.\n")