Compare commits

..

1 Commits
main ... v0.1

Author SHA1 Message Date
trueold89 21c99debf3
Add README.md 2024-07-07 15:42:44 +03:00
4 changed files with 21 additions and 126 deletions

View File

@ -13,7 +13,7 @@ pip install --extra-index-url https://git.orudo.ru/api/packages/trueold89/pypi/s
**From PyPi**
```bash
pip install mcaio
pip install trueold89/mcaio
```
## Build:
@ -68,8 +68,6 @@ print(name)
| motd | Server motd |
| players_count | Current number of players on the server |
| maxplayers | Max number of players on the server |
| players_list | List of current players on server |
| all_info | Dict with all information about server |
### As cli:
@ -85,5 +83,3 @@ MC_HOST=localhost MC_PORT=25565 mcaio name
| motd | Server motd |
| pcount | Current number of players on the server |
| pmax | Max number of players on the server |
| players | List of current players on server |
| all | Dict with all information about server |

View File

@ -33,31 +33,19 @@ async def get_max(server: AIOMCServer) -> int:
return await server.players_count
async def get_players(server: AIOMCServer) -> tuple:
return tuple(await server.players_list)
async def get_all(server: AIOMCServer) -> dict:
return await server.all_info
async def action() -> None:
try:
HOST, PORT = get_env("MC_HOST"), int(get_env("MC_PORT"))
server = AIOMCServer(HOST, PORT)
match arg():
case "name":
case 'name':
out = get_name(server)
case "pmax":
case 'pmax':
out = get_max(server)
case "pcount":
case 'pcount':
out = get_count(server)
case "motd":
case 'motd':
out = get_motd(server)
case "players":
out = get_players(server)
case 'all':
out = get_all(server)
case _:
raise RuntimeError
print(await out)

View File

@ -1,6 +1,5 @@
from asyncio import open_connection as aiocon, StreamReader, StreamWriter
from abc import ABC, abstractmethod
from collections.abc import Iterable
from struct import pack as struct_pack
from json import loads as jl
@ -37,89 +36,36 @@ class AIOConnection(object):
class IMCServer(ABC):
def __init__(self, host: str, port: int) -> None:
"""
:param host: Minecraft server ip or hostname
:param port: Minecraft server port
"""
self.host = host
self.port = port
@property
@abstractmethod
async def players_count(self) -> int | None:
"""
Returns current number of players on server
:return: Number of players on server
"""
async def players_count(self) -> int:
raise NotImplementedError
@property
@abstractmethod
async def name(self) -> str | None:
"""
Returns server core name
:return: Server name
"""
async def name(self) -> str:
raise NotImplementedError
@property
@abstractmethod
async def maxplayers(self) -> int | None:
"""
Returns max number of players available on server
:return: Max number of players
"""
async def maxplayers(self) -> int:
raise NotImplementedError
@property
@abstractmethod
async def motd(self) -> str | None:
"""
Returns Message of the day
:return: Server motd
"""
raise NotImplementedError
@property
@abstractmethod
async def players_list(self) -> Iterable:
"""
Returns iterable object with online players nicknames
:return: Players nicknames iterable object
"""
raise NotImplementedError
@property
@abstractmethod
async def all_info(self) -> dict:
"""
Returns dict with all information about server
:return: All information about server
"""
async def motd(self) -> str:
raise NotImplementedError
class AIOMCServer(IMCServer):
_name: str | None
_max: int | None
_count: int | None
_motd: str | None
_data: bytes
_players: Iterable
def __init__(self, host: str, port: int) -> None:
super().__init__(host, port)
self._data = self._pack_data(
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
_pack_port(self.port) + b"\x01")
self.clean()
_name: str
_max: int
_count: int
_motd: str
@staticmethod
async def _unpack_varint(s):
@ -148,20 +94,15 @@ class AIOMCServer(IMCServer):
d = bytes(d, "utf-8")
return h + d
def clean(self) -> None:
self._name = None
self._motd = None
self._max = None
self._count = None
self._players = ()
@staticmethod
def _pack_port(i):
return struct_pack('>H', i)
async def _get_data(self) -> dict:
async with AIOConnection(self.host, self.port) as socket:
await socket.send(self._data)
await socket.send(self._pack_data(
b"\x00\x00" + self._pack_data(self.host.encode('utf8')) + self.
_pack_port(self.port) + b"\x01"))
await socket.send(self._pack_data("\x00"))
await self._unpack_varint(socket)
await self._unpack_varint(socket)
@ -173,52 +114,28 @@ class AIOMCServer(IMCServer):
async def update(self) -> None:
data = await self._get_data()
self.clean()
self._name = data["version"]["name"]
self._motd = data["description"]
if isinstance(self._motd, dict):
self._motd = self._motd["text"]
players = data["players"]
self._count = int(players["online"])
self._max = int(players["max"])
if "sample" in tuple(players.keys()):
self._players = tuple(map(lambda player: player["name"],
players["sample"]))
@property
async def players_count(self) -> int | None | None:
async def players_count(self) -> int:
await self.update()
return self._count
@property
async def name(self) -> str | None | None:
async def name(self) -> str:
await self.update()
return self._name
@property
async def maxplayers(self) -> int | None | None:
async def maxplayers(self) -> int:
await self.update()
return self._max
@property
async def motd(self) -> str | None | None:
async def motd(self) -> str:
await self.update()
return self._motd
@property
async def players_list(self) -> Iterable:
await self.update()
return self._players
@property
async def all_info(self) -> dict:
await self.update()
return {
"name": self._name,
"motd": self._motd,
"players": {
"max": self._max,
"online": self._count,
"list": self._players
}
}

View File

@ -1,18 +1,12 @@
from setuptools import setup
from pathlib import Path
this_directory = Path(__file__).parent
long_description = (this_directory / "README.md").read_text()
setup(
name="mcaio",
version="0.2.1",
version="0.1",
url="https://git.orudo.ru/trueold89/mcaio",
author="trueold89",
author_email="trueold89@orudo.ru",
description="Asynс lib to get information about Minecraft server",
long_description=long_description,
long_description_content_type='text/markdown',
packages=["mcaio"],
entry_points={
"console_scripts": ["mcaio = mcaio.cli:main"]