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

141 lines
3 KiB
Python
Raw Normal View History

import re
import json
2021-10-08 19:35:20 +04:00
import lxml.html
from requests import Response
from typing import Optional, Dict
2021-10-08 19:35:20 +04:00
from . import atconnect
2021-10-08 19:35:20 +04:00
from . import aterrors
from . import atfm
2021-10-08 19:35:20 +04:00
class AternosServer:
def __init__(self, servid:str, atconn:atconnect.AternosConnect) -> None:
2021-10-08 19:35:20 +04:00
self.servid = servid
self.atconn = atconn
servreq = self.atserver_request(
'https://aternos.org/server',
atconnect.REQGET
2021-10-08 19:35:20 +04:00
)
servtree = lxml.html.fromstring(servreq.content)
self._info = json.loads(
re.search(
r'var\s*lastStatus\s*=\s*({.*})',
servtree.head.text
)[1]
2021-10-08 19:35:20 +04:00
)
self.atconn.parse_token(servreq.content)
2021-10-08 19:35:20 +04:00
self.atconn.generate_sec()
def start(self, accepteula:bool=True) -> None:
2021-10-08 19:35:20 +04:00
startreq = self.atserver_request(
'https://aternos.org/panel/ajax/start.php',
atconnect.REQGET, sendtoken=True
2021-10-08 19:35:20 +04:00
)
startresult = startreq.json()
if startresult['success']:
return
error = startresult['error']
if error == 'eula' and accepteula:
self.eula()
self.start(accepteula=False)
2021-10-08 19:35:20 +04:00
elif error == 'eula':
raise aterrors.AternosServerStartError(
'EULA was not accepted. Use start(accepteula=True)'
)
elif error == 'already':
raise aterrors.AternosServerStartError(
'Server is already running'
)
else:
raise aterrors.AternosServerStartError(
f'Unable to start server. Code: {error}'
)
def confirm(self) -> None:
2021-10-08 19:35:20 +04:00
self.atserver_request(
'https://aternos.org/panel/ajax/confirm.php',
atconnect.REQGET, 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',
atconnect.REQGET, 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',
atconnect.REQGET, 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',
atconnect.REQGET, 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',
atconnect.REQGET, sendtoken=True
2021-10-08 19:35:20 +04:00
)
def files(self) -> atfm.AternosFileManager:
2021-10-08 19:35:20 +04:00
return atfm.AternosFileManager(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 info(self) -> dict:
return self._info
@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['displayAddress']
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 software(self) -> str:
return self._info['software']
2021-10-08 19:35:20 +04:00
@property
def version(self) -> str:
return self._info['version']