|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2019 Dan Tès <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 Union, Generator |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from ...ext import BaseClient |
| 23 | + |
| 24 | + |
| 25 | +class IterHistory(BaseClient): |
| 26 | + def iter_history(self, |
| 27 | + chat_id: Union[int, str], |
| 28 | + limit: int = 0, |
| 29 | + offset: int = 0, |
| 30 | + offset_id: int = 0, |
| 31 | + offset_date: int = 0, |
| 32 | + reverse: bool = False) -> Generator["pyrogram.Message", None, None]: |
| 33 | + """Use this method to iterate through a chat history sequentially. |
| 34 | +
|
| 35 | + This convenience method does the same as repeatedly calling :meth:`get_history` in a loop, thus saving you from |
| 36 | + the hassle of setting up boilerplate code. It is useful for getting the whole chat history with a single call. |
| 37 | +
|
| 38 | + Args: |
| 39 | + chat_id (``int`` | ``str``): |
| 40 | + Unique identifier (int) or username (str) of the target chat. |
| 41 | + For your personal cloud (Saved Messages) you can simply use "me" or "self". |
| 42 | + For a contact that exists in your Telegram address book you can use his phone number (str). |
| 43 | +
|
| 44 | + limit (``int``, *optional*): |
| 45 | + Limits the number of messages to be retrieved. |
| 46 | + By default, no limit is applied and all messages are returned. |
| 47 | +
|
| 48 | + offset (``int``, *optional*): |
| 49 | + Sequential number of the first message to be returned.. |
| 50 | + Negative values are also accepted and become useful in case you set offset_id or offset_date. |
| 51 | +
|
| 52 | + offset_id (``int``, *optional*): |
| 53 | + Identifier of the first message to be returned. |
| 54 | +
|
| 55 | + offset_date (``int``, *optional*): |
| 56 | + Pass a date in Unix time as offset to retrieve only older messages starting from that date. |
| 57 | +
|
| 58 | + reverse (``bool``, *optional*): |
| 59 | + Pass True to retrieve the messages in reversed order (from older to most recent). |
| 60 | +
|
| 61 | + Returns: |
| 62 | + A generator yielding :obj:`Message <pyrogram.Message>` objects. |
| 63 | +
|
| 64 | + Raises: |
| 65 | + :class:`Error <pyrogram.Error>` in case of a Telegram RPC error. |
| 66 | + """ |
| 67 | + offset_id = offset_id or (1 if reverse else 0) |
| 68 | + current = 0 |
| 69 | + total = limit or (1 << 31) - 1 |
| 70 | + limit = min(100, total) |
| 71 | + |
| 72 | + while True: |
| 73 | + messages = self.get_history( |
| 74 | + chat_id=chat_id, |
| 75 | + limit=limit, |
| 76 | + offset=offset, |
| 77 | + offset_id=offset_id, |
| 78 | + offset_date=offset_date, |
| 79 | + reverse=reverse |
| 80 | + ).messages |
| 81 | + |
| 82 | + if not messages: |
| 83 | + return |
| 84 | + |
| 85 | + offset_id = messages[-1].message_id + (1 if reverse else 0) |
| 86 | + |
| 87 | + for message in messages: |
| 88 | + yield message |
| 89 | + |
| 90 | + current += 1 |
| 91 | + |
| 92 | + if current >= total: |
| 93 | + return |
0 commit comments