2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-09-02 15:25:41 +00:00

Fix asyncio merge issues

This commit is contained in:
Dan
2019-01-07 09:37:26 +01:00
parent 4d8c76463c
commit 0bae143d5d
7 changed files with 48 additions and 39 deletions

View File

@@ -321,7 +321,7 @@ class Client(Methods, BaseClient):
raise ConnectionError("Client is already stopped")
if self.takeout_id:
self.send(functions.account.FinishTakeoutSession())
await self.send(functions.account.FinishTakeoutSession())
log.warning("Takeout session {} finished".format(self.takeout_id))
await Syncer.remove(self)

View File

@@ -78,4 +78,4 @@ class GetDialogs(BaseClient):
else:
break
return pyrogram.Dialogs._parse(self, r)
return await pyrogram.Dialogs._parse(self, r)

View File

@@ -17,7 +17,9 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from string import ascii_lowercase
from typing import Union, Generator
from typing import Union, AsyncGenerator, Optional
from async_generator import async_generator, yield_
import pyrogram
from ...ext import BaseClient
@@ -37,11 +39,12 @@ QUERYABLE_FILTERS = (Filters.ALL, Filters.KICKED, Filters.RESTRICTED)
class IterChatMembers(BaseClient):
def iter_chat_members(self,
chat_id: Union[int, str],
limit: int = 0,
query: str = "",
filter: str = Filters.ALL) -> Generator["pyrogram.ChatMember", None, None]:
@async_generator
async def iter_chat_members(self,
chat_id: Union[int, str],
limit: int = 0,
query: str = "",
filter: str = Filters.ALL) -> Optional[AsyncGenerator["pyrogram.ChatMember", None]]:
"""Use this method to iterate through the members of a chat sequentially.
This convenience method does the same as repeatedly calling :meth:`get_chat_members` in a loop, thus saving you
@@ -95,13 +98,13 @@ class IterChatMembers(BaseClient):
offset = 0
while True:
chat_members = self.get_chat_members(
chat_members = (await self.get_chat_members(
chat_id=chat_id,
offset=offset,
limit=limit,
query=q,
filter=filter
).chat_members
)).chat_members
if not chat_members:
break
@@ -114,7 +117,7 @@ class IterChatMembers(BaseClient):
if user_id in yielded:
continue
yield chat_member
await yield_(chat_member)
yielded.add(chat_member.user.id)

View File

@@ -16,30 +16,33 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Generator
from typing import AsyncGenerator, Optional
from async_generator import async_generator, yield_
import pyrogram
from ...ext import BaseClient
class IterDialogs(BaseClient):
def iter_dialogs(self,
offset_date: int = 0,
limit: int = 0) -> Generator["pyrogram.Dialog", None, None]:
@async_generator
async def iter_dialogs(self,
limit: int = 0,
offset_date: int = 0) -> Optional[AsyncGenerator["pyrogram.Dialog", None]]:
"""Use this method to iterate through a user's dialogs sequentially.
This convenience method does the same as repeatedly calling :meth:`get_dialogs` in a loop, thus saving you from
the hassle of setting up boilerplate code. It is useful for getting the whole dialogs list with a single call.
Args:
offset_date (``int``):
The offset date in Unix time taken from the top message of a :obj:`Dialog`.
Defaults to 0 (most recent dialog).
limit (``str``, *optional*):
Limits the number of dialogs to be retrieved.
By default, no limit is applied and all dialogs are returned.
offset_date (``int``):
The offset date in Unix time taken from the top message of a :obj:`Dialog`.
Defaults to 0 (most recent dialog).
Returns:
A generator yielding :obj:`Dialog <pyrogram.Dialog>` objects.
@@ -50,12 +53,12 @@ class IterDialogs(BaseClient):
total = limit or (1 << 31) - 1
limit = min(100, total)
pinned_dialogs = self.get_dialogs(
pinned_dialogs = (await self.get_dialogs(
pinned_only=True
).dialogs
)).dialogs
for dialog in pinned_dialogs:
yield dialog
await yield_(dialog)
current += 1
@@ -63,10 +66,10 @@ class IterDialogs(BaseClient):
return
while True:
dialogs = self.get_dialogs(
dialogs = (await self.get_dialogs(
offset_date=offset_date,
limit=limit
).dialogs
)).dialogs
if not dialogs:
return
@@ -74,7 +77,7 @@ class IterDialogs(BaseClient):
offset_date = dialogs[-1].top_message.date
for dialog in dialogs:
yield dialog
await yield_(dialog)
current += 1

View File

@@ -16,20 +16,23 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from typing import Union, Generator
from typing import Union, Optional, AsyncGenerator
from async_generator import async_generator, yield_
import pyrogram
from ...ext import BaseClient
class IterHistory(BaseClient):
def iter_history(self,
chat_id: Union[int, str],
limit: int = 0,
offset: int = 0,
offset_id: int = 0,
offset_date: int = 0,
reverse: bool = False) -> Generator["pyrogram.Message", None, None]:
@async_generator
async def iter_history(self,
chat_id: Union[int, str],
limit: int = 0,
offset: int = 0,
offset_id: int = 0,
offset_date: int = 0,
reverse: bool = False) -> Optional[AsyncGenerator["pyrogram.Message", None]]:
"""Use this method to iterate through a chat history sequentially.
This convenience method does the same as repeatedly calling :meth:`get_history` in a loop, thus saving you from
@@ -70,14 +73,14 @@ class IterHistory(BaseClient):
limit = min(100, total)
while True:
messages = self.get_history(
messages = (await self.get_history(
chat_id=chat_id,
limit=limit,
offset=offset,
offset_id=offset_id,
offset_date=offset_date,
reverse=reverse
).messages
)).messages
if not messages:
return
@@ -85,7 +88,7 @@ class IterHistory(BaseClient):
offset_id = messages[-1].message_id + (1 if reverse else 0)
for message in messages:
yield message
await yield_(message)
current += 1

View File

@@ -65,7 +65,7 @@ class Messages(PyrogramType, Update):
parsed_messages = []
for message in messages.messages:
parsed_messages.appen(await Message._parse(client, message, users, chats, replies=0))
parsed_messages.append(await Message._parse(client, message, users, chats, replies=0))
if replies:
messages_with_replies = {i.id: getattr(i, "reply_to_msg_id", None) for i in messages.messages}

View File

@@ -47,7 +47,7 @@ class Dialogs(PyrogramType):
self.dialogs = dialogs
@staticmethod
def _parse(client, dialogs) -> "Dialogs":
async def _parse(client, dialogs) -> "Dialogs":
users = {i.id: i for i in dialogs.users}
chats = {i.id: i for i in dialogs.chats}
@@ -66,7 +66,7 @@ class Dialogs(PyrogramType):
else:
chat_id = int("-100" + str(to_id.channel_id))
messages[chat_id] = Message._parse(client, message, users, chats)
messages[chat_id] = await Message._parse(client, message, users, chats)
return Dialogs(
total_count=getattr(dialogs, "count", len(dialogs.dialogs)),