-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGradesMonitorLinux.py
250 lines (223 loc) · 8.25 KB
/
GradesMonitorLinux.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
import os
import re
import rsa
import time
import json
import email
import base64
import imaplib
import smtplib
import requests
from email.mime.text import MIMEText
from config import *
headers = {
"accept": (
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif, "
"image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
),
"accept-encoding": "deflate, br",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"referer": "https://ecampus.nwpu.edu.cn/main.html",
"sec-ch-ua": '".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Linux"',
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "none",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1",
"user-agent": (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/103.0.0.0 Safari/537.36"
),
}
headers2 = headers.copy()
headers2["X-Requested-With"] = "XMLHttpRequest"
URL = (
"https://uis.nwpu.edu.cn/cas/login?service=https%3A%2F%2Fecampus.nwpu.edu.cn"
"%2F%3Fpath%3Dhttps%3A%2F%2Fecampus.nwpu.edu.cn"
)
def email_msg(title: str, content: str) -> None:
# 设置email信息
# 邮件内容设置
message = MIMEText(content, "plain", "utf-8")
# 邮件主题'
message["Subject"] = title
# 发送方信息
message["From"] = sender
# 接受方信息
message["To"] = receivers[0]
# 初始化邮箱服务器
smtp_obj = smtplib.SMTP_SSL(mail_host, 465)
# 登录到服务器
smtp_obj.login(mail_user, mail_pass)
# 发送
smtp_obj.sendmail(sender, receivers, message.as_string())
# 退出
smtp_obj.quit()
def login():
session = requests.session()
is_cookies_valid = False # cookies是否有效
if os.path.isfile("cookies.json"):
with open("cookies.json", "r", encoding="utf-8") as f:
new_cookies = json.load(f)
session.cookies.update(new_cookies)
response = session.get(URL, headers=headers)
response.encoding = "utf-8"
if len(response.history) > 0:
is_cookies_valid = True
if not is_cookies_valid:
# 在主页开始输入账号
response = session.get(URL, headers=headers)
response.encoding = "utf-8"
str1 = re.search('var hmSiteId = "(.*?)"', response.text)
new_cookies = {
("Hm_lvt_" + str1.group(1)): str(int(time.time())),
("Hm_lpvt_" + str1.group(1)): str(int(time.time())),
}
session.cookies.update(new_cookies)
# RSA加密password
URL_key = "https://uis.nwpu.edu.cn/cas/jwt/publicKey"
public_key = session.get(URL_key, headers=headers2).text
public_key = rsa.PublicKey.load_pkcs1_openssl_pem(public_key.encode())
encrypted_password = rsa.encrypt(password.encode(), public_key)
encrypted_password = "__RSA__" + base64.b64encode(encrypted_password).decode()
execution = re.search('name="execution" value="(.*?)"', response.text)
data = {
"username": username,
"password": encrypted_password,
}
response = session.post(
"https://uis.nwpu.edu.cn/cas/mfa/detect", data=data, headers=headers2
)
state_code = json.loads(response.text)["data"]["state"]
response = session.get(
"https://uis.nwpu.edu.cn/cas/mfa/initByType/secureemail?state="
+ state_code,
headers=headers2,
)
gid = json.loads(response.text)["data"]["gid"]
data = {"gid": gid}
session.post(
"https://uis.nwpu.edu.cn/attest/api/guard/secureemail/send",
json=data,
headers=headers2,
)
# 获取邮件中的验证码
conn = imaplib.IMAP4_SSL(host=r"imap.qq.com", port=993)
code = conn.login(sender, mail_pass)
for _ in range(3):
time.sleep(12)
conn.select()
typ, data1 = conn.search(None, '(FROM "[email protected]")')
try:
typ, data2 = conn.fetch(data1[0].decode().split()[-1], "(RFC822)")
except IndexError as e:
continue
msg = email.message_from_string(data2[0][1].decode("utf-8"))
IDENTIFY_CODE = 0
for part in msg.walk():
if not part.is_multipart():
CONTENT = part.get_payload(decode=True).decode("utf-8")
if CONTENT.startswith("您正在进行验证身份"):
IDENTIFY_CODE = CONTENT[14:18]
conn.store(data1[0].decode().split()[-1], "+FLAGS", "\\Deleted")
conn.expunge()
conn.close()
conn.logout()
# 已经得到验证码,提交
data["code"] = IDENTIFY_CODE
session.post(
"https://uis.nwpu.edu.cn/attest/api/guard/secureemail/valid",
json=data,
headers=headers2,
)
data = {
"username": username,
"password": encrypted_password,
"rememberMe": "true",
"currentMenu": "1",
"mfaState": state_code,
"execution": execution.group(1),
"_eventId": "submit",
"geolocation": "",
"submit": "稍等片刻……",
}
response = session.post(
(
"https://uis.nwpu.edu.cn/cas/login?service=https%3A%2F%2Fecampus.nwpu.edu.cn"
"%2F%3Fpath%3Dhttps%3A%2F%2Fecampus.nwpu.edu.cn"
),
data=data,
headers=headers,
)
# 已经得到授权,保存cookies
cookies = session.cookies.get_dict()
with open("cookies.json", "w", encoding="utf-8") as f:
json.dump(cookies, f)
return session
def find_grades(session):
# 查询成绩
session.get("https://jwxt.nwpu.edu.cn/student/sso-login", headers=headers)
response = session.get(
"https://jwxt.nwpu.edu.cn/student/for-std/grade/sheet", headers=headers
)
online_code = re.search("semester-index/(.*)", response.url).group(1)
response = session.get(
"https://jwxt.nwpu.edu.cn/student/for-std/grade/sheet/semester-index/"
+ online_code,
headers=headers,
)
semester = re.findall('<option value="(.+?)"', response.text)
grades = []
for sem in semester:
response = session.get(
(
"https://jwxt.nwpu.edu.cn/student/for-std/grade/sheet/info/"
+ online_code
+ "?semester="
+ sem
),
headers=headers2,
)
response = json.loads(response.text)["semesterId2studentGrades"][sem]
for course in response:
name = course["course"]["nameZh"]
gaGrade = course["gaGrade"]
GP = str(course["gp"])
credit = course["course"]["credits"]
grades.append(f"{name}, {gaGrade}, {GP}, {credit}")
F_PATH = "grades.csv"
if os.path.isfile(F_PATH):
fo = open(F_PATH, "r+", encoding="utf-8")
grades2 = fo.read().split("\n")
new_grades = set(grades).symmetric_difference(set(grades2))
if len(new_grades) != 0:
response = session.get(
(
"https://jwxt.nwpu.edu.cn/student/for-std/"
"student-portrait/getMyGpa?studentAssoc=" + online_code
),
headers=headers,
)
GPA = str(json.loads(response.text)["stdGpaRankDto"]["gpa"])
TITLE = [",".join(x.split(", ")[0:3]) for x in new_grades]
TITLE = ";".join(TITLE)
CONTENT = (
"科目 成绩 绩点 学分\n"
+ "\n".join(new_grades)
+ f"\n成绩已更新,概要如上\n新的绩点诞生了:{GPA}\n继续加油~~~"
)
email_msg(TITLE, CONTENT)
fo.close()
fo = open(F_PATH, "w", encoding="utf-8")
fo.write("\n".join(grades))
else:
fo = open(F_PATH, "w", encoding="utf-8")
fo.write("\n".join(grades))
fo.close()
def main():
session = login()
find_grades(session)
if __name__ == "__main__":
main()