Compare commits
4 Commits
cd95928843
...
557d19ea0d
Author | SHA1 | Date |
---|---|---|
Анатолий Богомолов | 557d19ea0d | |
Анатолий Богомолов | 81f97ee3d1 | |
Анатолий Богомолов | 94302d09e9 | |
Анатолий Богомолов | 8dce3b1f62 |
|
@ -2,18 +2,18 @@ from app.extensions import db
|
||||||
from app.models.task import Task
|
from app.models.task import Task
|
||||||
|
|
||||||
def success_state(task_id):
|
def success_state(task_id):
|
||||||
task = Task.query.filter_by(task_id=task_id).one()
|
task = Task.query.get(task_id)
|
||||||
|
|
||||||
task.status = "SUCCESS"
|
task.status = "SUCCESS"
|
||||||
task.status_message = "Задача выполнена успешно"
|
task.status_message = "задача выполнена успешно"
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
def failure_state(task_id, exception):
|
def failure_state(task_id, exception):
|
||||||
task = Task.query.filter_by(task_id=task_id).one()
|
task = Task.query.get(task_id)
|
||||||
|
|
||||||
task.status = "FAILURE"
|
task.status = "FAILURE"
|
||||||
task.status_message = f"Произошла ошибка: {str(exception)}"
|
task.status_message = f"произошла ошибка {str(exception)}"
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
@ -22,6 +22,6 @@ def run_state(task_id, task_record_id):
|
||||||
|
|
||||||
task.task_id = task_id
|
task.task_id = task_id
|
||||||
task.status = "RUNNING"
|
task.status = "RUNNING"
|
||||||
task.status_message = "Задача запущена"
|
task.status_message = "задача запущена"
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
|
@ -31,11 +31,11 @@ def add_to_group_task(self, task_id):
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
failure_state(self.request.id, e)
|
failure_state(task_id, e)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
else:
|
else:
|
||||||
success_state(self.request.id)
|
success_state(task_id)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,8 @@ from telethon.errors.rpcerrorlist import (
|
||||||
UsersTooMuchError,
|
UsersTooMuchError,
|
||||||
UserChannelsTooMuchError,
|
UserChannelsTooMuchError,
|
||||||
UserIsBlockedError,
|
UserIsBlockedError,
|
||||||
YouBlockedUserError
|
YouBlockedUserError,
|
||||||
|
UsernameInvalidError,
|
||||||
)
|
)
|
||||||
|
|
||||||
class PaperClient(TelegramClient):
|
class PaperClient(TelegramClient):
|
||||||
|
@ -93,11 +94,13 @@ class PaperClient(TelegramClient):
|
||||||
return await super().get_participants(group, *args, **kwargs)
|
return await super().get_participants(group, *args, **kwargs)
|
||||||
|
|
||||||
async def __cast_to_entity(self, entity: Entity | EntityLike) -> Entity:
|
async def __cast_to_entity(self, entity: Entity | EntityLike) -> Entity:
|
||||||
if not isinstance(entity, Entity):
|
try:
|
||||||
if hasattr(entity, "username"):
|
if not isinstance(entity, Entity):
|
||||||
entity = await self.get_entity(entity.username)
|
if hasattr(entity, "username"):
|
||||||
else:
|
entity = await self.get_entity(entity.username)
|
||||||
entity = await self.get_entity(entity)
|
else:
|
||||||
|
entity = await self.get_entity(entity)
|
||||||
return entity # type: ignore
|
|
||||||
|
|
||||||
|
return entity # type: ignore
|
||||||
|
except UsernameInvalidError as e:
|
||||||
|
raise IgnoreException(e)
|
||||||
|
|
|
@ -18,8 +18,9 @@ class FloodException(Exception):
|
||||||
def __init__(self, error, *args: object):
|
def __init__(self, error, *args: object):
|
||||||
self.error = error
|
self.error = error
|
||||||
self.seconds = self.error.seconds if hasattr(self.error, "seconds") else -1 #type: ignore
|
self.seconds = self.error.seconds if hasattr(self.error, "seconds") else -1 #type: ignore
|
||||||
|
self.message = f"Flood wait for {self.seconds}" if self.seconds > 0 else "Flood error"
|
||||||
super().__init__(*args)
|
|
||||||
|
super().__init__(self.message, *args)
|
||||||
|
|
||||||
class IgnoreException(Exception):
|
class IgnoreException(Exception):
|
||||||
def __init__(self, error, *args):
|
def __init__(self, error, *args):
|
||||||
|
|
|
@ -35,15 +35,11 @@ class PaperParser:
|
||||||
|
|
||||||
except (UserPrivacyException, IgnoreException) as e:
|
except (UserPrivacyException, IgnoreException) as e:
|
||||||
self.users_to_delete.append(user)
|
self.users_to_delete.append(user)
|
||||||
logger.exception(e)
|
|
||||||
logger.warning("Exception occurred. Skipping user...")
|
logger.warning("Exception occurred. Skipping user...")
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
logger.exception(e)
|
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if not task.is_aborted():
|
if not task.is_aborted():
|
||||||
await asyncio.sleep(50) # FIXME: Change to config value
|
await asyncio.sleep(70) # FIXME: Change to config value
|
||||||
|
|
||||||
async def send_messages(self, users, message: str, file: str | None = None, task = None):
|
async def send_messages(self, users, message: str, file: str | None = None, task = None):
|
||||||
# TODO: Filter users, that already get this message
|
# TODO: Filter users, that already get this message
|
||||||
|
@ -61,12 +57,11 @@ class PaperParser:
|
||||||
|
|
||||||
except (UserPrivacyException, IgnoreException) as e:
|
except (UserPrivacyException, IgnoreException) as e:
|
||||||
self.users_to_delete.append(user)
|
self.users_to_delete.append(user)
|
||||||
logger.exception(e)
|
|
||||||
logger.warning("Exception occurred. Skipping user...")
|
logger.warning("Exception occurred. Skipping user...")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if not task.is_aborted():
|
if not task.is_aborted():
|
||||||
await asyncio.sleep(50) # FIXME: Change to config value
|
await asyncio.sleep(70) # FIXME: Change to config value
|
||||||
|
|
||||||
async def get_participants(self, group):
|
async def get_participants(self, group):
|
||||||
await self.client.invite_self(group)
|
await self.client.invite_self(group)
|
||||||
|
|
Loading…
Reference in New Issue