o
    PhW                     @   s   d Z ddlmZmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ h d	Zh d
ZG dd de
ZG dd de
ZG dd de
ZdS )a  
Google Translate

Class GoogleTranslateV1 are using Google's new batchexecute (JSONRPC) API.
The code for the functions used (_request and _parse_response) come from https://github.com/ssut/py-googletrans/pull/255 with few adjustments
Heavily inspired by ssut/googletrans and https://kovatch.medium.com/deciphering-google-batchexecute-74991e4e446c

Class GoogleTranslateV2 uses official API methods that are used in Google Translate mobile and web applications
    )dumpsloads)ServiceURLErrorUnsupportedMethod)Language)BaseTranslator)TokenAcquirer)Request)convert_to_float>   translate.google.actranslate.google.adtranslate.google.aetranslate.google.altranslate.google.amtranslate.google.astranslate.google.attranslate.google.aztranslate.google.batranslate.google.betranslate.google.bftranslate.google.bgtranslate.google.bitranslate.google.bjtranslate.google.bstranslate.google.bttranslate.google.bytranslate.google.catranslate.google.cctranslate.google.cdtranslate.google.cftranslate.google.cgtranslate.google.chtranslate.google.citranslate.google.cltranslate.google.cmtranslate.google.cntranslate.google.cotranslate.google.cvtranslate.google.cxtranslate.google.cztranslate.google.detranslate.google.djtranslate.google.dktranslate.google.dmtranslate.google.dztranslate.google.eetranslate.google.estranslate.google.eutranslate.google.fitranslate.google.fmtranslate.google.frtranslate.google.gatranslate.google.getranslate.google.gftranslate.google.ggtranslate.google.gltranslate.google.gmtranslate.google.gptranslate.google.grtranslate.google.gytranslate.google.hntranslate.google.hrtranslate.google.httranslate.google.hutranslate.google.ietranslate.google.imtranslate.google.iotranslate.google.iqtranslate.google.istranslate.google.ittranslate.google.jetranslate.google.jotranslate.google.kgtranslate.google.kitranslate.google.kztranslate.google.latranslate.google.litranslate.google.lktranslate.google.lttranslate.google.lutranslate.google.lvtranslate.google.mdtranslate.google.metranslate.google.mgtranslate.google.mktranslate.google.mltranslate.google.mntranslate.google.mstranslate.google.mutranslate.google.mvtranslate.google.mwtranslate.google.netranslate.google.nftranslate.google.nltranslate.google.notranslate.google.nrtranslate.google.nutranslate.google.pltranslate.google.pntranslate.google.pstranslate.google.pttranslate.google.rotranslate.google.rstranslate.google.rutranslate.google.rwtranslate.google.sctranslate.google.setranslate.google.shtranslate.google.sitranslate.google.sktranslate.google.smtranslate.google.sntranslate.google.sotranslate.google.srtranslate.google.sttranslate.google.tdtranslate.google.tgtranslate.google.tktranslate.google.tltranslate.google.tmtranslate.google.tntranslate.google.totranslate.google.tttranslate.google.ustranslate.google.vgtranslate.google.vutranslate.google.wstranslate.google.cattranslate.google.co.aotranslate.google.co.bwtranslate.google.co.cktranslate.google.co.crtranslate.google.co.idtranslate.google.co.iltranslate.google.co.intranslate.google.co.jptranslate.google.co.ketranslate.google.co.krtranslate.google.co.lstranslate.google.co.matranslate.google.co.mztranslate.google.co.nztranslate.google.co.thtranslate.google.co.tztranslate.google.co.ugtranslate.google.co.uktranslate.google.co.uztranslate.google.co.vetranslate.google.co.vitranslate.google.co.zatranslate.google.co.zmtranslate.google.co.zwtranslate.google.com.aftranslate.google.com.agtranslate.google.com.aitranslate.google.com.artranslate.google.com.autranslate.google.com.bdtranslate.google.com.bhtranslate.google.com.bntranslate.google.com.botranslate.google.com.brtranslate.google.com.bztranslate.google.com.cotranslate.google.com.cutranslate.google.com.cytranslate.google.com.dotranslate.google.com.ectranslate.google.com.egtranslate.google.com.ettranslate.google.com.fjtranslate.google.com.ghtranslate.google.com.gitranslate.google.com.gttranslate.google.com.hktranslate.google.com.jmtranslate.google.com.khtranslate.google.com.kwtranslate.google.com.lbtranslate.google.com.lctranslate.google.com.lytranslate.google.com.mmtranslate.google.com.mttranslate.google.com.mxtranslate.google.com.mytranslate.google.com.natranslate.google.com.ngtranslate.google.com.nitranslate.google.com.nptranslate.google.com.omtranslate.google.com.patranslate.google.com.petranslate.google.com.pgtranslate.google.com.phtranslate.google.com.pktranslate.google.com.prtranslate.google.com.pytranslate.google.com.qatranslate.google.com.satranslate.google.com.sbtranslate.google.com.sgtranslate.google.com.sltranslate.google.com.svtranslate.google.com.tjtranslate.google.com.trtranslate.google.com.twtranslate.google.com.uatranslate.google.com.uytranslate.google.com.vctranslate.google.com.vntranslate.google.com>k   afamarazbebgbnbscacocscydadeeleneoeseteufafifrfygagdglguhahehihrhthuhyidigisitjajwkakkkmknkokukylalbloltlvmgmimkmlmnmrmsmtmynenlnonyorpaplpsptrorusdsiskslsmsnsosqsrstsusvswtatetgthtltrugukuruzvixhyiyozucebhawautozh-CNzh-TWzh-cnc                   @   sp   e Zd ZeZe dfdedefddZdd Zdd	 Z	d
