o
    Ph#                  	   @   sh   d Z ddlZddlmZ ddlmZ ddlmZmZ ddlm	Z	 dddd	d
ddddZ
G dd dZdS )aM  
ORIGINAL FILENAME:
    gtoken.py
SOURCE PROJECT:
    ssut/py-googletrans (https://github.com/ssut/py-googletrans)
AUTHOR:
    SuHun Han (@ssut on GitHub)
EXPLANATION:
    Generates a ticket to access Google Translate's API
    Reverse engineered by ssut on the obfuscated and minified code used by Google to generate such token, and implemented on the top of Python.
    However, this could be blocked at any time.
COPYRIGHT:
    Copyright (c) 2015 SuHun Han
LICENSE:
    The MIT License (MIT)

    Copyright (c) 2015 SuHun Han

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    N)time)floor)compileDOTALL)getz?text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8zno-cachetranslate.google.comzwMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15zfr-frzgzip, deflate, brz
keep-alive)AcceptPragmazCache-ControlHostz
User-AgentzAccept-LanguagezAccept-Encoding
Connectionc                   @   sV   e Zd ZdZedeZedeZd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:\'(.+?)\'r   c                 C   s&   d| _ d|v r|| _d S d| | _d S )N0httpzhttps://)tkkhost)selfr    r   S/var/www/html/env_mimamsha/lib/python3.10/site-packages/translatepy/utils/gtoken.py__init__U   s    zTokenAcquirer.__init__c                 C   s4  t tt d d }| jrt| jdd |krdS t| jtd}| j	|j
}|r4|d| _dS z| j	|j
ddd	}| d
}W n tyV   td ty]    w |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|	|< qxt|
jtjrt|
jjtjr|
jjj |	|< qxt|
tj rd}qx|rt|
tjr|
j}qx|r|dkrt|
tj!rqxt|
tj"rd}qxt|
tj#rd}qxt|
tj$rd}qxt|
tj%rd}qxt&d'||	d |	d d	d}t(|ti d}d'||}|| _dS dS )zupdate tkk
        i  g    @wKA.r   N)headers   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__{}.{}))r   intr   r   splitr   r   HEADERS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BitXorr   formatr   )r   nowrraw_tkkcodetreevisit_returnoperatorr8   keysnodenameclauser6   resultr   r   r   _updateY   sr   

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   r6   r   r   <lambda>   s    z%TokenAcquirer._lazy.<locals>.<lambda>r   )r   r6   r   rR   r   _lazy   s   zTokenAcquirer._lazyc                 C   s   t |}d}||d k rN||d  }d|krt|d d nt|}d||d  kr0|d |? n||> }d|| kr@|| d@ n||A }|d	7 }||d k s|S )
Nr      r   W   r   r   l        l       )lenordr"   )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r3| jnd}|d}t|dkrFt|d	 nd	}g }d	}t|}	||	k r|| }
|
d
k rb||
 nh|
dk rp||
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 sT|}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!   )
rY   r   r   r#   rX   r"   append	enumerater]   rC   )r   r'   r   ivalr   r\   egsizelr6   r   r   r   acquire   sT   
 
zTokenAcquirer.acquirec                 C   s   |    | |}|S rQ   )rP   ro   )r   r'   tkr   r   r   do   s   
zTokenAcquirer.doN)r   )__name__
__module____qualname____doc__r   r   r%   	RE_RAWTKKr   rP   rT   r]   ro   rq   r   r   r   r   r   8   s    


B8r   )ru   r.   r   mathr   rer   r   requestsr   r$   r   r   r   r   r   <module>   s     %