-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
371 lines (281 loc) · 13.3 KB
/
main.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import UnexpectedAlertPresentException
# Chrome Driver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import os, json
import base64
from datetime import datetime
import time
import logging
from utils import extract_data_from_excel
# Create a logger
logger = logging.getLogger()
logger.setLevel(logging.INFO) # Set the base level for the logger
# Create a console handler for INFO level and above
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO) # Show INFO and higher levels on the console
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
# Create a file handler for INFO level and above
os.makedirs("logs", exist_ok=True)
timestamp = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
file_handler = logging.FileHandler(f'logs/{timestamp}.log', mode='a')
file_handler.setLevel(logging.INFO) # Log ERROR and higher levels to a file
file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)
class BaseResult:
"""
Stores students data such as rollno, name, dob extracted from excel sheet
"""
Students = []
def __init__(self):
self.driver_options = Options()
self.driver_options.add_argument('--kiosk-printing')
self.driver_options.add_argument('--log-level=3')
self.driver_options.add_argument('--guest')
self.driver_options.add_argument("--disable-dev-shm-usage")
self.driver_options.add_argument("--no-sandbox")
self.driver_options.add_argument("--headless")
self.driver_options.add_argument("--disable-gpu")
self.service = ChromeService(ChromeDriverManager().install())
self.driver = webdriver.Chrome(service=self.service, options=self.driver_options)
def read_excel_sheet(self, filename, fullname_col='Full Name', rollno_col='Roll Number', dob_col='Date of Birth'):
"""
Reads student data from an Excel sheet and stores it in the Students attribute.
Args:
filename (str): Path to the Excel file.
fullname_col (str): Column name for full names. Default is 'Full Name'.
rollno_col (str): Column name for roll numbers. Default is 'Roll Number'.
dob_col (str): Column name for dates of birth. Default is 'Date of Birth'.
"""
self.Students = extract_data_from_excel(filename, fullname_col, rollno_col, dob_col)
def save_as_pdf(self, path):
"""
Saves the current page displayed in the browser as a PDF file.
Args:
path (str): The file path where the PDF will be saved. If the specified
directory does not exist, it will be created.
"""
directory = os.path.dirname(path)
if not os.path.exists(directory):
os.makedirs(directory)
print_options = {
'landscape': False,
'displayHeaderFooter': False,
'printBackground': True,
'preferCSSPageSize': True
}
# Convert the page to PDF
result = self.driver.execute_cdp_cmd("Page.printToPDF", print_options)
# Decode the base64-encoded PDF data
pdf_data = base64.b64decode(result['data'])
# Write the PDF to a file
with open(path, 'wb') as file:
file.write(pdf_data)
def check_result_exist(self, name, rollno, result_folder):
"""
Checks if a result PDF file already exists for the given student.
Args:
name (str): The full name of the student.
rollno (str): The roll number of the student.
result_folder (str): The folder where results are stored.
Returns:
bool: True if the file exists, False otherwise.
"""
format_name = name.replace(' ', '_')
result_path = f"{result_folder}/result-{format_name}-{rollno}.pdf"
if os.path.exists(result_path):
logging.info(f"The file '{result_path}' already exists.")
return True
return False
def close(self):
"""
Closes the browser and quits the WebDriver session.
"""
self.driver.quit()
class CSJMUResult(BaseResult):
"""
Update this baseDetails according to your course and year
"""
baseDetails = {
"sessionId": "24",
"category": "RG",
"course": "BACHELOR OF COMPUTER APPLICATION",
"sem": "6"
}
def get_all_students(self):
"""
Loop through all students in list and process for result
"""
if not self.Students:
logging.info("Student list is EMPTY! Please provide the list to process result!")
return
for st in self.Students:
self.process_student(st['name'], st['rollno'], st['dob'])
def process_student(self, name, rollno, dob):
"""
Gets result from official portal
It takes name, rollnumber and date of birth as an arguement
"""
if self.check_result_exist(name, rollno, "Results"):
return
try:
# Open CSJMU Website
self.driver.get("https://admission.csjmu.ac.in/DisplayResult/Index")
# Fill session id
dropdown_sessionid = self.driver.find_element(By.ID, "SessionID")
select = Select(dropdown_sessionid)
select.select_by_value(self.baseDetails['sessionId'])
# Wait for api to populate exam category
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, f"//option[@value='{self.baseDetails['category']}']"))
)
dropdown_examcode = self.driver.find_element(By.ID, "ExamCategoryCode")
select = Select(dropdown_examcode)
select.select_by_value(self.baseDetails['category'])
# wait for api to populate course options
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, f"//option[text()='{self.baseDetails['course']}']"))
)
# click on course option
dropdown_course = self.driver.find_element(By.XPATH, "//span[@id='select2-SubCourseID-container']")
dropdown_course.click()
# wait and click on course
dropdown_option = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.XPATH, f"//li[@class='select2-results__option' and text()='{self.baseDetails['course']}']")))
dropdown_option.click()
# wait for api to populate semester option
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.XPATH, f"//option[@value='{self.baseDetails['sem']}']"))
)
dropdown_sem = self.driver.find_element(By.ID, "SemYearID")
select = Select(dropdown_sem)
select.select_by_value(self.baseDetails['sem'])
# Fill rollno
rollno_input = self.driver.find_element(By.ID, "StRollNo")
rollno_input.send_keys(rollno)
# Fill date
date_input = self.driver.find_element(By.ID, "StDOB")
date_input.send_keys(dob)
# Wait for button to appear and click
view_button = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, "btn-primary"))
)
view_button.click()
# Wait for print button and save the result
print_button = WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.XPATH, "//input[@type='button' and @value='Print Result']"))
)
if print_button:
# print(True)
format_name = name.replace(' ', '_')
self.save_as_pdf(f"Results/result-{format_name}-{rollno}.pdf")
logging.info(f"---- Download Result Pdf!! {name} ----")
except UnexpectedAlertPresentException as e:
# e.msg holds more details about error
details = f"---- Failed to Get Pdf!! {name} {rollno} {dob} ----"
logging.info(details)
logging.error("Error: {}".format(e.alert_text))
# Extra functions
def get_roll_no(self, enrollno):
"""
Misc function
Gets student roll number from enrollment number
It takes enrollment number as an arguement (format: CSJMA2209120121)
"""
try:
# Open the website
self.driver.get("https://admission.csjmu.ac.in/Search/SearchRollNumber")
# Find the input field by ID and enter a value
enrolment_field = self.driver.find_element(By.ID, "EnrolmentNo")
enrolment_field.send_keys(enrollno) # Replace with the actual value
# Find the search button by ID and click it
search_button = self.driver.find_element(By.ID, "btnSearch")
search_button.click()
# Wait for results or further actions
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "table.table.table-sm"))
)
table = self.driver.find_element(By.CSS_SELECTOR, "table.table.table-sm")
cells = table.find_elements(By.TAG_NAME, "td")
# The roll number should be in the second cell
roll_number = cells[0].text
return roll_number
except Exception as e:
logging.error(f"An error occurred: {e}")
return None
class AKTUResult(BaseResult):
def get_all_students(self):
"""
Loop through all students in list and process for result
"""
if not self.Students:
logging.info("Student list is EMPTY! Please provide the list to process result!")
return
for st in self.Students:
self.process_student(st['name'], st['rollno'], st['dob'])
def process_student(self, name, rollno, dob):
"""
Gets result from official portal
It takes name, rollnumber and date of birth as an arguement
"""
if self.check_result_exist(name, rollno, "AKTUResults"):
return
try:
self.driver.get("https://oneview.aktu.ac.in/WebPages/aktu/OneView.aspx")
self.roll_input = self.driver.find_element(By.ID, 'txtRollNo')
self.roll_input.send_keys(rollno)
self.submit_btn = self.driver.find_element(By.ID, 'btnProceed')
self.submit_btn.click()
input_dob = WebDriverWait(self.driver, 10).until(
EC.visibility_of_element_located((By.ID, 'txtDOB'))
)
input_dob.send_keys(dob)
input_dob.send_keys(Keys.RETURN)
# self.submit_btn = WebDriverWait(self.driver, 10).until(
# EC.element_to_be_clickable((By.ID, 'btnSearch'))
# )
# self.submit_btn.click()
# Result Page Open
# result_page = WebDriverWait(self.driver, 10).until(
# EC.visibility_of_element_located((By.ID, 'lblRollNo'))
# )
# if result_page:
self.scroll_expand()
format_name = name.replace(' ', '_')
self.save_as_pdf(f"AKTUResults/result-{format_name}-{rollno}.pdf")
logging.info(f"---- Download Result Pdf!! {name} ----")
except Exception as e:
# e.msg holds more info about the error
details = f"---- Failed to Get Pdf!! {name} {rollno} {dob} ----"
logging.info(details)
logging.error("An error occurred: ", e)
def scroll_expand(self):
self.elements = self.driver.find_elements(By.CLASS_NAME, 'headerclass')
for index, element in enumerate(self.elements):
try:
# Scroll the element into view
self.driver.execute_script("arguments[0].scrollIntoView(true);", element)
# Wait until the element is visible and clickable
WebDriverWait(self.driver, 10).until(
EC.visibility_of(element)
)
WebDriverWait(self.driver, 10).until(
EC.element_to_be_clickable((By.CLASS_NAME, 'headerclass'))
)
element.click()
time.sleep(0.5)
except Exception as e:
logging.error(f"An error occurred while clicking element {index+1}: {e}")
if __name__=="__main__":
result = CSJMUResult()
result.process_student("MANASVI MISHRA", 22015003575, "05/27/2006")
result.close()