From ea4e3b8a97cacbdbc9b43097090c12ca2a57c26b Mon Sep 17 00:00:00 2001 From: Soleil <60135704+GalacticDevOps@users.noreply.github.com> Date: Tue, 14 Jan 2025 14:48:24 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20fix=20some=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 8 ++-- package.json | 2 +- resources/workspace/browser_utils.py | 18 +++---- resources/workspace/cursor_pro_keep_alive.py | 38 +++++++-------- resources/workspace/get_email_code.py | 50 ++++++++++---------- src/main/index.ts | 2 +- workspace/browser_utils.py | 22 +++++---- workspace/cursor_pro_keep_alive.py | 38 +++++++-------- workspace/get_email_code.py | 50 ++++++++++---------- 9 files changed, 118 insertions(+), 110 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25b8703..8eb609d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,11 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [1.0.6-alpha1] - 2024-01-13 +## [1.0.8] - 2024-01-14 ### Fixed -- 修复MacOs下一键重置功能异常的问题 -- 修复了一些已知的可能问题 +- Fix some known issues + +### Pending +- Email problem for receiving emails ## [1.0.3] - 2024-01-10 diff --git a/package.json b/package.json index 7f77808..63ecb21 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ez-cursor-free", - "version": "1.0.7-alpha1", + "version": "1.0.8", "description": "Ez2 Use Cursor Free Tools", "main": "./out/main/index.js", "author": "Soleil", diff --git a/resources/workspace/browser_utils.py b/resources/workspace/browser_utils.py index d672075..75cad54 100644 --- a/resources/workspace/browser_utils.py +++ b/resources/workspace/browser_utils.py @@ -25,13 +25,12 @@ def _get_browser_options(self): try: extension_path = self._get_extension_path() if extension_path: - print(f"正在加载插件: {extension_path}") co.add_extension(extension_path) - print("插件加载成功") + logging.info("插件加载成功") else: - print("未找到插件目录") + logging.warning("未找到插件目录") except Exception as e: - print(f"加载插件时出错: {e}") + logging.warning(f"加载插件时出错: {e}") # 基本配置 co.set_user_agent( @@ -39,8 +38,11 @@ def _get_browser_options(self): ) co.set_pref("credentials_enable_service", False) co.set_argument("--hide-crash-restore-bubble") + proxy = os.getenv('BROWSER_PROXY') + if proxy: + co.set_proxy(proxy) co.auto_port() - co.headless(True) + co.headless(os.getenv('BROWSER_HEADLESS', 'True').lower() == 'true') if sys.platform == "darwin": co.set_argument("--no-sandbox") @@ -64,13 +66,13 @@ def _get_extension_path(self): # 检查插件文件是否完整 if os.path.exists(extension_path): - required_files = ['manifest.json', 'content.js'] + required_files = ['manifest.json', 'script.js'] if all(os.path.exists(os.path.join(extension_path, f)) for f in required_files): return extension_path else: - print(f"插件目录 {extension_path} 文件不完整") + logging.warning(f"插件目录 {extension_path} 文件不完整") else: - print(f"插件目录不存在: {extension_path}") + raise FileNotFoundError(f"插件不存在: {extension_path}") return None diff --git a/resources/workspace/cursor_pro_keep_alive.py b/resources/workspace/cursor_pro_keep_alive.py index 42b6181..4422223 100644 --- a/resources/workspace/cursor_pro_keep_alive.py +++ b/resources/workspace/cursor_pro_keep_alive.py @@ -69,7 +69,7 @@ def get_cursor_session_token(tab, max_attempts=3, retry_interval=2): :param retry_interval: 重试间隔(秒) :return: session token 或 None """ - print("开始获取cookie") + logging.info("开始获取cookie") attempts = 0 while attempts < max_attempts: @@ -123,18 +123,18 @@ def get_temp_email(tab): if current_email == last_email: stable_count += 1 if stable_count >= 2: # 连续两次获取到相同的邮箱地址 - print(f"邮箱地址已稳定: {current_email}") + logging.info(f"邮箱地址已稳定: {current_email}") return current_email else: stable_count = 0 last_email = current_email - print(f"检测到邮箱地址: {current_email},等待稳定...") + logging.info(f"检测到邮箱地址: {current_email},等待稳定...") print(f"等待邮箱加载... ({i+1}/{max_retries})") time.sleep(2) except Exception as e: - print(f"获取邮箱时出错: {str(e)}") + logging.warning(f"获取邮箱时出错: {str(e)}") time.sleep(2) stable_count = 0 # 发生错误时重置稳定计数 @@ -143,7 +143,7 @@ def get_temp_email(tab): def sign_up_account(browser, tab, account_info): """注册账号""" - print("开始注册...") + logging.info("开始注册...") tab.get(SIGN_UP_URL) # 使用常量 SIGN_UP_URL try: @@ -160,7 +160,7 @@ def sign_up_account(browser, tab, account_info): tab.actions.click("@type=submit") except Exception as e: - print("打开注册页面失败") + logging.warning("打开注册页面失败") return False handle_turnstile(tab) @@ -171,15 +171,15 @@ def sign_up_account(browser, tab, account_info): time.sleep(random.uniform(1, 3)) tab.ele("@type=submit").click() - print("请稍等...") + logging.info("请稍等...") except Exception as e: - print("执行失败") + logging.warning("执行失败") return False time.sleep(random.uniform(1, 3)) if tab.ele("This email is not available."): - print("执行失败") + logging.warning("执行失败") return False handle_turnstile(tab) @@ -218,16 +218,16 @@ def handle_verification_code(browser, tab, account_info): while time.time() - start_time < max_wait: try: if tab.ele("Account Settings"): - print("注册成功") + logging.info("注册成功") return True if tab.ele("@data-index=0"): code = email_handler.get_verification_code(account_info["email"]) if not code: - print("无法获取验证码") + logging.error("无法获取验证码") return False - print(f"输入验证码: {code}") + logging.info(f"输入验证码: {code}") for i, digit in enumerate(code): tab.ele(f"@data-index={i}").input(digit) time.sleep(random.uniform(0.1, 0.3)) @@ -236,10 +236,10 @@ def handle_verification_code(browser, tab, account_info): time.sleep(2) except Exception as e: - print(f"处理验证码时出错: {str(e)}") + logging.error(f"处理验证码时出错: {str(e)}") time.sleep(2) - print("验证码处理超时") + logging.error("验证码处理超时") return False @@ -333,7 +333,7 @@ def main(): extension_path = args.extension_path or default_extension_path if os.path.exists(extension_path): - required_files = ['manifest.json', 'content.js'] + required_files = ['manifest.json', 'script.js'] missing_files = [f for f in required_files if not os.path.exists(os.path.join(extension_path, f))] if missing_files: @@ -375,16 +375,16 @@ def main(): if sign_up_account(browser, signup_tab, account_info): token = get_cursor_session_token(signup_tab) if token: - print(f"注册成功! Token: {token}") + logging.info(f"注册成功! Token: {token}") update_cursor_auth( email=account_info["email"], access_token=token, refresh_token=token ) else: - print("获取token失败") + logging.error("获取token失败") else: - print("注册失败") + logging.error("注册失败") - print("执行完毕") + logging.info("执行完毕") cleanup_and_exit(browser_manager, 0) except Exception as e: diff --git a/resources/workspace/get_email_code.py b/resources/workspace/get_email_code.py index 513ef11..991b062 100644 --- a/resources/workspace/get_email_code.py +++ b/resources/workspace/get_email_code.py @@ -1,6 +1,7 @@ from DrissionPage.common import Keys import time import re +import logging class EmailVerificationHandler: @@ -13,56 +14,55 @@ def get_verification_code(self, email): code = None try: - print("正在处理...") - # 打开新标签页访问临时邮箱 + logging.info("正在处理...") tab_mail = self.browser.new_tab(self.mail_url) self.browser.activate_tab(tab_mail) - # 等待并获取最新邮件 code = self._get_latest_mail_code(tab_mail) - # 清理邮件 # self._cleanup_mail(tab_mail) - # 关闭标签页 tab_mail.close() except Exception as e: - print(f"获取验证码失败: {str(e)}") + logging.error(f"获取验证码失败: {str(e)}") return code def _get_latest_mail_code(self, tab): code = None - # 等待并点击验证邮件 - while True: + retry_count = 0 + max_retries = 3 + + while retry_count < max_retries: try: - # 查找邮件表格中的所有行 email_row = tab.ele("css:tbody > tr.border-b.cursor-pointer", timeout=2) if email_row: - # 查找主题单元格 subject_cell = email_row.ele("css:td:nth-child(2)") if subject_cell and "Verify your email address" in subject_cell.text: - print("找到验证邮件,正在打开...") + logging.info("找到验证邮件,正在打开...") email_row.click() time.sleep(2) break - - print("等待验证邮件...") + + logging.info("等待验证邮件...") time.sleep(2) - # 刷新页面 tab.refresh() time.sleep(3) - + retry_count += 1 + except Exception as e: - print(f"查找邮件时出错: {str(e)}") + logging.error(f"查找邮件时出错: {str(e)}") time.sleep(2) + retry_count += 1 + + if retry_count >= max_retries: + logging.error("无法找到验证邮件,结束任务。") + raise Exception("无法找到验证邮件。") - # 提取验证码 max_retries = 10 for attempt in range(max_retries): try: - # 查找邮件内容区域 content_td = tab.ele("css:td.px-3.text-black.text-base", timeout=2) if content_td: content = content_td.text @@ -70,17 +70,17 @@ def _get_latest_mail_code(self, tab): matches = re.findall(r'\b\d{6}\b', content) for match in matches: if "verification code" in content.lower() or "verify" in content.lower(): - print(f"从内容中提取到验证码: {match}") + logging.info(f"从内容中提取到验证码: {match}") return match - - print(f"等待验证码加载... ({attempt + 1}/{max_retries})") + + logging.info(f"等待验证码加载... ({attempt + 1}/{max_retries})") time.sleep(2) - + except Exception as e: - print(f"提取验证码时出错: {str(e)}") + logging.error(f"提取验证码时出错: {str(e)}") time.sleep(2) - - print("无法获取验证码") + + logging.error("无法获取验证码") return None def _cleanup_mail(self, tab): diff --git a/src/main/index.ts b/src/main/index.ts index 62f9730..da64fcb 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -474,7 +474,7 @@ app.whenReady().then(() => { let command: string; switch (process.platform) { case 'win32': - command = `start /MIN cmd.exe /K "cd /d "${workspacePath}" && python "${scriptPath}" && pause"`; + command = `start /B cmd.exe /K "cd /d "${workspacePath}" && python "${scriptPath}" && exit"`; break; case 'darwin': // macOS // 修复 macOS 命令,正确处理路径中的空格和引号 diff --git a/workspace/browser_utils.py b/workspace/browser_utils.py index a6837c8..75cad54 100644 --- a/workspace/browser_utils.py +++ b/workspace/browser_utils.py @@ -18,18 +18,19 @@ def init_browser(self): def _get_browser_options(self): """获取浏览器配置""" co = ChromiumOptions() - + browser_path = os.getenv("BROWSER_PATH", None) + if browser_path and os.path.exists(browser_path): + co.set_paths(browser_path=browser_path) # 尝试加载插件 try: extension_path = self._get_extension_path() if extension_path: - print(f"正在加载插件: {extension_path}") co.add_extension(extension_path) - print("插件加载成功") + logging.info("插件加载成功") else: - print("未找到插件目录") + logging.warning("未找到插件目录") except Exception as e: - print(f"加载插件时出错: {e}") + logging.warning(f"加载插件时出错: {e}") # 基本配置 co.set_user_agent( @@ -37,8 +38,11 @@ def _get_browser_options(self): ) co.set_pref("credentials_enable_service", False) co.set_argument("--hide-crash-restore-bubble") + proxy = os.getenv('BROWSER_PROXY') + if proxy: + co.set_proxy(proxy) co.auto_port() - co.headless(True) + co.headless(os.getenv('BROWSER_HEADLESS', 'True').lower() == 'true') if sys.platform == "darwin": co.set_argument("--no-sandbox") @@ -62,13 +66,13 @@ def _get_extension_path(self): # 检查插件文件是否完整 if os.path.exists(extension_path): - required_files = ['manifest.json', 'content.js'] + required_files = ['manifest.json', 'script.js'] if all(os.path.exists(os.path.join(extension_path, f)) for f in required_files): return extension_path else: - print(f"插件目录 {extension_path} 文件不完整") + logging.warning(f"插件目录 {extension_path} 文件不完整") else: - print(f"插件目录不存在: {extension_path}") + raise FileNotFoundError(f"插件不存在: {extension_path}") return None diff --git a/workspace/cursor_pro_keep_alive.py b/workspace/cursor_pro_keep_alive.py index 42b6181..4422223 100644 --- a/workspace/cursor_pro_keep_alive.py +++ b/workspace/cursor_pro_keep_alive.py @@ -69,7 +69,7 @@ def get_cursor_session_token(tab, max_attempts=3, retry_interval=2): :param retry_interval: 重试间隔(秒) :return: session token 或 None """ - print("开始获取cookie") + logging.info("开始获取cookie") attempts = 0 while attempts < max_attempts: @@ -123,18 +123,18 @@ def get_temp_email(tab): if current_email == last_email: stable_count += 1 if stable_count >= 2: # 连续两次获取到相同的邮箱地址 - print(f"邮箱地址已稳定: {current_email}") + logging.info(f"邮箱地址已稳定: {current_email}") return current_email else: stable_count = 0 last_email = current_email - print(f"检测到邮箱地址: {current_email},等待稳定...") + logging.info(f"检测到邮箱地址: {current_email},等待稳定...") print(f"等待邮箱加载... ({i+1}/{max_retries})") time.sleep(2) except Exception as e: - print(f"获取邮箱时出错: {str(e)}") + logging.warning(f"获取邮箱时出错: {str(e)}") time.sleep(2) stable_count = 0 # 发生错误时重置稳定计数 @@ -143,7 +143,7 @@ def get_temp_email(tab): def sign_up_account(browser, tab, account_info): """注册账号""" - print("开始注册...") + logging.info("开始注册...") tab.get(SIGN_UP_URL) # 使用常量 SIGN_UP_URL try: @@ -160,7 +160,7 @@ def sign_up_account(browser, tab, account_info): tab.actions.click("@type=submit") except Exception as e: - print("打开注册页面失败") + logging.warning("打开注册页面失败") return False handle_turnstile(tab) @@ -171,15 +171,15 @@ def sign_up_account(browser, tab, account_info): time.sleep(random.uniform(1, 3)) tab.ele("@type=submit").click() - print("请稍等...") + logging.info("请稍等...") except Exception as e: - print("执行失败") + logging.warning("执行失败") return False time.sleep(random.uniform(1, 3)) if tab.ele("This email is not available."): - print("执行失败") + logging.warning("执行失败") return False handle_turnstile(tab) @@ -218,16 +218,16 @@ def handle_verification_code(browser, tab, account_info): while time.time() - start_time < max_wait: try: if tab.ele("Account Settings"): - print("注册成功") + logging.info("注册成功") return True if tab.ele("@data-index=0"): code = email_handler.get_verification_code(account_info["email"]) if not code: - print("无法获取验证码") + logging.error("无法获取验证码") return False - print(f"输入验证码: {code}") + logging.info(f"输入验证码: {code}") for i, digit in enumerate(code): tab.ele(f"@data-index={i}").input(digit) time.sleep(random.uniform(0.1, 0.3)) @@ -236,10 +236,10 @@ def handle_verification_code(browser, tab, account_info): time.sleep(2) except Exception as e: - print(f"处理验证码时出错: {str(e)}") + logging.error(f"处理验证码时出错: {str(e)}") time.sleep(2) - print("验证码处理超时") + logging.error("验证码处理超时") return False @@ -333,7 +333,7 @@ def main(): extension_path = args.extension_path or default_extension_path if os.path.exists(extension_path): - required_files = ['manifest.json', 'content.js'] + required_files = ['manifest.json', 'script.js'] missing_files = [f for f in required_files if not os.path.exists(os.path.join(extension_path, f))] if missing_files: @@ -375,16 +375,16 @@ def main(): if sign_up_account(browser, signup_tab, account_info): token = get_cursor_session_token(signup_tab) if token: - print(f"注册成功! Token: {token}") + logging.info(f"注册成功! Token: {token}") update_cursor_auth( email=account_info["email"], access_token=token, refresh_token=token ) else: - print("获取token失败") + logging.error("获取token失败") else: - print("注册失败") + logging.error("注册失败") - print("执行完毕") + logging.info("执行完毕") cleanup_and_exit(browser_manager, 0) except Exception as e: diff --git a/workspace/get_email_code.py b/workspace/get_email_code.py index 513ef11..991b062 100644 --- a/workspace/get_email_code.py +++ b/workspace/get_email_code.py @@ -1,6 +1,7 @@ from DrissionPage.common import Keys import time import re +import logging class EmailVerificationHandler: @@ -13,56 +14,55 @@ def get_verification_code(self, email): code = None try: - print("正在处理...") - # 打开新标签页访问临时邮箱 + logging.info("正在处理...") tab_mail = self.browser.new_tab(self.mail_url) self.browser.activate_tab(tab_mail) - # 等待并获取最新邮件 code = self._get_latest_mail_code(tab_mail) - # 清理邮件 # self._cleanup_mail(tab_mail) - # 关闭标签页 tab_mail.close() except Exception as e: - print(f"获取验证码失败: {str(e)}") + logging.error(f"获取验证码失败: {str(e)}") return code def _get_latest_mail_code(self, tab): code = None - # 等待并点击验证邮件 - while True: + retry_count = 0 + max_retries = 3 + + while retry_count < max_retries: try: - # 查找邮件表格中的所有行 email_row = tab.ele("css:tbody > tr.border-b.cursor-pointer", timeout=2) if email_row: - # 查找主题单元格 subject_cell = email_row.ele("css:td:nth-child(2)") if subject_cell and "Verify your email address" in subject_cell.text: - print("找到验证邮件,正在打开...") + logging.info("找到验证邮件,正在打开...") email_row.click() time.sleep(2) break - - print("等待验证邮件...") + + logging.info("等待验证邮件...") time.sleep(2) - # 刷新页面 tab.refresh() time.sleep(3) - + retry_count += 1 + except Exception as e: - print(f"查找邮件时出错: {str(e)}") + logging.error(f"查找邮件时出错: {str(e)}") time.sleep(2) + retry_count += 1 + + if retry_count >= max_retries: + logging.error("无法找到验证邮件,结束任务。") + raise Exception("无法找到验证邮件。") - # 提取验证码 max_retries = 10 for attempt in range(max_retries): try: - # 查找邮件内容区域 content_td = tab.ele("css:td.px-3.text-black.text-base", timeout=2) if content_td: content = content_td.text @@ -70,17 +70,17 @@ def _get_latest_mail_code(self, tab): matches = re.findall(r'\b\d{6}\b', content) for match in matches: if "verification code" in content.lower() or "verify" in content.lower(): - print(f"从内容中提取到验证码: {match}") + logging.info(f"从内容中提取到验证码: {match}") return match - - print(f"等待验证码加载... ({attempt + 1}/{max_retries})") + + logging.info(f"等待验证码加载... ({attempt + 1}/{max_retries})") time.sleep(2) - + except Exception as e: - print(f"提取验证码时出错: {str(e)}") + logging.error(f"提取验证码时出错: {str(e)}") time.sleep(2) - - print("无法获取验证码") + + logging.error("无法获取验证码") return None def _cleanup_mail(self, tab):