U
    h}                     @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ ddlmZmZ ddlmZmZmZ dd	lmZ zddlZddlZddlZW n ek
r   dZY nX ed
d edd edd edd edd edd edd edd edd edd edd ed d! ed"d# ed$d% ed&d' ed(d) ed*d+ ed,Z ed-Z!ed.ej"Z#ed/d0Z$e%e&Z'dd2d3Z(d4d5 Z)d6d7 Z*d8d9 Z+dd;d<Z,d=d> Z-d?d@ Z.dAdB Z/dCdD Z0dEdF Z1dGdH Z2dIdJ Z3dKddKdKdKdKddLdMdNZ4dOdP Z5dQdR Z6e,fdSdTZ7dUdV Z8dWdX Z9dddKdKdKdKddYdZd[Z:d\d] Z;d^d_ Z<d`da Z=dbdc Z>ddde Z?dfdg Z@dhdi ZAdjdk ZBdldm ZCdndo ZDdpdq ZEdrds ZFdtdu ZGddvdwZHdxdy ZIdzd{ ZJd|d} ZKd~d ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdd ZTdd1ddddZUG dd dZVdd ZWdd ZXdS )z
Utilities for working with the Telegram API itself (such as handy methods
to convert between an entity like a User, Chat, etc. into its Input version)
    N)
namedtuple)guess_extension)GeneratorType   )markdownhtml)add_surrogatedel_surrogate
strip_text)typesz	image/pngz.pngz
image/jpegz.jpegz
image/webpz.webpz	image/gifz.gifz	image/bmpz.bmpzimage/x-tgaz.tgaz
image/tiffz.tiffzimage/vnd.adobe.photoshopz.psdz	video/mp4z.mp4zvideo/quicktimez.movz	video/aviz.aviz
audio/mpegz.mp3z	audio/m4az.m4az	audio/aacz.aacz	audio/oggz.oggz
audio/flacz.flaczapplication/x-tgstickerz.tgszJ@|(?:https?://)?(?:www\.)?(?:telegram\.(?:me|dog)|t\.me)/(@|\+|joinchat/)?ztg://(join)\?invite=z ^[a-z](?:(?!__)\w){1,30}[a-z\d]$ZFileInfozdc_id location sized   c                 c   s6   t | }|d8 }|D ]}t|gt||V  qdS )z
    Turns the given iterable into chunks of the specified size,
    which is 100 by default since that's what Telegram uses the most.
    r   N)iter	itertoolschainislice)iterablesizeithead r   2/tmp/pip-unpacked-wheel-c81u5j2r/telethon/utils.pychunksH   s    r   c                 C   sf   t | tjrF| jr(| jr(d| j| jS | jr4| jS | jr@| jS dS nt | tjtjtjfrb| j	S dS )z}
    Gets the display name for the given :tl:`User`,
    :tl:`Chat` or :tl:`Channel`. Returns an empty string otherwise.
    z{} {} )

isinstancer   User	last_name
first_nameformatChatChatForbiddenChanneltitleentityr   r   r   get_display_nameS   s    r$   c                 C   s   zt |  W dS  tk
r:   t| tjtjfr6Y dS Y nX t| tjrN| j} t| tjtj	tj
fr| jdkrrdS t| jp~dS dS )z8Gets the corresponding extension for any Telegram media.z.jpgapplication/octet-streamr   )get_input_photo	TypeErrorr   r   UserProfilePhoto	ChatPhotoMessageMediaDocumentdocumentDocumentZWebDocumentZWebDocumentNoProxy	mime_typer   )mediar   r   r   get_extensionh   s"      
r/   c                 C   s   t dt| j|d S )Nz!Cannot cast {} to any kind of {}.)r'   r   type__name__)r#   targetr   r   r   _raise_cast_fail   s     r3   Tc                 C   s0  z| j dkr| W S W nN tk
rb   |r<t| dr<| j Y S t| drTt| j Y S t| d Y nX t| tj	r| j
r|rt S | jdk	r| jr|st| j| jS tdt| tjtjtjfrt| jS t| tjr| jdk	r| jr|st| j| jS tdt| tjr*t| j| jS t| tjrHt| j| jS t| tjrft| j| jS t| tjr|t S t| tjrt| j| j | jS t| tj!rt"| j| j | jS t| tj#rt$ S t| tj%rt| j&S t| tj'rt| jS t| tj(r"t| j)S t| d dS )a  
    Gets the input peer for the given "entity" (user, chat or channel).

    A ``TypeError`` is raised if the given entity isn't a supported type
    or if ``check_hash is True`` but the entity's ``access_hash is None``
    *or* the entity contains ``min`` information. In this case, the hash
    cannot be used for general purposes, and thus is not returned to avoid
    any issues which can derive from invalid access hashes.

    Note that ``check_hash`` **is ignored** if an input peer is already
    passed since in that case we assume the user knows what they're doing.
    This is key to getting entities by explicitly passing ``hash = 0``.
       9 input_entityr#   Z	InputPeerNz4User without access_hash or min info cannot be inputz7Channel without access_hash or min info cannot be input)*SUBCLASS_OF_IDAttributeErrorhasattrr5   get_input_peerr#   r3   r   r   r   is_selfInputPeerSelfaccess_hashminInputPeerUseridr'   r   	ChatEmptyr   InputPeerChatr    InputPeerChannelChannelForbidden	InputUseruser_idInputChannel
