-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto_add_user.py
More file actions
362 lines (302 loc) · 12.8 KB
/
auto_add_user.py
File metadata and controls
362 lines (302 loc) · 12.8 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
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
import requests
import re
import qrcode
import time
from bs4 import BeautifulSoup
from PIL import Image
# 导入自定义模块
from utils.user_info import get_user_and_class_info
class QRCodeLogin:
def __init__(self, url="https://bjmf.k8n.cn/weixin/qrlogin/student"):
self.base_url = url
self.session = requests.Session()
self.session.headers.update({
'User-Agent': 'Mozilla/5.0 (Linux; Android 10; SM-G981B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36 MicroMessenger/7.0.10.1580(0x27000A50) Process/tools NetType/WIFI Language/zh_CN ABI/arm64',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
'Referer': 'https://wx.qq.com/',
'sec-ch-ua-platform': 'Android',
'X-Requested-With': 'XMLHttpRequest',
})
def extract_qr_content(self, html):
"""从HTML中提取二维码内容"""
soup = BeautifulSoup(html, 'html.parser')
# 直接从img标签获取二维码URL
qrcode_div = soup.find('div', id='qrcode')
img_tag = qrcode_div.find('img')
qrcode_url = img_tag['src']
print(f"找到二维码图片URL: {qrcode_url}")
# 从URL中提取ticket参数
ticket_pattern = r'ticket=([^\s"\']+)'
ticket_match = re.search(ticket_pattern, qrcode_url)
ticket = ticket_match.group(1) if ticket_match else None
# 返回包含二维码URL的字典
return {
'qrcode_url': qrcode_url,
'ticket': ticket
}
def create_qrcode_image(self, params, filename=None):
"""创建二维码图片"""
# 直接使用从HTML中获取的二维码图片URL
qrcode_img_url = params['qrcode_url']
print(f" 二维码图片URL: {qrcode_img_url}")
# 下载二维码图片
response = self.session.get(qrcode_img_url)
if response.status_code == 200:
with open("login_qrcode.png", "wb") as f:
f.write(response.content)
print(f" 二维码已保存: login_qrcode.png")
print(f" 请用微信扫描二维码登录")
# 显示二维码
image = Image.open('login_qrcode.png')
image.show()
# 这里的close很重要,否则在Windows下可能无法删除文件
try:
image.close()
except:
pass
else:
print(f" 下载二维码失败,状态码: {response.status_code}")
def cleanup_qrcode(self):
"""清理二维码文件"""
import os
if os.path.exists("login_qrcode.png"):
try:
os.remove("login_qrcode.png")
print("已清理过期的二维码文件")
except Exception as e:
print(f"清理二维码文件失败: {e}")
def poll_login_status(self, max_attempts=20):
"""轮询登录状态"""
check_url = f"{self.base_url}?op=checklogin"
for i in range(max_attempts):
print(f"轮询检查登录状态... ({i + 1}/{max_attempts})")
try:
response = self.session.get(check_url)
if response.status_code == 200:
try:
data = response.json()
if data.get('status'):
redirect_url = data.get('url')
print(f"登录成功!获取跳转URL: {redirect_url}")
# 登录成功,清理二维码
self.cleanup_qrcode()
return True, redirect_url
else:
print(f"等待扫码... ")
except:
print("响应不是JSON格式")
except Exception as e:
print(f"请求失败: {e}")
time.sleep(1) # 等待1秒
print("二维码已过期")
# 过期清理二维码
self.cleanup_qrcode()
return False, None
def run(self):
"""运行完整的二维码登录流程"""
# 运行前先清理可能存在的旧二维码
self.cleanup_qrcode()
print("=" * 50)
print("微信扫码登录流程开始")
print("=" * 50)
# 获取登录页面HTML
html = self.session.get(self.base_url).text
# 提取二维码内容
params = self.extract_qr_content(html)
# 创建二维码图片
print("\n1. 生成二维码图片...")
self.create_qrcode_image(params)
print("\n2. 请用微信扫描二维码...")
# 轮询登录状态
print("\n3. 等待扫码确认...")
return self.poll_login_status()
class StudentSession:
def __init__(self):
self.session = requests.Session()
self.session.headers.update({
"authority": "bj.k8n.cn",
"scheme": "https",
"path": "/student/uidlogin?",
"sec-ch-ua": '"Chromium";v="142", "Microsoft Edge";v="142", "Not_A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"dnt": "1",
"upgrade-insecure-requests": "1",
"prefer": "safe",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0",
"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.7",
"sec-fetch-site": "cross-site",
"sec-fetch-mode": "navigate",
"sec-fetch-dest": "document",
"referer": "https://login.b8n.cn/",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
"priority": "u=0, i",
})
self.cookie = None
self.course_id = None
def access_login_url(self, redirect_url: str) -> str:
"""
访问登录URL获取cookie
"""
# url处理
url = "https://bj.k8n.cn/student/uidlogin?" + redirect_url.split('?')[1]
response = self.session.get(url, allow_redirects=False)
print(f"状态码: {response.status_code}")
# 保存cookies供后续使用
cookies = self.session.cookies.get_dict()
# 去除掉没用的cookie
cookies.pop("s")
# 使用列表推导式生成键值对字符串
cookie = [f"{key}={value}" for key, value in cookies.items()][0]
print(f"获取的Cookies: {cookie}")
self.cookie = cookie
return cookie
def getClassId(self) -> str:
"""访问其他页面获取ClassID"""
url = "http://bj.k8n.cn/student"
resp = self.session.get(url)
print(f"学生页面状态码:{resp.status_code}")
soup = BeautifulSoup(resp.text, 'html.parser')
div_element = soup.find('div', class_='card mb-3 course')
course_id = div_element.get('course_id')
print(f"ClassID: {course_id}")
self.course_id = course_id
return course_id
def load_env_config():
"""读取.env文件配置"""
import os
config = {
"ENABLE_COMMON_CONFIG": False,
"COMMON_LAT": "",
"COMMON_LNG": "",
"COMMON_ACC": "",
"COMMON_QMSG_KEY": "",
"COMMON_WX_KEY": ""
}
if os.path.exists(".env"):
try:
with open(".env", "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"): continue
if "=" in line:
key, value = line.split("=", 1)
key = key.strip()
value = value.strip()
if key == "ENABLE_COMMON_CONFIG":
config[key] = value.lower() == "true"
elif key in config:
config[key] = value
print("已加载.env配置")
except Exception as e:
print(f"读取.env文件失败: {e}")
return config
def main():
"""主函数"""
# 创建QRCodeLogin实例
qr_login = QRCodeLogin()
# 运行登录流程,获取登录成功后的跳转URL
ok = False
url = ""
for retry in range(3):
print(f"\n\n尝试 #{retry + 1}: \n", end="")
ok, url = qr_login.run()
if ok:
break
if ok:
# 创建StudentSession实例,获取cookie
s = StudentSession()
cookie = s.access_login_url(url)
# 使用获取到的cookie调用get_user_and_class_info函数
student = {
'cookie': cookie
}
print("\n" + "=" * 50)
print("开始获取用户和班级信息")
print("=" * 50)
# 调用user_info.py中的函数获取信息
user_info, class_info = get_user_and_class_info(student)
# 清理临时文件 (已在流程中自动清理,此处保留作为双重保险)
qr_login.cleanup_qrcode()
# 将获取到的信息写入data.json
print("\n" + "=" * 50)
print("开始写入用户信息到data.json")
print("=" * 50)
import json
import os
# 读取环境配置
env_config = load_env_config()
use_common = env_config["ENABLE_COMMON_CONFIG"]
# 定义data.json文件路径
data_file = "data.json"
# 读取现有的data.json文件
existing_data = {
"students": []
}
if os.path.exists(data_file):
with open(data_file, "r", encoding="utf-8") as f:
existing_data = json.load(f)
# 构建新的学生信息
full_name = user_info.get("name", "")
# 处理用户名:如果存在同名的拼音格式,就使用相同的格式;否则使用中文姓名
# 例如:如果已经有"caiyonghao",就使用相同格式;否则使用"蔡永昊"
student_name = full_name
# 检查是否存在同名的拼音格式
for student in existing_data["students"]:
if student["name"] == full_name.lower():
# 如果已经存在拼音格式,就使用拼音格式
student_name = student["name"]
break
# 准备默认值
default_lat = ""
default_lng = ""
default_acc = "30"
default_qmsg = ""
default_wx = ""
if use_common:
print("使用.env公共配置填充参数")
default_lat = env_config["COMMON_LAT"]
default_lng = env_config["COMMON_LNG"]
default_acc = env_config["COMMON_ACC"] if env_config["COMMON_ACC"] else "30"
default_qmsg = env_config["COMMON_QMSG_KEY"]
default_wx = env_config["COMMON_WX_KEY"]
new_student = {
"name": student_name,
"class": class_info.get("class_id", ""),
"lat": default_lat,
"lng": default_lng,
"acc": default_acc,
"cookie": cookie,
"QmsgKEY": default_qmsg,
"WXKey": default_wx
}
# 检查新用户是否已经存在于现有数据中
# 可以通过name或cookie来判断
existing_names = [student["name"] for student in existing_data["students"]]
if new_student["name"] not in existing_names:
# 添加新用户
existing_data["students"].append(new_student)
print(f"用户 {new_student['name']} 已添加到data.json")
else:
# 更新现有用户信息
for i, student in enumerate(existing_data["students"]):
if student["name"] == new_student["name"]:
existing_data["students"][i] = new_student
print(f"用户 {new_student['name']} 信息已更新")
break
# 将更新后的数据写回到data.json文件
with open(data_file, "w", encoding="utf-8") as f:
json.dump(existing_data, f, ensure_ascii=False, indent=4)
print("data.json更新完成")
# 打印更新后的data.json内容
print("\n" + "=" * 50)
print("更新后的data.json内容:")
print("=" * 50)
with open(data_file, "r", encoding="utf-8") as f:
print(json.dumps(json.load(f), ensure_ascii=False, indent=2))
else:
print("\n登录失败,无法获取用户信息")
if __name__ == '__main__':
main()