d Z
defddZdd Zdd Zdd Zdd ZdS )GoogleTranslater   requestservice_urlc                 C   sB   |t vrtdjt|dt||d}t||d}||g| _d S )Nz {url} is not a valid service URL)url)rB  rA  )DOMAINSr   formatstrGoogleTranslateV1GoogleTranslateV2services)selfrA  rB  	google_v1	google_v2 rM  Y/var/www/html/env_mimamsha/lib/python3.10/site-packages/translatepy/translators/google.py__init__C   s
   zGoogleTranslate.__init__c              	   C   6   | j D ]}z
||||W   S  ty   Y qw d S N)rI  
_translate	ExceptionrJ  textdestination_languagesource_languageservicerM  rM  rN  rR  M      
zGoogleTranslate._translatec              	   C   rP  rQ  )rI  _transliteraterS  rT  rM  rM  rN  rZ  T   rY  zGoogleTranslate._transliteratec              	   C   s2   | j D ]}z||W   S  ty   Y qw d S rQ  )rI  	_languagerS  )rJ  rU  rX  rM  rM  rN  r[  [   s   
zGoogleTranslate._languagelanguagec                 C   "   |j dkrdS |j dkrdS |jS )Nzhor?  ochzh-twr   alpha2rJ  r\  rM  rM  rN  _language_normalizeb   
   

z#GoogleTranslate._language_normalizec                 C   8   t | dkrtdS t | dkrtdS t|S Nr?  r^  r`  r_  rF  lowerr   rJ  language_coderM  rM  rN  _language_denormalizei   
   z%GoogleTranslate._language_denormalizec                 C   s   t  rQ  )r   )rJ  rU  rW  rM  rM  rN  _spellcheckp   s   zGoogleTranslate._spellcheckc              	   C   s8   | j D ]}z|||||W   S  ty   Y qw d S rQ  )rI  _text_to_speechrS  )rJ  rU  speedgenderrW  rX  rM  rM  rN  ro  t   s   
zGoogleTranslate._text_to_speechc                 C      dS NGooglerM  rJ  rM  rM  rN  __str__{      zGoogleTranslate.__str__N)__name__
__module____qualname___google_supported_languages_supported_languagesr	   rF  rO  rR  rZ  r[  r   rd  rl  rn  ro  rv  rM  rM  rM  rN  r@  ?   s    
r@  c                   @   s   e Zd ZdZeZe dfdedefddZdd Z	d	d
 Z
dedededefddZdedededefddZdd ZdefddZdd Zdd ZdS )rG  z?
    A Python implementation of Google Translate's RPC API
    r   rA  rB  c                 C   s   || _ || _d S rQ  )sessionrB  rJ  rA  rB  rM  rM  rN  rO     s   
zGoogleTranslateV1.__init__c                 C   sv   t dt |||dgdggddddgggdd}d|i}ddd	d	d	d
d}| jjd| j||d}|jdk r9|jS dS )z
        Makes a translation request to Google Translate RPC API

        Most of the code comes from https://github.com/ssut/py-googletrans/pull/255
        MkEWBcTN),:)
