-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbunker_mod.py
261 lines (206 loc) · 7.84 KB
/
bunker_mod.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import math
import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
def data_json(data):
"""
here we convert the data to json format calculate the amount days we have to take leave.
"""
response_data = []
threshold = 0.75
for item in range(1, len(data)):
item = data[item]
temp = {}
# Extract data
temp["name"] = item[0]
temp["total_hours"] = int(item[1])
temp["exception_hour"] = int(item[2])
temp["total_present"] = int(item[4])
temp["percentage_of_attendance"] = int(item[5])
# temp["percentage_of_attendance_with_exemp"] = int(item[6])
# temp["percentage_of_attendance_with_med_exemp"] = int(item[7])
temp["attendance_from"] = item[8]
temp["attendance_to"] = item[9]
# temp["med_exception_hour"] = math.floor(
# (
# (temp["percentage_of_attendance_with_med_exemp"] / 100)
# * temp["total_hours"]
# )
# - temp["total_present"]
# )
# temp["total_present_with_exemp"] = (
# temp["total_present"] + temp["exception_hour"] + temp["med_exception_hour"]
# )
# Calculate bunker functionality
if temp['percentage_of_attendance'] <= 75:
temp['class_to_attend'] = math.ceil((threshold*temp['total_hours'] - temp['total_present'])/(1-threshold))
else:
temp['class_to_bunk'] = math.floor((temp['total_present']-(threshold*temp['total_hours']))/(threshold))
response_data.append(temp)
return response_data
def return_attendance(username, pwd):
try:
session = requests.Session()
r = session.get("https://ecampus.psgtech.ac.in/studzone2/")
loginpage = session.get(r.url)
soup = BeautifulSoup(loginpage.text, "html.parser")
viewstate = soup.select("#__VIEWSTATE")[0]["value"]
eventvalidation = soup.select("#__EVENTVALIDATION")[0]["value"]
viewstategen = soup.select("#__VIEWSTATEGENERATOR")[0]["value"]
item_request_body = {
"__EVENTTARGET": "",
"__EVENTARGUMENT": "",
"__LASTFOCUS": "",
"__VIEWSTATE": viewstate,
"__VIEWSTATEGENERATOR": viewstategen,
"__EVENTVALIDATION": eventvalidation,
"rdolst": "S",
"txtusercheck": username,
"txtpwdcheck": pwd,
"abcd3": "Login",
}
response = session.post(
url=r.url, data=item_request_body, headers={"Referer": r.url}
)
val = response.url
if response.status_code == 200:
defaultpage = "https://ecampus.psgtech.ac.in/studzone2/AttWfPercView.aspx"
page = session.get(defaultpage)
soup = BeautifulSoup(page.text, "html.parser")
data = []
column = []
table = soup.find("table", attrs={"class": "cssbody"})
if table == None:
message = str(soup.find("span", attrs={"id": "Message"}))
if "On Process" in message:
return "Table is being updated"
try:
rows = table.find_all("tr")
for index, row in enumerate(rows):
cols = row.find_all("td")
cols = [ele.text.strip() for ele in cols]
# Get rid of empty val
data.append([ele for ele in cols if ele])
# df = pd.DataFrame(data, columns=column)
# res = df.to_json(orient="split")
# return res
return data, session
except:
return "Invalid password"
else:
return "Try again after some time"
except:
return "Try again after some time"
def return_timetable(session):
defaultpage = "https://ecampus.psgtech.ac.in/studzone2/AttWfStudTimtab.aspx"
page = session.get(defaultpage)
soup = BeautifulSoup(page.text, "html.parser")
data = []
table = soup.find("table", attrs={"id": "TbCourDesc"})
if table == None:
return {"error": "no data"}
try:
rows = table.find_all("tr")
for index, row in enumerate(rows):
cols = row.find_all("td")
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
class_id = {}
for i in range(1, len(data)):
class_id[data[i][0]] = data[i][1]
return class_id
except:
return {"error": "no data"}
def gradeMap(grade):
grade_score_map = {
"O": 10,
"A+": 9,
"A": 8,
"B+": 7,
"B": 6,
"C+": 5,
"C": 4,
"W": 0,
"RA": 0,
"SA": 0,
}
if grade not in grade_score_map.keys():
return 0
return grade_score_map[grade]
def return_cgpa(session):
resultspage = "https://ecampus.psgtech.ac.in/studzone2/FrmEpsStudResult.aspx"
page = session.get(resultspage)
soup = BeautifulSoup(page.text, "html.parser")
latest_sem_data = []
table = soup.find("table", attrs={"id": "DgResult"})
if table != None:
try:
rows = table.find_all("tr")
for index, row in enumerate(rows):
cols = row.find_all("td")
cols = [ele.text for ele in cols]
latest_sem_data.append([ele for ele in cols if ele])
except:
print("No results available !!")
coursepage = "https://ecampus.psgtech.ac.in/studzone2/AttWfStudCourseSelection.aspx"
page = session.get(coursepage)
soup = BeautifulSoup(page.text, "html.parser")
data = []
table = soup.find("table", attrs={"id": "PDGCourse"})
if table != None:
try:
rows = table.find_all("tr")
for index, row in enumerate(rows):
cols = row.find_all("td")
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele])
except:
print("No Course Info available !!")
else:
print("No Course Info available !!")
if len(data) == 0 and len(latest_sem_data) == 0:
return {"error": "No data"}
global df
global latest_sem_records
# Preprocess latest sem results if available
if len(latest_sem_data) != 0:
latest_sem_data.pop(0)
# print(latest_sem_data)
latest_sem_records = pd.DataFrame(
latest_sem_data,
columns=[
"COURSE SEM",
"COURSE CODE",
"COURSE TITLE",
"CREDITS",
"GRADE",
"RESULT",
],
)
# print(latest_sem_records)
latest_sem_records["GRADE"] = latest_sem_records["GRADE"].str.split().str[-1]
# print(latest_sem_records["COURSE SEM"])
latest_sem_records["COURSE SEM"] = latest_sem_records["COURSE SEM"].replace(
r"^\s*$", np.nan, regex=True
)
# print(latest_sem_records["COURSE SEM"])
latest_sem_records["COURSE SEM"].fillna(method="ffill", inplace=True)
# print(latest_sem_records["COURSE SEM"])
try:
cols = data.pop(0)
df = pd.DataFrame(data, columns=cols)
# Add latest sem results if available
if len(latest_sem_data) != 0:
df = df.append(latest_sem_records, ignore_index=True)
df.drop_duplicates(subset="COURSE CODE", keep="last", inplace=True)
except:
df = latest_sem_records.copy()
# CPGA calculation
latest_sem = df["COURSE SEM"].max()
df["CREDITS"] = df["CREDITS"].astype(int)
df["GRADE"] = df["GRADE"].apply(gradeMap)
df["COURSE SCORE"] = df["GRADE"] * df["CREDITS"]
latest_cgpa = df["COURSE SCORE"].sum() / df["CREDITS"].sum()
res = {"lastest_sem": latest_sem, "latest_sem_cgpa": round(latest_cgpa, 3)}
return res