2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-28 21:07:59 +00:00

Improve get_history_count

This commit is contained in:
Dan 2019-06-12 11:43:24 +02:00
parent df6e174b55
commit b86373d28c

View File

@ -17,12 +17,10 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import logging import logging
import time
from typing import Union from typing import Union
from pyrogram.api import types, functions from pyrogram.api import types, functions
from pyrogram.client.ext import BaseClient from pyrogram.client.ext import BaseClient
from pyrogram.errors import FloodWait
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -51,34 +49,20 @@ class GetHistoryCount(BaseClient):
RPCError: In case of a Telegram RPC error. RPCError: In case of a Telegram RPC error.
""" """
peer = self.resolve_peer(chat_id) r = self.send(
functions.messages.GetHistory(
peer=self.resolve_peer(chat_id),
offset_id=0,
offset_date=0,
add_offset=0,
limit=1,
max_id=0,
min_id=0,
hash=0
)
)
if not isinstance(peer, types.InputPeerChannel): if isinstance(r, types.messages.Messages):
offset = 0 return len(r.messages)
limit = 100 else:
return r.count
while True:
try:
r = self.send(
functions.messages.GetHistory(
peer=peer,
offset_id=1,
offset_date=0,
add_offset=-offset - limit,
limit=limit,
max_id=0,
min_id=0,
hash=0
)
)
except FloodWait as e:
log.warning("Sleeping for {}s".format(e.x))
time.sleep(e.x)
continue
if not r.messages:
return offset
offset += len(r.messages)
return self.get_history(chat_id=chat_id, limit=1).total_count