separatorsgenericzf.reqz&boq_translate-webserver_20201207.13_p0   c)rpcidsblzsoc-appzsoc-platformz
soc-devicertz3https://{}/_/TranslateWebserverUi/data/batchexecute)paramsdata  )r   r}  postrE  rB  status_coderU  )rJ  rU  destinationsourcerpc_requestr  r  rA  rM  rM  rN  _request   s,   	
zGoogleTranslateV1._requestc           
      C   s   d}d}d}d}| dD ]I}|pd|dd v }|sqd}t|D ])\}}	|	dkr8|td|d	  d
kr8| }|sK|	dkrC|d	7 }q"|	dkrK|d	7 }q"||7 }||krV nqtt|d d S )z
        Parses the broken JSON response given by the new RPC API endpoint (batchexecute)

        Most of the code comes from https://github.com/ssut/py-googletrans/pull/255
        F r   
z"MkEWBc"N   "r  \[]   )split	enumeratemaxr   )
rJ  r  token_foundrespopening_bracketclosing_bracketlineis_in_stringindexcharrM  rM  rN  _parse_response   s.   
z!GoogleTranslateV1._parse_responserU  rV  rW  returnc                 C   s   |  |||}| |}|d d d d rdnddd |d d d d D }|d	ks2|d
u rBz|d }W n	 tyA   Y nw |d	krXz|d d }W n	 tyW   Y nw |d	ks`|d
u r|z|d d d d }W ||fS  ty{   Y ||fS w ||fS )z
        Translates the given text to the destination language with the new batchexecute API

        Heavily inspired by ssut/googletrans and https://kovatch.medium.com/deciphering-google-batchexecute-74991e4e446c
        r  r       r  c                 S      g | ]}|d  qS r   rM  ).0partrM  rM  rN  
<listcomp>       z0GoogleTranslateV1._translate.<locals>.<listcomp>   r<  Nr  )r  r  joinrS  )rJ  rU  rV  rW  rA  parsed
translatedrM  rM  rN  rR     s0   
<zGoogleTranslateV1._translatec                 C   s   |  |||}| |}z|d d }|d u rtdW n ty'   |}Y nw |dks0|d u r@z|d }W n	 ty?   Y nw |dkrVz|d d }W n	 tyU   Y nw |dks^|d u rzz|d d d d }W ||fS  tyy   Y ||fS w ||fS )Nr   z=translatepy internal exception: Origin Pronounciation is Noner<  r  r  )r  r  
ValueErrorrS  )rJ  rU  rV  rW  rA  r  origin_pronunciationrM  rM  rN  rZ     s>   
z GoogleTranslateV1._transliteratec                 C   s   |  |dd}| |}z|d }W n ty   d}Y nw |dks&|du r8z|d d }W n	 ty7   Y nw |dks@|du rXz|d d d d }W |S  tyW   Y |S w |S )z
        Returns the language of the given text with the new batchexecute API

        Heavily inspired by ssut/googletrans and https://kovatch.medium.com/deciphering-google-batchexecute-74991e4e446c
        r   r<  r  Nr   r  )r  r  rS  )rJ  rU  rA  r  rW  rM  rM  rN  r[    s,   
zGoogleTranslateV1._languager\  c                 C   r]  Nr^  r=  r_  r>  ra  rc  rM  rM  rN  rd     re  z%GoogleTranslateV1._language_normalizec                 C   rf  rg  rh  rj  rM  rM  rN  rl  '  rm  z'GoogleTranslateV1._language_denormalizec                 C   rr  rs  rM  ru  rM  rM  rN  rv  .  rw  zGoogleTranslateV1.__str__N)rx  ry  rz  __doc__r{  r|  r	   rF  rO  r  r  rR  rZ  r[  r   rd  rl  rv  rM  rM  rM  rN  rG     s     rG  c                
   @   s   e Zd ZdZeZe dfdedefddZdeded	ed
efddZ	deded	ed
efddZ
dededed	ed
ef
ddZded
efddZdefddZdd Zd
efddZdS )rH  z<
    A Python implementation of Google Translate's APIs
    r   rA  rB  c                 C   s   || _ || _t|| _d S rQ  )r}  rB  r   token_acquirerr~  rM  rM  rN  rO  9  s   zGoogleTranslateV2.__init__rU  rV  rW  r  c                 C   s  dd|||d}| j jd|d}| }|jdk r9z|d }W n ty*   |}Y nw |dd	d
 |d D fS d|||d}| j jd|d}| }|jdk rz#z
|d d d }W n tyg   |}Y nw ddd |d D W S  ty   z+z
|d d d }W n ty   |}Y nw ddd |d d d d D W  Y S  ty   Y nw Y nw g dd||||ddd}| j jd|d}| }|jdk rz*|dd }|d u r|di dd gd }|d u r|di dd gd }W n ty   |}Y nw |ddd
 |d D fS dddgdd|||d}| j jd|d}| }|jdk rVz|d }W n tyG   |}Y nw |dd d
 |d D fS d S )!Ngtxtclientdtr!  r/  q3https://translate.googleapis.com/translate_a/singler  r  r  r  c                 S   r  r  rM  r  sentencerM  rM  rN  r  G  r  z0GoogleTranslateV2._translate.<locals>.<listcomp>r   dict-chrome-exr  r!  r/  r  )https://clients5.google.com/translate_a/t	ld_resultsrclangsc                 s   s$    | ]}d |v r|d  ndV  qdS )transr  NrM  r  rM  rM  rN  	<genexpr>R  s   " z/GoogleTranslateV2._translate.<locals>.<genexpr>	sentencesc                 s   s    | ]}|V  qd S rQ  rM  r  rM  rM  rN  r  Y  s    r  bdexldmdqcarwrmssr  at1bubbler  r  r  hlr!  r/  djr  srcextended_srclangsr  c                 S      g | ]
}d |v r|d  qS r  rM  r  rM  rM  rN  r  i      r  input)r  r  r  r  r  r!  r/  c                 S   r  r  rM  r  rM  rM  rN  r  s  r  r}  getjsonr  rS  r  )rJ  rU  rV  rW  r  rA  response_detected_languagerM  rM  rN  rR  >  sx   

*
zGoogleTranslateV2._translatec           	   	   C   s   g dd||||ddd}| j jd|d}| }|jdk roz*|d	d }|d u rG|d
i dd gd }|d u rG|d
i dd gd }W n tyS   |}Y nw ddd |d D }||d url|dkrl|fS |fS d S )Nr  r  r  r  r  r  r  r  r  r  r  r   r  r  c                 S   r  )src_translitrM  r  rM  rM  rN  r    r  z4GoogleTranslateV2._transliterate.<locals>.<listcomp>r  r  r  )	rJ  rU  rV  rW  r  rA  r  r  resultrM  rM  rN  rZ  u  s"   
z GoogleTranslateV2._transliteraterp  rq  c           	      C   s   |dkr	|  |}dd||d}| jjd|d}|jdkr"||jfS d||d	}| jjd
|d}|jdkr:||jfS t|}| j|}d||dd||ddt|d
}| jjd
|d}|jdk re||jfS d S )Nr<  r  zUTF-8)r  ier/  r  z.https://translate.googleapis.com/translate_ttsr     ztw-ob)r  r  r/  z*https://translate.google.com/translate_ttsr  0webappr  )
r  r  r/  totalidxtextlentkr  prevttsspeedr  )	r[  r}  r  r  contentlenr  dor
   )	rJ  rU  rp  rq  rW  r  rA  r  tokenrM  rM  rN  ro    s"   






z!GoogleTranslateV2._text_to_speechc                 C   s~   dddd|d}| j jd|d}| }|jdk r|d	 S d
dd|d}| j jd|d}| }|jdk r=|d d d S d S )Nr  r  r<  r   r  r  r  r  r  r  r  r  r  r  r   )r}  r  r  r  )rJ  rU  r  rA  r  rM  rM  rN  r[    s   

zGoogleTranslateV2._languager\  c                 C   r]  r  ra  rc  rM  rM  rN  rd    re  z%GoogleTranslateV2._language_normalizec                 C   rf  rg  rh  rj  rM  rM  rN  rl    rm  z'GoogleTranslateV2._language_denormalizec                 C   rr  rs  rM  ru  rM  rM  rN  rv    rw  zGoogleTranslateV2.__str__N)rx  ry  rz  r  r{  r|  r	   rF  rO  rR  rZ  intbytesro  r[  r   rd  rl  rv  rM  rM  rM  rN  rH  2  s    7rH  N)r  r  r   r   translatepy.exceptionsr   r   translatepy.languager   translatepy.translators.baser   translatepy.utils.gtokenr   translatepy.utils.requestr	   translatepy.utils.utilsr
   rD  r{  r@  rG  rH  rM  rM  rM  rN  <module>   s    
&@ 4