
    i)3                        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mZ d dl	m
Z
mZmZ d dlmZmZmZ ddgZ ej$                  e      Zej+                   ej,                                 G d d      Z G d	 d      Z G d
 de      Zy)    N)_fallback_deprecated_lang	tts_langs)	Tokenizerpre_processorstokenizer_cases)_clean_tokens	_minimize_translate_urlgTTS	gTTSErrorc                       e Zd ZdZdZdZy)SpeedznRead Speed

    The Google TTS Translate API supports two speeds:
        Slow: True
        Normal: None
    TN)__name__
__module____qualname____doc__SLOWNORMAL     F/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/gtts/tts.pyr   r      s     DFr   r   c                   4   e Zd ZdZdZddddZdZdd	d
dej                  ej                  ej                  ej                  g eej                  ej                  ej                  ej                   g      j"                  dfdZd Zd Zd Zd Zd Zd Zd Zy)r   ac
  gTTS -- Google Text-to-Speech.

    An interface to Google Translate's Text-to-Speech API.

    Args:
        text (string): The text to be read.
        tld (string): Top-level domain for the Google Translate host,
            i.e `https://translate.google.<tld>`. Different Google domains
            can produce different localized 'accents' for a given
            language. This is also useful when ``google.com`` might be blocked
            within a network but a local or different Google host
            (e.g. ``google.com.hk``) is not. Default is ``com``.
        lang (string, optional): The language (IETF language tag) to
            read the text in. Default is ``en``.
        slow (bool, optional): Reads text more slowly. Defaults to ``False``.
        lang_check (bool, optional): Strictly enforce an existing ``lang``,
            to catch a language error early. If set to ``True``,
            a ``ValueError`` is raised if ``lang`` doesn't exist.
            Setting ``lang_check`` to ``False`` skips Web requests
            (to validate language) and therefore speeds up instantiation.
            Default is ``True``.
        pre_processor_funcs (list): A list of zero or more functions that are
            called to transform (pre-process) text before tokenizing. Those
            functions must take a string and return a string. Defaults to::

                [
                    pre_processors.tone_marks,
                    pre_processors.end_of_line,
                    pre_processors.abbreviations,
                    pre_processors.word_sub
                ]

        tokenizer_func (callable): A function that takes in a string and
            returns a list of string (tokens). Defaults to::

                Tokenizer([
                    tokenizer_cases.tone_marks,
                    tokenizer_cases.period_comma,
                    tokenizer_cases.colon,
                    tokenizer_cases.other_punctuation
                ]).run

        timeout (float or tuple, optional): Seconds to wait for the server to
            send data before giving up, as a float, or a ``(connect timeout,
            read timeout)`` tuple. ``None`` will wait forever (default).

    See Also:
        :doc:`Pre-processing and tokenizing <tokenizer>`

    Raises:
        AssertionError: When ``text`` is ``None`` or empty; when there's nothing
            left to speak after pre-processing, tokenizing and cleaning.
        ValueError: When ``lang_check`` is ``True`` and ``lang`` is not supported.
        RuntimeError: When ``lang_check`` is ``True`` but there's an error loading
            the languages dictionary.

    d   zhttp://translate.google.com/znMozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36z/application/x-www-form-urlencoded;charset=utf-8)Refererz
User-AgentzContent-TypejQ1olccomenFTNc	                    t        t                     j                         D ]"  \  }	}
|	dk(  rt        j	                  d|	|
       $ |sJ d       || _        || _        || _        || _        | j                  r8t        |      | _        	 t               }| j                  |vrt        d|z        	 |rt        j                   | _        nt        j$                  | _        || _        || _        || _        y # t        $ rH}t        j	                  t        |      d       t        j                  t        |             Y d }~d }~ww xY w)Nselfz%s: %szNo text to speakzLanguage not supported: %sT)exc_info)dictlocalsitemslogdebugtexttld
