From d231980883c1381c5546796d77cd5d62828bc94b Mon Sep 17 00:00:00 2001 From: quqiOnfree Date: Tue, 26 Aug 2025 14:26:08 +0800 Subject: [PATCH] refactor to async --- create_image.py | 11 ++++++++--- get_background.py | 8 ++++---- main.py | 26 +++++++++++++++++++------- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/create_image.py b/create_image.py index b5a06ab..270b82b 100644 --- a/create_image.py +++ b/create_image.py @@ -18,7 +18,8 @@ def create_background(input: bytes, width: int, height: int): midh - int(rheight / 2), midw + int(rwidth / 2), midh + int(rheight / 2))) - background = background.resize((width, height), Image.Resampling.LANCZOS) + background = background.resize((width, height), + Image.Resampling.LANCZOS) blurred_background = background.filter(ImageFilter.GaussianBlur(radius=5)) @@ -51,7 +52,10 @@ def draw_motd_text_with_shadow(image: Image.Image, draw.text((posx + pos + 1 - weight, posy + 1), text, font=font, fill='black') draw.text((posx + pos - weight, posy), text, font=font, fill=getrgb(color)) -def create_image(background: bytes, icon: str | None, text_list: list[str], motd_list: list[str]): +def create_image(background: bytes, + icon: str | None, + text_list: list[str], + motd_list: list[str]): # 图片尺寸 width, height = 1200, 400 if (len(text_list) + len(motd_list)) * 20 + 20 > height: @@ -77,7 +81,8 @@ def create_image(background: bytes, icon: str | None, text_list: list[str], motd if icon == None: small_image = Image.new('RGBA', (small_size, small_size), color='gray') else: - small_image = Image.open(BytesIO(icon)).resize((small_size, small_size), Image.Resampling.LANCZOS) + small_image = Image.open(BytesIO(icon)).resize((small_size, small_size), + Image.Resampling.LANCZOS) image.paste(small_image, (30, height // 2 - small_size // 2)) diff --git a/get_background.py b/get_background.py index 2a95676..94e4f10 100644 --- a/get_background.py +++ b/get_background.py @@ -1,6 +1,6 @@ import httpx -def download_image_with_httpx_auto_redirect(url:str): +async def download_image_with_httpx_auto_redirect(url:str): """ 使用httpx库自动处理重定向下载图片 @@ -10,9 +10,9 @@ def download_image_with_httpx_auto_redirect(url:str): """ try: # httpx默认也会自动跟随重定向 - with httpx.Client(follow_redirects=True) as client: - response = client.get(url, timeout=30.0) - + async with httpx.AsyncClient(follow_redirects=True) as client: + response = await client.get(url, timeout=30.0) + # 检查状态码 if response.status_code == 200: Background = response.content diff --git a/main.py b/main.py index b3f3f22..9330b4a 100644 --- a/main.py +++ b/main.py @@ -11,19 +11,21 @@ from mc_status_api.dnslookup import dns_lookup from mc_status_api.FormatData import format_java_data, format_bedrock_data, format_index, format_java_index, format_bedrock_index import base64 +import asyncio BACKGROUND_URL = "https://www.loliapi.com/acg/" -def generate_java_status_image(addr: str): +async def generate_java_status_image(addr: str): + loop = asyncio.get_event_loop() try: - ip, type = dns_lookup(addr) - status = java_status(ip) + ip, type = await loop.run_in_executor(None, dns_lookup, addr) + status = await loop.run_in_executor(None, java_status, ip) data = format_java_data(ip, type, status) except Exception as e: print(f"查询服务器时出错: {e}") return - background_data = download_image_with_httpx_auto_redirect(BACKGROUND_URL) + background_data = await download_image_with_httpx_auto_redirect(BACKGROUND_URL) if not background_data: background_data = None @@ -37,12 +39,22 @@ def generate_java_status_image(addr: str): ] if status.icon: - image = create_image(background_data, base64.b64decode(status.icon.split(",")[1]), text_list, motd_list) + image = await loop.run_in_executor(None, + create_image, + background_data, + base64.b64decode(status.icon.split(",")[1]), + text_list, + motd_list) else: - image = create_image(background_data, None, text_list, motd_list) + image = await loop.run_in_executor(None, + create_image, + background_data, + None, + text_list, + motd_list) return image if __name__ == "__main__": - image = generate_java_status_image("mc.hypixel.net") + image = asyncio.run(generate_java_status_image("mc.hypixel.net")) if image: image.save("output_image.png")