channel_idInputUserSelfInputUserFromMessageInputPeerUserFromMessagepeermsg_idInputChannelFromMessageInputPeerChannelFromMessage	UserEmptyInputPeerEmptyUserFulluserChatFullPeerChatchat_id)r#   
allow_self
check_hashr   r   r   r9      sT    !





r9   c                 C   s   z| j dkr| W S W n tk
r2   t| d Y nX t| tjtjfrZt| j| j	pVdS t| tj
rvt| j| j	S t| tjrt| j| j| jS t| d dS )a'  
    Similar to :meth:`get_input_peer`, but for :tl:`InputChannel`'s alone.

    .. important::

        This method does not validate for invalid general-purpose access
        hashes, unlike `get_input_peer`. Consider using instead:
        ``get_input_channel(get_input_peer(channel))``.
    i@rF   r   N)r6   r7   r3   r   r   r    rC   rF   r?   r<   rB   rG   rN   rM   rK   rL   r"   r   r   r   get_input_channel   s    


rX   c                 C   s   z| j dkr| W S W n tk
r2   t| d Y nX t| tjrb| jrNt S t| j	| j
p^dS t| tjrvt S t| tjtjfrt S t| tjrt| jS t| tjrt| j| j
S t| tjrt| j| j| jS t| d dS )a$  
    Similar to :meth:`get_input_peer`, but for :tl:`InputUser`'s alone.

    .. important::

        This method does not validate for invalid general-purpose access
        hashes, unlike `get_input_peer`. Consider using instead:
        ``get_input_channel(get_input_peer(channel))``.
    l   F?L rD   r   N)r6   r7   r3   r   r   r   r:   rH   rD   r?   r<   r;   rO   rP   ZInputUserEmptyrQ   get_input_userrR   r>   rE   rJ   rI   rK   rL   r"   r   r   r   rY     s(    



rY   c                 C   s~   z*| j dkr| W S | j dkr(t| W S W n tk
rH   t| d Y nX ztt| W S  tk
rn   Y nX t| d dS )z2Similar to :meth:`get_input_peer`, but for dialogsl   9D r4   InputDialogPeerN)r6   r   rZ   r7   r3   r9   r'   )dialogr   r   r   get_input_dialog+  s    

