outside-fetcher/tasks/executors/mtproto_task_abstraction.py

28 lines
743 B
Python
Raw Normal View History

2019-11-06 15:53:46 +01:00
import abc
import typing
from async_worker import AsyncTask
2019-11-06 15:58:55 +01:00
from pyrogram.errors.exceptions import FloodWait, RPCError, InternalServerError
2019-11-06 15:53:46 +01:00
class MtProtoTask(AsyncTask, abc.ABC):
async def _execute(self, func: typing.Callable) -> typing.Any:
try:
return await func()
except FloodWait as error:
return int(error.MESSAGE.split("_")[-1]) * 1e9
2019-11-06 15:58:55 +01:00
except InternalServerError:
return 1e9
except RPCError as error:
2019-11-06 16:03:31 +01:00
return False if abs(error.CODE) < 500 else 2e9
2019-11-06 15:53:46 +01:00
async def _setup(self) -> bool:
return await self._execute(super()._setup)
async def _process(self) -> typing.Union[bool, int]:
return await self._execute(super()._process)