2023-11-25 08:22:20 +00:00
|
|
|
'''
|
2023-11-13 13:18:59 +00:00
|
|
|
Date: 2023-11-13 19:55:22
|
|
|
|
LastEditors: Night-stars-1 nujj1042633805@gmail.com
|
2023-12-19 01:27:21 +00:00
|
|
|
LastEditTime: 2023-12-18 20:46:51
|
2023-11-25 08:22:20 +00:00
|
|
|
'''
|
2023-12-19 01:27:21 +00:00
|
|
|
import json
|
2023-11-13 13:18:59 +00:00
|
|
|
|
2023-11-18 10:37:51 +00:00
|
|
|
from .request import post
|
2023-11-13 13:18:59 +00:00
|
|
|
from .logger import log
|
2023-11-15 13:07:29 +00:00
|
|
|
from .config import ConfigManager
|
2023-11-13 13:18:59 +00:00
|
|
|
from .data_model import ApiResultHandler, GeetestResult
|
|
|
|
|
2023-11-15 13:07:29 +00:00
|
|
|
_conf = ConfigManager.data_obj
|
2023-11-13 13:18:59 +00:00
|
|
|
|
2023-11-25 08:22:20 +00:00
|
|
|
def find_key(data: dict, key: str):
|
|
|
|
"""递归查找字典中的key"""
|
|
|
|
for dkey, dvalue in data.items():
|
|
|
|
if dkey == key:
|
|
|
|
return dvalue
|
|
|
|
if isinstance(dvalue, dict):
|
|
|
|
find_key(dvalue, key)
|
|
|
|
return None
|
2023-11-22 04:02:10 +00:00
|
|
|
|
2023-11-18 10:37:51 +00:00
|
|
|
async def get_validate(gt: str, challenge: str) -> GeetestResult: # pylint: disable=invalid-name
|
|
|
|
"""获取人机验证结果"""
|
2023-11-13 13:18:59 +00:00
|
|
|
try:
|
2023-11-22 04:02:10 +00:00
|
|
|
validate = None
|
2023-11-25 08:22:20 +00:00
|
|
|
if _conf.preference.geetest_url:
|
|
|
|
params = _conf.preference.geetest_params.copy()
|
2023-12-19 01:27:21 +00:00
|
|
|
params = json.loads(json.dumps(params).replace("{gt}", gt).replace("{challenge}", challenge))
|
2023-11-25 08:22:20 +00:00
|
|
|
data = _conf.preference.geetest_data.copy()
|
2023-12-19 01:27:21 +00:00
|
|
|
data = json.loads(json.dumps(data).replace("{gt}", gt).replace("{challenge}", challenge))
|
2023-11-25 08:22:20 +00:00
|
|
|
response = await post(
|
|
|
|
_conf.preference.geetest_url,
|
|
|
|
params=params,
|
|
|
|
json=data,
|
|
|
|
)
|
|
|
|
log.debug(response.text)
|
|
|
|
geetest_data = response.json()
|
|
|
|
geetest = ApiResultHandler(geetest_data)
|
|
|
|
challenge = find_key(geetest.data, "challenge")
|
|
|
|
validate = find_key(geetest.data, "validate")
|
|
|
|
return GeetestResult(challenge=challenge, validate=validate)
|
|
|
|
else:
|
|
|
|
return GeetestResult(challenge="", validate="")
|
2023-11-22 04:02:10 +00:00
|
|
|
except Exception: # pylint: disable=broad-exception-caught
|
2023-11-13 13:18:59 +00:00
|
|
|
log.exception("获取人机验证结果异常")
|
|
|
|
return GeetestResult(challenge="", validate="")
|