r\   c                 C   s   z| j dkr| W S W n tk
r2   t| d Y nX t| tjrVtj| j| j| j	dS t| tj
rjt S t| tjrt| jS t| tjrt| jS t| d dS )z4Similar to :meth:`get_input_peer`, but for documents   h[f InputDocumentr?   r<   file_referenceN)r6   r7   r3   r   r   r,   r^   r?   r<   r`   DocumentEmptyZInputDocumentEmptyr*   get_input_documentr+   Messager.   )r+   r   r   r   rb   =  s$    

 

rb   c                 C   s(  z| j dkr| W S W n tk
r2   t| d Y nX t| tjrF| j} t| tjjtj	fr`| j
} t| tjrtj| j| j| jdS t| tjrt S t| tjjr| j} t| tjrt| jS t| tjrt| jS t| tjtjtjfrt| j
S t| tjtjtjtjfrt S t| d dS )z1Similar to :meth:`get_input_peer`, but for photos   c 
InputPhotor_   N) r6   r7   r3   r   r   rc   r.   photosPhotoMessageMediaPhotophotore   r?   r<   r`   
PhotoEmptyInputPhotoEmptymessagesrS   Z	full_chatChannelFullr&   Z
chat_photorQ   Zprofile_photor    r   r   rO   r@   r   rC   ri   r   r   r   r&   V  s:    




 r&   c                 C   s   z*| j dkr| W S | j dkr(t| W S W n tk
rH   t| d Y nX t| } t| tjrht| S t| tj	r|t
 S t| d dS )z6Similar to :meth:`get_input_peer`, but for chat photosl   t-) l   _N InputChatPhotoN)r6   r   ZInputChatUploadedPhotor7   r3   r&   r   re   ro   rk   ZInputChatPhotoEmptyrn   r   r   r   get_input_chat_photo|  s    


rp   c                 C   s   z| j dkr| W S W n tk
r2   t| d Y nX t| tjrRtj| j| jdS t| tj	rft
 S t| tjr|t| jS t| tjrt| jS t| d dS )z5Similar to :meth:`get_input_peer`, but for geo pointsi%0InputGeoPoint)latlongN)r6   r7   r3   r   r   ZGeoPointrq   rr   rs   ZGeoPointEmptyZInputGeoPointEmptyMessageMediaGeoget_input_geogeorc   r.   )rv   r   r   r   ru     s    



ru   F)is_photo
attributesforce_document
voice_note
video_notesupports_streamingttlc                C   s  zH| j dkr| W S | j dkr,tj| |dW S | j dkrFtj| |dW S W n tk
rf   t| d Y nX t| tjrtjt| j	| j
|p| jdS t| tjtjjtjfrtjt| |dS t| tjrtjt| j|p| jdS t| tjtjfrtjt| |dS t| tjtjfr\|r0tj| |dS t| |||||d	\}}	tj| |	|||d
S t| tjrtjtj| jj| jjddS t| tjrtj | j!| j"| j#ddS t| tj$rtj%t&| j'dS t| tj(rtj)t&| j'| j*| j+| j,dS t| tj-r,tj.t&| j'| j/| j0| j1| j2ddS t| tj3rFt4| j5S t| tj6tj7tj8tj9tj:tj;frrt< S t| tj=rt>| j?||dS t| tj@r| jAjBr| jCjCstDddd | jCjCD }
nd}
tjE| jA|
| jCjF| jCjGdS t| tjHrtE| S t| d dS )a  
    Similar to :meth:`get_input_peer`, but for media.

    If the media is :tl:`InputFile` and ``is_photo`` is known to be `True`,
    it will be treated as an :tl:`InputMediaUploadedPhoto`. Else, the rest
    of parameters will indicate how to treat it.
    l   Fu rd   )ttl_secondsr]   
InputMedia)r?   spoilerr~   )r?   r~   )filer~   )rx   ry   rz   r{   r|   )r   r-   rx   Z
force_filer~   r?   r<   )r?   r   )phone_numberr   r   Zvcard)	geo_point)r   periodheadingproximity_notification_radius)r   r!   addressprovidervenue_idZ
venue_type)rw   r}   z6Cannot cast unanswered quiz to any kind of InputMedia.c                 S   s   g | ]}|j r|jqS r   )correctoption).0rr   r   r   
<listcomp>  s      z#get_input_media.<locals>.<listcomp>N)pollcorrect_answerssolutionsolution_entities)Ir6   r   ZInputMediaPhotoZInputMediaDocumentr7   r3   r   rh   r&   ri   r   r~   rg   rf   rj   r*   rb   r+   r,   ra   Z	InputFileZInputFileBigZInputMediaUploadedPhotoget_attributesZInputMediaUploadedDocumentZMessageMediaGameZInputMediaGameZInputGameIDZgamer?   r<   ZMessageMediaContactZInputMediaContactr   r   r   rt   ZInputMediaGeoPointru   rv   ZMessageMediaGeoLiveZInputMediaGeoLiver   r   r   ZMessageMediaVenueZInputMediaVenuer!   r   r   r   ZMessageMediaDiceZInputMediaDiceZemoticonZMessageMediaEmptyZMessageMediaUnsupportedZChatPhotoEmptyZUserProfilePhotoEmptyr)   r(   ZInputMediaEmptyrc   get_input_mediar.   ZMessageMediaPollr   Zquizresultsr'   ZInputMediaPollr   r   ZPoll)r.   rw   rx   ry   rz   r{   r|   r}   attrsmimer   r   r   r   r     s    



   
	   


r   c                 C   sf   zBt | trt| W S | jdkr(| W S | jdkr@t| jW S W n tk
rV   Y nX t| d dS )z:Similar to :meth:`get_input_peer`, but for input messages.iżT	 yr   N)r   intr   InputMessageIDr6   r?   r7   r3   messager   r   r   get_input_message-  s    


r   c                 C   sV   z2| j dkr| W S | j dkr0tj| j| jdW S W n tk
rP   t| d Y nX dS )z7Similar to :meth:`get_input_peer`, but for input calls.iaXi  r   InputGroupCallN)r6   r   r   r?   r<   r7   r3   )callr   r   r   get_input_group_call<  s    

r   c                 C   sp   | sdS | | }z| t| d  }W n: tk
rf   z||}W n tk
r`   d}Y nX Y nX ||fS )zE
    Returns ``(entity, input_entity)`` for the given entity ID.
    )NNr   N)get
