mirror of
https://github.com/Murasame-Dev/McStatus-Img.git
synced 2025-12-14 12:17:59 +00:00
refactor to async
This commit is contained in:
parent
c8232f76bc
commit
d231980883
3 changed files with 31 additions and 14 deletions
|
|
@ -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))
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
26
main.py
26
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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue