This repository has been archived on 2024-07-30. You can view files and clone it, but cannot push or open issues or pull requests.
python-aternos/python_aternos/atwss.py

256 lines
8.1 KiB
Python
Raw Normal View History

2022-07-01 13:28:39 +03:00
"""Connects to Aternos API websocket
for real-time information"""
2022-01-22 14:10:30 +03:00
import enum
import json
import asyncio
2022-04-06 10:44:14 +03:00
import logging
2022-01-22 14:10:30 +03:00
import websockets
2022-07-01 13:28:39 +03:00
from typing import Union, Any
from typing import Dict, Tuple
from typing import Callable, Coroutine
2022-03-25 15:45:38 +03:00
from typing import TYPE_CHECKING
2022-01-22 14:10:30 +03:00
from .atconnect import REQUA
2022-03-25 15:45:38 +03:00
if TYPE_CHECKING:
2022-06-23 14:13:56 +03:00
from .atserver import AternosServer
2022-07-01 13:28:39 +03:00
OneArgT = Callable[[Any], Coroutine[Any, Any, None]]
TwoArgT = Callable[[Any, Tuple[Any, ...]], Coroutine[Any, Any, None]]
FunctionT = Union[OneArgT, TwoArgT]
ArgsTuple = Tuple[FunctionT, Tuple[Any, ...]]
2022-01-22 14:10:30 +03:00
2022-04-06 10:44:14 +03:00
class Streams(enum.Enum):
2022-06-23 14:13:56 +03:00
"""WebSocket streams types"""
status = (0, None)
queue = (1, None)
console = (2, 'console')
ram = (3, 'heap')
tps = (4, 'tick')
2022-07-01 13:28:39 +03:00
none = (-1, None)
2022-06-23 14:13:56 +03:00
def __init__(self, num: int, stream: str) -> None:
self.num = num
self.stream = stream
2022-04-06 10:44:14 +03:00
2022-01-22 14:10:30 +03:00
class AternosWss:
2022-06-23 14:13:56 +03:00
"""Class for managing websocket connection
:param atserv: :class:`python_aternos.atserver.AternosServer` instance
:type atserv: python_aternos.atserver.AternosServer
:param autoconfirm: Automatically start server status listener
when AternosWss connects to API to confirm
server launching, defaults to `False`
:type autoconfirm: bool, optional
"""
2022-07-01 13:28:39 +03:00
def __init__(
self,
atserv: 'AternosServer',
autoconfirm: bool = False) -> None:
2022-06-23 14:13:56 +03:00
self.atserv = atserv
self.cookies = atserv.atconn.session.cookies
self.session = self.cookies['ATERNOS_SESSION']
self.servid = atserv.servid
2022-07-01 13:28:39 +03:00
recvtype = Dict[Streams, ArgsTuple]
self.recv: recvtype = {}
2022-06-23 14:13:56 +03:00
self.autoconfirm = autoconfirm
self.confirmed = False
2022-07-01 13:28:39 +03:00
self.socket: Any
self.keep: asyncio.Task
self.msgs: asyncio.Task
2022-06-23 14:13:56 +03:00
async def confirm(self) -> None:
"""Simple way to call AternosServer.confirm from this class"""
self.atserv.confirm()
2022-07-01 13:28:39 +03:00
def wssreceiver(
self,
stream: Streams,
*args: Any) -> Callable[[FunctionT], Any]:
2022-06-23 14:13:56 +03:00
"""Decorator that marks your function as a stream receiver.
When websocket receives message from the specified stream,
it calls all listeners created with this decorator.
:param stream: Stream that your function should listen
:type stream: python_aternos.atwss.Streams
:param args: Arguments which will be passed to your function
:type args: tuple, optional
:return: ...
:rtype: Callable[[Callable[[Any], Coroutine[Any, Any, None]]], Any]
"""
def decorator(func: FunctionT) -> None:
2022-06-23 14:13:56 +03:00
self.recv[stream] = (func, args)
return decorator
async def connect(self) -> None:
"""Connect to the websocket server and start all stream listeners"""
headers = [
('Host', 'aternos.org'),
('User-Agent', REQUA),
(
'Cookie',
f'ATERNOS_SESSION={self.session}; '
f'ATERNOS_SERVER={self.servid}'
)
]
2022-07-01 13:28:39 +03:00
self.socket = await websockets.connect( # type: ignore
2022-06-23 14:13:56 +03:00
'wss://aternos.org/hermes/',
origin='https://aternos.org',
extra_headers=headers
)
@self.wssreceiver(Streams.status)
async def confirmfunc(msg):
"""Automatically confirm Minecraft server launching"""
if not self.autoconfirm:
return
in_queue = (msg['class'] == 'queueing')
pending = (msg['queue']['pending'] == 'pending')
confirmation = in_queue and pending
if confirmation and not self.confirmed:
self.confirm()
@self.wssreceiver(Streams.status)
async def streamsfunc(msg):
"""Automatically starts streams. Detailed description:
According to the websocket messages from the web site,
Aternos can't receive any data from a stream (e.g. console) until
it requests this stream via the special message
to the websocket server: `{"stream":"console","type":"start"}`
on which the server responses with: `{"type":"connected"}`
Also, there are RAM (used heap) and TPS (ticks per second)
streams that must be enabled before trying to get information.
Enabling the stream for listening the server status is not needed,
these data is sent from API by default, so there's None value in
the second item of its stream type tuple
(`<Streams.status: (0, None)>`).
https://github.com/DarkCat09/python-aternos/issues/22#issuecomment-1146788496
"""
if msg['status'] == 2:
# Automatically start streams
for strm in self.recv:
if not isinstance(strm, Streams):
continue
if strm.stream:
logging.debug(f'Enabling {strm.stream} stream')
await self.send({
'stream': strm.stream,
'type': 'start'
})
await self.wssworker()
async def close(self) -> None:
"""Closes websocket connection and stops all listeners"""
self.keep.cancel()
self.msgs.cancel()
await self.socket.close()
del self.socket
async def send(self, obj: Union[Dict[str, Any], str]) -> None:
"""Sends a message to websocket server
:param obj: Message, may be a string or a dict
:type obj: Union[Dict[str, Any],str]
"""
if isinstance(obj, dict):
obj = json.dumps(obj)
await self.socket.send(obj)
async def wssworker(self) -> None:
"""Starts async tasks in background
for receiving websocket messages
and sending keepalive ping"""
self.keep = asyncio.create_task(self.keepalive())
self.msgs = asyncio.create_task(self.receiver())
async def keepalive(self) -> None:
"""Each 49 seconds sends keepalive ping to websocket server"""
try:
while True:
await asyncio.sleep(49)
await self.socket.send('{"type":"\u2764"}')
except asyncio.CancelledError:
pass
async def receiver(self) -> None:
"""Receives messages from websocket servers
and calls user's streams listeners"""
2022-07-01 13:28:39 +03:00
while True:
try:
2022-06-23 14:13:56 +03:00
data = await self.socket.recv()
obj = json.loads(data)
2022-07-01 13:28:39 +03:00
msgtype = Streams.none
2022-06-23 14:13:56 +03:00
if obj['type'] == 'line':
msgtype = Streams.console
msg = obj['data'].strip('\r\n ')
elif obj['type'] == 'heap':
msgtype = Streams.ram
msg = int(obj['data']['usage'])
elif obj['type'] == 'tick':
msgtype = Streams.tps
ticks = 1000 / obj['data']['averageTickTime']
msg = 20 if ticks > 20 else ticks
elif obj['type'] == 'status':
msgtype = Streams.status
msg = json.loads(obj['message'])
if msgtype in self.recv:
2022-07-01 13:28:39 +03:00
# function info tuple
func: ArgsTuple = self.recv[msgtype]
2022-06-23 14:13:56 +03:00
# if arguments is not empty
if func[1]:
# call the function with args
2022-07-01 13:28:39 +03:00
coro = func[0](msg, func[1]) # type: ignore
2022-06-23 14:13:56 +03:00
else:
2022-07-01 13:28:39 +03:00
# mypy error: Too few arguments
# looks like bug, so it is ignored
coro = func[0](msg) # type: ignore
2022-06-23 14:13:56 +03:00
# run
2022-07-01 13:28:39 +03:00
asyncio.create_task(coro)
2022-06-23 14:13:56 +03:00
2022-07-01 13:28:39 +03:00
except asyncio.CancelledError:
break