o
    Xzjd%  ã                   @   sv   d dl Z d dlZd dlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ejr+ddlmZ G dd	„ d	ƒZG d
d„ dƒZdS )é    Né   )Ú_NOT_A_REQUESTé   )ÚhelpersÚutils)Ú	functionsÚ	TLRequest)ÚTelegramClientc                       s€   e Zd ZdZdZdd„ Zedd„ ƒZejdd„ ƒZdd	„ Z	d
d„ Z
ejZejZddd„Z‡ fdd„Zdd„ Z‡ fdd„Z‡  ZS )Ú_TakeoutClientz'
    Proxy object over the client.
    )Ú	__enter__Ú__exit__Ú
__aenter__Ú	__aexit__c                 C   s   || _ || _|| _d | _d S ©N)Ú_TakeoutClient__finalizeÚ_TakeoutClient__clientÚ_TakeoutClient__requestÚ_TakeoutClient__success)ÚselfÚfinalizeÚclientÚrequest© r   úR/var/www/kodo/tsheker/venv/lib/python3.10/site-packages/telethon/client/account.pyÚ__init__   s   
z_TakeoutClient.__init__c                 C   s   | j S r   ©r   )r   r   r   r   Úsuccess   s   z_TakeoutClient.successc                 C   s
   || _ d S r   r   )r   Úvaluer   r   r   r   "   s   
c                 Ã   sD   �| j }|jjd u r|| jƒI d H j|j_| S | jd ur tdƒ‚| S )NzgCan't send a takeout request while another takeout for the current session still not been finished yet.)r   ÚsessionÚ
takeout_idr   ÚidÚ
ValueError)r   r   r   r   r   r   &   s   €
ýz_TakeoutClient.__aenter__c                 Ã   sZ   �| j d u r| jr|d u | _ | j d ur+| tj | j ¡ƒI d H }|s%tdƒ‚d | j_d S d S )NzFailed to finish the takeout.)r   r   r   ÚaccountÚFinishTakeoutSessionRequestr!   r   r   )r   Úexc_typeÚ	exc_valueÚ	tracebackÚresultr   r   r   r   0   s   €

ÿûz_TakeoutClient.__aexit__Fc                 Ã   s˜   �| j jj}|d u rtdƒ‚t |¡ }|r|fn|}g }|D ]}t|tƒs)tƒ ‚| 	| t¡I d H  | 
t ||¡¡ q| j |rD|d n||d�I d H S )NzJTakeout mode has not been initialized (are you calling outside of "with"?)r   )Úordered)r   r   r   r!   r   Úis_list_likeÚ
isinstancer   r   ÚresolveÚappendr   ÚInvokeWithTakeoutRequest)r   r   r(   r   ÚsingleÚrequestsÚwrappedÚrr   r   r   Ú__call__>   s   €

ÿz_TakeoutClient.__call__c                    s(   |  d¡r|t| ƒjvrt‚tƒ  |¡S )NÚ__)Ú
startswithÚtypeÚ_TakeoutClient__PROXY_INTERFACEÚAttributeErrorÚsuperÚ__getattribute__)r   Úname©Ú	__class__r   r   r9   P   s   z_TakeoutClient.__getattribute__c                 C   s0   t | j|ƒ}t |¡rt t | jj|ƒ| ¡S |S r   )Úgetattrr   ÚinspectÚismethodÚ	functoolsÚpartialr<   ©r   r:   r   r   r   r   Ú__getattr__]   s   
ÿz_TakeoutClient.__getattr__c                    s8   |  d t| ƒj d¡¡¡rtƒ  ||¡S t| j||ƒS )Nz_{}__Ú_)	r4   Úformatr5   Ú__name__Úlstripr8   Ú__setattr__Úsetattrr   rB   r;   r   r   rH   g   s   z_TakeoutClient.__setattr__)F)rF   Ú
__module__Ú__qualname__Ú__doc__r6   r   Úpropertyr   Úsetterr   r   r   Ú_sync_enterr   Ú
_sync_exitr   r2   r9   rC   rH   Ú__classcell__r   r   r;   r   r
      s     	




r
   c                   @   sj   e Zd Z	dddddddddœdddededed	ed
