refactor and close loop?"
This commit is contained in:
@ -7,14 +7,20 @@ from jsoner import json_read_sync
|
||||
class BaseSession:
|
||||
def __init__(self, base_dir: Path, errors_dir: Path, banned_dir: Path):
|
||||
self.base_dir = base_dir
|
||||
self.base_dir.mkdir(exist_ok=True)
|
||||
self.base_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.errors_dir = errors_dir
|
||||
self.errors_dir.mkdir(exist_ok=True)
|
||||
self.errors_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.banned_dir = banned_dir
|
||||
self.banned_dir.mkdir(exist_ok=True)
|
||||
self.banned_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.json_errors: set[Path] = set()
|
||||
|
||||
def iter_sessions(self) -> Generator:
|
||||
def iter_sessions(self) -> Generator[tuple[Path, Path, dict]]:
|
||||
"""
|
||||
Поиск сессий в base_dir
|
||||
Возвращает генератор с кортежами (item, json_path, json_data)
|
||||
Если json_file не найден, то добавляет его (не существующий) в self.json_errors
|
||||
Если json_data не найден, то добавляет его (не существующий) в self.json_errors
|
||||
"""
|
||||
for item in self.base_dir.glob("*.session"):
|
||||
json_file = item.with_suffix(".json")
|
||||
if not json_file.is_file():
|
||||
@ -24,3 +30,20 @@ class BaseSession:
|
||||
self.json_errors.add(json_file)
|
||||
continue
|
||||
yield item, json_file, json_data
|
||||
|
||||
def __del__(self):
|
||||
"""
|
||||
Удаление пустых директорий в errors_dir и banned_dir
|
||||
"""
|
||||
if self.errors_dir.is_dir():
|
||||
items = []
|
||||
for item in self.errors_dir.iterdir():
|
||||
items.append(item)
|
||||
if not items:
|
||||
self.errors_dir.rmdir()
|
||||
if self.banned_dir.is_dir():
|
||||
items = []
|
||||
for item in self.banned_dir.iterdir():
|
||||
items.append(item)
|
||||
if not items:
|
||||
self.banned_dir.rmdir()
|
||||
|
||||
@ -25,6 +25,10 @@ class JsonConverter(BaseSession):
|
||||
self.__json_write = json_write
|
||||
|
||||
def _main(self, item: Path, json_file: Path, json_data: dict) -> dict:
|
||||
"""
|
||||
Конвертация сессии в json (string_session)
|
||||
Возвращает json_data с добавленными proxy и string_session
|
||||
"""
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
client = TelegramClient(str(item), self.__api_id, self.__api_hash)
|
||||
@ -36,18 +40,30 @@ class JsonConverter(BaseSession):
|
||||
ss._port = client.session.port # type: ignore
|
||||
string_session = ss.save()
|
||||
del ss, client
|
||||
loop.close()
|
||||
json_data["proxy"] = self.__proxy
|
||||
json_data["string_session"] = string_session
|
||||
if self.__json_write:
|
||||
json_write_sync(json_file, json_data)
|
||||
return json_data
|
||||
|
||||
def iter(self) -> Generator:
|
||||
def iter(self) -> Generator[tuple[Path, Path, dict]]:
|
||||
"""
|
||||
Поиск сессий в base_dir + конвертация сессии в json (string_session)
|
||||
Возвращает генератор с кортежами (item, json_file, json_data)
|
||||
json_data содержит:
|
||||
string_session: str
|
||||
proxy: dict
|
||||
"""
|
||||
for item, json_file, json_data in self.iter_sessions():
|
||||
_json_data = self._main(item, json_file, json_data)
|
||||
yield item, json_file, _json_data
|
||||
|
||||
def main(self) -> int:
|
||||
"""
|
||||
Основная функция для записи сессий в json файлы
|
||||
Возвращает количество записанных сессий
|
||||
"""
|
||||
count = 0
|
||||
self.__json_write = True
|
||||
for item, json_file, json_data in self.iter_sessions():
|
||||
|
||||
Reference in New Issue
Block a user