resolve_idZ_as_input_peerr7   r'   )Z	entity_identitiescacher9   r#   r5   r   r   r   _get_entity_pairG  s    
r   c                 C   sn   | dkrdS t | tr| S t | tjr,| jS z| jdkr@| jW S W n tk
rV   Y nX tdt	| dS )z7Similar to :meth:`get_input_peer`, but for message IDs.Nr   zInvalid message type: {})
r   r   r   r   r?   r6   r7   r'   r   r0   r   r   r   r   get_message_id\  s    

r   c              
   C   s&  t sd S d }d}d}zzt| tr.t| d}n8t| trDt| }n"| }d}t	| dd rb| 
 }nd}|stW W d S | }t	| dd}t jjt jj|d| g |d}t j|W W :S  tk
r } ztd	| |j| W 5 d }~X Y nX W 5 |r
|r
|  n|r |r || X d S )
NTrbFseekablenamer   zfile:)sourcetagsfilenamezFailed to analyze %s: %s %s)hachoircloseseekr   stropenbytesioBytesIOgetattrr   tellparserguessZguessParserstreamZInputIOStreammetadataZextractMetadata	Exception_logwarning	__class__)r   r   Zclose_streamr   posr   r   er   r   r   _get_metadataq  s@    



(
r   )rx   r-   ry   rz   r{   r|   thumbc                C   s<  t | tr| n
t| dd}|dkr0t|d }tjttj	|i}	t
| rt| }
|
r|
drr|
d}n|
dr|
d}nd}tj||
dr|
dnd|t|
dr|
djndd	|	tj< |st| rt| }
|
rNtj||
d
r|
d
nd|
dr$|
dndt|
dr@|
djnd|d}nx|rt|}d}d}|r|d
r|d
}|r|dr|d}tjd||||d}ntjddd||d}||	tj< |rtj|	krd|	tj _ntjddd|	tj< |r"|D ]}||	t|< q|s,d}t|	 |fS )zp
    Get a list of attributes for the given file and
    the mime type as a tuple ([attribute], mime_type).
    r   ZunnamedNr   authorZartistr!   duration)voicer!   	performerr   widthr   height)round_messagewhr   r|   )r   r|   T)r   r%   )r   r   r   	mimetypes
guess_typer   ZDocumentAttributeFilenameospathbasenameis_audior   hasr   DocumentAttributeAudior   secondsis_videoDocumentAttributeVideor   r0   listvalues)r   rx   r-   ry   rz   r{   r|   r   r   Z	attr_dictmr   docZt_mr   r   ar   r   r   r     s    

	

      
r   c                    s    sdS t  fdddD r<t dd  j jfD r< S t r\G dd d} |_|S t trzttttd   W S  t	k
r   t
d	 Y qX ntd
 dS )zn
    Converts the given parse mode into an object with
    ``parse`` and ``unparse`` callable properties.
    Nc                 3   s   | ]}t  |V  qd S N)r8   r   xmoder   r   	<genexpr>  s     z&sanitize_parse_mode.<locals>.<genexpr>)parseunparsec                 s   s   | ]}t |V  qd S r   )callabler   r   r   r   r     s     c                   @   s   e Zd Zedd ZdS )z'sanitize_parse_mode.<locals>.CustomModec                 S   s   t d S r   )NotImplementedError)textr   r   r   r   r     s    z/sanitize_parse_mode.<locals>.CustomMode.unparseN)r1   
__module____qualname__staticmethodr   r   r   r   r   
CustomMode  s   r   )Zmdr   Zhtmr   zUnknown parse mode {}zInvalid parse mode type {})allr   r   r   r   r   r   r   lowerKeyError
ValueErrorr   r'   )r   r   r   r   r   sanitize_parse_mode  s,    
r   c                 C   s   t | }|j|jfS )z
    Similar to :meth:`get_input_peer`, but for input messages.

    Note that this returns a tuple ``(dc_id, location)``, the
    ``dc_id`` being present if known.
    )_get_file_infodc_idlocation)r   infor   r   r   get_input_location  s    r   c                 C   s   z| j dkrtd | d W S W n tk
