From 813dd4431e6072a5fa9fe6df0ccac96ff4f083ff Mon Sep 17 00:00:00 2001 From: yeongpin Date: Mon, 3 Mar 2025 11:09:11 +0800 Subject: [PATCH] feat: Add version update checker with GitHub release support --- .env | 4 ++-- locales/en.json | 8 ++++++++ locales/zh_cn.json | 8 ++++++++ locales/zh_tw.json | 8 ++++++++ main.py | 35 ++++++++++++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/.env b/.env index cfc1394..2d17674 100644 --- a/.env +++ b/.env @@ -1,2 +1,2 @@ -version=1.4.07 -VERSION=1.4.07 +version=1.5.01 +VERSION=1.5.01 diff --git a/locales/en.json b/locales/en.json index b7cbf3c..52ba540 100644 --- a/locales/en.json +++ b/locales/en.json @@ -233,5 +233,13 @@ "directory_removed": "Directory Removed", "creating_block_file": "Creating Block File", "block_file_created": "Block File Created" + }, + "updater": { + "checking": "Checking for updates...", + "new_version_available": "New version available! (Current: {current}, Latest: {latest})", + "updating": "Updating to the latest version. The program will restart automatically.", + "up_to_date": "You are using the latest version.", + "check_failed": "Failed to check for updates: {error}", + "continue_anyway": "Continuing with current version..." } } \ No newline at end of file diff --git a/locales/zh_cn.json b/locales/zh_cn.json index 970c3a3..fed4a05 100644 --- a/locales/zh_cn.json +++ b/locales/zh_cn.json @@ -230,5 +230,13 @@ "directory_removed": "目录已删除", "creating_block_file": "创建阻止文件", "block_file_created": "阻止文件已创建" + }, + "updater": { + "checking": "检查更新...", + "new_version_available": "有新版本可用! (当前版本: {current}, 最新版本: {latest})", + "updating": "正在更新到最新版本。程序将自动重启。", + "up_to_date": "您使用的是最新版本。", + "check_failed": "检查更新失败: {error}", + "continue_anyway": "继续使用当前版本..." } } \ No newline at end of file diff --git a/locales/zh_tw.json b/locales/zh_tw.json index 023f401..4e9634d 100644 --- a/locales/zh_tw.json +++ b/locales/zh_tw.json @@ -211,5 +211,13 @@ "directory_removed": "目錄已刪除", "creating_block_file": "創建阻止文件", "block_file_created": "阻止文件已創建" + }, + "updater": { + "checking": "檢查更新...", + "new_version_available": "有新版本可用! (當前版本: {current}, 最新版本: {latest})", + "updating": "正在更新到最新版本。程序將自動重啟。", + "up_to_date": "您使用的是最新版本。", + "check_failed": "檢查更新失敗: {error}", + "continue_anyway": "繼續使用當前版本..." } } \ No newline at end of file diff --git a/main.py b/main.py index 458cc4e..8c1b37b 100644 --- a/main.py +++ b/main.py @@ -3,10 +3,12 @@ import os import sys import json -from logo import print_logo +from logo import print_logo, version from colorama import Fore, Style, init import locale import platform +import requests +import subprocess # 只在 Windows 系统上导入 windll if platform.system() == 'Windows': @@ -203,8 +205,39 @@ def select_language(): print(f"{Fore.RED}{EMOJI['ERROR']} {translator.get('menu.invalid_choice')}{Style.RESET_ALL}") return False +def check_latest_version(): + """Check if current version matches the latest release 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') + + 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) + else: + print(f"{Fore.GREEN}{EMOJI['SUCCESS']} {translator.get('updater.up_to_date')}{Style.RESET_ALL}") + + 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 + def main(): print_logo() + check_latest_version() # Add version check before showing menu print_menu() while True: