-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetcher.py
More file actions
276 lines (234 loc) · 11 KB
/
Copy pathfetcher.py
File metadata and controls
276 lines (234 loc) · 11 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
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
import logging
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
from flask import g, has_request_context
logger = logging.getLogger('SchoolGradesServer.Fetcher')
def _mask_user_id(user_id: str) -> str:
if not user_id:
return "Unknown"
s = str(user_id)
if len(s) > 3:
return s[:3] + "***"
return "***"
def _parse_year_term(year_value, default_year="114", default_term="1"):
try:
s = str(year_value)
if "_" in s:
return s.split("_", 1)
if len(s) >= 4:
return s[:-1], s[-1]
except Exception:
pass
return default_year, default_term
def _log(level: str, user_id: str, msg: str, req_id: str = None):
if not req_id and has_request_context() and 'request_id' in g:
req_id = g.request_id
r_str = f"[{req_id}] " if req_id else ""
u_str = f"[User:{_mask_user_id(user_id)}] " if user_id else ""
final_msg = f"{r_str}{u_str}{msg}"
if level == 'error':
logger.error(final_msg)
else:
logger.info(final_msg)
# Note: InsecureRequestWarning is not disabled here; the HTTP session enforces SSL verification.
class GradeFetcher:
BASE = "https://shcloud2.k12ea.gov.tw/CLHSTYC"
LOGIN_PAGE = f"{BASE}/Auth/Auth/CloudLogin"
DO_CHECK = f"{BASE}/Auth/Auth/DoCloudLoginCheck"
GRADES_PAGE = f"{BASE}/ICampus/StudentInfo/Index?page=%E6%88%90%E7%B8%BE%E6%9F%A5%E8%A9%A2"
API_BASE = f"{BASE}/ICampus"
def __init__(self, session_factory=None):
if session_factory is None:
# Delay import to avoid circular dependency if any, or just use the injected one
from app.services.http_client import get_http_session
self.session_factory = get_http_session
else:
self.session_factory = session_factory
def _get_hidden_token(self, html: str) -> str:
"""Extract __RequestVerificationToken from HTML."""
soup = BeautifulSoup(html, "html.parser")
el = soup.select_one('input[name="__RequestVerificationToken"]')
if not el or not el.get("value"):
raise RuntimeError("找不到 __RequestVerificationToken hidden input")
return el["value"]
def login_and_get_tokens(self, username, password):
"""Login via requests session, return (success, message, cookies_dict, student_no, token)."""
try:
_log('info', username, "Attempting login (requests mode)")
s = self.session_factory()
# 1) GET login page to obtain cookies + hidden token
r = s.get(self.LOGIN_PAGE)
r.raise_for_status()
login_token = self._get_hidden_token(r.text)
# 2) POST login
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Referer": GradeFetcher.LOGIN_PAGE,
"Origin": "https://shcloud2.k12ea.gov.tw",
"X-Requested-With": "XMLHttpRequest",
}
data = {
"SchoolCode": "030305",
"LoginId": username,
"PassString": password,
"LoginType": "Student",
"IsKeepLogin": "false",
"IdentityId": "6",
"SchoolName": "國立中大壢中",
"GoogleToken": "8",
"isRegistration": "false",
"ShCaptchaGenCode": "10",
"__RequestVerificationToken": login_token,
}
resp = s.post(self.DO_CHECK, data=data, headers=headers)
resp.raise_for_status()
try:
j = resp.json()
except Exception:
return False, "登入回應格式錯誤", None, None, None
ok = bool(j.get("Result", {}).get("IsLoginSuccess"))
if not ok:
msg = j.get("Result", {}).get("DisplayMsg") or j.get("Message") or "登入失敗"
return False, msg, None, None, None
_log('info', username, "Login OK, fetching grades page for API token...")
# 3) GET grades page to obtain the API-specific __RequestVerificationToken
r2 = s.get(self.GRADES_PAGE)
r2.raise_for_status()
api_token = self._get_hidden_token(r2.text)
# 4) Extract cookies as dict (filter out malformed cookies like 'no-cache')
cookies_dict = {}
for c in s.cookies:
try:
if c.name and c.value is not None and c.domain is not None:
cookies_dict[c.name] = c.value
except Exception:
pass
student_no = username
_log('info', student_no, f"Successfully obtained credentials for {student_no}")
return True, "登入成功", cookies_dict, student_no, api_token
except Exception as e:
_log('error', username, f"Login Exception: {e}")
return False, "登入錯誤: 伺服器內部錯誤", None, None, None
def get_structure_via_api(self, cookies, student_no, token, session=None):
"""Fetch structure using requests"""
url = "https://shcloud2.k12ea.gov.tw/CLHSTYC/ICampus/CommonData/GetGradeCanQueryYearTermListByStudentNo"
headers = {
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Origin": "https://shcloud2.k12ea.gov.tw",
"Referer": "https://shcloud2.k12ea.gov.tw/CLHSTYC/ICampus/StudentInfo/Index?page=%E6%88%90%E7%B8%BE%E6%9F%A5%E8%A9%A2"
}
data = {
"searchType": "各次考試單科成績",
"studentNo": student_no,
"__RequestVerificationToken": token
}
own_session = False
if session is None:
session = self.session_factory()
own_session = True
try:
_log('info', student_no, f"Requesting structure for {student_no}...")
response = session.post(url, headers=headers, data=data, cookies=cookies)
response.raise_for_status()
years_data = response.json()
structure = {}
# Collect items to fetch
items = []
for item in years_data:
name = item.get('DisplayText') or item.get('text')
value = item.get('Value') or item.get('value')
if value:
items.append((name, value))
# Fetch all exams in parallel
current_req_id = g.request_id if has_request_context() and 'request_id' in g else None
def _fetch_one(name_value):
n, v = name_value
exams = self.get_exams_via_api(cookies, student_no, token, v, req_id=current_req_id, session=session)
return n, v, exams
with ThreadPoolExecutor(max_workers=min(len(items) or 1, 10)) as pool:
for name, value, exams in pool.map(_fetch_one, items):
structure[name] = {
"year_value": value,
"exams": exams
}
return structure
except Exception as e:
_log('error', student_no, f"Error fetching structure: {e}")
return {}
finally:
if own_session:
session.close()
def get_exams_via_api(self, cookies, student_no, token, year_value, req_id=None, session=None):
"""Helper to fetch exams for a year"""
url = "https://shcloud2.k12ea.gov.tw/CLHSTYC/ICampus/CommonData/GetGradeCanQueryExamNoListByStudentNo"
year, term = _parse_year_term(year_value, default_year="114", default_term="1")
headers = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://shcloud2.k12ea.gov.tw/CLHSTYC/ICampus/StudentInfo/Index?page=%E6%88%90%E7%B8%BE%E6%9F%A5%E8%A9%A2"
}
data = {
"searchType": "單次考試所有成績",
"studentNo": student_no,
"year": year,
"term": term,
"__RequestVerificationToken": token
}
own_session = False
if session is None:
session = self.session_factory()
own_session = True
try:
resp = session.post(url, headers=headers, data=data, cookies=cookies)
if resp.status_code == 200:
exams = []
for item in resp.json():
exams.append({
"text": item.get('DisplayText') or item.get('text'),
"value": item.get('Value') or item.get('value')
})
return exams
except Exception as e:
_log('error', student_no, f"Error fetching exams via API: {e}", req_id=req_id)
finally:
if own_session:
session.close()
return []
def fetch_grades_via_api(self, cookies, student_no, token, year_value, exam_value, session=None):
"""Fetch grades using requests"""
url = "https://shcloud2.k12ea.gov.tw/CLHSTYC/ICampus/TutorShGrade/GetScoreForStudentExamContent"
headers = {
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://shcloud2.k12ea.gov.tw/CLHSTYC/ICampus/StudentInfo/Index?page=%E6%88%90%E7%B8%BE%E6%9F%A5%E8%A9%A2"
}
year, term = _parse_year_term(year_value, default_year="114", default_term="2")
data = {
"StudentNo": student_no,
"SearchType": "單次考試所有成績",
"__RequestVerificationToken": token,
"Year": year,
"Term": term,
"ExamNo": exam_value
}
_log('info', student_no, f"API Fetching grades: Year={year}, Term={term}, Exam={exam_value}")
own_session = False
if session is None:
session = self.session_factory()
own_session = True
try:
response = session.post(url, headers=headers, data=data, cookies=cookies)
response.raise_for_status()
return response.json()
except Exception as e:
_log('error', student_no, f"API Fetch Error: {e}")
raise e
finally:
if own_session:
session.close()