r:   t| d Y nX t| tjrN| j} t| tjrb| j	} nt| tj
rt| j} t| tjrt| jtj| j| j| jdd| jS t| tjrt| jtj| j| j| j| jd jdt| jd S t| d d S )Nib#ZInputFileLocationr   )r?   r<   r`   Z
thumb_size)r6   	_FileInfor7   r3   r   r   rc   r.   r*   r+   rh   ri   r,   r   ZInputDocumentFileLocationr?   r<   r`   r   rg   ZInputPhotoFileLocationsizesr0   _photo_size_byte_count)r   r   r   r   r   '  s<    



r   c                 C   sN   t | trtj| d S t | tjr,| jS t| ddrBt	| j
S t| S dS )z
    Gets the extension for the given file, which can be either a
    str or an ``open()``'ed file (which has a ``.name`` attribute).
    r   r   N)r   r   r   r   splitextpathlibPathsuffixr   _get_extensionr   r/   r   r   r   r   r   H  s    

r   c                 C   s0   t dt| t j}|rdS tt| tjS dS )zT
    Returns `True` if the file extension looks like an image file to Telegram.
    z\.(png|jpe?g)TN)rematchr   
IGNORECASEr   resolve_bot_file_idr   rg   )r   r  r   r   r   is_imageY  s    r  c                 C   s   t dt| t jS )zQ
    Returns `True` if the file extension looks like a gif file to Telegram.
    z\.gif)r   r  r   r  r   r   r   r   is_gifd  s    r  c                 C   s\   t | }|s8t| }|r2|dr2|ddS dS n d| } t| d pPddS dS )z2Returns `True` if the file has an audio mime type.r-   zaudio/Fr   r   r   Nr   r   r   r   
startswithr   r   r   extr   r   r   r   r   k  s    r   c                 C   s\   t | }|s8t| }|r2|dr2|ddS dS n d| } t| d pPddS dS )z1Returns `True` if the file has a video mime type.r-   zvideo/Fr   r   r   Nr  r  r   r   r   r   y  s    r   c                 C   s   t | ttttttfS )a   
    Returns `True` if the given object looks like a list.

    Checking ``if hasattr(obj, '__iter__')`` and ignoring ``str/bytes`` is not
    enough. Things like ``open()`` are also iterable (and probably many
    other things), so just support the commonly known list-like objects.
    )r   r   tuplesetdictranger   )objr   r   r   is_list_like  s    r  c                 C   s4   t | trt| S tddt| } |  r0| S dS )z:Parses the given phone, or returns `None` if it's invalid.z[+()\s-]r   N)r   r   r   r   subisdigit)Zphoner   r   r   parse_phone  s
    
r  c                 C   sr   |   } t| pt| }|rT| | d } t|d}|rJ| dfS | d} t| rj| 	 dfS dS dS )a<  
    Parses the given username or channel access hash, given
    a string, username or URL. Returns a tuple consisting of
    both the stripped, lowercase username and whether it is
    a joinchat/ hash (in which case is not lowercase'd).

    Returns ``(None, False)`` if the ``username`` or link is not valid.
    Nr   T/F)NF)
stripUSERNAME_REr  
TG_JOIN_REendboolgrouprstripVALID_USERNAME_REr   )usernamer   Z	is_inviter   r   r   parse_username  s    	

r  c                 C   sB   t | } g }|D ],}|j}|j|j }|t| ||  q|S )aK  
    Gets the inner text that's surrounded by the given entities.
    For instance: text = 'hey!', entity = MessageEntityBold(2, 2) -> 'y!'.

    :param text:     the original text.
    :param entities: the entity or entities that must be matched.
    :return: a single result or a list of the text surrounded by the entities.
    )r   offsetlengthappendr	   )r   r   resultr   startr  r   r   r   get_inner_text  s    	r#  c              	   C   sZ  z.t | tr$t| \}}||W S | jdkr4| W S t | tjjtjtjtj	tj
fr\| jW S t | tjrvt| jW S t | tjrt| jW S t | tjrt| jW S | jdkrt| jW S t| ddd} t | tjtjfrt| jW S t | tjrt| jW S t | tjtjfr.t| jW S W n ttfk
rJ   Y nX t| d d S )NiV)io|}l   |3 F)rV   rW   ZPeer)r   r   r   r6   r   ZcontactsZResolvedPeerZInputNotifyPeerZTopPeerDialogZ
DialogPeerrK   rm   PeerChannelr?   rO   PeerUserr@   rT   rE   r9   r>   rJ   rA   rU   rB   rN   rG   r7   r'   r3   )rK   pidclsr   r   r   get_peer  s@    


   
r)  c                 C   s   t | tr|r| S t| d S t | tjr4t| d zt| } W n tk
r^   t| d Y nX t | tjrr| j	S t | tj
rd| j  k rdksn t| jd | _|r| j S | jS d| j  k rdksn t| jd | _|s| jS d| j  S dS )a  
    Convert the given peer into its marked ID by default.

    This "mark" comes from the "bot api" format, and with it the peer type
    can be identified back. User ID is left unmodified, chat ID is negated,
    and channel ID is "prefixed" with -100:

    * ``user_id``
    * ``-chat_id``
    * ``-100channel_id``

    The original ID and the peer type class can be returned with
    a call to :meth:`resolve_id(marked_id)`.
    r   z.int (you might want to use client.get_peer_id)r   l   c(	     J)N)r   r   r   r   r;   r3   r)  r'   r&  rE   rT   rU   rG   )rK   Zadd_markr   r   r   get_peer_id  s&    

r+  c                 C   s@   | dkr| t jfS |  } | dkr2| d8 } | t jfS | t jfS dS )zCGiven a marked ID, returns the original ID and its :tl:`Peer` type.r   r*  N)r   r&  r%  rT   )Z	marked_idr   r   r   r     s    

r   c                 C   sN   | s| S d}d}| D ]0}|dkr2||| 7 }d}q||7 }t |g}q|| S )z,
    Decodes run-length-encoded `data`.
            r   )datanewlastcurr   r   r   _rle_decode'  s    r3  c                 C   sL   d}d}| D ]:}|s|d7 }q|r8|dt |g 7 }d}|t |g7 }q|S )Nr,  r   r   r-  r.  )stringr0  countr2  r   r   r   _rle_encode;  s    
r6  c              
   C   s@   zt | dt| d   W S  tjttfk
r:   Y dS X dS )a(  
    Decodes a url-safe base64-encoded string into its bytes
    by first adding the stripped necessary padding characters.

    This is the way Telegram shares binary data as strings,
    such as Bot API-style file IDs or invite links.

    Returns `None` if the input string was not valid.
    =   N)base64urlsafe_b64decodelenbinasciiErrorr   r'   r4  r   r   r   _decode_telegram_base64J  s    
r?  c              
   C   s<   zt | ddW S  tjttfk
r6   Y dS X dS )z0
    Inverse for `_decode_telegram_base64`.
       =asciiN)r9  urlsafe_b64encoder  decoder<  r=  r   r'   r>  r   r   r   _encode_telegram_base64Z  s    rD  c                 C   sx  t t| }|sdS |dd |d  }}|dkr6dS |dkrJt|dksb|dkrft|dkrf|dkrtd|\}}}}ntd	|\}}}}}d
|  krdksn dS g }|dks|dkr|tjd|dkd nl|dks|dkr|tjddd|dkd n<|dkr2|tj	dt
 d n|dkrJ|t  tj||dddd||dd	S |dkr~t|dks|dkrtt|dkrt|dkrtd|\}}}}}	}
}n^t|dkrtd|\}}}}}	}
}}n2t|dkrtd|\	}}}}}}	}}}ndS d
|  kr2dks8n dS |sD|rHdnd }tj||ddtj|dddd!g|dd"S dS )#a  
    Given a Bot API-style `file_id <telethon.tl.custom.file.File.id>`,
    returns the media it represents. If the `file_id <telethon.tl.custom.file.File.id>`
    is not valid, `None` is returned instead.

    Note that the `file_id <telethon.tl.custom.file.File.id>` does not have information
    such as image dimensions or file size, so these will be zero if present.

    For thumbnails, the photo ID and hash will always be zero.
    Nr   )   r8  rE     r8     z<iiqq<iiqqbr         	   r   )r   r      )r   r   r   r      r   )ZaltZ
stickerset
   r,  )	r?   r<   dater-   r   Zthumbsr   rx   r`   ,   )1   M   z<iiqqqqirQ  z
