refactor: implement retry mechanism for session token retrieval and improve error handling in get_cursor_session_token function
This commit is contained in:
@@ -181,19 +181,41 @@ def delete_account(browser, tab):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def get_cursor_session_token(tab):
|
def get_cursor_session_token(tab, max_attempts=3, retry_interval=2):
|
||||||
|
"""
|
||||||
|
获取Cursor会话token,带有重试机制
|
||||||
|
:param tab: 浏览器标签页
|
||||||
|
:param max_attempts: 最大尝试次数
|
||||||
|
:param retry_interval: 重试间隔(秒)
|
||||||
|
:return: session token 或 None
|
||||||
|
"""
|
||||||
print("开始获取cookie")
|
print("开始获取cookie")
|
||||||
try:
|
attempts = 0
|
||||||
# 获取指定域名的所有cookies
|
|
||||||
cookies = tab.cookies()
|
while attempts < max_attempts:
|
||||||
for cookie in cookies:
|
try:
|
||||||
if cookie.get("name") == "WorkosCursorSessionToken":
|
cookies = tab.cookies()
|
||||||
return cookie["value"].split("%3A%3A")[1]
|
for cookie in cookies:
|
||||||
print("未能获取到CursorSessionToken")
|
if cookie.get("name") == "WorkosCursorSessionToken":
|
||||||
return None
|
return cookie["value"].split("%3A%3A")[1]
|
||||||
except Exception as e:
|
|
||||||
print(f"获取cookie失败: {str(e)}")
|
attempts += 1
|
||||||
return None
|
if attempts < max_attempts:
|
||||||
|
print(
|
||||||
|
f"第 {attempts} 次尝试未获取到CursorSessionToken,{retry_interval}秒后重试..."
|
||||||
|
)
|
||||||
|
time.sleep(retry_interval)
|
||||||
|
else:
|
||||||
|
print(f"已达到最大尝试次数({max_attempts}),获取CursorSessionToken失败")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"获取cookie失败: {str(e)}")
|
||||||
|
attempts += 1
|
||||||
|
if attempts < max_attempts:
|
||||||
|
print(f"将在 {retry_interval} 秒后重试...")
|
||||||
|
time.sleep(retry_interval)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def update_cursor_auth(email=None, access_token=None, refresh_token=None):
|
def update_cursor_auth(email=None, access_token=None, refresh_token=None):
|
||||||
@@ -269,22 +291,7 @@ def sign_up_account(browser, tab):
|
|||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
handle_turnstile(tab)
|
handle_turnstile(tab)
|
||||||
print("等待进入设置页面")
|
time.sleep(8)
|
||||||
|
|
||||||
# 等待页面加载完成
|
|
||||||
max_wait_time = 30 # 最大等待时间(秒)
|
|
||||||
start_time = time.time()
|
|
||||||
while time.time() - start_time < max_wait_time:
|
|
||||||
try:
|
|
||||||
# 检查页面是否仍在加载
|
|
||||||
is_loading = tab.run_js("return document.readyState !== 'complete'")
|
|
||||||
if not is_loading:
|
|
||||||
break
|
|
||||||
time.sleep(1)
|
|
||||||
except Exception as e:
|
|
||||||
print(f"检查页面加载状态时出错: {str(e)}")
|
|
||||||
break
|
|
||||||
print("进入设置页面")
|
|
||||||
tab.get(settings_url)
|
tab.get(settings_url)
|
||||||
try:
|
try:
|
||||||
usage_selector = (
|
usage_selector = (
|
||||||
|
|||||||
Reference in New Issue
Block a user