def _parse(client, chat_photo: Union["raw.types.UserProfilePhoto", "raw.types.ChatPhoto"], peer_id: int, peer_access_hash: int): if not isinstance(chat_photo, (raw.types.UserProfilePhoto, raw.types.ChatPhoto)): return None return ChatPhoto(small_file_id=FileId( file_type=FileType.CHAT_PHOTO, dc_id=chat_photo.dc_id, media_id=chat_photo.photo_id, access_hash=0, volume_id=0, thumbnail_source=ThumbnailSource.CHAT_PHOTO_SMALL, local_id=0, chat_id=peer_id, chat_access_hash=peer_access_hash).encode(), small_photo_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=chat_photo.photo_id).encode(), big_file_id=FileId( file_type=FileType.CHAT_PHOTO, dc_id=chat_photo.dc_id, media_id=chat_photo.photo_id, access_hash=0, volume_id=0, thumbnail_source=ThumbnailSource.CHAT_PHOTO_BIG, local_id=0, chat_id=peer_id, chat_access_hash=peer_access_hash).encode(), big_photo_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=chat_photo.photo_id).encode(), client=client)
async def _parse( client, sticker: "raw.types.Document", image_size_attributes: "raw.types.DocumentAttributeImageSize", sticker_attributes: "raw.types.DocumentAttributeSticker", file_name: str) -> "Sticker": sticker_set = sticker_attributes.stickerset if isinstance(sticker_set, raw.types.InputStickerSetID): input_sticker_set_id = (sticker_set.id, sticker_set.access_hash) set_name = await Sticker._get_sticker_set_name( client.send, input_sticker_set_id) else: set_name = None return Sticker( file_id=FileId(file_type=FileType.STICKER, dc_id=sticker.dc_id, media_id=sticker.id, access_hash=sticker.access_hash, file_reference=sticker.file_reference).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=sticker.id).encode(), width=image_size_attributes.w if image_size_attributes else 512, height=image_size_attributes.h if image_size_attributes else 512, is_animated=sticker.mime_type == "application/x-tgsticker", # TODO: mask_position set_name=set_name, emoji=sticker_attributes.alt or None, file_size=sticker.size, mime_type=sticker.mime_type, file_name=file_name, date=sticker.date, thumbs=types.Thumbnail._parse(client, sticker), client=client)
def _parse(client, video: "raw.types.Document", video_attributes: "raw.types.DocumentAttributeVideo", file_name: str, ttl_seconds: int = None) -> "Video": return Video(file_id=FileId( file_type=FileType.VIDEO, dc_id=video.dc_id, media_id=video.id, access_hash=video.access_hash, file_reference=video.file_reference).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=video.id).encode(), width=video_attributes.w, height=video_attributes.h, duration=video_attributes.duration, file_name=file_name, mime_type=video.mime_type, supports_streaming=video_attributes.supports_streaming, file_size=video.size, date=utils.timestamp_to_datetime(video.date), ttl_seconds=ttl_seconds, thumbs=types.Thumbnail._parse(client, video), client=client)
def _parse( client, video_note: "raw.types.Document", video_attributes: "raw.types.DocumentAttributeVideo" ) -> "VideoNote": return VideoNote( file_id=FileId( file_type=FileType.VIDEO_NOTE, dc_id=video_note.dc_id, media_id=video_note.id, access_hash=video_note.access_hash, file_reference=video_note.file_reference ).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=video_note.id ).encode(), length=video_attributes.w, duration=video_attributes.duration, file_size=video_note.size, mime_type=video_note.mime_type, date=utils.timestamp_to_datetime(video_note.date), thumbs=types.Thumbnail._parse(client, video_note), client=client )
def _parse( client, audio: "raw.types.Document", audio_attributes: "raw.types.DocumentAttributeAudio", file_name: str ) -> "Audio": return Audio( file_id=FileId( file_type=FileType.AUDIO, dc_id=audio.dc_id, media_id=audio.id, access_hash=audio.access_hash, file_reference=audio.file_reference ).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=audio.id ).encode(), duration=audio_attributes.duration, performer=audio_attributes.performer, title=audio_attributes.title, mime_type=audio.mime_type, file_size=audio.size, file_name=file_name, date=audio.date, thumbs=types.Thumbnail._parse(client, audio), client=client )
def _parse( client, media: Union["raw.types.Photo", "raw.types.Document"] ) -> Union[List[Union["types.StrippedThumbnail", "Thumbnail"]], None]: if isinstance(media, raw.types.Photo): raw_thumbnails = media.sizes[:-1] elif isinstance(media, raw.types.Document): raw_thumbnails = media.thumbs if not raw_thumbnails: return None else: return None thumbnails = [] file_type = FileType.PHOTO if isinstance( media, raw.types.Photo) else FileType.THUMBNAIL thumbnail_file_type = file_type for thumbnail in raw_thumbnails: # TODO: Enable this # if isinstance(thumbnail, types.PhotoStrippedSize): # thumbnails.append(StrippedThumbnail._parse(client, thumbnail)) if isinstance(thumbnail, raw.types.PhotoSize): thumbnails.append( Thumbnail( file_id=FileId( file_type=file_type, dc_id=media.dc_id, media_id=media.id, access_hash=media.access_hash, file_reference=media.file_reference, thumbnail_file_type=thumbnail_file_type, thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_size=thumbnail.type, volume_id=thumbnail.location.volume_id, local_id=thumbnail.location.local_id).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.PHOTO, media_id=media.id, volume_id=thumbnail.location.volume_id, local_id=thumbnail.location.local_id).encode(), width=thumbnail.w, height=thumbnail.h, file_size=thumbnail.size, client=client)) return thumbnails or None
def _parse(client, voice: "raw.types.Document", attributes: "raw.types.DocumentAttributeAudio") -> "Voice": return Voice(file_id=FileId( file_type=FileType.VOICE, dc_id=voice.dc_id, media_id=voice.id, access_hash=voice.access_hash, file_reference=voice.file_reference).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=voice.id).encode(), duration=attributes.duration, mime_type=voice.mime_type, file_size=voice.size, waveform=attributes.waveform, date=voice.date, client=client)
def _parse(client, document: "raw.types.Document", file_name: str) -> "Document": return Document(file_id=FileId( file_type=FileType.DOCUMENT, dc_id=document.dc_id, media_id=document.id, access_hash=document.access_hash, file_reference=document.file_reference).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=document.id).encode(), file_name=file_name, mime_type=document.mime_type, file_size=document.size, date=utils.timestamp_to_datetime(document.date), thumbs=types.Thumbnail._parse(client, document), client=client)
def _parse(client, photo: "raw.types.Photo", ttl_seconds: int = None) -> "Photo": if isinstance(photo, raw.types.Photo): try: progressive = next( p for p in photo.sizes if isinstance(p, raw.types.PhotoSizeProgressive)) except StopIteration: photo_size_objs = [ p for p in photo.sizes if isinstance(p, raw.types.PhotoSize) ] photo_size_objs.sort(key=lambda p: p.size) big = photo_size_objs[-1] else: big = raw.types.PhotoSize(type=progressive.type, location=progressive.location, w=progressive.w, h=progressive.h, size=sorted(progressive.sizes)[-1]) return Photo(file_id=FileId( file_type=FileType.PHOTO, dc_id=photo.dc_id, media_id=photo.id, access_hash=photo.access_hash, file_reference=photo.file_reference, thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_file_type=FileType.PHOTO, thumbnail_size=big.type, volume_id=big.location.volume_id, local_id=big.location.local_id).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.PHOTO, media_id=photo.id, volume_id=big.location.volume_id, local_id=big.location.local_id).encode(), width=big.w, height=big.h, file_size=big.size, date=photo.date, ttl_seconds=ttl_seconds, thumbs=types.Thumbnail._parse(client, photo), client=client)
def _parse( client, media: Union["raw.types.Photo", "raw.types.Document"] ) -> Optional[List["Thumbnail"]]: if isinstance(media, raw.types.Photo): raw_thumbs = [ i for i in media.sizes if isinstance(i, raw.types.PhotoSize) ] raw_thumbs.sort(key=lambda p: p.size) raw_thumbs = raw_thumbs[:-1] file_type = FileType.PHOTO elif isinstance(media, raw.types.Document): raw_thumbs = media.thumbs file_type = FileType.THUMBNAIL else: return parsed_thumbs = [] for thumb in raw_thumbs: if not isinstance(thumb, raw.types.PhotoSize): continue parsed_thumbs.append( Thumbnail(file_id=FileId( file_type=file_type, dc_id=media.dc_id, media_id=media.id, access_hash=media.access_hash, file_reference=media.file_reference, thumbnail_file_type=file_type, thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_size=thumb.type, volume_id=0, local_id=0).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=media.id).encode(), width=thumb.w, height=thumb.h, file_size=thumb.size, client=client)) return parsed_thumbs or None
def _parse(client, photo: "raw.types.Photo", ttl_seconds: int = None) -> "Photo": if isinstance(photo, raw.types.Photo): photos: List[raw.types.PhotoSize] = [] for p in photo.sizes: if isinstance(p, raw.types.PhotoSize): photos.append(p) if isinstance(p, raw.types.PhotoSizeProgressive): photos.append( raw.types.PhotoSize(type=p.type, w=p.w, h=p.h, size=max(p.sizes))) photos.sort(key=lambda p: p.size) main = photos[-1] return Photo(file_id=FileId( file_type=FileType.PHOTO, dc_id=photo.dc_id, media_id=photo.id, access_hash=photo.access_hash, file_reference=photo.file_reference, thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_file_type=FileType.PHOTO, thumbnail_size=main.type, volume_id=0, local_id=0).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=photo.id).encode(), width=main.w, height=main.h, file_size=main.size, date=photo.date, ttl_seconds=ttl_seconds, thumbs=types.Thumbnail._parse(client, photo), client=client)
def _parse(client, photo: "raw.types.Photo", ttl_seconds: int = None) -> Optional["Photo"]: if isinstance(photo, raw.types.Photo): big = photo.sizes[-1] if isinstance( big, (raw.types.PhotoStrippedSize, raw.types.PhotoPathSize)): return None if isinstance(big, raw.types.PhotoSizeProgressive): big = raw.types.PhotoSize(type=big.type, location=big.location, w=big.w, h=big.h, size=big.sizes[-1]) return Photo(file_id=FileId( file_type=FileType.PHOTO, dc_id=photo.dc_id, media_id=photo.id, access_hash=photo.access_hash, file_reference=photo.file_reference, thumbnail_source=ThumbnailSource.THUMBNAIL, thumbnail_file_type=FileType.PHOTO, thumbnail_size=big.type, volume_id=big.location.volume_id, local_id=big.location.local_id).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.PHOTO, media_id=photo.id, volume_id=big.location.volume_id, local_id=big.location.local_id).encode(), width=big.w, height=big.h, file_size=big.size, date=photo.date, ttl_seconds=ttl_seconds, thumbs=types.Thumbnail._parse(client, photo), client=client)
def _parse(client, animation: "raw.types.Document", video_attributes: "raw.types.DocumentAttributeVideo", file_name: str) -> "Animation": return Animation(file_id=FileId( file_type=FileType.ANIMATION, dc_id=animation.dc_id, media_id=animation.id, access_hash=animation.access_hash, file_reference=animation.file_reference).encode(), file_unique_id=FileUniqueId( file_unique_type=FileUniqueType.DOCUMENT, media_id=animation.id).encode(), width=getattr(video_attributes, "w", 0), height=getattr(video_attributes, "h", 0), duration=getattr(video_attributes, "duration", 0), mime_type=animation.mime_type, file_size=animation.size, file_name=file_name, date=animation.date, thumbs=types.Thumbnail._parse(client, animation), client=client)
def check_unique(file_unique_id: str, expected_file_unique_type: FileUniqueType): decoded = FileUniqueId.decode(file_unique_id) assert decoded.file_unique_type == expected_file_unique_type assert decoded.encode() == file_unique_id
def test_stringify_file_unique_id(): file_unique_id = "AgADBAADgaS5SQ" string = "{'file_unique_type': <FileUniqueType.DOCUMENT: 2>, 'media_id': 5312458109417947140}" assert str(FileUniqueId.decode(file_unique_id)) == string