-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
175 lines (158 loc) · 6.21 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
import json
import smtplib
import time
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import requests
# 两种方式配置邮箱信息
# 1.直接在这里填写
# MAIL_HOST = "smtp.qq.com" # 设置SMTP服务器,如smtp.qq.com smtp.163.com
# MAIL_USER = "******@qq.com" # 发送邮箱的用户名,如xxxxxx@qq.com xxx@163.com
# MAIL_PASS = "**********" # 发送邮箱的密码(注:QQ邮箱需要开启SMTP服务后在此填写授权码)
# RECEIVER = "******@qq.com" # 收件邮箱,格式同发件邮箱
# 2.运行时输入
MAIL_HOST = input("请输入SMTP服务器,如smtp.qq.com smtp.163.com:")
MAIL_USER = input("请输入发送邮箱的用户名,如****@qq.com:")
MAIL_PASS = input("请输入发送邮箱的密码(注:QQ邮箱需要开启SMTP服务后在此填写授权码):")
RECEIVER = input("请输入收件邮箱,格式同发件邮箱:")
# 两种方式配置学号密码
# 1.直接在这里填写
# ndata = {'username': "********", 'password': "********", } # 请填写自己的学号和密码
# 2.运行时输入
username = input("请输入学号:")
password = input("请输入密码:")
ndata = {'username': username, 'password': password}
# 请填写想要查询的学年和学期
year = '2022-2023' # 学年
term = '2' # 1 2 3分别代表秋 春 夏季学期
# 下面的代码不需要修改
cookies = {'eai-sess': '', }
headers = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'DNT': '1',
'Origin': 'https://app.buaa.edu.cn',
'Referer': 'https://app.buaa.edu.cn/buaascore/wap/default/index',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
base = []
def get_sess():
headerss = {
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'zh,en-US;q=0.9,en;q=0.8,zh-CN;q=0.7',
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'DNT': '1',
'Origin': 'https://app.buaa.edu.cn',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
}
try:
response = requests.post('https://app.buaa.edu.cn/uc/wap/login/check', headers=headerss, data=ndata)
resp = json.loads(response.text)
if resp['e'] == 0 and resp['m'] == '操作成功':
sess = response.cookies.get("eai-sess")
else:
sess = "error"
except:
sess = "error"
return sess
def get_score_list(year, xq): # year 格式为 2020-2021 字符串 xq取值为1 2 3 字符串
data = {'xq': xq, 'year': year}
try:
response = requests.post('https://app.buaa.edu.cn/buaascore/wap/default/index',
cookies=cookies,
headers=headers,
data=data)
print("请求时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
except:
return "error"
dic = json.loads(response.text)
res = []
for i in dic['d'].values():
res.append({'km': i['kcmc'], 'cj': i['kccj'], 'xf': i['xf']})
return res
def to_content(lis):
msg = '''
<html>
<head></head>
<body>
<p>本学期的课程成绩如下:</p>
<table border="1">
<tr>
<td>课程</td>
<td>成绩</td>
<td>学分</td>
</tr>
'''
for i in lis:
msg += "<tr><td>{}</td><td>{}</td><td>{}</td></tr>".format(i['km'], i['cj'], i['xf'])
msg += "</table></body></html>"
return msg
def send_mail(title, content):
# 第三方 SMTP 服务
print("准备发送邮件")
mail_host = MAIL_HOST
mail_user = MAIL_USER
mail_pass = MAIL_PASS
sender = mail_user
receivers = RECEIVER
message = MIMEMultipart('related')
message['From'] = Header(sender) # 发件人
message['To'] = Header(receivers, 'utf-8') # 收件人
subject = title # 主题
message['Subject'] = Header(subject, 'utf-8')
message.attach(MIMEText(content, 'html', 'utf-8'))
print('准备发送邮件')
try:
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host, 25) # 25 为 SMTP 端口号
print('连接成功')
smtpObj.login(mail_user, mail_pass)
print('邮箱登录成功')
smtpObj.sendmail(sender, receivers, str(message))
print("邮件发送成功")
except smtplib.SMTPException:
print("无法发送邮件!")
succeed = False
right_username = ""
right_password = ""
while True:
while True:
cookies['eai-sess'] = get_sess()
if cookies['eai-sess'] == 'error':
print("登录失败,请重新输入账号密码!!!")
username = right_username if succeed else input("请输入学号:")
password = right_password if succeed else input("请输入密码:")
else:
print("登录成功")
succeed = True
right_username = ndata['username']
right_password = ndata['password']
break
raw = get_score_list(year, term)
if raw == "error":
time.sleep(60)
continue
tmp = [i for i in raw if i['cj'] is not None]
if len(tmp) > len(base):
new_objs = [i['km'] for i in tmp if i not in base]
send_mail("新出了{}门:{}".format(len(new_objs), ",".join(new_objs)), to_content(tmp))
base = tmp
time.sleep(60)