lastStatus object parsing from the server page

This commit is contained in:
DarkCat09 2023-05-24 18:09:37 +04:00
parent 9d3de01395
commit 1e4fdd2e89
2 changed files with 26 additions and 11 deletions

View file

@ -1,14 +1,16 @@
"""Aternos Minecraft server""" """Aternos Minecraft server"""
import enum import re
import json import json
import enum
from typing import Optional from typing import Optional
from typing import List, Dict, Any from typing import List, Dict, Any
import requests import requests
from .atconnect import AJAX_URL from .atconnect import BASE_URL, AJAX_URL
from .atconnect import AternosConnect from .atconnect import AternosConnect
from .atwss import AternosWss from .atwss import AternosWss
@ -18,9 +20,15 @@ from .atplayers import Lists
from .atfm import FileManager from .atfm import FileManager
from .atconf import AternosConfig from .atconf import AternosConfig
from .aterrors import AternosError
from .aterrors import ServerStartError from .aterrors import ServerStartError
status_re = re.compile(
r'<script>\s*var lastStatus\s*?=\s*?(\{.+?\});?\s*<\/script>'
)
class Edition(enum.IntEnum): class Edition(enum.IntEnum):
"""Server edition type enum (java, bedrock)""" """Server edition type enum (java, bedrock)"""
@ -56,7 +64,7 @@ class AternosServer:
def __init__( def __init__(
self, servid: str, self, servid: str,
atconn: AternosConnect, atconn: AternosConnect,
reqinfo: bool = True) -> None: reqinfo: bool = False) -> None:
"""Class for controlling your Aternos Minecraft server """Class for controlling your Aternos Minecraft server
Args: Args:
@ -69,17 +77,24 @@ class AternosServer:
self.servid = servid self.servid = servid
self.atconn = atconn self.atconn = atconn
if reqinfo: if reqinfo:
self.fetch() self.fetch()
def fetch(self) -> None: def fetch(self) -> None:
"""Send a request to Aternos API to get all server info""" """Get all server info"""
servreq = self.atserver_request( page = self.atserver_request(
f'{AJAX_URL}/status.php', f'{BASE_URL}/server', 'GET'
'GET', sendtoken=True
) )
self._info = json.loads(servreq.content) with open('server.html', 'wt') as f:
f.write(page.text)
match = status_re.search(page.text)
if match is None:
raise AternosError('Unable to parse lastStatus object')
self._info = json.loads(match[1])
def wss(self, autoconfirm: bool = False) -> AternosWss: def wss(self, autoconfirm: bool = False) -> AternosWss:
"""Returns AternosWss instance for """Returns AternosWss instance for

View file

@ -26,13 +26,13 @@ class TestLogin(unittest.TestCase):
def test_auth(self) -> None: def test_auth(self) -> None:
self.at = Client.from_hashed(self.user, self.pswd) self.at = Client.from_credentials(self.user, self.pswd)
self.assertIsNotNone(self.at) self.assertIsNotNone(self.at)
def test_servers(self) -> None: def test_servers(self) -> None:
if self.at is None: if self.at is None:
self.at = Client.from_hashed( self.at = Client.from_credentials(
self.user, self.pswd self.user, self.pswd
) )
@ -46,7 +46,7 @@ class TestLogin(unittest.TestCase):
def test_logout(self) -> None: def test_logout(self) -> None:
if self.at is None: if self.at is None:
self.at = Client.from_hashed( self.at = Client.from_credentials(
self.user, self.pswd self.user, self.pswd
) )