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

201 lines
5.4 KiB
Python
Raw Normal View History

2022-07-01 14:28:39 +04:00
"""Entry point. Authorizes on Aternos
and allows to manage your account"""
2022-06-16 15:40:10 +04:00
import os
import re
2023-08-08 10:57:41 +04:00
from typing import Optional
2022-06-16 15:40:10 +04:00
2023-08-08 10:57:41 +04:00
from .atselenium import SeleniumHelper, Remote
from .atconnect import AJAX_URL
2023-05-24 17:41:33 +04:00
2023-08-08 10:57:41 +04:00
from .atlog import log, is_debug, set_debug
2022-06-16 15:40:10 +04:00
from .aterrors import CredentialsError
2022-06-23 15:13:56 +04:00
2022-06-16 15:40:10 +04:00
class Client:
2022-09-23 16:53:47 +04:00
"""Aternos API Client class, object
of which contains user's auth data"""
2022-06-23 15:13:56 +04:00
2023-08-08 10:57:41 +04:00
def __init__(self, driver: Remote) -> None:
self.se = SeleniumHelper(driver)
# Config
self.sessions_dir = '~'
# ###
self.saved_session = '~/.aternos' # will be rewritten by login()
2023-08-08 10:57:41 +04:00
# self.atconn = AternosConnect()
# self.account = AternosAccount(self)
def login(
self,
username: str,
password: str,
code: Optional[int] = None) -> None:
"""Log in to your Aternos account
with a username and a plain password
Args:
username (str): Username
password (str): Plain-text password
code (Optional[int], optional): 2FA code
"""
2023-08-08 10:57:41 +04:00
self.se.load_page('/go')
2022-06-23 15:13:56 +04:00
2023-08-08 10:57:41 +04:00
user_input = self.se.find_by_id('user')
user_input.clear()
user_input.send_keys(username)
2023-08-08 10:57:41 +04:00
pswd_input = self.se.find_by_id('password')
pswd_input.clear()
pswd_input.send_keys(password)
2023-08-08 10:57:41 +04:00
err_msg = self.se.find_by_class('login-error')
totp_input = self.se.find_by_id('twofactor-code')
2022-06-23 15:13:56 +04:00
2023-08-08 10:57:41 +04:00
def logged_in_or_error(driver: Remote):
return \
driver.current_url.find('/servers') != -1 or \
err_msg.is_displayed() or \
totp_input.is_displayed()
2022-09-23 17:21:17 +04:00
2023-08-08 10:57:41 +04:00
self.se.exec_js('login()')
self.se.wait.until(logged_in_or_error)
2022-06-23 15:13:56 +04:00
2023-08-08 10:57:41 +04:00
print(self.se.driver.get_cookie('ATERNOS_SESSION'))
2023-05-29 12:02:45 +04:00
def login_with_session(self, session: str) -> None:
"""Log in using ATERNOS_SESSION cookie
Args:
session (str): Session cookie value
"""
2023-08-08 10:57:41 +04:00
self.se.driver.add_cookie({
'name': 'ATERNOS_SESSION',
'value': session,
})
def logout(self) -> None:
"""Log out from the Aternos account"""
2022-06-23 15:13:56 +04:00
self.atconn.request_cloudflare(
f'{AJAX_URL}/account/logout',
'GET', sendtoken=True,
)
2022-06-23 15:13:56 +04:00
self.remove_session(self.saved_session)
2022-06-23 15:13:56 +04:00
def restore_session(self, file: str = '~/.aternos') -> None:
"""Restores ATERNOS_SESSION cookie and,
if included, servers list, from a session file
2022-06-23 15:13:56 +04:00
Args:
file (str, optional): Filename
Raises:
FileNotFoundError: If the file cannot be found
CredentialsError: If the session cookie
(or the file at all) has incorrect format
2022-06-23 15:13:56 +04:00
"""
file = os.path.expanduser(file)
log.debug('Restoring session from %s', file)
if not os.path.exists(file):
raise FileNotFoundError()
with open(file, 'rt', encoding='utf-8') as f:
saved = f.read() \
.strip() \
.replace('\r\n', '\n') \
.split('\n')
session = saved[0].strip()
if session == '' or not session.isalnum():
raise CredentialsError(
'Session cookie is invalid or the file is empty'
)
if len(saved) > 1:
self.account.refresh_servers(saved[1:])
self.atconn.session.cookies['ATERNOS_SESSION'] = session
self.saved_session = file
def save_session(
self,
file: str = '~/.aternos',
incl_servers: bool = True) -> None:
2022-06-23 15:13:56 +04:00
"""Saves an ATERNOS_SESSION cookie to a file
Args:
file (str, optional): File where a session cookie must be saved
incl_servers (bool, optional): If the function
should include the servers IDs in this file
to reduce API requests count on the next restoration
(recommended)
2022-06-23 15:13:56 +04:00
"""
file = os.path.expanduser(file)
log.debug('Saving session to %s', file)
with open(file, 'wt', encoding='utf-8') as f:
2022-06-23 15:13:56 +04:00
f.write(self.atconn.atsession + '\n')
if not incl_servers:
return
for s in self.account.servers:
f.write(s.servid + '\n')
def remove_session(self, file: str = '~/.aternos') -> None:
"""Removes a file which contains
ATERNOS_SESSION cookie saved
with `save_session()`
Args:
file (str, optional): Filename
"""
file = os.path.expanduser(file)
log.debug('Removing session file: %s', file)
try:
os.remove(file)
except OSError as err:
log.warning('Unable to delete session file: %s', err)
@staticmethod
def session_filename(username: str, sessions_dir: str = '~') -> str:
"""Generates a session file name
Args:
username (str): Authenticated user
sessions_dir (str, optional): Path to directory
with automatically saved sessions
2022-06-16 15:40:10 +04:00
Returns:
Filename
2022-09-29 19:17:38 +04:00
"""
# unsafe symbols replacement
repl = '_'
2022-09-23 17:21:17 +04:00
secure = re.sub(
r'[^A-Za-z0-9_-]',
repl, username,
2022-09-23 17:21:17 +04:00
)
return f'{sessions_dir}/.at_{secure}'
@property
def debug(self) -> bool:
return is_debug()
@debug.setter
def debug(self, state: bool) -> None:
return set_debug(state)