<iiqqqqi5srR  z<ii28sqqq12sibsr   )r0   r   r   r   )r?   r<   r`   rO  r   r   Zhas_stickers)r3  r?  r;  structunpackr   r   r   r   DocumentAttributeStickerZInputStickerSetEmptyDocumentAttributeAnimatedr,   rg   	PhotoSize)Zfile_idr/  version	file_typer   Zmedia_idr<   _rx   	volume_idsecretlocal_idZ
photo_sizer   r   r   r  d  s    ,




0


  

    r  c                 C   s0  t | tjr| j} nt | tjr&| j} t | tjrd}| jD ]f}t |tjr\|j	rVdnd}nBt |tj
rx|jrrdnd}n&t |tjrd}nt |tjr<d}nq< qq<tttd|| j| j| jd	S t | tjr(td
d t| jD d}|sdS |j}tttdd	| j| j| j|jd|jd		S dS dS )a  
    Inverse operation for `resolve_bot_file_id`.

    The only parameters this method will accept are :tl:`Document` and
    :tl:`Photo`, and it will return a variable-length ``file_id`` string.

    If an invalid parameter is given, it will ``return None``.
    rI  rJ  rK  rL  r8  rM  rN  rH  rE  c                 s   s$   | ]}t |tjtjfr|V  qd S r   )r   r   rX  PhotoCachedSizer   r   r   r   r     s      
 z#pack_bot_file_id.<locals>.<genexpr>Nz	<iiqqqqibr   )r   r   r*   r+   rh   ri   r,   rx   r   r   r   r   rV  rW  rD  r6  rT  packr   r?   r<   rg   nextreversedr   r   r\  r^  )r   rZ  	attributer   r   r   r   pack_bot_file_id  sX    	
            rd  c              	   C   s   t | \}}|s| }td|r8t|dkr8t|}nt|}z<t|dkr`d	td|W S t|dkrztd|W S W n tj	t
