Correctly waiting for server start; choosing interpreter (atclient)

This commit is contained in:
DarkCat09 2022-12-26 15:48:08 +04:00
parent 45b4a10e87
commit 69a8f83c9c
3 changed files with 36 additions and 5 deletions

View file

@ -8,7 +8,8 @@ import logging
import base64 import base64
from typing import List, Dict, Optional from typing import List, Dict
from typing import Optional, Type
import lxml.html import lxml.html
@ -17,6 +18,10 @@ from .atconnect import AternosConnect
from .aterrors import CredentialsError from .aterrors import CredentialsError
from .aterrors import TwoFactorAuthError from .aterrors import TwoFactorAuthError
from . import atjsparse
from .atjsparse import Interpreter
from .atjsparse import Js2PyInterpreter
class Client: class Client:
@ -54,6 +59,7 @@ class Client:
md5: str, md5: str,
code: Optional[int] = None, code: Optional[int] = None,
sessions_dir: str = '~', sessions_dir: str = '~',
js: Type[Interpreter] = Js2PyInterpreter,
**custom_args): **custom_args):
"""Log in to an Aternos account with """Log in to an Aternos account with
a username and a hashed password a username and a hashed password
@ -64,6 +70,9 @@ class Client:
code (Optional[int]): 2FA code code (Optional[int]): 2FA code
sessions_dir (str): Path to the directory sessions_dir (str): Path to the directory
where session will be automatically saved where session will be automatically saved
js (Type[Interpreter]): Preferred JS interpreter,
any class from `atjsparse`
inheriting `Interpreter` class
**custom_args (tuple, optional): Keyword arguments **custom_args (tuple, optional): Keyword arguments
which will be passed to CloudScraper `__init__` which will be passed to CloudScraper `__init__`
@ -83,6 +92,7 @@ class Client:
except (OSError, CredentialsError): except (OSError, CredentialsError):
pass pass
atjsparse.get_interpreter(create=js)
atconn = AternosConnect() atconn = AternosConnect()
if len(custom_args) > 0: if len(custom_args) > 0:
@ -129,6 +139,7 @@ class Client:
password: str, password: str,
code: Optional[int] = None, code: Optional[int] = None,
sessions_dir: str = '~', sessions_dir: str = '~',
js: Type[Interpreter] = Js2PyInterpreter,
**custom_args): **custom_args):
"""Log in to Aternos with a username and a plain password """Log in to Aternos with a username and a plain password
@ -138,6 +149,9 @@ class Client:
code (Optional[int]): 2FA code code (Optional[int]): 2FA code
sessions_dir (str): Path to the directory sessions_dir (str): Path to the directory
where session will be automatically saved where session will be automatically saved
js (Type[Interpreter]): Preferred JS interpreter,
any class from `atjsparse`
inheriting `Interpreter` class
**custom_args (tuple, optional): Keyword arguments **custom_args (tuple, optional): Keyword arguments
which will be passed to CloudScraper `__init__` which will be passed to CloudScraper `__init__`
""" """
@ -145,7 +159,8 @@ class Client:
md5 = Client.md5encode(password) md5 = Client.md5encode(password)
return cls.from_hashed( return cls.from_hashed(
username, md5, code, username, md5, code,
sessions_dir, **custom_args sessions_dir, js,
**custom_args
) )
@classmethod @classmethod
@ -153,15 +168,21 @@ class Client:
cls, cls,
session: str, session: str,
servers: Optional[List[str]] = None, servers: Optional[List[str]] = None,
js: Type[Interpreter] = Js2PyInterpreter,
**custom_args): **custom_args):
"""Log in to Aternos using a session cookie value """Log in to Aternos using a session cookie value
Args: Args:
session (str): Value of ATERNOS_SESSION cookie session (str): Value of ATERNOS_SESSION cookie
servers (Optional[List[str]]): List of cached servers IDs.
js (Type[Interpreter]): Preferred JS interpreter,
any class from `atjsparse`
inheriting `Interpreter` class
**custom_args (tuple, optional): Keyword arguments **custom_args (tuple, optional): Keyword arguments
which will be passed to CloudScraper `__init__` which will be passed to CloudScraper `__init__`
""" """
atjsparse.get_interpreter(create=js)
atconn = AternosConnect() atconn = AternosConnect()
atconn.add_args(**custom_args) atconn.add_args(**custom_args)
@ -176,12 +197,16 @@ class Client:
def restore_session( def restore_session(
cls, cls,
file: str = '~/.aternos', file: str = '~/.aternos',
js: Type[Interpreter] = Js2PyInterpreter,
**custom_args): **custom_args):
"""Log in to Aternos using """Log in to Aternos using
a saved ATERNOS_SESSION cookie a saved ATERNOS_SESSION cookie
Args: Args:
file (str, optional): File where a session cookie was saved file (str, optional): File where a session cookie was saved
js (Type[Interpreter]): Preferred JS interpreter,
any class from `atjsparse`
inheriting `Interpreter` class
**custom_args (tuple, optional): Keyword arguments **custom_args (tuple, optional): Keyword arguments
which will be passed to CloudScraper `__init__` which will be passed to CloudScraper `__init__`
""" """
@ -209,11 +234,13 @@ class Client:
obj = cls.from_session( obj = cls.from_session(
session=session, session=session,
servers=saved[1:], servers=saved[1:],
js=js,
**custom_args **custom_args
) )
else: else:
obj = cls.from_session( obj = cls.from_session(
session, session,
js=js,
**custom_args **custom_args
) )

View file

@ -5,7 +5,7 @@ import abc
import json import json
import base64 import base64
import time import logging
import subprocess import subprocess
from pathlib import Path from pathlib import Path
@ -89,9 +89,13 @@ class NodeInterpreter(Interpreter):
node, server_js, node, server_js,
f'{port}', host, f'{port}', host,
], ],
stdout=subprocess.PIPE,
) )
# pylint: enable=consider-using-with # pylint: enable=consider-using-with
time.sleep(0.1)
assert self.proc.stdout is not None
ok_msg = self.proc.stdout.readline()
logging.debug('Received from server.js: %s', ok_msg)
def exec_js(self, func: str) -> None: def exec_js(self, func: str) -> None:
resp = requests.post(self.url, data=func) resp = requests.post(self.url, data=func)

View file

@ -27,4 +27,4 @@ window = global
document = window.document || {} document = window.document || {}
const server = http.createServer(listener) const server = http.createServer(listener)
server.listen(port, host) server.listen(port, host, () => console.log('OK'))