from aiomcstats.ping import Ping
from aiomcstats.utils import create_status, get_raw, create_bedrock, ip_type
from aiomcstats.models import Debug, Offline, Status, BedrockStatus
from typing import Optional, Union
from aiomcstats.bedrock import bedrock_status
[docs]async def status(
host: str, port: Optional[int] = None, tries: Optional[int] = 3
) -> Union[Status, Offline]:
"""Get status from Minecraft server.
Args:
host (str): minecraft server address
port (Optional[int], optional): port to query server otherwise
it is found. Defaults to None.
tries (Optional[int], optional): The amount of tries to get
data from server. Defaults to 3.
Returns:
Union[Status, OfflineStatus]: Online or Offline status object.
"""
exception = ""
srv=False
if ip_type(host):
hostname = None
ip=host
port=port if port is not None else 25565
else:
try:
hostname, port, ip, srv = await get_raw(host, port)
except Exception as e:
port = port if port is not None else 25565
ip = None
hostname=None
exception = str(e)
tries = 0
for _ in range(tries):
try:
pinger = Ping(hostname, port)
await pinger.connect()
await pinger.handshake()
result = await pinger.status()
data = create_status(result, ip, port, hostname, srv)
return data
except Exception as e:
exception = str(e)
debug = Debug(
ping=True,
query=False,
srv=srv,
)
return Offline(
online=False,
ip=ip,
port=port,
debug=debug,
hostname=hostname,
error=exception,
)
[docs]async def bedrock(
host: str, port: Optional[int] = 19132, tries: Optional[int] = 3
) -> Union[BedrockStatus, Offline]:
"""Get status from Minecraft Bedrock server.
Args:
host (str): minecraft server address
port (Optional[int], optional): port to query server otherwise
it is found. Defaults to 19132.
tries (Optional[int], optional): The amount of tries to get
data from server. Defaults to 3.
Returns:
Union[BedrockStatus, BedrockOffline]: Online or Offline status object.
"""
exception = ""
if ip_type(host):
hostname = None
ip=host
else:
try:
hostname, _, ip, _ = await get_raw(host, port)
except Exception as e:
ip = None
hostname=None
exception = str(e)
tries = 0
for _ in range(tries):
try:
result = await bedrock_status(hostname, port)
print(2)
data = create_bedrock(result, ip, port, hostname, False)
return data
except Exception as e:
exception = str(e)
debug = Debug(
ping=False,
query=False,
srv=False,
error=exception,
)
return Offline(
online=False,
debug=debug,
port=port,
ip=ip,
hostname=hostname,
)