fk
r   Y nX dS )
a  
    Resolves the given invite link. Returns a tuple of
    ``(link creator user id, global chat id, random int)``.

    Note that for broadcast channels or with the newest link format, the link
    creator user ID will be zero to protect their identity. Normal chats and
    megagroup channels will have such ID.

    Note that the chat ID may not be accurate for chats with a link that were
    upgraded to megagroup, since the link can remain the same, but the chat
    ID will be correct once a new link is generated.
    z[a-fA-F\d]+)rF         r   z>LQ   z>LLQ)NNN)r   )r  r   r  r;  r   fromhexr?  rT  rU  errorr'   )linkZ	link_hashZis_linkpayloadr   r   r   resolve_invite_link  s    rl  c              	   C   sf   zDt dt| \}}}}|dk r.t| nt|}||||fW S  t jtfk
r`   Y dS X dS )a}  
    Resolves an inline message ID. Returns a tuple of
    ``(message id, peer, dc id, access hash)``

    The ``peer`` may either be a :tl:`PeerUser` referencing
    the user who sent the message via the bot in a private
    conversation or small group chat, or a :tl:`PeerChannel`
    if the message was sent in a channel.

    The ``access_hash`` does not have any use yet.
    z<iiiqr   )NNNNN)rT  rU  r?  r   r%  r&  ri  r'   )Zinline_msg_idr   Z
message_idr'  r<   rK   r   r   r   resolve_inline_message_id$  s    
rm  c                 C   s   | dkrdS | dkrdS dS )zn
    Gets the appropriated part size when uploading or downloading files,
    given an initial file size.
    i  @   i  .   i   r   )	file_sizer   r   r   get_appropriated_part_size9  s
    rq  c           	      C   s   t | d }|d d }t|d }tt | D ]b}t|d d\}}| | d@ |> }td|||d  d }||O }td||||d < q0t|d	| S )
af  
    Encodes the input `bytes` into a 5-bit byte-string
    to be used as a voice note's waveform. See `decode_waveform`
    for the reverse operation.

    Example
        .. code-block:: python

            chat = ...
            file = 'my.ogg'

            # Send 'my.ogg' with a ascending-triangle waveform
            await client.send_file(chat, file, attributes=[types.DocumentAttributeAudio(
                duration=7,
                voice=True,
                waveform=utils.encode_waveform(bytes(range(2 ** 5))  # 2**5 because 5-bit
            )]

            # Send 'my.ogg' with a square waveform
            await client.send_file(chat, file, attributes=[types.DocumentAttributeAudio(
                duration=7,
                voice=True,
                waveform=utils.encode_waveform(bytes((31, 31, 15, 15, 15, 15, 31, 31)) * 4)
            )]
    rI     rM  r      <HrE  r   N)r;  	bytearrayr  divmodrT  rU  r`  r   )	waveformZ
