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,
|
|
|
|
md5:Optional[str]=None,
|
|
|
|
password: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()
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
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}',
|
2021-10-14 17:56:01 +04:00
|
|
|
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'
|
|
|
|
)
|
|
|
|
|
2021-10-14 17:56:01 +04:00
|
|
|
@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/',
|
2021-10-14 17:56:01 +04:00
|
|
|
atconnect.REQGET
|
2021-10-08 19:35:20 +04:00
|
|
|
)
|
|
|
|
serverstree = lxml.html.fromstring(serverspage.content)
|
|
|
|
serverslist = serverstree.xpath('//div[@class="servers"]/div')
|
|
|
|
|
|
|
|
servers = []
|
|
|
|
for server in serverslist:
|
|
|
|
servid = server.xpath('./div[@class="server-body"]/@data-id')[0]
|
|
|
|
servers.append(atserver.AternosServer(servid, self.atconn))
|
|
|
|
|
|
|
|
return servers
|