BASE_URL constants

This commit is contained in:
DarkCat09 2023-05-24 17:41:33 +04:00
parent 345ba7e6ed
commit 9d3de01395
9 changed files with 71 additions and 52 deletions

View file

@ -13,8 +13,10 @@ from typing import Optional, Type
import lxml.html import lxml.html
from .atserver import AternosServer
from .atconnect import AternosConnect from .atconnect import AternosConnect
from .atconnect import BASE_URL, AJAX_URL
from .atserver import AternosServer
from .aterrors import CredentialsError from .aterrors import CredentialsError
from .aterrors import TwoFactorAuthError from .aterrors import TwoFactorAuthError
@ -110,7 +112,7 @@ class Client:
credentials['code'] = str(code) credentials['code'] = str(code)
loginreq = atconn.request_cloudflare( loginreq = atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/login.php', f'{AJAX_URL}/account/login.php',
'POST', data=credentials, sendtoken=True 'POST', data=credentials, sendtoken=True
) )
@ -343,7 +345,7 @@ class Client:
return self.servers return self.servers
serverspage = self.atconn.request_cloudflare( serverspage = self.atconn.request_cloudflare(
'https://aternos.org/servers/', 'GET' f'{BASE_URL}/servers/', 'GET'
) )
serverstree = lxml.html.fromstring(serverspage.content) serverstree = lxml.html.fromstring(serverspage.content)
@ -396,7 +398,7 @@ class Client:
"""Log out from Aternos account""" """Log out from Aternos account"""
self.atconn.request_cloudflare( self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/logout.php', f'{AJAX_URL}/account/logout.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
@ -410,7 +412,7 @@ class Client:
""" """
self.atconn.request_cloudflare( self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/username.php', f'{AJAX_URL}/account/username.php',
'POST', data={'username': value}, sendtoken=True 'POST', data={'username': value}, sendtoken=True
) )
@ -432,7 +434,7 @@ class Client:
raise ValueError('Invalid e-mail!') raise ValueError('Invalid e-mail!')
self.atconn.request_cloudflare( self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/email.php', f'{AJAX_URL}/account/email.php',
'POST', data={'email': value}, sendtoken=True 'POST', data={'email': value}, sendtoken=True
) )
@ -460,7 +462,7 @@ class Client:
""" """
self.atconn.request_cloudflare( self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/password.php', f'{AJAX_URL}/account/password.php',
'POST', data={ 'POST', data={
'oldpassword': old, 'oldpassword': old,
'newpassword': new, 'newpassword': new,
@ -472,7 +474,7 @@ class Client:
a QR code for enabling 2FA""" a QR code for enabling 2FA"""
return self.atconn.request_cloudflare( return self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/secret.php', f'{AJAX_URL}/account/secret.php',
'GET', sendtoken=True 'GET', sendtoken=True
).json() ).json()
@ -499,7 +501,7 @@ class Client:
""" """
self.atconn.request_cloudflare( self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/twofactor.php', f'{AJAX_URL}/account/twofactor.php',
'POST', data={ 'POST', data={
'code': code 'code': code
}, sendtoken=True }, sendtoken=True
@ -513,7 +515,7 @@ class Client:
""" """
self.atconn.request_cloudflare( self.atconn.request_cloudflare(
'https://aternos.org/panel/ajax/account/disbaleTwofactor.php', f'{AJAX_URL}/account/disbaleTwofactor.php',
'POST', data={ 'POST', data={
'code': code 'code': code
}, sendtoken=True }, sendtoken=True

View file

@ -1,5 +1,7 @@
"""Modifying server and world options""" """Modifying server and world options"""
# TODO: Still needs refactoring
import enum import enum
import re import re
@ -8,9 +10,11 @@ from typing import TYPE_CHECKING
import lxml.html import lxml.html
from .atconnect import BASE_URL, AJAX_URL
if TYPE_CHECKING: if TYPE_CHECKING:
from .atserver import AternosServer from .atserver import AternosServer
DAT_PREFIX = 'Data:' DAT_PREFIX = 'Data:'
DAT_GR_PREFIX = 'Data:GameRules:' DAT_GR_PREFIX = 'Data:GameRules:'
@ -111,11 +115,12 @@ class Difficulty(enum.IntEnum):
# checking timezone format # checking timezone format
tzcheck = re.compile(r'(^[A-Z]\w+\/[A-Z]\w+$)|^UTC$') tzcheck = re.compile(r'(^[A-Z]\w+\/[A-Z]\w+$)|^UTC$')
# options types converting # options types converting
convert = { convert = {
'config-option-number': int, 'config-option-number': int,
'config-option-select': int, 'config-option-select': int,
'config-option-toggle': bool 'config-option-toggle': bool,
} }
@ -141,7 +146,7 @@ class AternosConfig:
""" """
optreq = self.atserv.atserver_request( optreq = self.atserv.atserver_request(
'https://aternos.org/options', 'GET' f'{BASE_URL}/options', 'GET'
) )
opttree = lxml.html.fromstring(optreq) opttree = lxml.html.fromstring(optreq)
@ -169,7 +174,7 @@ class AternosConfig:
) )
self.atserv.atserver_request( self.atserv.atserver_request(
'https://aternos.org/panel/ajax/timezone.php', f'{AJAX_URL}/timezone.php',
'POST', data={'timezone': value}, 'POST', data={'timezone': value},
sendtoken=True sendtoken=True
) )
@ -182,7 +187,7 @@ class AternosConfig:
""" """
optreq = self.atserv.atserver_request( optreq = self.atserv.atserver_request(
'https://aternos.org/options', 'GET' f'{BASE_URL}/options', 'GET'
) )
opttree = lxml.html.fromstring(optreq) opttree = lxml.html.fromstring(optreq)
imgopt = opttree.xpath( imgopt = opttree.xpath(
@ -203,7 +208,7 @@ class AternosConfig:
""" """
self.atserv.atserver_request( self.atserv.atserver_request(
'https://aternos.org/panel/ajax/image.php', f'{AJAX_URL}/image.php',
'POST', data={'image': f'openjdk:{value}'}, 'POST', data={'image': f'openjdk:{value}'},
sendtoken=True sendtoken=True
) )
@ -238,7 +243,7 @@ class AternosConfig:
`server.properties` dictionary `server.properties` dictionary
""" """
return self.__get_all_props('https://aternos.org/options', proptyping) return self.__get_all_props(f'{BASE_URL}/options', proptyping)
def set_server_props(self, props: Dict[str, Any]) -> None: def set_server_props(self, props: Dict[str, Any]) -> None:
"""Updates server.properties options with the given dict """Updates server.properties options with the given dict
@ -296,7 +301,7 @@ class AternosConfig:
""" """
return self.__get_all_props( return self.__get_all_props(
f'https://aternos.org/files/{world}/level.dat', f'{BASE_URL}/files/{world}/level.dat',
proptyping, [DAT_PREFIX, DAT_GR_PREFIX] proptyping, [DAT_PREFIX, DAT_GR_PREFIX]
) )
@ -327,7 +332,7 @@ class AternosConfig:
def __set_prop(self, file: str, option: str, value: Any) -> None: def __set_prop(self, file: str, option: str, value: Any) -> None:
self.atserv.atserver_request( self.atserv.atserver_request(
'https://aternos.org/panel/ajax/config.php', f'{AJAX_URL}/config.php',
'POST', data={ 'POST', data={
'file': file, 'file': file,
'option': option, 'option': option,

View file

@ -1,4 +1,4 @@
"""Stores API connection session and sends requests""" """Stores API session and sends requests"""
import re import re
import time import time
@ -22,6 +22,10 @@ from .aterrors import TokenError
from .aterrors import CloudflareError from .aterrors import CloudflareError
from .aterrors import AternosPermissionError from .aterrors import AternosPermissionError
BASE_URL = 'https://aternos.org'
AJAX_URL = f'{BASE_URL}/ajax'
REQUA = \ REQUA = \
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' \ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ' \
'(KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47' '(KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36 OPR/85.0.4341.47'
@ -94,7 +98,7 @@ class AternosConnect:
""" """
loginpage = self.request_cloudflare( loginpage = self.request_cloudflare(
'https://aternos.org/go/', 'GET' f'{BASE_URL}/go/', 'GET'
).content ).content
# Using the standard string methods # Using the standard string methods

View file

@ -7,6 +7,7 @@ from typing import TYPE_CHECKING
import lxml.html import lxml.html
from .atconnect import BASE_URL, AJAX_URL
from .aterrors import FileError from .aterrors import FileError
if TYPE_CHECKING: if TYPE_CHECKING:
@ -87,7 +88,7 @@ class AternosFile:
name = name.strip().replace('/', '_') name = name.strip().replace('/', '_')
req = self.atserv.atserver_request( req = self.atserv.atserver_request(
'https://aternos.org/panel/ajax/files/create.php', f'{AJAX_URL}/files/create.php',
'POST', data={ 'POST', data={
'file': f'{self._path}/{name}', 'file': f'{self._path}/{name}',
'type': 'file' 'type': 'file'
@ -114,7 +115,7 @@ class AternosFile:
) )
req = self.atserv.atserver_request( req = self.atserv.atserver_request(
'https://aternos.org/panel/ajax/delete.php', f'{AJAX_URL}/delete.php',
'POST', data={'file': self._path}, 'POST', data={'file': self._path},
sendtoken=True sendtoken=True
) )
@ -140,7 +141,7 @@ class AternosFile:
) )
file = self.atserv.atserver_request( file = self.atserv.atserver_request(
'https://aternos.org/panel/ajax/files/download.php', f'{AJAX_URL}/files/download.php',
'GET', params={ 'GET', params={
'file': self._path 'file': self._path
} }
@ -165,7 +166,7 @@ class AternosFile:
""" """
req = self.atserv.atserver_request( req = self.atserv.atserver_request(
'https://aternos.org/panel/ajax/save.php', f'{AJAX_URL}/save.php',
'POST', data={ 'POST', data={
'file': self._path, 'file': self._path,
'content': value 'content': value
@ -200,7 +201,7 @@ class AternosFile:
filepath = self._path.lstrip("/") filepath = self._path.lstrip("/")
editor = self.atserv.atserver_request( editor = self.atserv.atserver_request(
f'https://aternos.org/files/{filepath}', 'GET' f'{BASE_URL}/files/{filepath}', 'GET'
) )
edittree = lxml.html.fromstring(editor.content) edittree = lxml.html.fromstring(editor.content)
editblock = edittree.xpath('//div[@id="editor"]') editblock = edittree.xpath('//div[@id="editor"]')

View file

@ -5,7 +5,9 @@ from typing import TYPE_CHECKING
import lxml.html import lxml.html
from .atconnect import BASE_URL, AJAX_URL
from .atfile import AternosFile, FileType from .atfile import AternosFile, FileType
if TYPE_CHECKING: if TYPE_CHECKING:
from .atserver import AternosServer from .atserver import AternosServer
@ -41,7 +43,7 @@ class FileManager:
path = path.lstrip('/') path = path.lstrip('/')
filesreq = self.atserv.atserver_request( filesreq = self.atserv.atserver_request(
f'https://aternos.org/files/{path}', 'GET' f'{BASE_URL}/files/{path}', 'GET'
) )
filestree = lxml.html.fromstring(filesreq.content) filestree = lxml.html.fromstring(filesreq.content)
@ -163,7 +165,7 @@ class FileManager:
""" """
file = self.atserv.atserver_request( # type: ignore file = self.atserv.atserver_request( # type: ignore
'https://aternos.org/panel/ajax/files/download.php' f'{AJAX_URL}/files/download.php'
'GET', params={ 'GET', params={
'file': path.replace('/', '%2F') 'file': path.replace('/', '%2F')
} }
@ -183,7 +185,7 @@ class FileManager:
""" """
resp = self.atserv.atserver_request( # type: ignore resp = self.atserv.atserver_request( # type: ignore
'https://aternos.org/panel/ajax/worlds/download.php' f'{AJAX_URL}/worlds/download.php'
'GET', params={ 'GET', params={
'world': world.replace('/', '%2F') 'world': world.replace('/', '%2F')
} }

View file

@ -7,6 +7,7 @@ from typing import TYPE_CHECKING
import lxml.html import lxml.html
from .atconnect import BASE_URL, AJAX_URL
if TYPE_CHECKING: if TYPE_CHECKING:
from .atserver import AternosServer from .atserver import AternosServer
@ -73,7 +74,7 @@ class PlayersList:
return self.players return self.players
listreq = self.atserv.atserver_request( listreq = self.atserv.atserver_request(
f'https://aternos.org/players/{self.lst.value}', f'{BASE_URL}/players/{self.lst.value}',
'GET' 'GET'
) )
listtree = lxml.html.fromstring(listreq.content) listtree = lxml.html.fromstring(listreq.content)
@ -98,7 +99,7 @@ class PlayersList:
""" """
self.atserv.atserver_request( self.atserv.atserver_request(
'https://aternos.org/panel/ajax/players/add.php', f'{AJAX_URL}/players/add.php',
'POST', data={ 'POST', data={
'list': self.lst.value, 'list': self.lst.value,
'name': name 'name': name
@ -115,7 +116,7 @@ class PlayersList:
""" """
self.atserv.atserver_request( self.atserv.atserver_request(
'https://aternos.org/panel/ajax/players/remove.php', f'{AJAX_URL}/players/remove.php',
'POST', data={ 'POST', data={
'list': self.lst.value, 'list': self.lst.value,
'name': name 'name': name

View file

@ -8,13 +8,17 @@ from typing import List, Dict, Any
import requests import requests
from .atconnect import AJAX_URL
from .atconnect import AternosConnect from .atconnect import AternosConnect
from .aterrors import ServerStartError from .atwss import AternosWss
from .atfm import FileManager
from .atconf import AternosConfig
from .atplayers import PlayersList from .atplayers import PlayersList
from .atplayers import Lists from .atplayers import Lists
from .atwss import AternosWss
from .atfm import FileManager
from .atconf import AternosConfig
from .aterrors import ServerStartError
class Edition(enum.IntEnum): class Edition(enum.IntEnum):
@ -72,7 +76,7 @@ class AternosServer:
"""Send a request to Aternos API to get all server info""" """Send a request to Aternos API to get all server info"""
servreq = self.atserver_request( servreq = self.atserver_request(
'https://aternos.org/panel/ajax/status.php', f'{AJAX_URL}/status.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
self._info = json.loads(servreq.content) self._info = json.loads(servreq.content)
@ -112,7 +116,7 @@ class AternosServer:
""" """
startreq = self.atserver_request( startreq = self.atserver_request(
'https://aternos.org/panel/ajax/start.php', f'{AJAX_URL}/start.php',
'GET', params={'headstart': int(headstart)}, 'GET', params={'headstart': int(headstart)},
sendtoken=True sendtoken=True
) )
@ -134,7 +138,7 @@ class AternosServer:
"""Confirms server launching""" """Confirms server launching"""
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/confirm.php', f'{AJAX_URL}/confirm.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
@ -142,7 +146,7 @@ class AternosServer:
"""Stops the server""" """Stops the server"""
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/stop.php', f'{AJAX_URL}/stop.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
@ -150,7 +154,7 @@ class AternosServer:
"""Cancels server launching""" """Cancels server launching"""
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/cancel.php', f'{AJAX_URL}/cancel.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
@ -158,7 +162,7 @@ class AternosServer:
"""Restarts the server""" """Restarts the server"""
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/restart.php', f'{AJAX_URL}/restart.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
@ -166,7 +170,7 @@ class AternosServer:
"""Accepts the Mojang EULA""" """Accepts the Mojang EULA"""
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/eula.php', f'{AJAX_URL}/eula.php',
'GET', sendtoken=True 'GET', sendtoken=True
) )
@ -259,7 +263,7 @@ class AternosServer:
""" """
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/options/subdomain.php', f'{AJAX_URL}/options/subdomain.php',
'GET', params={'subdomain': value}, 'GET', params={'subdomain': value},
sendtoken=True sendtoken=True
) )
@ -285,7 +289,7 @@ class AternosServer:
""" """
self.atserver_request( self.atserver_request(
'https://aternos.org/panel/ajax/options/motd.php', f'{AJAX_URL}/options/motd.php',
'POST', data={'motd': value}, 'POST', data={'motd': value},
sendtoken=True sendtoken=True
) )

View file

@ -39,7 +39,7 @@ errored = []
def get_code() -> bool: def get_code() -> bool:
r = conn.request_cloudflare( r = conn.request_cloudflare(
'https://aternos.org/go', 'GET' f'{BASE_URL}/go', 'GET'
) )
if r.status_code != 200: if r.status_code != 200:
print(r.status_code) print(r.status_code)

View file

@ -6,27 +6,27 @@ mock = Mocker()
with mock: with mock:
mock.get( mock.get(
'https://aternos.org/go/', f'{BASE_URL}/go/',
content=files.read_html('aternos_go'), content=files.read_html('aternos_go'),
) )
mock.get( mock.get(
'https://aternos.org/servers/', f'{BASE_URL}/servers/',
content=files.read_html('aternos_servers'), content=files.read_html('aternos_servers'),
) )
mock.get( mock.get(
'https://aternos.org/server/', f'{BASE_URL}/server/',
content=files.read_html('aternos_server1'), content=files.read_html('aternos_server1'),
) )
mock.get( mock.get(
'https://aternos.org/panel/ajax/status.php', f'{AJAX_URL}/status.php',
content=files.read_html('aternos_status'), content=files.read_html('aternos_status'),
) )
mock.post( mock.post(
'https://aternos.org/panel/ajax/account/login.php', f'{AJAX_URL}/account/login.php',
json={ json={
'success': True, 'success': True,
'error': None, 'error': None,
@ -39,11 +39,11 @@ with mock:
) )
mock.get( mock.get(
'https://aternos.org/players/', f'{BASE_URL}/players/',
content=files.read_html('aternos_players'), content=files.read_html('aternos_players'),
) )
mock.get( mock.get(
'https://aternos.org/files/', f'{BASE_URL}/files/',
content=files.read_html('aternos_file_root'), content=files.read_html('aternos_file_root'),
) )