bits_countZbytes_countr!  i
byte_index	bit_shiftvalueZor_whatr   r   r   encode_waveformE  s    r|  c                 C   s   t | d }|d }|dkr dS t|}t|d D ]B}t|d d\}}td| ||d  d }||? d@ ||< q4t|d d\}}|t | d kr| | }ntd| ||d  d }||? d@ ||d < t|S )	z1
    Inverse operation of `encode_waveform`.
    rM  rI  r   r,  r   rt  rE  rs  )r;  ru  r  rv  rT  rU  r   )rw  Z	bit_countZvalue_countr!  rx  ry  rz  r{  r   r   r   decode_waveformn  s    
r}  i   )z\nz\s.)limitmax_entitiessplit_atc                c   s  dd }t | } tttj|}t||krL||d  }t||j|j }n|}t| |kr`q|D ]}t	t
|D  ]}	|j| |	d}
|
rv| d|
  | |
 d  }}g g  }}|D ]}|j|
 k r6|j|j |
 kr*||||
 |j d |||d|j|j |
  d n
|| q||||j|
  d	 qt||fV  || } } qvqvqd q qdqq t| |fV  dS )
a  
    Split a message text and entities into multiple messages, each with their
    own set of entities. This allows sending a very large message as multiple
    messages while respecting the formatting.

    Arguments
        text (`str`):
            The message text.

        entities (List[:tl:`MessageEntity`])
            The formatting entities.

        limit (`int`):
            The maximum message length of each individual message.

        max_entities (`int`):
            The maximum amount of entities that will be present in each
            individual message.

        split_at (Tuplel[`str`]):
            The list of regular expressions that will determine where to split
            the text. By default, a newline is searched. If no newline is
            present, a space is searched. If no space is found, the split will
            be made at any character.

            The last expression should always match a character, or else the
            text will stop being splitted and the resulting text may be larger
            than the limit.

    Yields
        Pairs of ``(str, entities)`` with the split message.

    Example
        .. code-block:: python

            from telethon import utils
            from telethon.extensions import markdown

            very_long_markdown_text = "..."
            text, entities = markdown.parse(very_long_markdown_text)

            for text, entities in utils.split_text(text, entities):
                await client.send_message(chat, text, formatting_entities=entities)
    c                 [   s$   |   }|d= || | jf |S )Nr[  )Zto_dictupdater   )entZupdateskwargsr   r   r   r    s    
zsplit_text.<locals>.updater   )r   N)r  r   )r  r  )r  )r   r
  mapr   compiler;  r=   r  r  rb  r  r  r  r   r	   )r   r   r  r  r  r  Zlast_entZ	cur_limitsplitrx  r   Zcur_textZnew_textZcur_entZnew_entr  r   r   r   
split_text  s:    /
"
&
r  c                   @   s   e Zd Zdd Zdd ZdS )AsyncClassWrapperc                 C   s
   || _ d S r   )wrapped)selfr  r   r   r   __init__  s    zAsyncClassWrapper.__init__c                    s,   t | j|  fdd}t r$|S  S d S )Nc                     s"    | |}t |r|I d H S |S r   )inspectisawaitable)argsr  valr   r   r   wrapper  s    
z.AsyncClassWrapper.__getattr__.<locals>.wrapper)r   r  r   )r  itemr  r   r  r   __getattr__  s
    zAsyncClassWrapper.__getattr__N)r1   r   r   r  r  r   r   r   r   r    s   r  c                 C   sX   t | dk s| d dkr| S td}d}| d |d< | d |d< t|| dd	  | S )
z
    Adds the JPG header and footer to a stripped image.

    Ported from https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/Telegram/SourceFiles/ui/image/image.cpp#L225
    rJ  r   r   so   JFIF       C (#(#!#-+(0<dA<77<{X]Idàڭ C+--<5<vAAv     "            	
    } !1AQa"q2#BR$3br	
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz        	
   w !1AQaq"2B	#3Rbr
$4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz   ? s      rE     N)r;  ru  r   )strippedheaderfooterr   r   r   stripped_photo_to_jpg  s    r  c                 C   s   t | tjr| jS t | tjrRt| jdk s:| jd dkrDt| jS t| jd S t | tjrht| jS t | tjrxdS t | tj	rt
| jS d S d S )NrJ  r   r   in  )r   r   rX  r   ZPhotoStrippedSizer;  r   r_  ZPhotoSizeEmptyZPhotoSizeProgressivemaxr   )r   r   r   r   r     s    


r   )r   )TT)T)Y__doc__r9  r<  r  r   r   loggingmathr   r   r   r   rT  collectionsr   r   r   r   
extensionsr   r   Zhelpersr   r	   r
   tlr   Zhachoir.metadataZhachoir.parserImportErroradd_typer  r  r  r  r  r   	getLoggerr1   r   r   r$   r/   r3   r9   rX   rY   r\   rb   r&   rp   ru   r   r   r   r   r   r   r   r   r   r   r   r  r  r   r   r  r  r  r#  r)  r+  r   r3  r6  r?  rD  r  rd  rl  rm  rq  r|  r}  r  r  r  r   r   r   r   r   <module>   s   




`(&     
3   W!!
"
0
k0%)_