outside-fetcher/tasks/mtproto_task_abstraction.py

33 lines
802 B
Python
Raw Normal View History

2019-11-04 16:58:31 +01:00
import abc
import typing
from async_worker import AsyncTask
from pyrogram.errors.exceptions import FloodWait, ChannelInvalid, ChannelPrivate, Unauthorized
class MtProtoTask(AsyncTask, abc.ABC):
@abc.abstractmethod
def setup(self, *args, **kwargs):
raise NotImplementedError
@abc.abstractmethod
2019-11-05 19:48:14 +01:00
async def process(self) -> typing.Union[bool, int]:
2019-11-04 16:58:31 +01:00
raise NotImplementedError
2019-11-05 19:48:14 +01:00
async def _process(self) -> typing.Union[bool, int]:
2019-11-04 16:58:31 +01:00
try:
2019-11-05 19:56:24 +01:00
result = await self.process()
if result is False:
return False
return result * 1e9
2019-11-04 16:58:31 +01:00
except FloodWait as error:
2019-11-05 19:48:14 +01:00
return int(error.MESSAGE.split("_")[-1]) * 1e9
2019-11-04 16:58:31 +01:00
except (ChannelInvalid, ChannelPrivate, Unauthorized):
return False