o
    i                  	   @   s   d Z ddlZddlZddlmZ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 eje d	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)AnyListOptional)Unpack   )SEC_MS_GEC_VERSIONVOICE_HEADERS
VOICE_LIST)DRM)VoiceVoicesManagerFindVoicesManagerVoice)cafilesessionssl_ctxproxyreturnc              	      s   | j t dt  dt tt||dd4 I dH }t|	 I dH }W d  I dH  n1 I dH s7w   Y  |D ]"}d|vrHi |d< d|d vrTg |d d< d|d vr`g |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_statusNZVoiceTagZContentCategoriesZVoicePersonalities)
getr	   r
   Zgenerate_sec_ms_gecr   Zheaders_with_muidr   jsonloadstext)r   r   r   urldatavoice r   I/home/kim/smarthome/.venv/lib/python3.10/site-packages/edge_tts/voices.py__list_voices   s,   (
r   )	connectorr   r    c                    s   t j| dd4 I dH J}zt|t|I dH }W n' t jy? } z|jdkr' t| 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 s\w   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.
    T)r    	trust_envNi  )aiohttpClientSessionr   _SSL_CTXZClientResponseErrorstatusr
   Zhandle_client_response_error)r    r   r   r   er   r   r   list_voices:   s&   

			r'   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 )ZLanguageZLocale-r   )split.0r   r   r   r   
<listcomp>h   s    z(VoicesManager.create.<locals>.<listcomp>T)r(   r'   r)   r*   )clsr-   r+   r)   r   r   r   create_   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   )itemsr0   r5   r   r   r2   w   s    z&VoicesManager.find.<locals>.<listcomp>)r*   RuntimeErrorr)   )r+   r5   Zmatching_voicesr   r7   r   findn   s   
zVoicesManager.find)r   N)N)__name__
__module____qualname____doc__r,   classmethodr   r   r   r4   r   r   r   r9   r   r   r   r   r(   V   s    

r(   )r=   r   r   typingr   r   r   r"   certifiZtyping_extensionsr   	constantsr   r   r	   Zdrmr
   r   r   r   create_default_contextwherer$   r#   
SSLContextstrr   ZBaseConnectorr'   r(   r   r   r   r   <module>   s:    
(
