更新 login.py
This commit is contained in:
parent
5a7bcc49f2
commit
e88109e61c
180
login.py
180
login.py
@ -1,81 +1,99 @@
|
|||||||
import requests
|
import requests
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import base64
|
import base64
|
||||||
import time
|
import time
|
||||||
|
import os
|
||||||
# 获取当前时间戳
|
|
||||||
def get_current_timestamp():
|
# 新增函数:从api.txt获取服务器地址
|
||||||
return int(time.time())
|
def get_api_base_url():
|
||||||
|
try:
|
||||||
# 获取unikey
|
with open("api.txt", "r") as f:
|
||||||
def get_unikey():
|
# 读取第一行有效内容(忽略空行和空白)
|
||||||
url = f"http://localhost:3000/login/qr/key?time={get_current_timestamp()}"
|
lines = [line.strip() for line in f.readlines() if line.strip()]
|
||||||
response = requests.get(url)
|
if lines:
|
||||||
data = response.json()
|
return lines[0].rstrip('/') # 移除末尾的斜杠
|
||||||
if data['code'] == 200:
|
except FileNotFoundError:
|
||||||
return data['data']['unikey']
|
print("api.txt文件未找到,使用默认地址")
|
||||||
return None
|
except Exception as e:
|
||||||
|
print(f"读取api.txt失败: {e},使用默认地址")
|
||||||
# 创建二维码
|
|
||||||
def create_qr(unikey):
|
# 默认地址
|
||||||
url = f"http://localhost:3000/login/qr/create?key={unikey}&qrimg=1&time={get_current_timestamp()}"
|
return "http://localhost:3000"
|
||||||
response = requests.get(url)
|
|
||||||
data = response.json()
|
# 获取当前时间戳
|
||||||
if data['code'] == 200:
|
def get_current_timestamp():
|
||||||
qrimg_base64 = data['data']['qrimg']
|
return int(time.time())
|
||||||
return qrimg_base64
|
|
||||||
return None
|
# 获取unikey
|
||||||
|
def get_unikey():
|
||||||
# 显示二维码图像
|
base_url = get_api_base_url()
|
||||||
def display_qr_image(qrimg_base64):
|
url = f"{base_url}/login/qr/key?time={get_current_timestamp()}"
|
||||||
img_data = base64.b64decode(qrimg_base64.split(",")[1])
|
response = requests.get(url)
|
||||||
img = Image.open(BytesIO(img_data))
|
data = response.json()
|
||||||
img.show()
|
if data['code'] == 200:
|
||||||
|
return data['data']['unikey']
|
||||||
# 监控扫码状态
|
return None
|
||||||
def check_scan_status(unikey):
|
|
||||||
url = f"http://localhost:3000/login/qr/check?key={unikey}&time={get_current_timestamp()}"
|
# 创建二维码
|
||||||
response = requests.get(url)
|
def create_qr(unikey):
|
||||||
#print(f"响应内容: {response.text}") # 打印每次请求的响应内容
|
base_url = get_api_base_url()
|
||||||
return response.json()
|
url = f"{base_url}/login/qr/create?key={unikey}&qrimg=1&time={get_current_timestamp()}"
|
||||||
|
response = requests.get(url)
|
||||||
# 登录函数
|
data = response.json()
|
||||||
def login():
|
if data['code'] == 200:
|
||||||
unikey = get_unikey()
|
qrimg_base64 = data['data']['qrimg']
|
||||||
if not unikey:
|
return qrimg_base64
|
||||||
print("无法获取unikey")
|
return None
|
||||||
return None
|
|
||||||
|
# 显示二维码图像
|
||||||
qrimg_base64 = create_qr(unikey)
|
def display_qr_image(qrimg_base64):
|
||||||
if not qrimg_base64:
|
img_data = base64.b64decode(qrimg_base64.split(",")[1])
|
||||||
print("无法创建二维码")
|
img = Image.open(BytesIO(img_data))
|
||||||
return None
|
img.show()
|
||||||
|
|
||||||
# 显示二维码图像
|
# 监控扫码状态
|
||||||
display_qr_image(qrimg_base64)
|
def check_scan_status(unikey):
|
||||||
|
base_url = get_api_base_url()
|
||||||
print("请扫描二维码,等待扫码成功...")
|
url = f"{base_url}/login/qr/check?key={unikey}&time={get_current_timestamp()}"
|
||||||
|
response = requests.get(url)
|
||||||
# 监控扫码状态
|
return response.json()
|
||||||
while True:
|
|
||||||
status = check_scan_status(unikey)
|
# 登录函数(保持原有实现不变)
|
||||||
|
def login():
|
||||||
if status['code'] == 803:
|
unikey = get_unikey()
|
||||||
print("授权登录成功")
|
if not unikey:
|
||||||
cookie = status['cookie']
|
print("无法获取unikey")
|
||||||
print(f"保存的 Cookie: {cookie}")
|
return None
|
||||||
# 保存 cookie 到文件
|
|
||||||
with open("cookies.txt", "w") as f:
|
qrimg_base64 = create_qr(unikey)
|
||||||
f.write(cookie)
|
if not qrimg_base64:
|
||||||
return cookie # 返回 cookie
|
print("无法创建二维码")
|
||||||
elif status['code'] == 801:
|
return None
|
||||||
print("等待扫码,继续请求中...")
|
|
||||||
elif status['code'] == 802:
|
display_qr_image(qrimg_base64)
|
||||||
print("授权中,继续请求中...")
|
|
||||||
else:
|
print("请扫描二维码,等待扫码成功...")
|
||||||
print(f"扫码失败: {status['message']}")
|
|
||||||
return None # 返回 None,表示扫码失败
|
while True:
|
||||||
|
status = check_scan_status(unikey)
|
||||||
time.sleep(2) # 每隔2秒检查一次扫码状态
|
|
||||||
|
if status['code'] == 803:
|
||||||
|
print("授权登录成功")
|
||||||
|
cookie = status['cookie']
|
||||||
|
with open("cookies.txt", "w") as f:
|
||||||
|
f.write(cookie)
|
||||||
|
return cookie
|
||||||
|
elif status['code'] == 801:
|
||||||
|
print("等待扫码,继续请求中...")
|
||||||
|
elif status['code'] == 802:
|
||||||
|
print("授权中,继续请求中...")
|
||||||
|
else:
|
||||||
|
print(f"扫码失败: {status['message']}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
time.sleep(2)
|
||||||
|
|
||||||
|
# 使用示例
|
||||||
|
if __name__ == "__main__":
|
||||||
|
login()
|
||||||
Loading…
Reference in New Issue
Block a user