lang_checklangr   r   
ValueErrorRuntimeErrorstrwarningr   r   speedr   pre_processor_funcstokenizer_functimeout)r   r&   r'   r)   slowr(   r/   r0   r1   kvlangses                r   __init__zgTTS.__init__f   s   2 N((* 	&DAqF{IIh1%	& '''t	  %	??1$7DI$!99E)$%AD%HII * DJDJ $7 ,   $		#a&4	0CF##$s   	&C3 3	E<>D??Ec                    |j                         }| j                  D ]   }t        j                  d|        ||      }" t	        |      | j
                  k  rt        |g      S t        j                  d| j                         | j                  |      }t        |      }g }|D ]  }|t        |d| j
                        z  } |D cg c]  }|s|	 }}|S c c}w )Nzpre-processing: %sztokenizing: %s )	stripr/   r$   r%   lenGOOGLE_TTS_MAX_CHARSr   r0   r	   )r   r&   pptokens
min_tokensts         r   	_tokenizezgTTS._tokenize   s    zz| ** 	BII*B/d8D	 t9111 $(( 			"D$7$78$$T* v& 
 	GA)AsD,E,EFFJ	G (-1!-- .s   CCc                    t        | j                  d      }| j                  | j                        }t        j                  dt        |             t        j                  dt        |             |sJ d       g }t        |      D ]o  \  }}| j                  |      }t        j                  d||       t        j                  d||| j                        }|j                  |j                                q |S )	zCreated the TTS API the request(s) without sending them.

        Returns:
            list: ``requests.PreparedRequests_``. <https://2.python-requests.org/en/master/api/#requests.PreparedRequest>`_``.
        z(_/TranslateWebserverUi/data/batchexecute)r'   pathztext_parts: %sztext_parts: %izNo text to send to TTS APIzdata-%i: %sPOST)methodurldataheaders)r
   r'   rA   r&   r$   r%   r,   r;   	enumerate_package_rpcrequestsRequestGOOGLE_TTS_HEADERSappendprepare)r   translate_url
text_partsprepared_requestsidxpartrG   rs           r   _prepare_requestszgTTS._prepare_requests   s     'I
 ^^DII.
		"C
O4		"C
O4777z":. 	2IC$$T*DIImS$/   !//	A $$QYY[1	2  ! r   c                    || j                   | j                  dg}t        j                  |d      }| j                  |d dggg}t        j                  |d      }dj                  t        j                  j                  |            S )Nnull),:)
separatorsgenericz	f.req={}&)	r)   r.   jsondumpsGOOGLE_TTS_RPCformaturllibparsequote)r   r&   	parameterescaped_parameterrpcespaced_rpcs         r   rJ   zgTTS._package_rpc   st    499djj&9	 JJyZH$$&7yIJKjj<!!&,,"4"4["ABBr   c                 \    | j                         D cg c]  }|j                   c}S c c}w )zGet TTS API request bodies(s) that would be sent to the TTS API.

        Returns:
            list: A list of TTS API request bodies to make.
        )rV   body)r   prs     r   
get_bodieszgTTS.get_bodies   s%     #'"8"8":;B;;;s   )c              #   ^  K   	 t         j                  j                  j                  t         j                  j                  j                  j
                         | j                         }t        |      D ]  \  }}	 t        j                         5 }|j                  |dt        j                  j                         | j                        }ddd       t        j                  d|j                  j                          t        j                  d||j                  j"                         t        j                  d||j$                         |j'                          |j1                  d	
      D ]t  }|j3                  d      }d|v st5        j6                  d|      }	|	r8|	j9                  d      j;                  d      }
t=        j>                  |
       it-        | |       t        j                  d|        y#  Y xY w# 1 sw Y   <xY w# t         j                  j(                  $ r0}t        j                  t+        |             t-        |       d}~wt         j                  j.                  $ r/}t        j                  t+        |             t-        |       d}~ww xY ww)zDo the TTS API request(s) and stream bytes

        Raises:
            :class:`gTTSError`: When there's an error with the API request.

        F)requestverifyproxiesr1   Nzheaders-%i: %sz
