2024-01-31 19:37:01 +10:00
|
|
|
import asyncio
|
|
|
|
|
|
|
|
from loguru import logger
|
2024-03-13 18:48:52 +10:00
|
|
|
from app.extensions import db
|
2024-01-31 19:37:01 +10:00
|
|
|
from paper.client import PaperClient
|
|
|
|
from paper.errors import IgnoreException, NeedPasswordException, UserPrivacyException
|
|
|
|
|
|
|
|
|
|
|
|
class PaperParser:
|
|
|
|
def __init__(self, session: str) -> None:
|
|
|
|
if not session:
|
|
|
|
raise ValueError("Session name can't be None")
|
|
|
|
|
|
|
|
self.client = PaperClient(session)
|
|
|
|
|
2024-03-08 16:39:04 +10:00
|
|
|
async def invite_users(self, users, group, task):
|
2024-01-31 19:37:01 +10:00
|
|
|
await self.client.invite_self(group)
|
|
|
|
|
|
|
|
group_participants = await self.client.get_participants(group)
|
|
|
|
|
|
|
|
if group_participants:
|
|
|
|
participants_usernames = [participant.username for participant in group_participants if participant.username]
|
2024-03-13 16:49:37 +10:00
|
|
|
users = filter(lambda user: user.username not in participants_usernames and not user.delete, users)
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
for user in users:
|
|
|
|
try:
|
|
|
|
if task.is_aborted():
|
|
|
|
return self.users_to_delete
|
|
|
|
|
|
|
|
await self.client.invite_user(user, group)
|
|
|
|
|
2024-03-08 16:39:04 +10:00
|
|
|
except (UserPrivacyException, IgnoreException):
|
2024-03-13 16:49:37 +10:00
|
|
|
logger.warning(f"Exception occurred. Marking {user.username} for deletion...")
|
|
|
|
user.delete = True
|
2024-03-13 18:48:52 +10:00
|
|
|
db.session.commit()
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
finally:
|
|
|
|
if not task.is_aborted():
|
2024-02-08 17:59:17 +10:00
|
|
|
await asyncio.sleep(70) # FIXME: Change to config value
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
async def send_messages(self, users, message: str, file: str | None = None, task = None):
|
|
|
|
# TODO: Filter users, that already get this message
|
|
|
|
# for dialog in await self.client.get_dialogs():
|
|
|
|
# if dialog.is_user:
|
|
|
|
# messages = tuple(filter(lambda chat_message: message.text == chat_message.text, await self.client.get_messages(dialog)))
|
|
|
|
# print(messages)
|
2024-03-08 16:39:04 +10:00
|
|
|
if not task:
|
|
|
|
return
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
for user in users:
|
|
|
|
try:
|
|
|
|
if task.is_aborted():
|
|
|
|
return self.users_to_delete
|
|
|
|
|
|
|
|
await self.client.send_message(user, message, file)
|
|
|
|
|
2024-03-08 16:39:04 +10:00
|
|
|
except (UserPrivacyException, IgnoreException):
|
2024-03-13 18:48:52 +10:00
|
|
|
logger.warning(f"Exception occurred. Marking {user.username} for deletion...")
|
|
|
|
user.delete = True
|
|
|
|
db.session.commit()
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
finally:
|
|
|
|
if not task.is_aborted():
|
2024-02-08 17:59:17 +10:00
|
|
|
await asyncio.sleep(70) # FIXME: Change to config value
|
2024-01-31 19:37:01 +10:00
|
|
|
|
|
|
|
async def get_participants(self, group):
|
|
|
|
await self.client.invite_self(group)
|
|
|
|
|
|
|
|
return await self.client.get_participants(group)
|
|
|
|
|
|
|
|
async def sign_in(self, phone: str, password: str | None = None, code: str | None = None, phone_hash: str | None = None, **kwargs):
|
|
|
|
if not await self.client.is_user_authorized():
|
|
|
|
try:
|
|
|
|
return await self.client.sign_in(phone=phone, code=code, phone_code_hash=phone_hash) #type: ignore
|
|
|
|
except:
|
|
|
|
if not password:
|
|
|
|
raise NeedPasswordException()
|
|
|
|
|
|
|
|
return await self.client.sign_in(password=password)
|
|
|
|
|
|
|
|
async def __aenter__(self, *args, **kwargs):
|
|
|
|
await self.client.connect()
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def __aexit__(self, *args, **kwargs):
|
2024-03-08 16:39:04 +10:00
|
|
|
await self.client.disconnect()
|