mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2026-01-07 15:34:52 +00:00
pyrofork: Support for Chat Video Type
Add has_animation, is_personal, minithumbnail to ChatPhoto Change return type of get_chat_photos Inspiration from KurimuzonAkuma/pyrogram#60 but MVB_#1333 Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
parent
622616b38d
commit
21de92d909
4 changed files with 112 additions and 28 deletions
|
|
@ -28,7 +28,12 @@ class GetChatPhotos:
|
||||||
self: "pyrogram.Client",
|
self: "pyrogram.Client",
|
||||||
chat_id: Union[int, str],
|
chat_id: Union[int, str],
|
||||||
limit: int = 0,
|
limit: int = 0,
|
||||||
) -> Optional[AsyncGenerator["types.Photo", None]]:
|
) -> Optional[
|
||||||
|
Union[
|
||||||
|
AsyncGenerator["types.Photo", None],
|
||||||
|
AsyncGenerator["types.Animation", None]
|
||||||
|
]
|
||||||
|
]:
|
||||||
"""Get a chat or a user profile photos sequentially.
|
"""Get a chat or a user profile photos sequentially.
|
||||||
|
|
||||||
.. include:: /_includes/usable-by/users-bots.rst
|
.. include:: /_includes/usable-by/users-bots.rst
|
||||||
|
|
@ -45,7 +50,7 @@ class GetChatPhotos:
|
||||||
By default, no limit is applied and all profile photos are returned.
|
By default, no limit is applied and all profile photos are returned.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
``Generator``: A generator yielding :obj:`~pyrogram.types.Photo` objects.
|
``Generator``: A generator yielding :obj:`~pyrogram.types.Photo` | :obj:`~pyrogram.types.Video` objects.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
@ -63,7 +68,15 @@ class GetChatPhotos:
|
||||||
)
|
)
|
||||||
|
|
||||||
current = types.Photo._parse(self, r.full_chat.chat_photo) or []
|
current = types.Photo._parse(self, r.full_chat.chat_photo) or []
|
||||||
|
current = [current]
|
||||||
|
current_animation = types.Animation._parse_chat_animation(
|
||||||
|
self,
|
||||||
|
r.full_chat.chat_photo
|
||||||
|
)
|
||||||
|
if current_animation:
|
||||||
|
current = current + [current_animation]
|
||||||
|
extra = []
|
||||||
|
if not self.me.is_bot:
|
||||||
r = await utils.parse_messages(
|
r = await utils.parse_messages(
|
||||||
self,
|
self,
|
||||||
await self.invoke(
|
await self.invoke(
|
||||||
|
|
@ -87,12 +100,12 @@ class GetChatPhotos:
|
||||||
|
|
||||||
if extra:
|
if extra:
|
||||||
if current:
|
if current:
|
||||||
photos = ([current] + extra) if current.file_id != extra[0].file_id else extra
|
photos = (current + extra) if current[0].file_id != extra[0].file_id else extra
|
||||||
else:
|
else:
|
||||||
photos = extra
|
photos = extra
|
||||||
else:
|
else:
|
||||||
if current:
|
if current:
|
||||||
photos = [current]
|
photos = current
|
||||||
else:
|
else:
|
||||||
photos = []
|
photos = []
|
||||||
|
|
||||||
|
|
@ -121,7 +134,17 @@ class GetChatPhotos:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
photos = [types.Photo._parse(self, photo) for photo in r.photos]
|
photos = []
|
||||||
|
for photo in r.photos:
|
||||||
|
photos.append(
|
||||||
|
types.Photo._parse(self, photo)
|
||||||
|
)
|
||||||
|
current_animation = types.Animation._parse_chat_animation(
|
||||||
|
self,
|
||||||
|
photo
|
||||||
|
)
|
||||||
|
if current_animation:
|
||||||
|
photos.append(current_animation)
|
||||||
|
|
||||||
if not photos:
|
if not photos:
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class Animation(Object):
|
||||||
height (``int``):
|
height (``int``):
|
||||||
Animation height as defined by sender.
|
Animation height as defined by sender.
|
||||||
|
|
||||||
duration (``int``):
|
duration (``int``, *optional*):
|
||||||
Duration of the animation in seconds as defined by sender.
|
Duration of the animation in seconds as defined by sender.
|
||||||
|
|
||||||
file_name (``str``, *optional*):
|
file_name (``str``, *optional*):
|
||||||
|
|
@ -71,7 +71,7 @@ class Animation(Object):
|
||||||
file_unique_id: str,
|
file_unique_id: str,
|
||||||
width: int,
|
width: int,
|
||||||
height: int,
|
height: int,
|
||||||
duration: int,
|
duration: int = None,
|
||||||
file_name: str = None,
|
file_name: str = None,
|
||||||
mime_type: str = None,
|
mime_type: str = None,
|
||||||
file_size: int = None,
|
file_size: int = None,
|
||||||
|
|
@ -120,3 +120,44 @@ class Animation(Object):
|
||||||
thumbs=types.Thumbnail._parse(client, animation),
|
thumbs=types.Thumbnail._parse(client, animation),
|
||||||
client=client
|
client=client
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_chat_animation(
|
||||||
|
client,
|
||||||
|
video: "raw.types.Photo"
|
||||||
|
) -> "Animation":
|
||||||
|
if isinstance(video, raw.types.Photo):
|
||||||
|
if not video.video_sizes:
|
||||||
|
return
|
||||||
|
video_sizes: List[raw.types.VideoSize] = []
|
||||||
|
for p in video.video_sizes:
|
||||||
|
if isinstance(p, raw.types.VideoSize):
|
||||||
|
video_sizes.append(p)
|
||||||
|
# TODO: VideoSizeEmojiMarkup
|
||||||
|
video_sizes.sort(key=lambda p: p.size)
|
||||||
|
video_size = video_sizes[-1]
|
||||||
|
return Animation(
|
||||||
|
file_id=FileId(
|
||||||
|
file_type=FileType.PHOTO,
|
||||||
|
dc_id=video.dc_id,
|
||||||
|
media_id=video.id,
|
||||||
|
access_hash=video.access_hash,
|
||||||
|
file_reference=video.file_reference,
|
||||||
|
thumbnail_source=ThumbnailSource.THUMBNAIL,
|
||||||
|
thumbnail_file_type=FileType.PHOTO,
|
||||||
|
thumbnail_size=video_size.type,
|
||||||
|
volume_id=0,
|
||||||
|
local_id=0
|
||||||
|
).encode() if video else None,
|
||||||
|
file_unique_id=FileUniqueId(
|
||||||
|
file_unique_type=FileUniqueType.DOCUMENT,
|
||||||
|
media_id=video.id
|
||||||
|
).encode() if video else None,
|
||||||
|
width=video_size.w,
|
||||||
|
height=video_size.h,
|
||||||
|
file_size=video_size.size,
|
||||||
|
date=utils.timestamp_to_datetime(video.date) if video else None,
|
||||||
|
file_name=f"chat_video_{video.date}_{client.rnd_id()}.mp4",
|
||||||
|
mime_type="video/mp4",
|
||||||
|
client=client
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ from typing import List
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw, utils
|
from pyrogram import raw, utils
|
||||||
from pyrogram import types
|
from pyrogram import types
|
||||||
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType
|
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource
|
||||||
from ..object import Object
|
from ..object import Object
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
import pyrogram
|
import pyrogram
|
||||||
from pyrogram import raw
|
from pyrogram import raw, types
|
||||||
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource
|
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource
|
||||||
from ..object import Object
|
from ..object import Object
|
||||||
|
|
||||||
|
|
@ -44,6 +44,16 @@ class ChatPhoto(Object):
|
||||||
big_photo_unique_id (``str``):
|
big_photo_unique_id (``str``):
|
||||||
Unique file identifier of big (640x640) chat photo, which is supposed to be the same over time and for
|
Unique file identifier of big (640x640) chat photo, which is supposed to be the same over time and for
|
||||||
different accounts. Can't be used to download or reuse the file.
|
different accounts. Can't be used to download or reuse the file.
|
||||||
|
|
||||||
|
has_animation (``bool``):
|
||||||
|
True, if the photo has animated variant
|
||||||
|
|
||||||
|
is_personal (``bool``):
|
||||||
|
True, if the photo is visible only for the current user
|
||||||
|
|
||||||
|
minithumbnail (:obj:`~pyrogram.types.StrippedThumbnail`, *optional*):
|
||||||
|
User profile photo minithumbnail; may be None.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -53,8 +63,10 @@ class ChatPhoto(Object):
|
||||||
small_file_id: str,
|
small_file_id: str,
|
||||||
small_photo_unique_id: str,
|
small_photo_unique_id: str,
|
||||||
big_file_id: str,
|
big_file_id: str,
|
||||||
big_photo_unique_id: str
|
big_photo_unique_id: str,
|
||||||
|
has_animation: bool,
|
||||||
|
is_personal: bool,
|
||||||
|
minithumbnail: "types.StrippedThumbnail" = None
|
||||||
):
|
):
|
||||||
super().__init__(client)
|
super().__init__(client)
|
||||||
|
|
||||||
|
|
@ -62,6 +74,9 @@ class ChatPhoto(Object):
|
||||||
self.small_photo_unique_id = small_photo_unique_id
|
self.small_photo_unique_id = small_photo_unique_id
|
||||||
self.big_file_id = big_file_id
|
self.big_file_id = big_file_id
|
||||||
self.big_photo_unique_id = big_photo_unique_id
|
self.big_photo_unique_id = big_photo_unique_id
|
||||||
|
self.has_animation = has_animation
|
||||||
|
self.is_personal = is_personal
|
||||||
|
self.minithumbnail = minithumbnail
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse(
|
def _parse(
|
||||||
|
|
@ -104,5 +119,10 @@ class ChatPhoto(Object):
|
||||||
file_unique_type=FileUniqueType.DOCUMENT,
|
file_unique_type=FileUniqueType.DOCUMENT,
|
||||||
media_id=chat_photo.photo_id
|
media_id=chat_photo.photo_id
|
||||||
).encode(),
|
).encode(),
|
||||||
|
has_animation=chat_photo.has_video,
|
||||||
|
is_personal=getattr(chat_photo, "personal", False),
|
||||||
|
minithumbnail=types.StrippedThumbnail(
|
||||||
|
data=chat_photo.stripped_thumb
|
||||||
|
) if chat_photo.stripped_thumb else None,
|
||||||
client=client
|
client=client
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue