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

343 lines
8.5 KiB
Python
Raw Normal View History

import enum
import json
from requests import Response
from typing import Optional, List
2021-10-08 18:35:20 +03:00
2022-03-18 17:38:36 +03:00
from .atconnect import AternosConnect
from .aterrors import ServerError
2022-06-16 14:40:10 +03:00
from .aterrors import ServerEulaError
from .aterrors import ServerRunningError
from .aterrors import ServerSoftwareError
from .aterrors import ServerStorageError
2022-03-18 17:38:36 +03:00
from .atfm import FileManager
from .atconf import AternosConfig
from .atplayers import PlayersList
2022-06-16 14:40:10 +03:00
from .atplayers import Lists
2022-01-22 14:10:30 +03:00
from .atwss import AternosWss
2022-03-18 17:38:36 +03:00
class Edition(enum.IntEnum):
2022-06-16 14:40:10 +03:00
"""Server edition type enum"""
2022-03-18 17:38:36 +03:00
java = 0
bedrock = 1
class Status(enum.IntEnum):
2022-06-16 14:40:10 +03:00
"""Server numeric status enum.
It is highly recommended to use
`AternosServer.status` instead of
`AternosServer.status_num`"""
off = 0
on = 1
2022-04-06 10:44:14 +03:00
starting = 2
shutdown = 3
2022-01-22 14:10:30 +03:00
unknown = 6
error = 7
2022-01-22 14:10:30 +03:00
confirm = 10
2021-10-08 18:35:20 +03:00
class AternosServer:
2022-06-16 14:40:10 +03:00
"""Class for controlling your Aternos Minecraft server
:param servid: Unique server IDentifier
:type servid: str
:param atconn: :class:`python_aternos.atconnect.AternosConnect`
instance with initialized Aternos session
:type atconn: python_aternos.atconnect.AternosConnect
:param reqinfo: Automatically call AternosServer.fetch() to get all info, defaults to `True`
:type reqinfo: bool, optional
"""
def __init__(
self, servid:str,
atconn:AternosConnect,
reqinfo:bool=True) -> None:
2021-10-08 18:35:20 +03:00
self.servid = servid
self.atconn = atconn
if reqinfo:
self.fetch()
2022-03-25 15:45:38 +03:00
def fetch(self) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Send a request to Aternos API to get all server info"""
2021-10-08 18:35:20 +03:00
servreq = self.atserver_request(
2022-03-25 15:45:38 +03:00
'https://aternos.org/panel/ajax/status.php',
'GET', sendtoken=True
2021-10-08 18:35:20 +03:00
)
2022-03-25 15:45:38 +03:00
self._info = json.loads(servreq.content)
2021-10-08 18:35:20 +03:00
2022-03-25 15:45:38 +03:00
def wss(self, autoconfirm:bool=False) -> AternosWss:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Returns :class:`python_aternos.atwss.AternosWss` instance for listening server streams in real-time
:param autoconfirm: Automatically start server status listener
when AternosWss connects to API to confirm server launching, defaults to `False`
:type autoconfirm: bool, optional
:return: :class:`python_aternos.atwss.AternosWss` object
:rtype: python_aternos.atwss.AternosWss
"""
2022-03-25 15:45:38 +03:00
return AternosWss(self, autoconfirm)
def start(self, headstart:bool=False, accepteula:bool=True) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Starts a server
:param headstart: Start a server in the headstart mode which allows you to skip all queue, defaults to `False`
:type headstart: bool, optional
:param accepteula: Automatically accept the Mojang EULA, defaults to `True`
:type accepteula: bool, optional
:raises ServerEulaError: When trying to start a server
without accepting the Mojang EULA
:raises ServerRunningError: When trying to start a server
which is alreday running
:raises ServerSoftwareError: When Aternos notifies about
incorrect software version
:raises ServerStorageError: When Aternos notifies about
voilation of storage limits (4 GB for now)
:raises ServerError: When API is unable to start a Minecraft server
due to unavailability of Aternos' file servers or other problems
"""
2021-10-08 18:35:20 +03:00
startreq = self.atserver_request(
'https://aternos.org/panel/ajax/start.php',
2022-03-18 17:38:36 +03:00
'GET', params={'headstart': int(headstart)},
sendtoken=True
2021-10-08 18:35:20 +03:00
)
startresult = startreq.json()
if startresult['success']:
return
error = startresult['error']
2021-10-08 18:35:20 +03:00
if error == 'eula' and accepteula:
self.eula()
self.start(accepteula=False)
2021-10-08 18:35:20 +03:00
elif error == 'eula':
2022-06-16 14:40:10 +03:00
raise ServerEulaError(
2021-10-08 18:35:20 +03:00
'EULA was not accepted. Use start(accepteula=True)'
)
2021-10-08 18:35:20 +03:00
elif error == 'already':
2022-06-16 14:40:10 +03:00
raise ServerRunningError(
2021-10-08 18:35:20 +03:00
'Server is already running'
)
elif error == 'wrongversion':
2022-06-16 14:40:10 +03:00
raise ServerSoftwareError(
'Incorrect software version installed'
)
elif error == 'file':
2022-03-18 17:38:36 +03:00
raise ServerError(
2022-03-25 15:45:38 +03:00
'File server is unavailbale, view https://status.aternos.gmbh'
)
elif error == 'size':
2022-06-16 14:40:10 +03:00
raise ServerStorageError(
f'Available storage size is 4GB, ' + \
f'your server used: {startresult["size"]}'
)
2021-10-08 18:35:20 +03:00
else:
2022-03-18 17:38:36 +03:00
raise ServerError(
f'Unable to start server, code: {error}'
2021-10-08 18:35:20 +03:00
)
def confirm(self) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Confirms server launching"""
2021-10-08 18:35:20 +03:00
self.atserver_request(
'https://aternos.org/panel/ajax/confirm.php',
2022-03-18 17:38:36 +03:00
'GET', sendtoken=True
2021-10-08 18:35:20 +03:00
)
def stop(self) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Stops the server"""
2021-10-08 18:35:20 +03:00
self.atserver_request(
'https://aternos.org/panel/ajax/stop.php',
2022-03-18 17:38:36 +03:00
'GET', sendtoken=True
2021-10-08 18:35:20 +03:00
)
def cancel(self) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Cancels server launching"""
2021-10-08 18:35:20 +03:00
self.atserver_request(
'https://aternos.org/panel/ajax/cancel.php',
2022-03-18 17:38:36 +03:00
'GET', sendtoken=True
2021-10-08 18:35:20 +03:00
)
def restart(self) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Restarts the server"""
2021-10-08 18:35:20 +03:00
self.atserver_request(
'https://aternos.org/panel/ajax/restart.php',
2022-03-18 17:38:36 +03:00
'GET', sendtoken=True
2021-10-08 18:35:20 +03:00
)
def eula(self) -> None:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Accepts the Mojang EULA"""
2021-10-08 18:35:20 +03:00
self.atserver_request(
'https://aternos.org/panel/ajax/eula.php',
2022-03-18 17:38:36 +03:00
'GET', sendtoken=True
2021-10-08 18:35:20 +03:00
)
2022-03-18 17:38:36 +03:00
def files(self) -> FileManager:
2021-10-08 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Returns :class:`python_aternos.atfm.FileManager`
instance for file operations
:return: :class:`python_aternos.atfm.FileManager` object
:rtype: python_aternos.atfm.FileManager
"""
2022-03-18 17:38:36 +03:00
return FileManager(self)
2021-10-08 18:35:20 +03:00
2022-03-18 17:38:36 +03:00
def config(self) -> AternosConfig:
2022-06-16 14:40:10 +03:00
"""Returns :class:`python_aternos.atconf.AternosConfig`
instance for changing server settings
:return: :class:`python_aternos.atconf.AternosConfig` object
:rtype: python_aternos.atconf.AternosConfig
"""
2022-03-18 17:38:36 +03:00
return AternosConfig(self)
2022-06-16 14:40:10 +03:00
def players(self, lst:Lists) -> PlayersList:
"""Returns :class:`python_aternos.atplayers.PlayersList`
instance for managing operators, whitelist or banned players list
:param lst: Players list type, must be
the :class:`python_aternos.atplayers.Lists` enum value
:type lst: python_aternos.atplayers.Lists
:return: :class:`python_aternos.atplayers.PlayersList`
:rtype: python_aternos.atplayers.PlayersList
"""
2022-03-18 17:38:36 +03:00
return PlayersList(lst, self)
2021-10-08 18:35:20 +03: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 18:35:20 +03:00
2022-06-16 14:40:10 +03:00
"""Sends a request to Aternos API
with server IDenitfier parameter
:param url: Request URL
:type url: str
:param method: Request method, must be GET or POST
:type method: str
:param params: URL parameters, defaults to an empty dictionary
:type params: dict, optional
:param data: POST request data. If the method is set to GET,
it will be combined with params. Defaults to an empty dictionary
:type data: dict, optional
:param headers: Custom headers, defaults to an empty dictionary
:type headers: dict, optional
:param sendtoken: Send ajax token in params
:type sendtoken: bool
:return: API response
:rtype: requests.Response
"""
2021-10-08 18:35:20 +03: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 17:38:36 +03: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 17:38:36 +03:00
'POST', data={'motd': value},
2021-11-01 17:04:19 +03:00
sendtoken=True
)
@property
def address(self) -> str:
return self._info['displayAddress']
2021-10-08 18:35:20 +03:00
@property
def domain(self) -> str:
return self._info['ip']
2021-10-08 18:35:20 +03:00
@property
def port(self) -> int:
return self._info['port']
2021-10-08 18:35:20 +03:00
@property
def edition(self) -> int:
soft_type = self._info['bedrock']
return int(soft_type)
2021-10-08 18:35:20 +03:00
@property
def software(self) -> str:
return self._info['software']
2021-10-08 18:35:20 +03:00
@property
def version(self) -> str:
return self._info['version']
2022-03-25 15:45:38 +03:00
@property
def status(self) -> str:
return self._info['class']
@property
2022-03-25 15:45:38 +03:00
def status_num(self) -> int:
return int(self._info['status'])
@property
def players_list(self) -> List[str]:
return self._info['playerlist']
@property
def players_count(self) -> int:
return int(self._info['players'])
@property
def slots(self) -> int:
return int(self._info['slots'])
@property
def ram(self) -> int:
2022-03-25 15:45:38 +03:00
return int(self._info['ram'])