Skip to content

Commit 1ea5e79

Browse files
authored
Refactor Photo to return all sizes instead of only the largest
Closes KurimuzonAkuma/kurigram#159
1 parent f867143 commit 1ea5e79

File tree

3 files changed

+89
-47
lines changed

3 files changed

+89
-47
lines changed

docs/source/releases/changes-in-this-fork.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Changes in this Fork
3434
| Scheme layer used: 200 |
3535
+------------------------+
3636

37+
- Added ``sizes`` to :obj:`~pyrogram.types.Photo` to return all available sizes.
3738
- Add :meth:`~pyrogram.Client.send_screenshot_notification`.
3839
- Add ``media`` in :obj:`~pyrogram.types.ExternalReplyInfo`.
3940
- Add :obj:`~pyrogram.enums.MessageOriginType` as enum instead of str, and updated the appropriate filters.

pyrogram/methods/messages/download_media.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,12 @@ async def progress(current, total):
174174

175175
else:
176176
if message.media:
177-
medium = [getattr(message, message.media.value, None)]
177+
if message.photo:
178+
medium = [
179+
getattr(message, message.media.value, None).sizes[-1]
180+
]
181+
else:
182+
medium = [getattr(message, message.media.value, None)]
178183
else:
179184
medium = []
180185

pyrogram/types/messages_and_media/photo.py

Lines changed: 82 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import logging
1920
from datetime import datetime
2021

2122
import pyrogram
@@ -24,26 +25,15 @@
2425
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource
2526
from ..object import Object
2627

28+
log = logging.getLogger(__name__)
29+
2730

2831
class Photo(Object):
2932
"""A Photo.
3033
3134
Parameters:
32-
file_id (``str``):
33-
Identifier for this file, which can be used to download or reuse the file.
34-
35-
file_unique_id (``str``):
36-
Unique identifier for this file, which is supposed to be the same over time and for different accounts.
37-
Can't be used to download or reuse the file.
38-
39-
width (``int``):
40-
Photo width.
41-
42-
height (``int``):
43-
Photo height.
44-
45-
file_size (``int``):
46-
File size.
35+
sizes (List of :obj:`~pyrogram.types.Thumbnail`):
36+
Available variants of the photo, in different sizes.
4737
4838
date (:py:obj:`~datetime.datetime`):
4939
Date the photo was sent.
@@ -53,29 +43,22 @@ class Photo(Object):
5343
5444
thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*):
5545
Available thumbnails of this photo.
46+
5647
"""
5748

5849
def __init__(
5950
self,
6051
*,
6152
client: "pyrogram.Client" = None,
62-
file_id: str,
63-
file_unique_id: str,
64-
width: int,
65-
height: int,
66-
file_size: int,
53+
sizes: list["types.Thumbnail"],
6754
date: datetime,
6855
ttl_seconds: int = None,
6956
has_spoiler: bool = None,
7057
thumbs: list["types.Thumbnail"] = None
7158
):
7259
super().__init__(client)
7360

74-
self.file_id = file_id
75-
self.file_unique_id = file_unique_id
76-
self.width = width
77-
self.height = height
78-
self.file_size = file_size
61+
self.sizes = sizes
7962
self.date = date
8063
self.ttl_seconds = ttl_seconds
8164
self.has_spoiler = has_spoiler
@@ -107,31 +90,84 @@ def _parse(
10790

10891
photos.sort(key=lambda p: p.size)
10992

110-
main = photos[-1]
111-
11293
return Photo(
113-
file_id=FileId(
114-
file_type=FileType.PHOTO,
115-
dc_id=photo.dc_id,
116-
media_id=photo.id,
117-
access_hash=photo.access_hash,
118-
file_reference=photo.file_reference,
119-
thumbnail_source=ThumbnailSource.THUMBNAIL,
120-
thumbnail_file_type=FileType.PHOTO,
121-
thumbnail_size=main.type,
122-
volume_id=0,
123-
local_id=0
124-
).encode(),
125-
file_unique_id=FileUniqueId(
126-
file_unique_type=FileUniqueType.DOCUMENT,
127-
media_id=photo.id
128-
).encode(),
129-
width=main.w,
130-
height=main.h,
131-
file_size=main.size,
94+
sizes=[
95+
types.Thumbnail(
96+
file_id=FileId(
97+
file_type=FileType.PHOTO,
98+
dc_id=photo.dc_id,
99+
media_id=photo.id,
100+
access_hash=photo.access_hash,
101+
file_reference=photo.file_reference,
102+
thumbnail_source=ThumbnailSource.THUMBNAIL,
103+
thumbnail_file_type=FileType.PHOTO,
104+
thumbnail_size=main.type,
105+
volume_id=0,
106+
local_id=0
107+
).encode(),
108+
file_unique_id=FileUniqueId(
109+
file_unique_type=FileUniqueType.DOCUMENT,
110+
media_id=photo.id
111+
).encode(),
112+
width=main.w,
113+
height=main.h,
114+
file_size=main.size,
115+
)
116+
for main in photos
117+
],
132118
date=utils.timestamp_to_datetime(photo.date),
133119
ttl_seconds=ttl_seconds,
134120
has_spoiler=has_spoiler,
135121
thumbs=types.Thumbnail._parse(client, photo),
136122
client=client
137123
)
124+
125+
@property
126+
def file_id(self) -> str:
127+
log.warning(
128+
"This property is deprecated. "
129+
"Please use sizes instead"
130+
)
131+
if len(self.sizes) > 0:
132+
return self.sizes[-1].file_id
133+
return None
134+
135+
@property
136+
def file_unique_id(self) -> str:
137+
log.warning(
138+
"This property is deprecated. "
139+
"Please use sizes instead"
140+
)
141+
if len(self.sizes) > 0:
142+
return self.sizes[-1].file_unique_id
143+
return None
144+
145+
@property
146+
def width(self) -> int:
147+
log.warning(
148+
"This property is deprecated. "
149+
"Please use sizes instead"
150+
)
151+
if len(self.sizes) > 0:
152+
return self.sizes[-1].width
153+
return None
154+
155+
@property
156+
def height(self) -> int:
157+
log.warning(
158+
"This property is deprecated. "
159+
"Please use sizes instead"
160+
)
161+
if len(self.sizes) > 0:
162+
return self.sizes[-1].height
163+
return None
164+
165+
@property
166+
def file_size(self) -> int:
167+
log.warning(
168+
"This property is deprecated. "
169+
"Please use sizes instead"
170+
)
171+
if len(self.sizes) > 0:
172+
return self.sizes[-1].file_size
173+
return None

0 commit comments

Comments
 (0)