引入重试机制

This commit is contained in:
zayac 2024-04-06 12:07:11 +08:00
parent 23ac228925
commit 907692c5e8

View File

@ -39,11 +39,11 @@ def perform_login(playwright, account: Account) -> Account:
page = context.new_page() page = context.new_page()
page.goto(account.url) page.goto(account.url)
fill_login_form(page, account) fill_login_form(page, account)
if handle_captcha(page,account.type): if handle_captcha(page, account.type, retry_count=0):
account.headers = capture_request_headers(page, account) account.headers = capture_request_headers(page, account)
logger.info('登录成功') logger.info('登录成功')
else: else:
logger.error('登录失败或验证码处理失败') logger.error('登录失败')
close_resources(page, context, browser) close_resources(page, context, browser)
return account return account
@ -59,21 +59,24 @@ def fill_login_form(page, account: Account):
logger.info(f'{account.name}登录ing...........') logger.info(f'{account.name}登录ing...........')
def handle_captcha(page, account_type) -> bool: def handle_captcha(page, account_type, retry_count) -> bool:
try: if retry_count < 3:
validate_code = page.locator('.geetest_box') try:
validate_code.wait_for(state='visible') validate_code = page.locator('.geetest_box')
time.sleep(1) validate_code.wait_for(state='visible')
return process_validate_code(page,validate_code,account_type) time.sleep(1)
except TimeoutError: return process_validate_code(page, validate_code, account_type)
logger.error('超时了') except TimeoutError:
return False retry_count += 1
logger.error(f'验证码识别失败,正在重试:{retry_count}')
return handle_captcha(page, account_type, retry_count)
return False
def process_validate_code(page,validate_code, account_type): def process_validate_code(page, validate_code, account_type):
validate_code_buffer = validate_code.screenshot() validate_code_buffer = validate_code.screenshot()
img = base64.b64encode(validate_code_buffer).decode('utf-8') img = base64.b64encode(validate_code_buffer).decode('utf-8')
#九游滑动验证 需要单独处理 # 九游滑动验证 需要单独处理
if account_type == AccountType.jy: if account_type == AccountType.jy:
res = base64_api(img=img, typeid=33) res = base64_api(img=img, typeid=33)
drag_btn = validate_code.locator(".geetest_btn") drag_btn = validate_code.locator(".geetest_btn")
@ -99,6 +102,9 @@ def process_validate_code(page,validate_code, account_type):
except ValueError: except ValueError:
logger.error(f"获取移动距离失败,实际返回内容为:{res}") logger.error(f"获取移动距离失败,实际返回内容为:{res}")
return False return False
except TimeoutError:
logger.debug("验证码滑动失败")
return process_validate_code(page, validate_code, account_type)
else: else:
res = base64_api(img=img) res = base64_api(img=img)
if '|' in res: if '|' in res:
@ -157,10 +163,9 @@ def persistence(account: Account, headers: dict):
session.commit() session.commit()
logger.info(f'Headers persisted for account {account.name}') logger.info(f'Headers persisted for account {account.name}')
# if __name__ == '__main__': # if __name__ == '__main__':
# with open('C:\\Users\\Administrator\\Desktop\\Snipaste_2024-03-26_21-46-35.png', 'rb') as f: # with open('C:\\Users\\Administrator\\Desktop\\Snipaste_2024-03-26_21-46-35.png', 'rb') as f:
# base64_data = base64.b64encode(f.read()) # base64_data = base64.b64encode(f.read())
# b64 = base64_data.decode('utf-8') # b64 = base64_data.decode('utf-8')
# res = base64_api(img= b64,typeid=33) # res = base64_api(img= b64,typeid=33)
# print(res) # print(res)