diff --git a/basethon/base_thon.py b/basethon/base_thon.py index bdfb763..1aff158 100644 --- a/basethon/base_thon.py +++ b/basethon/base_thon.py @@ -5,6 +5,10 @@ from typing import Self from telethon import TelegramClient from telethon.sessions import StringSession +from telethon.tl.functions.account import UpdateStatusRequest +from telethon.tl.functions.help import GetCountriesListRequest, GetNearestDcRequest +from telethon.tl.functions.langpack import GetLangPackRequest +from telethon.types import JsonNumber, JsonObject, JsonObjectValue from .consts import API_PACKS from .exceptions import ThonBannedError @@ -13,6 +17,7 @@ from .exceptions import ThonBannedError class BaseData: def __init__(self, json_data: dict, raise_error: bool): self.__json_data, self.__raise_error = json_data, raise_error + self._logger = logging.getLogger("basethon") @property def json_data(self) -> dict: @@ -112,21 +117,24 @@ class BaseData: return {} return proxy + @property + def tz_offset(self) -> int | None: + if tz_offset := self.json_data.get("tz_offset"): + return tz_offset + class BaseThon(BaseData): def __init__( self, item: Path | None, json_data: dict, - retries: int = 50, + retries: int = 10, timeout: int = 10, - debug: bool = False, raise_error: bool = True, ): self.__item, self.__retries, self.__timeout = item, retries, timeout super().__init__(json_data, raise_error) self.__client = self.__get_client() - self.__debug = debug @property def client(self) -> TelegramClient: @@ -147,15 +155,30 @@ class BaseThon(BaseData): proxy=self.proxy, timeout=self.__timeout, ) + if self.tz_offset: + tz_offset = JsonObjectValue("tz_offset", JsonNumber(self.tz_offset)) + client._init_request.params = JsonObject([tz_offset]) client._init_request.lang_pack = API_PACKS.get(self.app_id, "android") return client + async def get_additional_data(self): + lang_pack = API_PACKS.get(self.app_id, "") + with contextlib.suppress(Exception): + await self.client(GetLangPackRequest(lang_pack, self.lang_pack)) + with contextlib.suppress(Exception): + await self.client(GetNearestDcRequest()) + with contextlib.suppress(Exception): + await self.client(GetCountriesListRequest(self.lang_pack, 0)) + async def check(self) -> str: try: await self.client.connect() if not await self.client.is_user_authorized(): return "ERROR_AUTH:BAN_ERROR" + await self.get_additional_data() + with contextlib.suppress(Exception): + await self.client(UpdateStatusRequest(offline=False)) return "OK" except ConnectionError: await self.disconnect() @@ -165,11 +188,12 @@ class BaseThon(BaseData): return "ERROR_AUTH:BAN_ERROR" except Exception as e: await self.disconnect() - if self.__debug: - logging.exception(e) + self._logger.exception(e) return f"ERROR_AUTH:{e}" async def disconnect(self): + with contextlib.suppress(Exception): + await self.client(UpdateStatusRequest(offline=True)) with contextlib.suppress(Exception): await self.client.disconnect() # type: ignore