o
    ${g                  	   @   s   d Z ddlZddlZddlmZmZ ddlZddlZddlm	Z	 ddl
mZmZmZ ddlmZ ddlmZmZmZ d	ejd
ejdee dee fddZddddeej dee dee fddZG dd dZdS )z|This module contains functions to list all available voices and a class to find the
correct voice based on their attributes.    N)ListOptional)Unpack   )SEC_MS_GEC_VERSIONVOICE_HEADERS
VOICE_LIST)DRM)VoiceVoicesManagerFindVoicesManagerVoicesessionssl_ctxproxyreturnc              	      s   | j t dt  dt t||dd4 I dH }t| I dH }W d  I dH  n1 I dH s4w   Y  |D ] }dd |d d	 D |d d	< d
d |d d D |d d< q;|S )a  
    Private function that makes the request to the voice list URL and parses the
    JSON response. This function is used by list_voices() and makes it easier to
    handle client response errors related to clock skew.

    Args:
        session (aiohttp.ClientSession): The aiohttp session to use for the request.
        ssl_ctx (ssl.SSLContext): The SSL context to use for the request.
        proxy (Optional[str]): The proxy to use for the request.

    Returns:
        List[Voice]: A list of voices and their attributes.
    z&Sec-MS-GEC=z&Sec-MS-GEC-Version=T)headersr   sslraise_for_statusNc                 S      g | ]}|  qS  strip).0categoryr   r   l/var/www/bot.gig.net.ua/public_html/telegram/P1/HellBot/venv/lib/python3.10/site-packages/edge_tts/voices.py
<listcomp>/       z!__list_voices.<locals>.<listcomp>VoiceTagContentCategoriesc                 S   r   r   r   )r   personalityr   r   r   r   3   r   VoicePersonalities)	getr   r	   generate_sec_ms_gecr   r   jsonloadstext)r   r   r   urldatavoicer   r   r   __list_voices   s*   (


r)   )	connectorr   r*   c                    s   t jt d}tj| dd4 I dH J}zt|||I dH }W n' tjyG } z|jdkr/ t	
| t|||I dH }W Y d}~nd}~ww W d  I dH  |S W d  I dH  |S 1 I dH sdw   Y  |S )a  
    List all available voices and their attributes.

    This pulls data from the URL used by Microsoft Edge to return a list of
    all available voices.

    Args:
        connector (Optional[aiohttp.BaseConnector]): The connector to use for the request.
        proxy (Optional[str]): The proxy to use for the request.

    Returns:
        List[Voice]: A list of voices and their attributes.
    )cafileT)r*   	trust_envNi  )r   create_default_contextcertifiwhereaiohttpClientSessionr)   ClientResponseErrorstatusr	   handle_client_response_error)r*   r   r   r   r'   er   r   r   list_voices;   s(   

			r6   c                   @   sV   e Zd ZdZdddZe	ddeee  dd fddZ	d	e
e dee fd
dZdS )VoicesManagerzF
    A class to find the correct voice based on their attributes.
    r   Nc                 C   s   g | _ d| _d S )NF)voicescalled_create)selfr   r   r   __init__]   s   
zVoicesManager.__init__custom_voicesc                    s:   t  }|du rt I dH n|}dd |D |_d|_|S )z\
        Creates a VoicesManager object and populates it with all available voices.
        Nc                 S   s*   g | ]}i |d |d  dd iqS )LanguageLocale-r   )splitr   r(   r   r   r   r   j   s    z(VoicesManager.create.<locals>.<listcomp>T)r7   r6   r8   r9   )clsr<   r:   r8   r   r   r   createa   s   zVoicesManager.createkwargsc                    s&   | j std fdd| jD }|S )zM
        Finds all matching voices based on the provided attributes.
        z9VoicesManager.find() called before VoicesManager.create()c                    s    g | ]}   |  kr|qS r   )itemsrA   rD   r   r   r   y   s    z&VoicesManager.find.<locals>.<listcomp>)r9   RuntimeErrorr8   )r:   rD   matching_voicesr   rF   r   findp   s   
zVoicesManager.find)r   N)N)__name__
__module____qualname____doc__r;   classmethodr   r   r
   rC   r   r   r   rI   r   r   r   r   r7   X   s    

r7   )rM   r#   r   typingr   r   r0   r.   typing_extensionsr   	constantsr   r   r   drmr	   r
   r   r   r1   
SSLContextstrr)   BaseConnectorr6   r7   r   r   r   r   <module>   s8    
+
