Ë
    X˜€i«  ã                   ó|   — d Z ddl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
Z
ddlmZ ddlmZ d	Zd
Z G d„ d«      Zy)zÝDRM module is used to handle DRM operations with clock skew correction.
Currently the only DRM operation is generating the Sec-MS-GEC token value
used in all API requests to Microsoft Edge's online text-to-speech service.é    N)Údatetime)Útimezone)ÚDictÚOptionalé   )ÚTRUSTED_CLIENT_TOKEN)ÚSkewAdjustmentErrorl    !l
 g    eÍÍAc                   óø   — e Zd ZU dZdZeed<   ededdfd„«       Zedefd„«       Z	ed	e
dee   fd
„«       Zedej                  ddfd„«       Zede
fd„«       Zede
fd„«       Zedee
e
f   dee
e
f   fd„«       Zy)ÚDRMzD
    Class to handle DRM operations with clock skew correction.
    g        Úclock_skew_secondsÚskew_secondsÚreturnNc                 ó6   — t         xj                  | z  c_        y)aW  
        Adjust the clock skew in seconds in case the system clock is off.

        This method updates the `clock_skew_seconds` attribute of the DRM class
        to the specified number of seconds.

        Args:
            skew_seconds (float): The number of seconds to adjust the clock skew to.

        Returns:
            None
        N)r   r   )r   s    úV/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/edge_tts/drm.pyÚadj_clock_skew_secondszDRM.adj_clock_skew_seconds   s   € ô 	×Ò ,Ñ.Öó    c                  ó†   — t        j                  t        j                  «      j	                  «       t
        j                  z   S )z½
        Gets the current timestamp in Unix format with clock skew correction.

        Returns:
            float: The current timestamp in Unix format with clock skew correction.
        )ÚdtÚnowÚtzÚutcÚ	timestampr   r   © r   r   Úget_unix_timestampzDRM.get_unix_timestamp+   s*   € ô v‰v”b—f‘f‹~×'Ñ'Ó)¬C×,BÑ,BÑBÐBr   Údatec                 ó¦   — 	 t        j                  | d«      j                  t        j                  ¬«      j                  «       S # t        $ r Y yw xY w)aN  
        Parses an RFC 2616 date string into a Unix timestamp.

        This function parses an RFC 2616 date string into a Unix timestamp.

        Args:
            date (str): RFC 2616 date string to parse.

        Returns:
            Optional[float]: Unix timestamp of the parsed date string, or None if parsing failed.
        z%a, %d %b %Y %H:%M:%S %Z)ÚtzinfoN)r   ÚstrptimeÚreplacer   r   r   Ú
ValueError)r   s    r   Úparse_rfc2616_datezDRM.parse_rfc2616_date5   sF   € ð	ä—‘˜DÐ"<Ó=ß‘¤§¡Ó'ß‘“ðøô
 ò 	Ùð	ús   ‚AA Á	AÁAÚec                 ól  — | j                   €t        d«      | ‚| j                   j                  dd«      }|t        |t        «      st        d«      | ‚t
        j                  |«      }|t        |t        «      st        d|› «      | ‚t
        j                  «       }t
        j                  ||z
  «       y)aS  
        Handle a client response error.

        This method adjusts the clock skew based on the server date in the response headers
        and raises a SkewAdjustmentError if the server date is missing or invalid.

        Args:
            e (Exception): The client response error to handle.

        Returns:
            None
        NzNo server date in headers.ÚDatezFailed to parse server date: )
Úheadersr	   ÚgetÚ
isinstanceÚstrr   r!   Úfloatr   r   )r"   Úserver_dateÚserver_date_parsedÚclient_dates       r   Úhandle_client_response_errorz DRM.handle_client_response_errorK   s¬   € ð 9‰9ÐÜ%Ð&BÓCÈÐJØ%&§Y¡Y§]¡]°6¸4Ó%@ˆØÐ¤j°¼cÔ&BÜ%Ð&BÓCÈÐJÜ.1×.DÑ.DÀ[Ó.QÐØÐ%¬ZÐ8JÌEÔ-RÜ%Ø/°¨}Ð=óàðô ×,Ñ,Ó.ˆÜ×"Ñ"Ð#5¸Ñ#CÕDr   c                  óú   — t         j                  «       } | t        z  } | | dz  z  } | t        dz  z  } | d›t        › }t        j                  |j                  d«      «      j                  «       j                  «       S )aí  
        Generates the Sec-MS-GEC token value.

        This function generates a token value based on the current time in Windows file time format
        adjusted for clock skew, and rounded down to the nearest 5 minutes. The token is then hashed
        using SHA256 and returned as an uppercased hex digest.

        Returns:
            str: The generated Sec-MS-GEC token value.

        See Also:
            https://github.com/rany2/edge-tts/issues/290#issuecomment-2464956570
        i,  éd   z.0fÚascii)
r   r   Ú	WIN_EPOCHÚS_TO_NSr   ÚhashlibÚsha256ÚencodeÚ	hexdigestÚupper)ÚticksÚstr_to_hashs     r   Úgenerate_sec_ms_geczDRM.generate_sec_ms_gecf   s€   € ô" ×&Ñ&Ó(ˆð 	”Ñˆð 	˜‘Ñˆð 	”˜3‘Ñˆð ˜s˜Ô$8Ð#9Ð:ˆô ~‰~˜k×0Ñ0°Ó9Ó:×DÑDÓF×LÑLÓNÐNr   c                  óH   — t        j                  d«      j                  «       S )za
        Generates a random MUID.

        Returns:
            str: The generated MUID.
        é   )ÚsecretsÚ	token_hexr7   r   r   r   Úgenerate_muidzDRM.generate_muidˆ   s   € ô × Ñ  Ó$×*Ñ*Ó,Ð,r   r%   c                 óh   — | j                  «       }d|vsJ ‚dt        j                  «       › d|d<   |S )zÞ
        Returns a copy of the given headers with the MUID header added.

        Args:
            headers (dict): The original headers.

        Returns:
            dict: The headers with the MUID header added.
        ÚCookiezmuid=ú;)Úcopyr   r?   )r%   Úcombined_headerss     r   Úheaders_with_muidzDRM.headers_with_muid’   sC   € ð #Ÿ<™<›>ÐØÐ/Ñ/Ð/Ð/Ø',¬S×->Ñ->Ó-@Ð,AÀÐ%CÐ˜Ñ"ØÐr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   Ú__annotations__Ústaticmethodr   r   r(   r   r!   ÚaiohttpÚClientResponseErrorr-   r:   r?   r   rE   r   r   r   r   r      s  … ñð !$Ð˜Ó#àð/¨Uð /°tò /ó ð/ð ðC ò Có ðCð ð ð ¨°%©ò ó ðð* ðE¨×(CÑ(Cð EÈò Eó ðEð4 ðO ò Oó ðOðB ð-˜3ò -ó ð-ð ð  4¨¨S¨¡>ð  °d¸3À¸8±nò  ó ñ r   r   )rI   r3   r=   r   r   r   r   Útypingr   r   rL   Ú	constantsr   Ú
exceptionsr	   r1   r2   r   r   r   r   ú<module>rQ      s;   ðñOó Û Ý #Ý #ß !ã å +Ý +à€	Ø
€÷L ò L r   