mirror of
https://github.com/Murasame-Dev/McStatus-API.git
synced 2025-12-15 12:48:02 +00:00
* 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 <quqiOnfree@outlook.com>
This commit is contained in:
parent
802b883d68
commit
f6b9b36713
5 changed files with 58 additions and 85 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -2,3 +2,4 @@
|
||||||
/__pycache__
|
/__pycache__
|
||||||
uv.lock
|
uv.lock
|
||||||
.python-version
|
.python-version
|
||||||
|
.vscode/
|
||||||
|
|
|
||||||
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
|
|
@ -1,4 +0,0 @@
|
||||||
{
|
|
||||||
"python-envs.defaultEnvManager": "ms-python.python:system",
|
|
||||||
"python-envs.pythonProjects": []
|
|
||||||
}
|
|
||||||
81
app.py
81
app.py
|
|
@ -5,6 +5,7 @@
|
||||||
# 基础模块
|
# 基础模块
|
||||||
import asyncio
|
import asyncio
|
||||||
import base64
|
import base64
|
||||||
|
import uvicorn
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
# API
|
# 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 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 generate_img import get_icon_image, generate_java_status_image, generate_bedrock_status_image, get_background_image
|
||||||
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(
|
app = FastAPI(
|
||||||
title="McStatus API",
|
title="McStatus API",
|
||||||
|
|
@ -98,84 +94,23 @@ async def get_status_image(ip: str = Query(None, description="服务器IP地址
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# 背景图获取方法
|
# 背景图获取方法
|
||||||
if BACKGROUND_URL.startswith("http://") or BACKGROUND_URL.startswith("https://"):
|
background_data = await get_background_image()
|
||||||
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()
|
|
||||||
|
|
||||||
# java 服务器的方法
|
# java 服务器的方法
|
||||||
if type == "java":
|
if type == "java":
|
||||||
ip, type = await loop.run_in_executor(None, dns_lookup, ip)
|
image = await generate_java_status_image(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 服务器的方法
|
# bedrock 服务器的方法
|
||||||
if type == "bedrock":
|
if type == "bedrock":
|
||||||
status = await loop.run_in_executor(None, bedrock_status, ip)
|
image = await generate_bedrock_status_image(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)
|
|
||||||
img_io = BytesIO()
|
img_io = BytesIO()
|
||||||
image.save(img_io, 'JPEG')
|
image.save(img_io, 'JPEG')
|
||||||
img_io.seek(0)
|
img_io.seek(0)
|
||||||
return Response(content=img_io.getvalue(), media_type="image/jpeg")
|
return Response(content=img_io.getvalue(), media_type="image/jpeg")
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import uvicorn
|
uvicorn.run("app:app", host="0.0.0.0", port=5000, reload=True)
|
||||||
uvicorn.run("app:app", host="0.0.0.0", port=5000, reload=True)
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
BACKGROUND_URL = os.environ.get("BACKGROUND_URL", "https://www.loliapi.com/acg/")
|
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")
|
DEFAULT_ICON = os.environ.get("DEFAULT_ICON", "mc_status_img/minecraft-creeper-face.png")
|
||||||
FONT_PATH = os.environ.get("FONT_PATH", "mcstatus_img/MiSans-Bold.ttf")
|
FONT_PATH = os.environ.get("FONT_PATH", "mc_status_img/MiSans-Bold.ttf")
|
||||||
IMAGE_WIDTH = int(os.environ.get("IMAGE_WIDTH", "0"))
|
IMAGE_WIDTH = int(os.environ.get("IMAGE_WIDTH", "0"))
|
||||||
IMAGE_HEIGHT = int(os.environ.get("IMAGE_HEIGHT", "0"))
|
IMAGE_HEIGHT = int(os.environ.get("IMAGE_HEIGHT", "0"))
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
|
# 图片生成模块
|
||||||
from mc_status_img.get_background import download_image_with_httpx_auto_redirect
|
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.create_image import create_image
|
||||||
|
from mc_status_img.get_icon import get_icon_image
|
||||||
|
|
||||||
# Java版查询模块
|
# Java版查询模块
|
||||||
from JavaServerStatus import java_status
|
from JavaServerStatus import java_status
|
||||||
|
|
@ -10,11 +12,36 @@ from dnslookup import dns_lookup
|
||||||
# 格式化文本
|
# 格式化文本
|
||||||
from FormatData import format_java_data, format_bedrock_data
|
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 base64
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
BACKGROUND_URL = "https://www.loliapi.com/acg/"
|
async def get_font_url():
|
||||||
DEFAULT_ICON = "./minecraft-creeper-face.png"
|
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):
|
async def get_icon_image(url: str):
|
||||||
if url.startswith("http"):
|
if url.startswith("http"):
|
||||||
|
|
@ -55,20 +82,28 @@ async def generate_java_status_image(addr: str):
|
||||||
f"players: {data['players']['online']}/{data['players']['max']}",
|
f"players: {data['players']['online']}/{data['players']['max']}",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
font_url = await get_font_url()
|
||||||
|
|
||||||
|
image_size = await get_image_size()
|
||||||
|
|
||||||
if status.icon:
|
if status.icon:
|
||||||
image = await loop.run_in_executor(None,
|
image = await loop.run_in_executor(None,
|
||||||
create_image,
|
create_image,
|
||||||
background_data,
|
background_data,
|
||||||
base64.b64decode(status.icon.split(",")[1]),
|
base64.b64decode(status.icon.split(",")[1]),
|
||||||
text_list,
|
text_list,
|
||||||
motd_list)
|
motd_list,
|
||||||
|
font_url,
|
||||||
|
image_size)
|
||||||
else:
|
else:
|
||||||
image = await loop.run_in_executor(None,
|
image = await loop.run_in_executor(None,
|
||||||
create_image,
|
create_image,
|
||||||
background_data,
|
background_data,
|
||||||
icon_data,
|
icon_data,
|
||||||
text_list,
|
text_list,
|
||||||
motd_list)
|
motd_list,
|
||||||
|
font_url,
|
||||||
|
image_size)
|
||||||
return image
|
return image
|
||||||
|
|
||||||
async def generate_bedrock_status_image(addr: str):
|
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']}",
|
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,
|
image = await loop.run_in_executor(None,
|
||||||
create_image,
|
create_image,
|
||||||
background_data,
|
background_data,
|
||||||
icon_data,
|
icon_data,
|
||||||
text_list,
|
text_list,
|
||||||
motd_list)
|
motd_list,
|
||||||
|
font_url,
|
||||||
|
image_size)
|
||||||
return image
|
return image
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue