B
    f’»da  ã               @   sN   d dl Z d dlmZmZmZ d dlZd dlmZ d dlmZ G dd„ dƒZ	dS )é    N)ÚUnionÚOptionalÚBinaryIO)Útypes)ÚFileIdc            	   @   s8   e Zd Zddedef eeeeeef  dœdd„ZdS )	ÚStreamMediar   zpyrogram.Clientztypes.Message)ÚselfÚmessageÚlimitÚoffsetÚreturnc          	   C  sâ   d}t |tjƒr>x0|D ]}t||dƒ}|dk	rP qW tdƒ‚n|}t |tƒrR|}n|j}t |¡}t|ddƒ}	|dk r |	dkr†tdƒ‚t	 
|	d d ¡}
||
7 }x<|  ||	||¡2 y3 dH }W n
 tk
sÒX |V  q²   Y  W dS )as  Stream the media from a message chunk by chunk.

        You can use this method to partially download a file into memory or to selectively download chunks of file.
        The chunk maximum size is 1 MiB (1024 * 1024 bytes).

        .. include:: /_includes/usable-by/users-bots.rst

        Parameters:
            message (:obj:`~pyrogram.types.Message` | ``str``):
                Pass a Message containing the media, the media itself (message.audio, message.video, ...) or a file id
                as string.

            limit (``int``, *optional*):
                Limit the amount of chunks to stream.
                Defaults to 0 (stream the whole media).

            offset (``int``, *optional*):
                How many chunks to skip before starting to stream.
                Defaults to 0 (start from the beginning).

        Returns:
            ``Generator``: A generator yielding bytes chunk by chunk

        Example:
            .. code-block:: python

                # Stream the whole media
                async for chunk in app.stream_media(message):
                    print(len(chunk))

                # Stream the first 3 chunks only
                async for chunk in app.stream_media(message, limit=3):
                    print(len(chunk))

                # Stream the rest of the media by skipping the first 3 chunks
                async for chunk in app.stream_media(message, offset=3):
                    print(len(chunk))

                # Stream the last 3 chunks only (negative offset)
                async for chunk in app.stream_media(message, offset=-3):
                    print(len(chunk))
        )	ÚaudioÚdocumentZphotoZstickerZ	animationÚvideoZvoiceZ
video_noteZnew_chat_photoNz3This message doesn't contain any downloadable mediaÚ	file_sizer   zNNegative offsets are not supported for file ids, pass a Message object insteadi   )Ú
isinstancer   ÚMessageÚgetattrÚ
ValueErrorÚstrZfile_idr   ÚdecodeÚmathÚceilZget_fileÚStopAsyncIteration)r   r	   r
   r   Zavailable_mediaÚkindZmediaZfile_id_strZfile_id_objr   ÚchunksÚchunk© r   úJ/tmp/pip-unpacked-wheel-rcokkf2l/pyrogram/methods/messages/stream_media.pyÚstream_media   s(    0



*zStreamMedia.stream_mediaN)r   r   )	Ú__name__Ú
__module__Ú__qualname__r   r   Úintr   r   r   r   r   r   r   r      s    r   )
r   Útypingr   r   r   Zpyrogramr   Zpyrogram.file_idr   r   r   r   r   r   Ú<module>   s
   