From f6b9b36713a2c5bc217d4e4a76e36faab5997bd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=AE=E3=83=AA=E7=8C=AB?= Date: Tue, 2 Sep 2025 22:42:20 +0800 Subject: [PATCH] feat: compact pull #4 and delete unusable things. (#6) * Add module mc-status-img * delete: more submodule dir * feat: compact pull #5 and delete unuseable things. * chore: move the functions and modules to great place. * add: ignore the .vscode directory * chore: add line break in the end. --------- Co-authored-by: quqiOnfree --- .gitignore | 1 + .vscode/settings.json | 4 --- app.py | 81 +++++-------------------------------------- config.py | 6 ++-- generate_img.py | 51 ++++++++++++++++++++++++--- 5 files changed, 58 insertions(+), 85 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 2c19066..e34662f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ /__pycache__ uv.lock .python-version +.vscode/ diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index ba2a6c0..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "python-envs.defaultEnvManager": "ms-python.python:system", - "python-envs.pythonProjects": [] -} \ No newline at end of file diff --git a/app.py b/app.py index cdb8824..32a4236 100644 --- a/app.py +++ b/app.py @@ -5,6 +5,7 @@ # 基础模块 import asyncio import base64 +import uvicorn from io import BytesIO # API @@ -22,12 +23,7 @@ from dnslookup import dns_lookup from FormatData import format_java_data, format_bedrock_data, format_index, format_java_index, format_bedrock_index, format_img_index # 图片生成模块 -from mcstatus_img.get_background import download_image_with_httpx_auto_redirect -from mcstatus_img.create_image import create_image -from mcstatus_img.get_icon import get_icon_image - -# 环境变量 -from config import BACKGROUND_URL, DEFAULT_ICON, FONT_PATH, IMAGE_WIDTH, IMAGE_HEIGHT +from generate_img import get_icon_image, generate_java_status_image, generate_bedrock_status_image, get_background_image app = FastAPI( title="McStatus API", @@ -98,84 +94,23 @@ async def get_status_image(ip: str = Query(None, description="服务器IP地址 try: # 背景图获取方法 - if BACKGROUND_URL.startswith("http://") or BACKGROUND_URL.startswith("https://"): - background_data = await download_image_with_httpx_auto_redirect(BACKGROUND_URL) - elif BACKGROUND_URL == "none": - background_data = None - else: - with open(BACKGROUND_URL, "rb") as f: - background_data = f.read() - - # 字体设置方法 - if not FONT_PATH: - font_url = None - else: - font_url = FONT_PATH - - loop = asyncio.get_event_loop() - + background_data = await get_background_image() + # java 服务器的方法 if type == "java": - ip, type = await loop.run_in_executor(None, dns_lookup, ip) - status = await loop.run_in_executor(None, java_status, ip) - data = format_java_data(ip, type, status) - # JAVA服务器字典 - text_list = [ - f"ip: {data['ip']}", - f"type: {data['type']}", - f"version: {data['version']}", - f"latency: {round(data['latency'], 2)} ms", - f"players: {data['players']['online']}/{data['players']['max']}", - ] + image = await generate_java_status_image(ip) # bedrock 服务器的方法 if type == "bedrock": - status = await loop.run_in_executor(None, bedrock_status, ip) - data = format_bedrock_data(ip, status) - data['type'] = 'normal' - status.icon = None - # BE服务器字典 - text_list = [ - f"ip: {data['ip']}", - f"version: {data['version']}", - f"latency: {round(data['latency'], 2)} ms", - f"players: {data['players']['online']}/{data['players']['max']}", - ] - - # MOTD信息 - motd_list = data['motd'].split("\n") - - # 图标获取方法 - if status.icon: - icon_data = base64.b64decode(status.icon.split(",")[1]) - else: - icon_data = await get_icon_image(DEFAULT_ICON) - - # 图片尺寸 - if not IMAGE_WIDTH or not IMAGE_HEIGHT: - image_size = [0,0] - else: - image_size = [IMAGE_WIDTH, IMAGE_HEIGHT] - - image = await loop.run_in_executor(None, - create_image, - background_data, - icon_data, - text_list, - motd_list, - font_url, - image_size) + image = await generate_bedrock_status_image(ip) + img_io = BytesIO() image.save(img_io, 'JPEG') img_io.seek(0) return Response(content=img_io.getvalue(), media_type="image/jpeg") - except Exception as e: raise HTTPException(status_code=500, detail=str(e)) - - if __name__ == '__main__': - import uvicorn - uvicorn.run("app:app", host="0.0.0.0", port=5000, reload=True) \ No newline at end of file + uvicorn.run("app:app", host="0.0.0.0", port=5000, reload=True) diff --git a/config.py b/config.py index 0574eec..b445a90 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,7 @@ import os BACKGROUND_URL = os.environ.get("BACKGROUND_URL", "https://www.loliapi.com/acg/") -DEFAULT_ICON = os.environ.get("DEFAULT_ICON", "mcstatus_img/minecraft-creeper-face.png") -FONT_PATH = os.environ.get("FONT_PATH", "mcstatus_img/MiSans-Bold.ttf") +DEFAULT_ICON = os.environ.get("DEFAULT_ICON", "mc_status_img/minecraft-creeper-face.png") +FONT_PATH = os.environ.get("FONT_PATH", "mc_status_img/MiSans-Bold.ttf") IMAGE_WIDTH = int(os.environ.get("IMAGE_WIDTH", "0")) -IMAGE_HEIGHT = int(os.environ.get("IMAGE_HEIGHT", "0")) \ No newline at end of file +IMAGE_HEIGHT = int(os.environ.get("IMAGE_HEIGHT", "0")) diff --git a/generate_img.py b/generate_img.py index 6a81730..64acc4c 100644 --- a/generate_img.py +++ b/generate_img.py @@ -1,5 +1,7 @@ +# 图片生成模块 from mc_status_img.get_background import download_image_with_httpx_auto_redirect from mc_status_img.create_image import create_image +from mc_status_img.get_icon import get_icon_image # Java版查询模块 from JavaServerStatus import java_status @@ -10,11 +12,36 @@ from dnslookup import dns_lookup # 格式化文本 from FormatData import format_java_data, format_bedrock_data +# 环境变量 +from config import BACKGROUND_URL, DEFAULT_ICON, FONT_PATH, IMAGE_WIDTH, IMAGE_HEIGHT + import base64 import asyncio -BACKGROUND_URL = "https://www.loliapi.com/acg/" -DEFAULT_ICON = "./minecraft-creeper-face.png" +async def get_font_url(): + if not FONT_PATH: + font_url = None + else: + font_url = FONT_PATH + return font_url + +async def get_image_size(): + if not IMAGE_WIDTH or not IMAGE_HEIGHT: + image_size = [0,0] + else: + image_size = [IMAGE_WIDTH, IMAGE_HEIGHT] + return image_size + +async def get_background_image(): + if BACKGROUND_URL.startswith("http://") or BACKGROUND_URL.startswith("https://"): + background_data = await download_image_with_httpx_auto_redirect(BACKGROUND_URL) + elif BACKGROUND_URL == "": + background_data = None + else: + with open(BACKGROUND_URL, "rb") as f: + background_data = f.read() + return background_data + async def get_icon_image(url: str): if url.startswith("http"): @@ -55,20 +82,28 @@ async def generate_java_status_image(addr: str): f"players: {data['players']['online']}/{data['players']['max']}", ] + font_url = await get_font_url() + + image_size = await get_image_size() + if status.icon: image = await loop.run_in_executor(None, create_image, background_data, base64.b64decode(status.icon.split(",")[1]), text_list, - motd_list) + motd_list, + font_url, + image_size) else: image = await loop.run_in_executor(None, create_image, background_data, icon_data, text_list, - motd_list) + motd_list, + font_url, + image_size) return image async def generate_bedrock_status_image(addr: str): @@ -95,10 +130,16 @@ async def generate_bedrock_status_image(addr: str): f"players: {data['players']['online']}/{data['players']['max']}", ] + font_url = await get_font_url() + + image_size = await get_image_size() + image = await loop.run_in_executor(None, create_image, background_data, icon_data, text_list, - motd_list) + motd_list, + font_url, + image_size) return image