Skip to content

Commit

Permalink
feat: 日志优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mikumifa committed Jun 16, 2024
1 parent 659abc3 commit 3db0f10
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
43 changes: 26 additions & 17 deletions geetest/RROCRValidator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
from urllib import parse

import aiohttp
import loguru
import requests
from retry import retry

from config import cookies_config_path, global_cookieManager
Expand All @@ -25,13 +26,7 @@ def __init__(self):
}
self.cookieManager = global_cookieManager

@retry(tries=10)
def validate(self, appkey, gt, challenge, referer="http://www.baidu.com", ip='', host='') -> str:
loguru.logger.info("start rrocr validate")
if appkey is None or appkey == "":
appkey = self.cookieManager.get_config_value("appkey", "")
else:
self.cookieManager.set_config_value("appkey", appkey)
async def _async_validate(self, appkey, gt, challenge, referer, ip, host):
data = {
"appkey": appkey,
"gt": gt,
Expand All @@ -41,17 +36,31 @@ def validate(self, appkey, gt, challenge, referer="http://www.baidu.com", ip='',
"host": host
}
data = parse.urlencode(data)
response = requests.post(self.url, headers=self.headers, data=data)

if response.status_code == 200:
result = response.json()
loguru.logger.info(result)
if result.get("status") == 0:
return result['data']['validate']
else:
raise ValueError(f"识别失败: {result.get('msg')}")
async with aiohttp.ClientSession() as session:
async with session.post(self.url, headers=self.headers, data=data) as response:
async def inner():
if response.status == 200:
result = await response.json()
loguru.logger.info(result)
if result.get("status") == 0:
return result['data']['validate']
else:
raise ValueError(f"识别失败: {result.get('msg')}")
else:
raise ConnectionError(f"Request failed with status code: {response.status}")

await inner()

@retry(tries=5)
def validate(self, appkey, gt, challenge, referer="http://www.baidu.com", ip='', host='') -> str:
if appkey is None or appkey == "":
appkey = self.cookieManager.get_config_value("appkey", "")
else:
raise ConnectionError(f"Request failed with status code: {response.status_code}")
self.cookieManager.set_config_value("appkey", appkey)

loop = asyncio.get_event_loop()
return loop.run_until_complete(self._async_validate(appkey, gt, challenge, referer, ip, host))


if __name__ == "__main__":
Expand Down
11 changes: 7 additions & 4 deletions tab/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def go_tab():
""")
with gr.Column():
ticket_ui = gr.TextArea(
label="填入配置", info="再次填入配置信息", interactive=True
label="填入配置",
info="再次填入配置信息 (不同版本的配置文件可能存在差异,升级版本时候不要偷懒,老版本的配置文件在新版本上可能出问题",
interactive=True
)
gr.HTML(
"""<label for="datetime">选择抢票的时间</label><br>
Expand All @@ -51,7 +53,7 @@ def go_tab():

# 验证码选择

way_select_ui = gr.Radio(ways, label="过验证码的方式", info="详细说明请前往 `训练你的验证码速度`那一栏",
way_select_ui = gr.Radio(ways, label="过验证码的方式", info="详细说明请前往 `训练你的验证码速度` 那一栏",
type="index", value="手动")
api_key_input_ui = gr.Textbox(label="填写你的api_key",
value=global_cookieManager.get_config_value("appkey", ""),
Expand Down Expand Up @@ -141,8 +143,7 @@ def start_go(tickets_info_str, time_start, interval, mode, total_attempts, api_k
)
request_result = request_result_normal.json()
logger.info(f"1)订单准备")
logger.info(f"prepare header: {request_result_normal.headers}")
logger.info(f"prepare: {request_result}")
logger.info(f"请求头: {request_result_normal.headers} // 请求体: {request_result}")
code = int(request_result["code"])
# 完成验证码
if code == -401:
Expand Down Expand Up @@ -241,6 +242,7 @@ def run_validation():
).json()
logger.info(f"prepare: {request_result}")
tickets_info["token"] = request_result["data"]["token"]
# 金额通过手动计算,减少一次请求,提高速度
# logger.info(f"2)核实订单,填写支付金额信息")
# request_result = _request.get(
# url=f"https://show.bilibili.com/api/ticket/order/confirmInfo?token={tickets_info['token']}&voucher"
Expand All @@ -257,6 +259,7 @@ def run_validation():
).json()
errno = int(request_result["errno"])
left_time_str = "无限" if mode == 0 else left_time

logger.info(
f'状态码: {errno}({ERRNO_DICT.get(errno, "未知错误码")}), 请求体: {request_result} 剩余次数: {left_time_str}'
)
Expand Down

0 comments on commit 3db0f10

Please sign in to comment.