url-%i: %szstatus-%i: %s)ttsresponse)rp   i   )
chunk_sizezutf-8r   zjQ1olc","\[\\"(.*)\\"]   asciizpart-%i created) rK   packagesurllib3disable_warnings
exceptionsInsecureRequestWarningrV   rI   Sessionsendra   rm   
getproxiesr1   r$   r%   rH   rF   status_coderaise_for_status	HTTPErrorr,   r   RequestException
iter_linesdecoderesearchgroupencodebase64	b64decode)r   rR   rS   rj   srU   r6   linedecoded_lineaudio_searchas_bytess              r   streamzgTTS.stream   s    	%%66!!))44KK !224 !23 %	.GC*%%' 1 "$ & 9 9 ; $	  A 		*C1B1BC		,QYY]];		/3>""$ 5 
>#{{73|+#%99-F#UL##/#5#5a#8#?#?#H$..x88 (D1==
> II'-K%	.		
  &&00 6		#a&!D155&&77 *		#a&!D))*sm   J-AG> "J-<H<HBH*J-A8J->H J-H	HJ*0+I J*;*J%%J**J-c                     	 t        | j                               D ]-  \  }}|j                  |       t        j	                  d||       / y# t
        t        f$ r}t        dt        |      z        d}~ww xY w)aQ  Do the TTS API request(s) and write bytes to a file-like object.

        Args:
            fp (file object): Any file-like object to write the ``mp3`` to.

        Raises:
            :class:`gTTSError`: When there's an error with the API request.
            TypeError: When ``fp`` is not a file-like object that takes bytes.

        zpart-%i written to %sz<'fp' is not a file-like object or it does not take bytes: %sN)rI   r   writer$   r%   AttributeError	TypeErrorr,   )r   fprS   decodedr6   s        r   write_to_fpzgTTS.write_to_fp/  su    	 )$++- 8 <W!		13;< 	* 	NQTUVQWW 	s   A	A A7A22A7c                     t        t        |      d      5 }| j                  |       |j                          t        j                  d|       ddd       y# 1 sw Y   yxY w)zDo the TTS API request and write result to file.

        Args:
            savefile (string): The path and file name to save the ``mp3`` to.

        Raises:
            :class:`gTTSError`: When there's an error with the API request.

        wbzSaved to %sN)openr,   r   flushr$   r%   )r   savefilefs      r   savez	gTTS.saveD  sN     #h-& 	/!QGGIIImX.	/ 	/ 	/s   8AA )r   r   r   r   r<   rM   r_   r   
tone_marksend_of_lineabbreviationsword_subr   r   period_commacolonother_punctuationrunr7   rA   rV   rJ   rk   r   r   r   r   r   r   r   r   !   s    8t 1- J N
 %%&&((##	
 !**,,%%11	
 #+?B:!!FC<6.p*/r   c                   ,     e Zd ZdZd fd	ZddZ xZS )r   zAException that uses context to present a meaningful error messagec                 .   |j                  dd       | _        |j                  dd       | _        |r|| _        n?| j                  ,| j	                  | j                  | j                        | _        nd | _        t
        t        |   | j                         y )Nrp   rq   )poprp   rspmsg	infer_msgsuperr   r7   )r   r   kwargs	__class__s      r   r7   zgTTSError.__init__W  so    ::eT*::j$/DHXX!~~dhh9DHDHi'1r   c                    d}|9d}|j                   dk7  rt        |j                         }dj                  |      }n|j                  }|j                  }dj                  ||      }|dk(  rd}nb|d	k(  r+|j                   dk7  rd
j                  |j                         }n2|dk(  r&|j
                  sd| j                  j                  z  }n|dk\  rd}dj                  ||      S )zyAttempt to guess what went wrong by using known
        information (e.g. http response) and observed behaviour

        UnknownzFailed to connectr   )r'   zHost '{}' is not reachablez{:d} ({}) from TTS APIi  z!Bad token or upstream API changesi  zUnsupported tld '{}'   z6No audio stream in response. Unsupported language '%s'i  z$Upstream API error. Try again later.z{}. Probable cause: {})r'   r
   r`   r}   reasonr(   rp   r)   )r   rp   r   causepremisehoststatusr   s           r   r   zgTTSError.infer_msgb  s    
 ;)Gww%%#''24;;DA
 __FZZF.55ffEG};3377e#3.55cgg>3s~~Lhhmm$  3>'..w>>r   )N)r   r   r   r   r7   r   __classcell__)r   s   @r   r   r   T  s    K	2"?r   )r   r]   loggingr   ra   rK   	gtts.langr   r   gtts.tokenizerr   r   r   
gtts.utilsr   r	   r
   __all__	getLoggerr   r$   
addHandlerNullHandlerr   r   	Exceptionr   r   r   r   <module>r      s       	   : E E ? ?;
 g! "w""$ %	 	p/ p/f	0?	 0?r   