ededededdfdd„Zdddedefdd„ZdS )ÚAccountMethodsTN)ÚcontactsÚusersÚchatsÚ
megagroupsÚchannelsÚfilesÚmax_file_sizer   r	   r   rS   rT   rU   rV   rW   rX   rY   Úreturnc             	   C   s`   t |||||||d�}	dd„ |	 ¡ D ƒ}
| jjdu st|
ƒr(tjjdi |	¤Ž}nd}t|| |ƒS )a~  
        Returns a :ref:`telethon-client` which calls methods behind a takeout session.

        It does so by creating a proxy object over the current client through
        which making requests will use :tl:`InvokeWithTakeoutRequest` to wrap
        them. In other words, returns the current client modified so that
        requests are done as a takeout:

        Some of the calls made through the takeout session will have lower
        flood limits. This is useful if you want to export the data from
        conversations or mass-download media, since the rate limits will
        be lower. Only some requests will be affected, and you will need
        to adjust the `wait_time` of methods like `client.iter_messages
        <telethon.client.messages.MessageMethods.iter_messages>`.

        By default, all parameters are `None`, and you need to enable those
        you plan to use by setting them to either `True` or `False`.

        You should ``except errors.TakeoutInitDelayError as e``, since this
        exception will raise depending on the condition of the session. You
        can then access ``e.seconds`` to know how long you should wait for
        before calling the method again.

        There's also a `success` property available in the takeout proxy
        object, so from the `with` body you can set the boolean result that
        will be sent back to Telegram. But if it's left `None` as by
        default, then the action is based on the `finalize` parameter. If
        it's `True` then the takeout will be finished, and if no exception
        occurred during it, then `True` will be considered as a result.
        Otherwise, the takeout will not be finished and its ID will be
        preserved for future usage as `client.session.takeout_id
        <telethon.sessions.abstract.Session.takeout_id>`.

        Arguments
            finalize (`bool`):
                Whether the takeout session should be finalized upon
                exit or not.

            contacts (`bool`):
                Set to `True` if you plan on downloading contacts.

            users (`bool`):
                Set to `True` if you plan on downloading information
                from users and their private conversations with you.

            chats (`bool`):
                Set to `True` if you plan on downloading information
                from small group chats, such as messages and media.

            megagroups (`bool`):
                Set to `True` if you plan on downloading information
                from megagroups (channels), such as messages and media.

            channels (`bool`):
                Set to `True` if you plan on downloading information
                from broadcast channels, such as messages and media.

            files (`bool`):
                Set to `True` if you plan on downloading media and
                you don't only wish to export messages.

            max_file_size (`int`):
                The maximum file size, in bytes, that you plan
                to download for each message with media.

        Example
            .. code-block:: python

                from telethon import errors

                try:
                    async with client.takeout() as takeout:
                        await client.get_messages('me')  # normal call
                        await takeout.get_messages('me')  # wrapped through takeout (less limits)

                        async for message in takeout.iter_messages(chat, wait_time=0):
                            ...  # Do something with the message

                except errors.TakeoutInitDelayError as e:
                    print('Must wait', e.seconds, 'before takeout')
        )rS   Úmessage_usersÚmessage_chatsÚmessage_megagroupsÚmessage_channelsrX   Úfile_max_sizec                 s   s   � | ]}|d uV  qd S r   r   )Ú.0Úargr   r   r   Ú	<genexpr>Ô   s   € z)AccountMethods.takeout.<locals>.<genexpr>Nr   )	ÚdictÚvaluesr   r   Úanyr   r"   ÚInitTakeoutSessionRequestr
   )r   r   rS   rT   rU   rV   rW   rX   rY   Úrequest_kwargsÚarg_specifiedr   r   r   r   Útakeouto   s    \ù	
ÿzAccountMethods.takeoutr   c              	   Ã   sh   �z(t d| dƒ4 I dH š}||_W d  ƒI dH  W dS 1 I dH s"w   Y  W dS  ty3   Y dS w )ap  
        Finishes the current takeout session.

        Arguments
            success (`bool`):
                Whether the takeout completed successfully or not.

        Returns
            `True` if the operation was successful, `False` otherwise.

        Example
            .. code-block:: python

                await client.end_takeout(success=False)
        TNF)r
   r   r!   )r   r   ri   r   r   r   Úend_takeoutÞ   s   €ÿüþÿzAccountMethods.end_takeout)T)rF   rJ   rK   Úboolri   rj   r   r   r   r   rR   n   s@    þöÿþüûúùø	÷
ö

öorR   )r@   r>   ÚtypingrT   r   Ú r   r   Útlr   r   ÚTYPE_CHECKINGÚtelegramclientr	   r
   rR   r   r   r   r   Ú<module>   s    _