From 288553420316290b4ac3d7e5b3b280a760936710 Mon Sep 17 00:00:00 2001 From: Robofish <1683502971@qq.com> Date: Mon, 30 Mar 2026 22:51:00 +0800 Subject: [PATCH] =?UTF-8?q?fix=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +++ app/tools/update_code.py | 57 +++++++++++++++++++++------------------- 2 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c9ebf2d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python-envs.defaultEnvManager": "ms-python.python:system" +} \ No newline at end of file diff --git a/app/tools/update_code.py b/app/tools/update_code.py index a29dce8..398f405 100644 --- a/app/tools/update_code.py +++ b/app/tools/update_code.py @@ -8,9 +8,12 @@ import tempfile import time def update_code(parent=None, info_callback=None, error_callback=None): - # 优先使用 GitHub,备用 Gitea - github_url = "https://github.com/lvzucheng/MRobot/archive/refs/heads/User_code.zip" - gitea_url = "http://gitea.qutrobot.top/robofish/MRobot/archive/User_code.zip" + # 优先使用当前有效的 GitHub 源,备用 Gitea + download_sources = [ + ("GitHub CodeLoad", "https://codeload.github.com/goldenfishs/MRobot/zip/refs/heads/User_code", (5, 20)), + ("GitHub Archive", "https://github.com/goldenfishs/MRobot/archive/refs/heads/User_code.zip", (5, 20)), + ("Gitea", "http://gitea.qutrobot.top/robofish/MRobot/archive/User_code.zip", (5, 30)), + ] # 导入 CodeGenerator 以使用统一的路径获取逻辑 try: @@ -55,36 +58,36 @@ def update_code(parent=None, info_callback=None, error_callback=None): local_dir = os.path.join(assets_dir, "User_code") print(f"更新代码:最终目标目录: {local_dir}") - # 尝试从 GitHub 下载,失败则使用 Gitea + # 依次尝试下载源 download_successful = False resp = None - - # 首先尝试 GitHub - try: - print("尝试从 GitHub 下载代码...") - resp = requests.get(github_url, timeout=15) - resp.raise_for_status() - download_successful = True - print("成功从 GitHub 下载") - except Exception as github_error: - print(f"GitHub 下载失败: {github_error}") - print("切换到备用源 Gitea...") - - # 切换到 Gitea + download_errors = [] + + headers = { + "User-Agent": "MRobot-Code-Updater/1.0", + "Accept": "application/zip,application/octet-stream,*/*", + } + + for source_name, source_url, timeout in download_sources: try: - resp = requests.get(gitea_url, timeout=30) + print(f"尝试从 {source_name} 下载代码...") + resp = requests.get(source_url, timeout=timeout, headers=headers) resp.raise_for_status() download_successful = True - print("成功从 Gitea 下载") - except Exception as gitea_error: - print(f"Gitea 下载也失败: {gitea_error}") - if error_callback: - error_callback(parent, f"所有下载源均失败\nGitHub: {github_error}\nGitea: {gitea_error}") - return False - + print(f"成功从 {source_name} 下载") + break + except Exception as error: + print(f"{source_name} 下载失败: {error}") + download_errors.append(f"{source_name}: {error}") + if not download_successful or resp is None: if error_callback: - error_callback(parent, "下载失败") + message = "所有下载源均失败" + if download_errors: + message += "\n" + "\n".join(download_errors) + if os.path.exists(local_dir): + message += "\n当前将继续使用本地已有的 User_code 模板。" + error_callback(parent, message) return False try: @@ -176,4 +179,4 @@ def update_code(parent=None, info_callback=None, error_callback=None): except Exception as e: if error_callback: error_callback(parent, str(e)) - return False \ No newline at end of file + return False