This commit is contained in:
2025-05-11 20:36:48 +03:00
parent 1292c69e36
commit 521e2a5266

View File

@ -5,6 +5,10 @@ from typing import Self
from telethon import TelegramClient from telethon import TelegramClient
from telethon.sessions import StringSession 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 .consts import API_PACKS
from .exceptions import ThonBannedError from .exceptions import ThonBannedError
@ -13,6 +17,7 @@ from .exceptions import ThonBannedError
class BaseData: class BaseData:
def __init__(self, json_data: dict, raise_error: bool): def __init__(self, json_data: dict, raise_error: bool):
self.__json_data, self.__raise_error = json_data, raise_error self.__json_data, self.__raise_error = json_data, raise_error
self._logger = logging.getLogger("basethon")
@property @property
def json_data(self) -> dict: def json_data(self) -> dict:
@ -112,21 +117,24 @@ class BaseData:
return {} return {}
return proxy return proxy
@property
def tz_offset(self) -> int | None:
if tz_offset := self.json_data.get("tz_offset"):
return tz_offset
class BaseThon(BaseData): class BaseThon(BaseData):
def __init__( def __init__(
self, self,
item: Path | None, item: Path | None,
json_data: dict, json_data: dict,
retries: int = 50, retries: int = 10,
timeout: int = 10, timeout: int = 10,
debug: bool = False,
raise_error: bool = True, raise_error: bool = True,
): ):
self.__item, self.__retries, self.__timeout = item, retries, timeout self.__item, self.__retries, self.__timeout = item, retries, timeout
super().__init__(json_data, raise_error) super().__init__(json_data, raise_error)
self.__client = self.__get_client() self.__client = self.__get_client()
self.__debug = debug
@property @property
def client(self) -> TelegramClient: def client(self) -> TelegramClient:
@ -147,15 +155,30 @@ class BaseThon(BaseData):
proxy=self.proxy, proxy=self.proxy,
timeout=self.__timeout, 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") client._init_request.lang_pack = API_PACKS.get(self.app_id, "android")
return client 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: async def check(self) -> str:
try: try:
await self.client.connect() await self.client.connect()
if not await self.client.is_user_authorized(): if not await self.client.is_user_authorized():
return "ERROR_AUTH:BAN_ERROR" return "ERROR_AUTH:BAN_ERROR"
await self.get_additional_data()
with contextlib.suppress(Exception):
await self.client(UpdateStatusRequest(offline=False))
return "OK" return "OK"
except ConnectionError: except ConnectionError:
await self.disconnect() await self.disconnect()
@ -165,11 +188,12 @@ class BaseThon(BaseData):
return "ERROR_AUTH:BAN_ERROR" return "ERROR_AUTH:BAN_ERROR"
except Exception as e: except Exception as e:
await self.disconnect() await self.disconnect()
if self.__debug: self._logger.exception(e)
logging.exception(e)
return f"ERROR_AUTH:{e}" return f"ERROR_AUTH:{e}"
async def disconnect(self): async def disconnect(self):
with contextlib.suppress(Exception):
await self.client(UpdateStatusRequest(offline=True))
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
await self.client.disconnect() # type: ignore await self.client.disconnect() # type: ignore