|
17 | 17 | # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | 19 | from base64 import b64decode, b64encode |
| 20 | +from struct import pack |
20 | 21 |
|
21 | 22 | from pyrogram.api import types |
| 23 | +from pyrogram.client import types as pyrogram_types |
22 | 24 |
|
23 | 25 |
|
24 | 26 | def get_peer_id(input_peer) -> int: |
@@ -48,6 +50,58 @@ def get_offset_date(dialogs): |
48 | 50 | return 0 |
49 | 51 |
|
50 | 52 |
|
| 53 | +def parse_photos(photos): |
| 54 | + if isinstance(photos, types.photos.Photos): |
| 55 | + total_count = len(photos.photos) |
| 56 | + else: |
| 57 | + total_count = photos.count |
| 58 | + |
| 59 | + user_profile_photos = [] |
| 60 | + |
| 61 | + for photo in photos.photos: |
| 62 | + if isinstance(photo, types.Photo): |
| 63 | + sizes = photo.sizes |
| 64 | + photo_sizes = [] |
| 65 | + |
| 66 | + for size in sizes: |
| 67 | + if isinstance(size, (types.PhotoSize, types.PhotoCachedSize)): |
| 68 | + loc = size.location |
| 69 | + |
| 70 | + if isinstance(size, types.PhotoSize): |
| 71 | + file_size = size.size |
| 72 | + else: |
| 73 | + file_size = len(size.bytes) |
| 74 | + |
| 75 | + if isinstance(loc, types.FileLocation): |
| 76 | + photo_size = pyrogram_types.PhotoSize( |
| 77 | + file_id=encode( |
| 78 | + pack( |
| 79 | + "<iiqqqqi", |
| 80 | + 2, |
| 81 | + loc.dc_id, |
| 82 | + photo.id, |
| 83 | + photo.access_hash, |
| 84 | + loc.volume_id, |
| 85 | + loc.secret, |
| 86 | + loc.local_id |
| 87 | + ) |
| 88 | + ), |
| 89 | + width=size.w, |
| 90 | + height=size.h, |
| 91 | + file_size=file_size, |
| 92 | + date=photo.date |
| 93 | + ) |
| 94 | + |
| 95 | + photo_sizes.append(photo_size) |
| 96 | + |
| 97 | + user_profile_photos.append(photo_sizes) |
| 98 | + |
| 99 | + return pyrogram_types.UserProfilePhotos( |
| 100 | + total_count=total_count, |
| 101 | + photos=user_profile_photos |
| 102 | + ) |
| 103 | + |
| 104 | + |
51 | 105 | def decode(s: str) -> bytes: |
52 | 106 | s = b64decode(s + "=" * (-len(s) % 4), "-_") |
53 | 107 | r = b"" |
|
0 commit comments