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

63 lines
1.6 KiB
Python
Raw Normal View History

2021-10-08 19:35:20 +04:00
import hashlib
import lxml.html
2021-10-14 18:41:57 +04:00
from typing import Optional, List
2021-10-08 19:35:20 +04:00
from . import atserver
from . import atconnect
from . import aterrors
class Client:
2021-10-14 18:41:57 +04:00
def __init__(
self, username:str,
2021-11-01 18:04:19 +04:00
password:Optional[str]=None,
md5:Optional[str]=None) -> None:
2021-10-08 19:35:20 +04:00
if (password == None) and (md5 == None):
raise AttributeError('Password was not specified')
if (password != None):
self.__init__(
username,
md5=hashlib.md5(password.encode('utf-8'))\
.hexdigest().lower()
)
return
self.atconn = atconnect.AternosConnect()
self.token = self.atconn.parse_token()
2021-10-08 19:35:20 +04:00
self.sec = self.atconn.generate_sec()
self.credentials = {
'user': username,
'password': md5
}
loginreq = self.atconn.request_cloudflare(
f'https://aternos.org/panel/ajax/account/login.php?' + \
f'SEC={self.sec}&TOKEN={self.token}',
atconnect.REQPOST, data=self.credentials
2021-10-08 19:35:20 +04:00
)
if loginreq.cookies.get('ATERNOS_SESSION', None) == None:
raise aterrors.AternosCredentialsError(
'Check your username and password'
)
@property
2021-10-14 18:41:57 +04:00
def servers(self) -> List[atserver.AternosServer]:
2021-10-08 19:35:20 +04:00
serverspage = self.atconn.request_cloudflare(
'https://aternos.org/servers/',
atconnect.REQGET
2021-10-08 19:35:20 +04:00
)
serverstree = lxml.html.fromstring(serverspage.content)
2021-11-01 18:04:19 +04:00
serverslist = serverstree.xpath('//div[contains(@class,"servers ")]/div')
2021-10-08 19:35:20 +04:00
servers = []
for server in serverslist:
servid = server.xpath('./div[@class="server-body"]/@data-id')[0]
servers.append(atserver.AternosServer(servid, self.atconn))
return servers