o
    _0                     @   sF   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 G dd dZdS )    N)rshiftc                   @   sf   e Zd ZdZedejZedejZdde	j
fddZdd	 Zd
d Zdd Zdd Zdd ZdS )TokenAcquirera  Google Translate API token generator

    translate.google.com uses a token to authorize the requests. If you are
    not Google, you do have this token and will have to pay for use.
    This class is the result of reverse engineering on the obfuscated and
    minified code used by Google to generate such token.

    The token is based on a seed which is updated once per hour and on the
    text that will be translated.
    Both are combined - by some strange math - in order to generate a final
    token (e.g. 744915.856682) which is used by the API to validate the
    request.

    This operation will cause an additional request to get an initial
    token from translate.google.com.

    Example usage:
        >>> from googletrans.gtoken import TokenAcquirer
        >>> acquirer = TokenAcquirer()
        >>> text = 'test'
        >>> tk = acquirer.do(text)
        >>> tk
        950629.577246
    ztkk:\'(.+?)\'0translate.google.comclientc                 C   s,   || _ || _d|v r|| _d S d| | _d S )Nhttpzhttps://)r   tkkhost)selfr   r   r	    r   M/var/www/html/env_mimamsha/lib/python3.10/site-packages/googletrans/gtoken.py__init__)   s    zTokenAcquirer.__init__c                 C   s0  t tt d d }| jrt| jdd |krdS | j| j}| j	
|j}|r6|d| _dS z| j	
|jddd}| d	}W n tyX   td
    |rt|}d}d}dtddd}}	t|D ]}}
t|
tjr|
jd j}||	v rt|
jtjr|
jj|	|< qvt|
jtjrt|
jjtjr|
jj j |	|< qvt|
tj!rd}qv|rt|
tjr|
j}qv|r|dkrt|
tj"rqvt|
tj#rd}qvt|
tj$rd}qvt|
tj%rd}qvt|
tj&rd}qvt'd(||	d |	d dd}t)|ti d}d(||}|| _dS dS )zupdate tkk
        i  g    @wKA.r   N   zvar  zunicode-escapezrCould not find TKK token for this request.
See https://github.com/ssut/py-googletrans/issues/234 for more details.F+)abT-*z**^z	{1}{0}{2}r   r   eval)__builtin__{}.{})*mathfloorinttimer   splitr   getr	   RE_TKKsearchtextgroupreplaceencodedecodeAttributeError	Exceptionastparsedictwalk
isinstanceAssigntargetsidvalueNumnUnaryOpopUSuboperandReturnAddSubMultPowBitXorcompileformatr   )r
   nowrraw_tkkcodetreevisit_returnoperatorr3   keysnodenameclauser1   resultr   r   r   _update.   sp   

zTokenAcquirer._updatec                    s    fddS )a  like lazy evaluation, this method returns a lambda function that
        returns value given.
        We won't be needing this because this seems to have been built for
        code obfuscation.

        the original code of this method is as follows:

           ... code-block: javascript

               var ek = function(a) {
                return function() {
                    return a;
                };
               }
        c                      s    S Nr   r   r1   r   r   <lambda>   s    z%TokenAcquirer._lazy.<locals>.<lambda>r   )r
   r1   r   rN   r   _lazyp   s   zTokenAcquirer._lazyc                 C   s   t |}d}||d k rM||d  }d|krt|d d nt|}d||d  kr/t||n||> }d|| kr?|| d@ n||A }|d7 }||d k s|S )	Nr      r   W   r   r   l       )lenordr   r   )r
   r   r   size_bcdr   r   r   _xr   s    " zTokenAcquirer._xrc                 C   s,  g }|D ](}t |}|dk r||g7 }q|t|d d d t|d d d g7 }q| jdkr5| jnd}|d}t|dkrHt|d	 nd	}g }d	}t|}	||	k r|| }
|
d
k rd||
 nh|
dk rr||
d? dB  nQ|
d@ dkr|d |	k r||d  d@ dkr|d7 }d|
d@ d>  || d@  }
||
d? dB  ||
d? d@ d
B  n	||
d? dB  ||
d? d@ d
B  ||
d@ d
B  |d7 }||	k sV|}t|D ]\}}||7 }| 	|d}q| 	|d}|t|dkrt|d nd	N }|d	k r
|d@ d }|d; }d
|||A S )Ni   i   i   i   r   r   r   r   r      i         i   i  
            ?      z+-a^+6z	+-3^+b+-fil        i@B r   )rU   r   r   r   r   rT   r   append	enumeraterY   r?   )r
   r"   r   ivalr   rX   egsizelr1   r   r   r   acquire   sT   
 
zTokenAcquirer.acquirec                 C   s   |    | |}|S rM   )rL   rk   )r
   r"   tkr   r   r   do   s   
zTokenAcquirer.doN)r   r   )__name__
__module____qualname____doc__rer>   DOTALLr    	RE_RAWTKKhttpxClientr   rL   rP   rY   rk   rm   r   r   r   r   r      s    B8r   )r)   r   rr   r   ru   googletrans.utilsr   r   r   r   r   r   <module>   s   