From e42ce7dd86a5b11900835edc9940410b2fff474d Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Mon, 20 Jan 2025 23:27:13 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E4=BA=86=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/utils/io.py | 40 ++++++++++++++++++++++++----- astrbot/dashboard/routes/chat.py | 3 +-- main.py | 6 ++--- packages/python_interpreter/main.py | 2 +- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/astrbot/core/utils/io.py b/astrbot/core/utils/io.py index a268ad522..9272920ea 100644 --- a/astrbot/core/utils/io.py +++ b/astrbot/core/utils/io.py @@ -101,34 +101,57 @@ async def download_image_by_url(url: str, post: bool = False, post_data: dict = except Exception as e: raise e -async def download_file(url: str, path: str): +async def download_file(url: str, path: str, show_progress: bool = False): ''' 从指定 url 下载文件到指定路径 path ''' try: async with aiohttp.ClientSession() as session: - async with session.get(url, timeout=20) as resp: + async with session.get(url, timeout=300) as resp: if resp.status != 200: raise Exception(f"下载文件失败: {resp.status}") + total_size = int(resp.headers.get('content-length', 0)) + downloaded_size = 0 + start_time = time.time() + if show_progress: + print(f"文件大小: {total_size / 1024:.2f} KB | 文件地址: {url}") with open(path, 'wb') as f: while True: chunk = await resp.content.read(8192) if not chunk: break f.write(chunk) + downloaded_size += len(chunk) + if show_progress: + elapsed_time = time.time() - start_time + speed = downloaded_size / 1024 / elapsed_time # KB/s + print(f"\r下载进度: {downloaded_size / total_size:.2%} 速度: {speed:.2f} KB/s", end='') except aiohttp.client.ClientConnectorSSLError: # 关闭SSL验证 ssl_context = ssl.create_default_context() ssl_context.set_ciphers('DEFAULT') async with aiohttp.ClientSession(trust_env=False) as session: - async with session.get(url, ssl=ssl_context, timeout=20) as resp: + async with session.get(url, ssl=ssl_context, timeout=300) as resp: + total_size = int(resp.headers.get('content-length', 0)) + downloaded_size = 0 + start_time = time.time() + if show_progress: + print(f"文件大小: {total_size / 1024:.2f} KB | 文件地址: {url}") with open(path, 'wb') as f: while True: chunk = await resp.content.read(8192) if not chunk: break f.write(chunk) - + downloaded_size += len(chunk) + if show_progress: + elapsed_time = time.time() - start_time + speed = downloaded_size / 1024 / elapsed_time # KB/s + print(f"\r下载进度: {downloaded_size / total_size:.2%} 速度: {speed:.2f} KB/s", end='') + if show_progress: + print() + + def file_to_base64(file_path: str) -> str: with open(file_path, "rb") as f: data_bytes = f.read() @@ -149,7 +172,12 @@ def get_local_ip_addresses(): async def download_dashboard(): '''下载管理面板文件''' - dashboard_release_url = "https://astrbot-registry.lwl.lol/download/astrbot-dashboard/latest/dist.zip" - await download_file(dashboard_release_url, "data/dashboard.zip") + dashboard_release_url = "https://astrbot-registry.soulter.top/download/astrbot-dashboard/latest/dist.zip" + try: + await download_file(dashboard_release_url, "data/dashboard.zip", show_progress=True) + except BaseException as _: + dashboard_release_url = "https://github.com/Soulter/AstrBot/releases/latest/download/dist.zip" + await download_file(dashboard_release_url, "data/dashboard.zip", show_progress=True) + print("解压管理面板文件中...") with zipfile.ZipFile("data/dashboard.zip", "r") as z: z.extractall("data") \ No newline at end of file diff --git a/astrbot/dashboard/routes/chat.py b/astrbot/dashboard/routes/chat.py index 382132121..9ec348235 100644 --- a/astrbot/dashboard/routes/chat.py +++ b/astrbot/dashboard/routes/chat.py @@ -182,8 +182,7 @@ class ChatRoute(Route): await asyncio.sleep(0.5) except BaseException as e: - logger.error(e) - logger.error(f"与用户 {username} 断开聊天长连接。") + logger.debug(f"用户 {username} 断开聊天长连接: {str(e)}。") self.curr_chat_sse.pop(username) return diff --git a/main.py b/main.py index 3b0b79481..82dff0677 100644 --- a/main.py +++ b/main.py @@ -42,17 +42,17 @@ async def check_dashboard_files(): with open("data/dist/assets/version", "r") as f: v = f.read().strip() if v != f"v{VERSION}": - logger.warning("检测到管理面板有更新。可以使用 /dashboard update 命令更新。") + logger.warning("检测到管理面板有更新。可以使用 /dashboard_update 命令更新。") else: logger.info("管理面板文件已是最新。") return - logger.info("开始下载管理面板文件...") + logger.info("开始下载管理面板文件...高峰期(晚上)可能导致较慢的速度。如多次下载失败,请前往 https://github.com/Soulter/AstrBot/releases/latest 下载 dist.zip,并将其中的 dist 文件夹解压至 data 目录下。") try: await download_dashboard() except Exception as e: - logger.critical(f"下载管理面板文件失败: {e}") + logger.critical(f"下载管理面板文件失败: {e}。") return logger.info("管理面板下载完成。") diff --git a/packages/python_interpreter/main.py b/packages/python_interpreter/main.py index d89c0a53e..5b5dc7325 100644 --- a/packages/python_interpreter/main.py +++ b/packages/python_interpreter/main.py @@ -140,7 +140,7 @@ class Main(star.Star): docker = aiodocker.Docker() await docker.version() return True - except aiodocker.exceptions.DockerError as e: + except BaseException as e: logger.info(f"检查 Docker 可用性: {e}") return False