-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherp_fetch.py
More file actions
232 lines (187 loc) · 7.26 KB
/
Copy patherp_fetch.py
File metadata and controls
232 lines (187 loc) · 7.26 KB
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
from dotenv import load_dotenv
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup
from datetime import datetime
import pytz
import time
import os
import json
# Current day
IST = pytz.timezone("Asia/Kolkata")
now = datetime.now(IST)
CURRENT_DAY_INDEX = now.weekday()
DAY_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
CURRENT_DAY_NAME = DAY_NAMES[CURRENT_DAY_INDEX]
print(f"Running for: {CURRENT_DAY_NAME} ({now.strftime('%d %b %Y, %I:%M %p IST')})")
options = webdriver.ChromeOptions()
options.add_argument("--headless")
# options.add_argument("--no-sandbox")
# options.add_argument("--disable-dev-shm-usage")
# options.add_argument("--disable-gpu")
# options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(
options=options
)
def login():
load_dotenv()
driver.get("https://erp.psit.ac.in")
wait = WebDriverWait(driver, 15)
try:
accept_btn = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Accept')]")))
driver.execute_script("arguments[0].click();", accept_btn)
except:
pass
get_username = os.getenv("username")
get_password = os.getenv("password")
try:
username_field = wait.until(EC.presence_of_element_located((By.NAME, "username")))
password_field = driver.find_element(By.NAME, "password")
username_field.send_keys(get_username)
time.sleep(2)
password_field.send_keys(get_password)
submit_button = driver.find_element(By.XPATH, "//input[@type='submit'] | //button[@type='submit']")
driver.execute_script("arguments[0].click();", submit_button)
try:
WebDriverWait(driver, 20).until(EC.url_contains("/Student"))
print("login successful")
return True
except:
print("login failed - invalid credentials or unexpected redirect")
return False
except Exception as e:
print("login error:", e)
return False
def parse_attendance_stats(soup):
target_labels = [
"Total Lecture",
"Total Absent + OAA",
"O.A. Attendance",
"Attendance % without PF",
"How To Calculate Percentage",
"Total PF",
"Attendance % with PF",
]
extracted = {}
page_text = soup.get_text(separator="\n")
lines = [line.strip() for line in page_text.splitlines() if line.strip()]
for i, line in enumerate(lines):
for label in target_labels:
if label.lower() in line.lower():
if ":" in line:
parts = line.split(":", 1)
value = parts[1].strip() if len(parts) > 1 else ""
elif "=" in line:
parts = line.split("=", 1)
value = parts[1].strip() if len(parts) > 1 else ""
else:
value = lines[i + 1] if i + 1 < len(lines) else ""
extracted[label] = value
break
return extracted
def get_attendance():
driver.get("https://erp.psit.ac.in/Student/MyAttendanceDetail")
wait = WebDriverWait(driver, 15)
try:
wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(3)
soup = BeautifulSoup(driver.page_source, "html.parser")
stats = parse_attendance_stats(soup)
if stats:
print("\n===== Attendance Summary =====")
for label, value in stats.items():
print(f" {label} : {value}")
print("==============================\n")
else:
print("could not find attendance stats")
print(soup.get_text(separator="\n")[:3000])
return stats
except Exception as e:
print("error loading attendance page:", e)
return {}
def extract_timetable_for_day(soup, day_name):
day_name_lower = day_name.lower()
# Strategy 1: day is a column header
for table in soup.find_all("table"):
headers = [th.get_text(strip=True).lower() for th in table.find_all("th")]
if any(day_name_lower in h for h in headers):
col_index = next(
(i for i, h in enumerate(headers) if day_name_lower in h), None
)
if col_index is not None:
slots = []
for row in table.find_all("tr")[1:]:
cells = row.find_all(["td", "th"])
if col_index < len(cells):
cell_text = cells[col_index].get_text(strip=True)
if cell_text:
slots.append(cell_text)
if slots:
return slots
# Strategy 2: day is the first cell of a row
for table in soup.find_all("table"):
for row in table.find_all("tr"):
cells = row.find_all(["td", "th"])
if cells and day_name_lower in cells[0].get_text(strip=True).lower():
slots = [c.get_text(strip=True) for c in cells[1:] if c.get_text(strip=True)]
if slots:
return slots
# Strategy 3: day appears as a heading, content in next sibling
day_header = soup.find(
lambda tag: tag.name in ("h2", "h3", "h4", "h5", "th", "td", "div", "span", "p")
and day_name_lower in tag.get_text(strip=True).lower()
)
if day_header:
sibling = day_header.find_next_sibling()
while sibling:
text = sibling.get_text(separator=" | ", strip=True)
if text:
return [text]
sibling = sibling.find_next_sibling()
return []
def get_timetable():
driver.get("https://erp.psit.ac.in/Student/MyTimeTable")
wait = WebDriverWait(driver, 15)
try:
wait.until(EC.presence_of_element_located((By.TAG_NAME, "body")))
time.sleep(3)
soup = BeautifulSoup(driver.page_source, "html.parser")
slots = extract_timetable_for_day(soup, CURRENT_DAY_NAME)
print(f"\n===== Timetable for {CURRENT_DAY_NAME} =====")
if slots:
for i, slot in enumerate(slots, 1):
print(f" Period {i}: {slot}")
else:
print(" no classes found")
print(soup.get_text(separator="\n")[:3000])
print("=" * (len(CURRENT_DAY_NAME) + 22) + "\n")
return slots
except Exception as e:
print("error loading timetable page:", e)
return []
def save_to_json(attendance, timetable, success=True):
payload = {
"success": success,
"day": CURRENT_DAY_NAME,
"date": now.strftime("%d %b %Y, %I:%M %p IST"),
"attendance": attendance,
"timetable": timetable
}
with open("data.json", "w") as f:
json.dump(payload, f, indent=2)
print("data saved to data.json")
#main flow
if login():
time.sleep(2)
attendance_data = get_attendance()
time.sleep(2)
timetable_data = get_timetable()
save_to_json(attendance_data, timetable_data, success=True)
else:
save_to_json({}, [], success=False)
print("aborting - login unsuccessful")
driver.quit()