|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +from typing import AsyncGenerator |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import enums, raw, types, utils |
| 23 | + |
| 24 | + |
| 25 | +class GetTopChats: |
| 26 | + async def get_top_chats( |
| 27 | + self: "pyrogram.Client", |
| 28 | + category: "enums.TopChatCategory", |
| 29 | + limit: int = 0, |
| 30 | + ) -> AsyncGenerator["types.Chat", None]: |
| 31 | + """Returns a list of frequently used chats. |
| 32 | +
|
| 33 | + .. include:: /_includes/usable-by/users.rst |
| 34 | +
|
| 35 | + Parameters: |
| 36 | + category (:obj:`~pyrogram.enums.TopChatCategory`): |
| 37 | + Category of chats to be returned. |
| 38 | +
|
| 39 | + limit (``int``, *optional*): |
| 40 | + The maximum number of chats to be returned. |
| 41 | + By default, no limit is applied and all chats are returned. |
| 42 | +
|
| 43 | + Returns: |
| 44 | + ``Generator``: A generator yielding :obj:`~pyrogram.types.Chat` objects. |
| 45 | +
|
| 46 | + Example: |
| 47 | + .. code-block:: python |
| 48 | +
|
| 49 | + # Iterate through all top chats in the "users" category |
| 50 | + async for chat in app.get_top_chats(enums.TopChatCategory.USERS): |
| 51 | + print(chat.full_name) |
| 52 | + """ |
| 53 | + current = 0 |
| 54 | + total = limit or (1 << 31) - 1 |
| 55 | + limit = min(30, total) |
| 56 | + |
| 57 | + offset = 0 |
| 58 | + |
| 59 | + while True: |
| 60 | + r = await self.invoke( |
| 61 | + raw.functions.contacts.GetTopPeers( |
| 62 | + offset=offset, |
| 63 | + limit=limit, |
| 64 | + hash=0, |
| 65 | + correspondents=category == enums.TopChatCategory.USERS, |
| 66 | + bots_pm=category == enums.TopChatCategory.BOTS, |
| 67 | + bots_inline=category == enums.TopChatCategory.INLINE_BOTS, |
| 68 | + phone_calls=category == enums.TopChatCategory.CALLS, |
| 69 | + forward_users=category == enums.TopChatCategory.FORWARD_CHATS, |
| 70 | + forward_chats=category == enums.TopChatCategory.FORWARD_CHATS, |
| 71 | + groups=category == enums.TopChatCategory.GROUPS, |
| 72 | + channels=category == enums.TopChatCategory.CHANNELS, |
| 73 | + bots_app=category == enums.TopChatCategory.WEB_APP_BOTS, |
| 74 | + bots_guestchat=category == enums.TopChatCategory.GUEST_BOTS, |
| 75 | + ), |
| 76 | + sleep_threshold=60 |
| 77 | + ) |
| 78 | + |
| 79 | + if not isinstance(r, raw.types.contacts.TopPeers): |
| 80 | + return |
| 81 | + |
| 82 | + users = {i.id: i for i in r.users} |
| 83 | + chats = {i.id: i for i in r.chats} |
| 84 | + |
| 85 | + chats = [] |
| 86 | + |
| 87 | + for cat in r.categories: |
| 88 | + for top_peer in cat.peers: |
| 89 | + peer_id = utils.get_raw_peer_id(top_peer.peer) |
| 90 | + |
| 91 | + chats.append(types.Chat._parse_chat(self, users.get(peer_id) or chats.get(peer_id))) |
| 92 | + |
| 93 | + if not chats: |
| 94 | + return |
| 95 | + |
| 96 | + offset += len(chats) |
| 97 | + |
| 98 | + for chat in chats: |
| 99 | + yield chat |
| 100 | + |
| 101 | + current += 1 |
| 102 | + |
| 103 | + if current >= total: |
| 104 | + return |
0 commit comments