2023-03-08 12:48:12 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import cqwu
|
|
|
|
from cqwu.errors.auth import NeedCaptchaError
|
|
|
|
|
|
|
|
|
|
|
|
class CheckCaptcha:
|
|
|
|
async def check_captcha(
|
|
|
|
self: "cqwu.Client",
|
|
|
|
username: int = None,
|
|
|
|
show_qrcode: bool = True,
|
|
|
|
):
|
2023-12-29 10:04:00 +00:00
|
|
|
"""检查是否需要验证码"""
|
2023-03-08 12:48:12 +00:00
|
|
|
username = username or self.username
|
|
|
|
params = {
|
|
|
|
"username": username,
|
|
|
|
"pwdEncrypt2": "pwdEncryptSalt",
|
2023-12-29 10:04:00 +00:00
|
|
|
"_": str(round(time.time() * 1000)),
|
2023-03-08 12:48:12 +00:00
|
|
|
}
|
|
|
|
url = f"{self.auth_host}/authserver/needCaptcha.html"
|
2023-12-29 10:04:00 +00:00
|
|
|
captcha_html = await self.request.get(
|
|
|
|
url, params=params, follow_redirects=False
|
|
|
|
)
|
|
|
|
if captcha_html.text == "true":
|
|
|
|
params = {"ts": str(round(time.time()))}
|
2023-03-08 12:48:12 +00:00
|
|
|
captcha_url = f"{self.auth_host}/authserver/captcha.html"
|
2023-12-29 10:04:00 +00:00
|
|
|
res = await self.request.get(
|
|
|
|
captcha_url, params=params, follow_redirects=False
|
|
|
|
)
|
2023-03-08 12:48:12 +00:00
|
|
|
if not show_qrcode:
|
|
|
|
raise NeedCaptchaError(res.content)
|
|
|
|
with open("captcha.jpg", mode="wb") as f:
|
|
|
|
f.write(res.content)
|
|
|
|
print("验证码已保存在当前目录下的 captcha.jpg 文件中。")
|
|
|
|
return self.get_input("验证码")
|
|
|
|
return False
|