Skip to content

Commit 1741608

Browse files
committed
Use larger batches for .get_dialogs(limit=None)
1 parent 0bfd8ff commit 1741608

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

telethon/telegram_client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,22 +279,21 @@ def get_dialogs(self,
279279
The peer to be used as an offset.
280280
:return: A tuple of lists ([dialogs], [entities]).
281281
"""
282-
if limit is None:
283-
limit = float('inf')
282+
limit = float('inf') if limit is None else int(limit)
283+
if limit == 0:
284+
return [], []
284285

285286
dialogs = {} # Use peer id as identifier to avoid dupes
286287
messages = {} # Used later for sorting TODO also return these?
287288
entities = {}
288289
while len(dialogs) < limit:
289-
need = limit - len(dialogs)
290+
real_limit = min(limit - len(dialogs), 100)
290291
r = self(GetDialogsRequest(
291292
offset_date=offset_date,
292293
offset_id=offset_id,
293294
offset_peer=offset_peer,
294-
limit=need if need < float('inf') else 0
295+
limit=real_limit
295296
))
296-
if not r.dialogs:
297-
break
298297

299298
for d in r.dialogs:
300299
dialogs[utils.get_peer_id(d.peer, True)] = d
@@ -307,8 +306,9 @@ def get_dialogs(self,
307306
for c in r.chats:
308307
entities[c.id] = c
309308

310-
if not isinstance(r, DialogsSlice):
311-
# Don't enter next iteration if we already got all
309+
if len(r.dialogs) < real_limit or not isinstance(r, DialogsSlice):
310+
# Less than we requested means we reached the end, or
311+
# we didn't get a DialogsSlice which means we got all.
312312
break
313313

314314
offset_date = r.messages[-1].date

0 commit comments

Comments
 (0)