Skip to content

Commit cb3adda

Browse files
committed
Add iter_profile_photos method
1 parent 4ca8768 commit cb3adda

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 IterProfilePhotos(BaseClient):
26+
def iter_profile_photos(
27+
self,
28+
chat_id: Union[int, str],
29+
offset: int = 0,
30+
limit: int = 0,
31+
) -> Generator["pyrogram.Photo", None, None]:
32+
"""Iterate through a chat or a user profile photos sequentially.
33+
34+
This convenience method does the same as repeatedly calling :meth:`~Client.get_profile_photos` in a loop, thus
35+
saving you from the hassle of setting up boilerplate code. It is useful for getting all the profile photos with
36+
a single call.
37+
38+
Parameters:
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 profile photos to be retrieved.
46+
By default, no limit is applied and all profile photos are returned.
47+
48+
offset (``int``, *optional*):
49+
Sequential number of the first profile photo to be returned.
50+
51+
Returns:
52+
``Generator``: A generator yielding :obj:`Photo` objects.
53+
54+
Raises:
55+
RPCError: In case of a Telegram RPC error.
56+
"""
57+
current = 0
58+
total = limit or (1 << 31)
59+
limit = min(100, total)
60+
61+
while True:
62+
photos = self.get_profile_photos(
63+
chat_id=chat_id,
64+
offset=offset,
65+
limit=limit
66+
).photos
67+
68+
if not photos:
69+
return
70+
71+
offset += len(photos)
72+
73+
for photo in photos:
74+
yield photo
75+
76+
current += 1
77+
78+
if current >= total:
79+
return

0 commit comments

Comments
 (0)