schedule webhook data forwarding

This commit is contained in:
andrew (from workstation) 2019-11-05 19:48:14 +01:00
parent f34504deb4
commit eaba19a078
4 changed files with 52 additions and 36 deletions

View File

@ -1,7 +1,9 @@
from .channel_history import ChannelHistoryReadTask from .channel_history import ChannelHistoryReadTask
from .mtproto_task_abstraction import MtProtoTask from .mtproto_task_abstraction import MtProtoTask
from .webhook import WebHookDataForward
__all__ = [ __all__ = [
"ChannelHistoryReadTask", "ChannelHistoryReadTask",
"MtProtoTask" "MtProtoTask",
"WebHookDataForward"
] ]

View File

@ -1,9 +1,9 @@
import pyrogram import pyrogram
import aiohttp
import json import json
import typing import typing
from .mtproto_task_abstraction import MtProtoTask from .mtproto_task_abstraction import MtProtoTask
from .webhook import WebHookDataForward
from pyrogram.api.types import ChannelMessagesFilterEmpty from pyrogram.api.types import ChannelMessagesFilterEmpty
from pyrogram.api.types.updates import ChannelDifference, ChannelDifferenceTooLong, ChannelDifferenceEmpty from pyrogram.api.types.updates import ChannelDifference, ChannelDifferenceTooLong, ChannelDifferenceEmpty
@ -61,42 +61,40 @@ class JsonSerializer:
class ChannelHistoryReadTask(MtProtoTask): class ChannelHistoryReadTask(MtProtoTask):
channel: pyrogram.Chat _channel: pyrogram.Chat
client: pyrogram.Client _client: pyrogram.Client
pts: int _pts: int
webhook: str _webhook: str
http: aiohttp.ClientSession
def setup(self, client: pyrogram.Client, channel: pyrogram.Chat, webhook: str): def setup(self, client: pyrogram.Client, channel: pyrogram.Chat, webhook: str):
self.client = client self._pts = False
self.channel = channel
self.pts = False
self.webhook = webhook
self.http = aiohttp.ClientSession()
async def mt_process(self) -> typing.Union[bool, int]: self._client = client
response = await self.client.send( self._channel = channel
self._webhook = webhook
async def process(self) -> typing.Union[bool, int]:
response = await self._client.send(
GetChannelDifference( GetChannelDifference(
channel=self.channel, channel=self._channel,
filter=ChannelMessagesFilterEmpty(), filter=ChannelMessagesFilterEmpty(),
pts=self.pts if self.pts else 0xFFFFFFF, pts=self._pts if self._pts else 0xFFFFFFF,
limit=0xFFFFFFF, limit=0xFFFFFFF,
force=True force=True
) )
) )
if isinstance(response, ChannelDifference): if isinstance(response, ChannelDifference):
self.pts = response.pts self._pts = response.pts
users = {i.id: i for i in response.users} users = {i.id: i for i in response.users}
chats = {i.id: i for i in response.chats} chats = {i.id: i for i in response.chats}
for message in response.new_messages: for message in response.new_messages:
message = await MessagePyrogram._parse(self.client, message, users, chats) message = await MessagePyrogram._parse(self._client, message, users, chats)
message = {"update_id": 1, "message": message}
data = json.dumps( data = json.dumps(
message, {"update_id": 1, "message": message},
default=JsonSerializer.default, default=JsonSerializer.default,
ensure_ascii=True, ensure_ascii=True,
allow_nan=False, allow_nan=False,
@ -104,16 +102,9 @@ class ChannelHistoryReadTask(MtProtoTask):
sort_keys=False sort_keys=False
) )
result = await self.http.post( forwarder = WebHookDataForward()
self.webhook, forwarder.setup(self._webhook, data)
data=data, await self.future(forwarder)
headers=[
("Content-Type", "application/json")
]
)
await result.read()
result.close()
if not response.final: if not response.final:
return 1 return 1
@ -121,7 +112,7 @@ class ChannelHistoryReadTask(MtProtoTask):
return response.timeout return response.timeout
if isinstance(response, ChannelDifferenceEmpty): if isinstance(response, ChannelDifferenceEmpty):
self.pts = response.pts self._pts = response.pts
return response.timeout return response.timeout
if isinstance(response, ChannelDifferenceTooLong): if isinstance(response, ChannelDifferenceTooLong):

View File

@ -12,16 +12,15 @@ class MtProtoTask(AsyncTask, abc.ABC):
raise NotImplementedError raise NotImplementedError
@abc.abstractmethod @abc.abstractmethod
async def mt_process(self) -> typing.Union[bool, int]: async def process(self) -> typing.Union[bool, int]:
raise NotImplementedError raise NotImplementedError
async def process(self) -> int: async def _process(self) -> typing.Union[bool, int]:
try: try:
return await self.process() * 1e9
return await self.mt_process()
except FloodWait as error: except FloodWait as error:
return int(error.MESSAGE.split("_")[-1]) return int(error.MESSAGE.split("_")[-1]) * 1e9
except (ChannelInvalid, ChannelPrivate, Unauthorized): except (ChannelInvalid, ChannelPrivate, Unauthorized):
return False return False

24
tasks/webhook.py Normal file
View File

@ -0,0 +1,24 @@
import typing
import aiohttp
from async_worker import OneLoopAsyncTask
HEADERS = [("Content-Type", "application/json")]
class WebHookDataForward(OneLoopAsyncTask):
_webhook: str
_data: typing.Union[str, bytes]
_http: aiohttp.ClientSession
async def process(self) -> typing.NoReturn:
res = await self._http.post(self._webhook, data=self._data, headers=HEADERS)
await res.read()
res.close()
def setup(self, webhook: str, data: typing.Union[str, bytes]):
self._http = aiohttp.ClientSession()
self._webhook = webhook
self._data = data