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/atserver.py

223 lines
4.5 KiB
Python
Raw Normal View History

import enum
import json
from requests import Response
2022-03-18 18:38:36 +04:00
from typing import Optional
2021-10-08 19:35:20 +04:00
2022-03-18 18:38:36 +04:00
from .atconnect import AternosConnect
from .aterrors import ServerError
from .atfm import FileManager
from .atconf import AternosConfig
from .atplayers import PlayersList
2022-01-22 15:10:30 +04:00
from .atwss import AternosWss
2022-03-18 18:38:36 +04:00
class Edition(enum.IntEnum):
java = 0
bedrock = 1
class Status(enum.IntEnum):
off = 0
on = 1
2022-04-06 11:44:14 +04:00
starting = 2
shutdown = 3
2022-01-22 15:10:30 +04:00
unknown = 6
error = 7
2022-01-22 15:10:30 +04:00
confirm = 10
2021-10-08 19:35:20 +04:00
class AternosServer:
def __init__(
self, servid:str,
atconn:AternosConnect,
reqinfo:bool=True) -> None:
2021-10-08 19:35:20 +04:00
self.servid = servid
self.atconn = atconn
if reqinfo:
self.fetch()
2022-03-25 16:45:38 +04:00
def fetch(self) -> None:
2021-10-08 19:35:20 +04:00
servreq = self.atserver_request(
2022-03-25 16:45:38 +04:00
'https://aternos.org/panel/ajax/status.php',
'GET', sendtoken=True
2021-10-08 19:35:20 +04:00
)
2022-03-25 16:45:38 +04:00
self._info = json.loads(servreq.content)
2021-10-08 19:35:20 +04:00
2022-03-25 16:45:38 +04:00
def wss(self, autoconfirm:bool=False) -> AternosWss:
2021-10-08 19:35:20 +04:00
2022-03-25 16:45:38 +04:00
return AternosWss(self, autoconfirm)
def start(self, headstart:bool=False, accepteula:bool=True) -> None:
2021-10-08 19:35:20 +04:00
startreq = self.atserver_request(
'https://aternos.org/panel/ajax/start.php',
2022-03-18 18:38:36 +04:00
'GET', params={'headstart': int(headstart)},
sendtoken=True
2021-10-08 19:35:20 +04:00
)
startresult = startreq.json()
if startresult['success']:
return
error = startresult['error']
2021-10-08 19:35:20 +04:00
if error == 'eula' and accepteula:
self.eula()
self.start(accepteula=False)
2021-10-08 19:35:20 +04:00
elif error == 'eula':
2022-03-18 18:38:36 +04:00
raise ServerError(
2021-10-08 19:35:20 +04:00
'EULA was not accepted. Use start(accepteula=True)'
)
2021-10-08 19:35:20 +04:00
elif error == 'already':
2022-03-18 18:38:36 +04:00
raise ServerError(
2021-10-08 19:35:20 +04:00
'Server is already running'
)
elif error == 'wrongversion':
2022-03-18 18:38:36 +04:00
raise ServerError(
'Incorrect software version installed'
)
elif error == 'file':
2022-03-18 18:38:36 +04:00
raise ServerError(
2022-03-25 16:45:38 +04:00
'File server is unavailbale, view https://status.aternos.gmbh'
)
elif error == 'size':
2022-03-18 18:38:36 +04:00
raise ServerError(
f'Available storage size is 4GB, ' + \
f'your server used: {startresult["size"]}'
)
2021-10-08 19:35:20 +04:00
else:
2022-03-18 18:38:36 +04:00
raise ServerError(
f'Unable to start server, code: {error}'
2021-10-08 19:35:20 +04:00
)
def confirm(self) -> None:
2021-10-08 19:35:20 +04:00
self.atserver_request(
'https://aternos.org/panel/ajax/confirm.php',
2022-03-18 18:38:36 +04:00
'GET', sendtoken=True
2021-10-08 19:35:20 +04:00
)
def stop(self) -> None:
2021-10-08 19:35:20 +04:00
self.atserver_request(
'https://aternos.org/panel/ajax/stop.php',
2022-03-18 18:38:36 +04:00
'GET', sendtoken=True
2021-10-08 19:35:20 +04:00
)
def cancel(self) -> None:
2021-10-08 19:35:20 +04:00
self.atserver_request(
'https://aternos.org/panel/ajax/cancel.php',
2022-03-18 18:38:36 +04:00
'GET', sendtoken=True
2021-10-08 19:35:20 +04:00
)
def restart(self) -> None:
2021-10-08 19:35:20 +04:00
self.atserver_request(
'https://aternos.org/panel/ajax/restart.php',
2022-03-18 18:38:36 +04:00
'GET', sendtoken=True
2021-10-08 19:35:20 +04:00
)
def eula(self) -> None:
2021-10-08 19:35:20 +04:00
self.atserver_request(
'https://aternos.org/panel/ajax/eula.php',
2022-03-18 18:38:36 +04:00
'GET', sendtoken=True
2021-10-08 19:35:20 +04:00
)
2022-03-18 18:38:36 +04:00
def files(self) -> FileManager:
2021-10-08 19:35:20 +04:00
2022-03-18 18:38:36 +04:00
return FileManager(self)
2021-10-08 19:35:20 +04:00
2022-03-18 18:38:36 +04:00
def config(self) -> AternosConfig:
2022-03-18 18:38:36 +04:00
return AternosConfig(self)
2022-03-25 16:45:38 +04:00
def players(self, lst:str) -> PlayersList:
2022-03-18 18:38:36 +04:00
return PlayersList(lst, self)
2021-10-08 19:35:20 +04:00
def atserver_request(
self, url:str, method:int,
params:Optional[dict]=None,
data:Optional[dict]=None,
headers:Optional[dict]=None,
sendtoken:bool=False) -> Response:
2021-10-08 19:35:20 +04:00
return self.atconn.request_cloudflare(
url=url, method=method,
params=params, data=data,
headers=headers,
reqcookies={
'ATERNOS_SERVER': self.servid
},
sendtoken=sendtoken
)
@property
def subdomain(self) -> str:
atdomain = self.domain
return atdomain[:atdomain.find('.')]
@subdomain.setter
def subdomain(self, value:str) -> None:
self.atserver_request(
'https://aternos.org/panel/ajax/options/subdomain.php',
2022-03-18 18:38:36 +04:00
'GET', params={'subdomain': value},
sendtoken=True
)
@property
def motd(self) -> str:
return self._info['motd']
@motd.setter
def motd(self, value:str) -> None:
self.atserver_request(
'https://aternos.org/panel/ajax/options/motd.php',
2022-03-18 18:38:36 +04:00
'POST', data={'motd': value},
2021-11-01 18:04:19 +04:00
sendtoken=True
)
@property
def address(self) -> str:
return f'{self.domain}:{self.port}'
2021-10-08 19:35:20 +04:00
@property
def domain(self) -> str:
return self._info['ip']
2021-10-08 19:35:20 +04:00
@property
def port(self) -> int:
return self._info['port']
2021-10-08 19:35:20 +04:00
@property
def edition(self) -> int:
soft_type = self._info['bedrock']
return int(soft_type)
2021-10-08 19:35:20 +04:00
@property
def software(self) -> str:
return self._info['software']
2021-10-08 19:35:20 +04:00
@property
def version(self) -> str:
return self._info['version']
2022-03-25 16:45:38 +04:00
@property
def status(self) -> str:
return self._info['class']
@property
2022-03-25 16:45:38 +04:00
def status_num(self) -> int:
return int(self._info['status'])
@property
def ram(self) -> int:
2022-03-25 16:45:38 +04:00
return int(self._info['ram'])