Skip to content

Commit b86373d

Browse files
committed
Improve get_history_count
1 parent df6e174 commit b86373d

1 file changed

Lines changed: 17 additions & 33 deletions

File tree

pyrogram/client/methods/messages/get_history_count.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import logging
20-
import time
2120
from typing import Union
2221

2322
from pyrogram.api import types, functions
2423
from pyrogram.client.ext import BaseClient
25-
from pyrogram.errors import FloodWait
2624

2725
log = logging.getLogger(__name__)
2826

@@ -51,34 +49,20 @@ def get_history_count(
5149
RPCError: In case of a Telegram RPC error.
5250
"""
5351

54-
peer = self.resolve_peer(chat_id)
55-
56-
if not isinstance(peer, types.InputPeerChannel):
57-
offset = 0
58-
limit = 100
59-
60-
while True:
61-
try:
62-
r = self.send(
63-
functions.messages.GetHistory(
64-
peer=peer,
65-
offset_id=1,
66-
offset_date=0,
67-
add_offset=-offset - limit,
68-
limit=limit,
69-
max_id=0,
70-
min_id=0,
71-
hash=0
72-
)
73-
)
74-
except FloodWait as e:
75-
log.warning("Sleeping for {}s".format(e.x))
76-
time.sleep(e.x)
77-
continue
78-
79-
if not r.messages:
80-
return offset
81-
82-
offset += len(r.messages)
83-
84-
return self.get_history(chat_id=chat_id, limit=1).total_count
52+
r = self.send(
53+
functions.messages.GetHistory(
54+
peer=self.resolve_peer(chat_id),
55+
offset_id=0,
56+
offset_date=0,
57+
add_offset=0,
58+
limit=1,
59+
max_id=0,
60+
min_id=0,
61+
hash=0
62+
)
63+
)
64+
65+
if isinstance(r, types.messages.Messages):
66+
return len(r.messages)
67+
else:
68+
return r.count

0 commit comments

Comments
 (0)