From 005aa2cd954d0c38a5813fdb88dd82ef2f0f399e Mon Sep 17 00:00:00 2001 From: yeongpin Date: Thu, 6 Mar 2025 11:18:21 +0800 Subject: [PATCH] feat: Improve Version Update Mechanism with Robust Error Handling - Enhance GitHub API version check with proper headers and timeout - Add comprehensive error handling for version retrieval - Implement more reliable update script download and execution - Improve cross-platform update process with better error reporting - Add fallback mechanisms for network and update failures --- main.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index 8c1b37b..d8002ba 100644 --- a/main.py +++ b/main.py @@ -210,30 +210,78 @@ def check_latest_version(): try: print(f"\n{Fore.CYAN}{EMOJI['UPDATE']} {translator.get('updater.checking')}{Style.RESET_ALL}") - # Get latest version from GitHub API with timeout - response = requests.get("https://api.github.com/repos/yeongpin/cursor-free-vip/releases/latest", timeout=5) - latest_version = response.json()["tag_name"].lstrip('v') + # Get latest version from GitHub API with timeout and proper headers + headers = { + 'Accept': 'application/vnd.github.v3+json', + 'User-Agent': 'CursorFreeVIP-Updater' + } + response = requests.get( + "https://api.github.com/repos/yeongpin/cursor-free-vip/releases/latest", + headers=headers, + timeout=10 + ) + + # Check if response is successful + if response.status_code != 200: + raise Exception(f"GitHub API returned status code {response.status_code}") + + response_data = response.json() + if "tag_name" not in response_data: + raise Exception("No version tag found in GitHub response") + + latest_version = response_data["tag_name"].lstrip('v') + + # Validate version format + if not latest_version: + raise Exception("Invalid version format received") if latest_version != version: print(f"\n{Fore.YELLOW}{EMOJI['INFO']} {translator.get('updater.new_version_available', current=version, latest=latest_version)}{Style.RESET_ALL}") - # Execute update command based on platform - if platform.system() == 'Windows': - update_command = 'irm https://raw.githubusercontent.com/yeongpin/cursor-free-vip/main/scripts/install.ps1 | iex' - subprocess.run(['powershell', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', update_command], check=True) - else: - update_command = 'curl -fsSL https://raw.githubusercontent.com/yeongpin/cursor-free-vip/main/scripts/install.sh -o install.sh && chmod +x install.sh && ./install.sh' - subprocess.Popen(update_command, shell=True) - - print(f"\n{Fore.GREEN}{EMOJI['SUCCESS']} {translator.get('updater.updating')}{Style.RESET_ALL}") - sys.exit(0) + try: + # Execute update command based on platform + if platform.system() == 'Windows': + update_command = 'irm https://raw.githubusercontent.com/yeongpin/cursor-free-vip/main/scripts/install.ps1 | iex' + subprocess.run(['powershell', '-NoProfile', '-ExecutionPolicy', 'Bypass', '-Command', update_command], check=True) + else: + # For Linux/Mac, download and execute the install script + install_script_url = 'https://raw.githubusercontent.com/yeongpin/cursor-free-vip/main/scripts/install.sh' + + # First verify the script exists + script_response = requests.get(install_script_url, timeout=5) + if script_response.status_code != 200: + raise Exception("Installation script not found") + + # Save and execute the script + with open('install.sh', 'wb') as f: + f.write(script_response.content) + + os.chmod('install.sh', 0o755) # Make executable + subprocess.run(['./install.sh'], check=True) + + # Clean up + if os.path.exists('install.sh'): + os.remove('install.sh') + + print(f"\n{Fore.GREEN}{EMOJI['SUCCESS']} {translator.get('updater.updating')}{Style.RESET_ALL}") + sys.exit(0) + + except Exception as update_error: + print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('updater.update_failed', error=str(update_error))}{Style.RESET_ALL}") + print(f"{Fore.YELLOW}{EMOJI['INFO']} {translator.get('updater.manual_update_required')}{Style.RESET_ALL}") + return else: print(f"{Fore.GREEN}{EMOJI['SUCCESS']} {translator.get('updater.up_to_date')}{Style.RESET_ALL}") + except requests.exceptions.RequestException as e: + print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('updater.network_error', error=str(e))}{Style.RESET_ALL}") + print(f"{Fore.YELLOW}{EMOJI['INFO']} {translator.get('updater.continue_anyway')}{Style.RESET_ALL}") + return + except Exception as e: print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('updater.check_failed', error=str(e))}{Style.RESET_ALL}") print(f"{Fore.YELLOW}{EMOJI['INFO']} {translator.get('updater.continue_anyway')}{Style.RESET_ALL}") - return # Continue with the program instead of blocking + return def main(): print_logo()