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

83 lines
2 KiB
Python
Raw Normal View History

2021-10-08 18:35:20 +03:00
import hashlib
import lxml.html
2022-03-17 09:24:58 +03:00
from typing import List
2022-03-17 09:24:58 +03:00
from .atserver import AternosServer
from .atconnect import AternosConnect
2022-03-18 17:38:36 +03:00
from .aterrors import CredentialsError
__all__ = ['Client', 'atconf', 'atconnect', 'aterrors', 'atfile', 'atfm', 'atjsparse', 'atplayers', 'atserver', 'atwss']
2021-10-08 18:35:20 +03:00
class Client:
2022-01-06 18:57:26 +03:00
def __init__(self, atconn:atconnect.AternosConnect) -> None:
2021-10-08 18:35:20 +03:00
2022-01-06 18:57:26 +03:00
self.atconn = atconn
2021-10-08 18:35:20 +03:00
2022-01-06 18:57:26 +03:00
@classmethod
def from_hashed(cls, username:str, md5:str):
2021-10-08 18:35:20 +03:00
2022-03-17 09:24:58 +03:00
atconn = AternosConnect()
atconn.parse_token()
atconn.generate_sec()
credentials = {
'user': username,
'password': md5
}
2021-10-08 18:35:20 +03:00
loginreq = atconn.request_cloudflare(
2022-01-06 18:57:26 +03:00
f'https://aternos.org/panel/ajax/account/login.php',
2022-03-25 15:45:38 +03:00
'POST', data=credentials,
2022-01-06 18:57:26 +03:00
sendtoken=True
2021-10-08 18:35:20 +03:00
)
if loginreq.cookies.get('ATERNOS_SESSION', None) == None:
2022-03-18 17:38:36 +03:00
raise CredentialsError(
2021-10-08 18:35:20 +03:00
'Check your username and password'
)
return cls(atconn)
2022-01-06 18:57:26 +03:00
@classmethod
def from_credentials(cls, username:str, password:str):
pswd_bytes = password.encode('utf-8')
md5 = hashlib.md5(pswd_bytes).hexdigest().lower()
return cls.from_hashed(username, md5)
2022-01-06 18:57:26 +03:00
@classmethod
def from_session(cls, session:str):
2022-03-17 09:24:58 +03:00
atconn = AternosConnect()
2022-03-25 15:45:38 +03:00
atconn.session.cookies['ATERNOS_SESSION'] = session
atconn.parse_token()
atconn.generate_sec()
return cls(atconn)
@staticmethod
def google() -> str:
2022-03-17 09:24:58 +03:00
atconn = AternosConnect()
auth = atconn.request_cloudflare(
'https://aternos.org/auth/google-login',
2022-03-25 15:45:38 +03:00
'GET', redirect=False
)
return auth.headers['Location']
2022-01-06 18:57:26 +03:00
2022-03-18 17:38:36 +03:00
def list_servers(self) -> List[atserver.AternosServer]:
2021-10-08 18:35:20 +03:00
serverspage = self.atconn.request_cloudflare(
2022-03-25 15:45:38 +03:00
'https://aternos.org/servers/', 'GET'
2021-10-08 18:35:20 +03:00
)
serverstree = lxml.html.fromstring(serverspage.content)
2021-11-01 17:04:19 +03:00
serverslist = serverstree.xpath('//div[contains(@class,"servers ")]/div')
2021-10-08 18:35:20 +03:00
servers = []
for server in serverslist:
servid = server.xpath('./div[@class="server-body"]/@data-id')[0]
2022-03-17 09:24:58 +03:00
servers.append(AternosServer(servid, self.atconn))
2021-10-08 18:35:20 +03:00
return servers