153 lines
4.9 KiB
Python
153 lines
4.9 KiB
Python
import os
|
|
import logging
|
|
|
|
from shutil import move
|
|
from pathlib import Path
|
|
from countryflag import getflag
|
|
|
|
from checker import Result, Checker, SessionInvalid
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class Worker:
|
|
def __init__(
|
|
self,
|
|
input_path: str = "tdata",
|
|
output_path: str = "data",
|
|
temp_path: str = "temp",
|
|
):
|
|
self.input_path = Path(input_path).absolute()
|
|
self.output_path = Path(output_path).absolute()
|
|
self.temp_path = Path(temp_path).absolute()
|
|
|
|
self.valid_path = self.output_path.joinpath("valid")
|
|
self.invalid_path = self.output_path.joinpath("invalid")
|
|
|
|
self.all_path = self.valid_path.joinpath("all.txt")
|
|
|
|
async def __aenter__(self):
|
|
if not self.input_path.exists():
|
|
self.input_path.mkdir()
|
|
if not self.output_path.exists():
|
|
self.output_path.mkdir()
|
|
|
|
if not self.temp_path.exists():
|
|
self.temp_path.mkdir()
|
|
else:
|
|
self.__cleanup()
|
|
|
|
if not self.valid_path.exists():
|
|
self.valid_path.mkdir()
|
|
if not self.invalid_path.exists():
|
|
self.invalid_path.mkdir()
|
|
|
|
return self
|
|
|
|
async def __aexit__(self, exc_type, exc_val, exc_tb):
|
|
self.__cleanup()
|
|
if self.client:
|
|
logger.debug("Отключение клиента Telethon")
|
|
try:
|
|
await self.client.disconnect()
|
|
except:
|
|
pass
|
|
|
|
def __cleanup(self):
|
|
if self.temp_path.exists():
|
|
deleted_count = 0
|
|
for file in os.listdir(path=self.temp_path):
|
|
file_path = self.temp_path.joinpath(str(file))
|
|
try:
|
|
if file_path.isfile() and ".gitinclude" not in file_path:
|
|
file_path.unlink(missing_ok=True)
|
|
elif file_path.isdir():
|
|
file_path.rmdir()
|
|
deleted_count += 1
|
|
except Exception as e:
|
|
logger.warning(f"Не удалось удалить {file_path}: {e}")
|
|
|
|
if deleted_count > 0:
|
|
logger.debug(f"Удалено {deleted_count} временных файлов")
|
|
|
|
def __list_tdata(self):
|
|
tdatas = []
|
|
|
|
if self.input_path.exists():
|
|
for content in os.listdir(path=self.input_path):
|
|
joined = self.input_path.joinpath(content)
|
|
if joined.is_dir():
|
|
tdatas.append(joined.absolute())
|
|
|
|
return tdatas
|
|
|
|
def __template(self, result: Result):
|
|
return (
|
|
f"👤 [{getflag(countries=result.country)} {result.country}] {result.me.phone} — {result.me.id}\n"
|
|
f"⭐️ {result.stars}\n"
|
|
f"💎 Premium: {'true' if result.me.premium else 'false'}\n"
|
|
f"👥 {len(result.chats)}/{len(result.contacts)}\n"
|
|
f"🍀 {len(result.admin_channels)}"
|
|
)
|
|
|
|
def __append(self, content: str, path: Path):
|
|
with open(file=path, mode="+a", encoding="utf-8") as file:
|
|
file.write(content)
|
|
|
|
def __write(self, content: str, path: Path):
|
|
with open(file=path, mode="+w", encoding="utf-8") as file:
|
|
file.write(content)
|
|
|
|
def __save_valid(self, user_id: int, result: Result, tdata: Path):
|
|
id_path = self.output_path.joinpath(str(user_id))
|
|
if not id_path.absolute().exists():
|
|
id_path.mkdir()
|
|
|
|
content = self.__template(result=result)
|
|
id_txt_path = id_path.joinpath(f"{user_id}.txt")
|
|
|
|
# Append to all.txt
|
|
self.__append(content=content, path=self.all_path)
|
|
|
|
# Write to id.txt
|
|
self.__write(content=content, path=id_txt_path)
|
|
|
|
tdata_old_path = self.input_path.joinpath(tdata).absolute()
|
|
|
|
tdata_relative_id_path = id_path.joinpath(tdata)
|
|
tdata_new_path = self.valid_path.joinpath(tdata_relative_id_path).absolute()
|
|
|
|
# Move tdata
|
|
move(src=tdata_old_path, dst=tdata_new_path)
|
|
|
|
def __save_invalid(self, tdata: Path):
|
|
tdata_old_path = self.input_path.joinpath(tdata).absolute()
|
|
tdata_new_path = self.invalid_path.joinpath(tdata).absolute()
|
|
|
|
# Move tdata
|
|
move(src=tdata_old_path, dst=tdata_new_path)
|
|
|
|
async def check_one(self, tdata: str):
|
|
tdata_absolute = Path(tdata).absolute()
|
|
tdata_relative = Path(tdata).relative_to(self.input_path)
|
|
|
|
logger.info(f"Начало проверки TDATA: {tdata_absolute}")
|
|
try:
|
|
async with Checker(path=tdata_absolute) as checker:
|
|
result = await checker.check_all()
|
|
return
|
|
|
|
self.__save_valid(user_id=result.me.id, tdata=tdata_relative)
|
|
|
|
except SessionInvalid:
|
|
self.__save_invalid(tdata=tdata_relative)
|
|
|
|
except:
|
|
pass
|
|
|
|
async def check_all(self):
|
|
tdatas = self.__list_tdata()
|
|
|
|
for tdata in tdatas:
|
|
await self.check_one(tdata=tdata)
|