mirror of
https://github.com/Murasame-Dev/McStatus-API.git
synced 2025-12-16 05:08:00 +00:00
feat: image generater support.
This commit is contained in:
parent
099a79fe5f
commit
e9b2033c3e
5 changed files with 133 additions and 347 deletions
107
app.py
107
app.py
|
|
@ -2,8 +2,13 @@
|
|||
# 重写 Flask-MCMOTD,早期版本用的是面向过程的方式进行写的,一个文件写了400多行,真是要爆了T.T
|
||||
# 2025/8/28 by Murasame:使用 *Claude Sonnet 4* 重写成了 FastAPI
|
||||
|
||||
# 基础模块
|
||||
import asyncio
|
||||
import base64
|
||||
from io import BytesIO
|
||||
|
||||
# API
|
||||
from fastapi import FastAPI, HTTPException, Query
|
||||
from fastapi import FastAPI, HTTPException, Query, Response
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
# Java版查询模块
|
||||
|
|
@ -14,12 +19,20 @@ from BedrockServerStatus import bedrock_status
|
|||
from dnslookup import dns_lookup
|
||||
|
||||
# 格式化文本
|
||||
from FormatData import format_java_data, format_bedrock_data, format_index, format_java_index, format_bedrock_index
|
||||
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
|
||||
|
||||
app = FastAPI(
|
||||
title="McStatus API",
|
||||
description="Minecraft服务器状态查询API",
|
||||
version="2.0.0"
|
||||
version="3.0.0"
|
||||
)
|
||||
|
||||
# 添加CORS中间件
|
||||
|
|
@ -75,6 +88,94 @@ async def get_bedrock_status(ip: str = Query(None, description="服务器IP地
|
|||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
# 生成服务器状态图片
|
||||
@app.get("/img")
|
||||
async def get_status_image(ip: str = Query(None, description="服务器IP地址或域名"), type: str = Query(None, description="服务器版本,java 或 bedrock")):
|
||||
# 空值输出 API 用法
|
||||
if not ip or not type or type not in ["java", "bedrock"]:
|
||||
message = format_img_index()
|
||||
raise HTTPException(status_code=400, detail=message)
|
||||
|
||||
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 or FONT_PATH == "":
|
||||
font_url = None
|
||||
else:
|
||||
font_url = FONT_PATH
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
# 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']}",
|
||||
]
|
||||
|
||||
# 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 == 0 or IMAGE_HEIGHT == 0:
|
||||
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)
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue