
    uki*D                         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Zd dlZ	ddl
mZ ddlmZmZ ddlmZmZ  G d de      Z G d	 d
e      Z G d de      Z G d d      Zy)    N   )InfoExtractor   )ExtractorErrorint_or_none)	get_firsttraverse_objc                       e Zd ZdZdZdZdZddddd	d
ddddd	ddiddddddddddidddddddddddd d!	ddiddgZd"Zd# Z	d$ Z
d% Zy")&GoPlayIEzplay.tvzPLAY (formerly goplay.be)zAhttps?://(www\.)?play\.tv/video/([^/?#]+/[^/?#]+/|)(?P<id>[^/#]+)goplayzhttps://www.play.tv/video/de-slimste-mens-ter-wereld/de-slimste-mens-ter-wereld-s22/de-slimste-mens-ter-wereld-s22-aflevering-1z$2baa4560-87a0-421b-bffc-359914e3c387mp4z/De Slimste Mens ter Wereld - S22 - Aflevering 1zJre:In aflevering 1 nemen Daan Alferink, Tess Elst en Xander De Rycke .{66}zDe Slimste Mens ter WereldzWordt aangekondigd   r   z	Season 22)	idexttitledescriptionseriesepisodeseason_numberepisode_numberseasonskip_downloadTz1This video is only available for registered users)url	info_dictparamsskipzhttps://www.play.tv/video/1917z$40cac41d-8d29-4ef5-aa11-75047b9f09071917zHre:Op het hoogtepunt van de Eerste Wereldoorlog krijgen twee jonge .{94})r   r   r   r   zLhttps://www.play.tv/video/de-mol/de-mol-s11/de-mol-s11-aflevering-1#autoplayz$ecb79672-92b9-4cd9-a0d7-e2f0250681eezDe Mol - S11 - Aflevering 1zNre:Tien kandidaten beginnen aan hun verovering van Amerika en ontmoeten .{102}z	Episode 1zDe Mol   z	Season 11)	r   r   r   r   r   r   r   r   r   Nc                 v    | j                          t        | dd      }|j                  ||      \  | _        }y )Nzeu-west-1_dViSsKM5Y6s1h851s8uplco5h6mqh1jac8m)iepool_id	client_id)usernamepassword)report_loginAwsIdpauthenticate	_id_token)selfr$   r%   aws_s        R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/yt_dlp/extractor/goplay.py_perform_loginzGoPlayIE._perform_loginB   s:    &;Gcd,,h,R    c                 @    | j                   s| j                  d      y )Nr%   )method)r)   raise_login_requiredr*   s    r-   _real_initializezGoPlayIE._real_initializeG   s"    ~~++:+>> r/   c                 2   | j                        }| j                  |      }| j                  ||      }t        |fd      }|d   t	        |dt
        hfdt
        j                  hfd      }t	        |d      dk7  rt	        |dd	dt        hf      D ]  }t	        |d
fdt        f      }|st	        |dt        hf      }	|j                  t	        |dt
        hf      t	        |dt        hf      |	| j                  d|	 d|j                  d      dd       d        n | j                  d dd| j                   i| j                               }
d|
v r| j!                  |
d   d   dd      \  }}n}d|
vrt#        d      |
d   d   }|
d   d    }| j                  d!| d"| d#d$d%d&i'      }| j%                  |d(         }|D cg c]  }d)|d*   vs| }}| j'                  |      \  }}|j                  ||d+       |S c c}w ),Nc                 n    | dv xr/ |d   t         j                  j                        j                  k(  S )N)videometapath)urllibparseurlparser9   )kvr   s     r-   <lambda>z(GoPlayIE._real_extract.<locals>.<lambda>Q   s1    //`AfIAVAVWZA[A`A`4` r/   uuidr   r   )r   r   )programsubtypemovie.	playlistsvideosc                     |d   k(  S )NvideoId )r,   r>   video_ids     r-   r?   z(GoPlayIE._real_extract.<locals>.<lambda>[   s    QRS\Q]aiQi r/   r   episodeTitleepisodeNumberz
^(.+)? - Sz - r   )default)r   r   r   r   z,https://api.play.tv/web/v1/videos/long-form/AuthorizationzBearer )headersmanifestUrlshlsr   HLS)r   m3u8_idssaizexpecting Google SSAI streamcontentSourceIDvideoIDz-https://dai.google.com/ondemand/dash/content/z/vid/z/streamss   {"api-key":"null"}zcontent-typezapplication/json)datarN   stream_manifestz-ad-r   )r   formats	subtitles)	_match_id_download_webpage_search_nextjs_v13_datar   r	   strstripdictanyr   update_search_regexget_download_jsonr)   geo_verification_headers#_extract_m3u8_formats_and_subtitlesr   _extract_mpd_periods_merge_mpd_periods)r*   r   
display_idwebpagenextjs_datar8   r   season_dataepisode_datar   apirX   rY   ssai_content_source_idssai_video_iddaiperiodsprI   s    `                @r-   _real_extractzGoPlayIE._real_extractK   s   ^^C(
((j9227JG`c < u%)CII;7(
 	
 45@+K#{CRVQW9XY +K(Dikn9op# ,[8k]:S T  +L>C5:QR&2</T_S`Aa&b%2"00%m_C8)--:PRZdh 1 j	"   !!:8*E74>>*:!;//1 "  S !%!I!IN#E*H% "J "PGY S $%CDD%([1B%C"K	2M%%?@V?WW\]j\kkst4');< & >C
 //4E0FQG #*CQV1T7-BqCGC!%!8!8!AGY"
 	
  Ds   H#H)__name__
__module____qualname__IE_NAMEIE_DESC
_VALID_URL_NETRC_MACHINE_TESTSr)   r.   r4   rt   rH   r/   r-   r   r      s    G)GUJN Q8Fh2+!

 #D)C  08f	
 #D)C
 ^82l"!

 #D)C3(FT IS
?Ar/   r   c                       e Zd ZdZy)InvalidLoginExceptionz# The login credentials are invalid Nru   rv   rw   __doc__rH   r/   r-   r~   r~      s    -r/   r~   c                       e Zd ZdZy)AuthenticationExceptionz' Something went wrong while logging in Nr   rH   r/   r-   r   r      s    1r/   r   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zd Zy)r'   z AWS Identity Provider c                 f   || _         || _        d| j                  vrt        d      || _        | j                  j	                  d      d   | _        d| j
                   d| _        d| _        d| _        t        dd	      | _
        | j                  | j                        | _        | j                  | j                        | _        | j                  | j                  d
| j                  z   dz   | j                  z               | _        | j!                         | _        | j%                         | _        y)aM  
        :param InfoExtrator ie: The extractor that instantiated this class.
        :param str pool_id:     The AWS user pool to connect to (format: <region>_<poolid>).
                                E.g.: eu-west-1_aLkOfYN3T
        :param str client_id:   The client application ID (the ID of the application connecting)
        r,   z4Invalid pool_id format. Should be <region>_<poolid>.r   zhttps://cognito-idp.z.amazonaws.com/   FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF6955817183995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7DB3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D2261AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200CBBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFCE0FD108E4B82D120A93AD2CAFFFFFFFFFFFFFFFF2zCaldera Derived Keyutf-8000N)r!   r"   
ValueErrorr#   splitregionr   n_hexg_hex	bytearray	info_bits_AwsIdp__hex_to_longbig_ng_AwsIdp__hex_hashr=    _AwsIdp__generate_random_small_asmall_a_value_AwsIdp__calculate_alarge_a_value)r*   r!   r"   r#   s       r-   __init__zAwsIdp.__init__   s    dll"STT"ll((-a0)$++oF
? 	
& 
"#8'B''

3
##DJJ/##DOOD4::4E4Kdjj4X$YZ!;;=!//1r/   c                 @   | j                  |      }t        j                  |      j                         }dddd}| j                  j                  | j                  d||dd      }|j                  d	      }|j                  d
      dk7  rt        |d         | j                  ||      }t        j                  |      j                         }	ddd}
| j                  j                  | j                  d|	|
dd      }d|v rt        |d         |d   d   |d   d   fS )z, Authenticate with a username and password. z.AWSCognitoIdentityProviderService.InitiateAuthidentityzapplication/x-amz-json-1.1)X-Amz-TargetzAccept-EncodingContent-TypeNzAuthenticating usernamezInvalid username)rV   rN   noteerrnoteChallengeParametersChallengeNamePASSWORD_VERIFIERmessagez8AWSCognitoIdentityProviderService.RespondToAuthChallenge)r   r   zAuthenticating passwordzInvalid passwordAuthenticationResultIdTokenRefreshToken)#_AwsIdp__get_authentication_requestjsondumpsencoder!   rd   r   rc   r   '_AwsIdp__get_challenge_response_requestr~   )r*   r$   r%   auth_data_dict	auth_dataauth_headersauth_response_jsonchallenge_parameterschallenge_requestchallenge_datachallenge_headerss              r-   r(   zAwsIdp.authenticate   sT    ::8DJJ~.557	L)8

 "WW33HHdL*4F 4 H  2556KL!!/26II)*<Y*GHH !AABVX`a$56==?V8
 "WW33HHd9J*4F 4 H **'(:9(EFF56yA56~F
 	
r/   c                 Z    || j                  | j                        dd| j                  dS )z

        :param str username:    The username to use

        :return: A full Authorization request.
        :rtype: dict
        )USERNAMESRP_AUSER_SRP_AUTH)AuthParametersAuthFlowClientId)_AwsIdp__long_to_hexr   r#   )r*   r$   s     r-   __get_authentication_requestz#AwsIdp.__get_authentication_request   s6     %++D,>,>? (
 	
r/   c                 :   |d   }|d   }|d   }|d   }|d   }| j                         }| j                  ||| j                  |      |      }	t        j                  |      }
t        | j                  j                  d      d   d      t        |d      z   t        |
      z   t        |d      z   }t        j                  |	|t        j                  	      }t        j                  |j                               j                  d      }||||d
d| j                  dS )aH   Create a Challenge Response Request object.

        :param dict[str,str|imt] challenge_parameters:  The parameters for the challenge.
        :param str password:                            The password.

        :return: A valid and full request data object to use as a response for a challenge.
        :rtype: dict
        r   USER_ID_FOR_SRPSRP_BSALTSECRET_BLOCKr,   r   r   )	digestmod)r   	TIMESTAMPPASSWORD_CLAIM_SECRET_BLOCKPASSWORD_CLAIM_SIGNATUREr   )ChallengeResponsesr   r   )_AwsIdp__get_current_timestamp"_AwsIdp__get_hkdf_key_for_passwordr   base64standard_b64decoder   r"   r   hmacnewhashlibsha256standard_b64encodedigestdecoder#   )r*   r   r%   user_iduser_id_for_srpsrp_bsaltsecret_block	timestamphkdfsecret_block_bytesmsghmac_objsignature_strings                 r-    __get_challenge_response_requestz'AwsIdp.__get_challenge_response_request  s8    'z2./@A$W-#F++N;002	 //u%	
 $66|D dll((-a0':ow/0()* i)* 	
 88D#@!44X__5FGNNwW $&/;,<	# 1	
 		
r/   c           
         | j                  | j                  |      }|dk(  rt        d      dj                  | j                  j                  d      d   ||      }| j                  |j                               }| j                  | j                  | j                  |      |z               }t        | j                  || j                        }	|| j                  |	z  z
  }
t        |
| j                  ||z  z   | j                        }| j!                  t"        j%                  | j                  |            t"        j%                  | j                  | j'                  |                        S )ab   Calculates the final hkdf based on computed S value, and computed U value and the key.

        :param str username:        Username.
        :param str password:        Password.
        :param int server_b_value:  Server B value.
        :param int salt:            Generated salt.

        :return Computed HKDF value.
        :rtype: object
        r   zU cannot be zero.z{}{}:{}r,   r   )_AwsIdp__calculate_ur   r   formatr"   r   _AwsIdp__hash_sha256r   r   r   _AwsIdp__pad_hexpowr   r   r=   r   _AwsIdp__compute_hkdfr   fromhexr   )r*   r$   r%   server_b_valuer   u_valueusername_passwordusername_password_hashx_valueg_mod_pow_xn
int_value2s_values               r-   __get_hkdf_key_for_passwordz"AwsIdp.__get_hkdf_key_for_password/  s3    $$T%7%7Ha<011%,,T\\-?-?-DQ-GS[\!%!3!34E4L4L4N!O$$T__T^^D5ILb5b%cd4667DJJ7#dff|&;;
j$"4"4w7H"H$**U""dnnW56dnnT-?-?-HIJ
 	
r/   c                    t        j                  ||t        j                        j	                         }| j
                  t        t        d      d      z   }t        j                  ||t        j                        j	                         }|dd S )z Standard hkdf algorithm

        :param {Buffer} ikm Input key material.
        :param {Buffer} salt Salt value.
        :return {Buffer} Strong key material.
        r   r   N   )r   r   r   r   r   r   r   chr)r*   ikmr   prkinfo_bits_update	hmac_hashs         r-   __compute_hkdfzAwsIdp.__compute_hkdfJ  si     hhtS'..188:>>Ic!fg,FFHHS"2GNNCJJL	"~r/   c                     | j                  | j                  |      | j                  |      z         }| j                  |      S )z Calculate the client's value U which is the hash of A and B

        :param int big_a:   Large A value.
        :param int big_b:   Server B value.

        :return Computed U value.
        :rtype: int
        )r   r   r   )r*   big_abig_b
u_hex_hashs       r-   __calculate_uzAwsIdp.__calculate_uW  s:     __T^^E%:T^^E=R%RS
!!*--r/   c                 B    | j                  d      }|| j                  z  S )zo Helper function to generate a random big integer

        :return a random value.
        :rtype: int
           )_AwsIdp__get_randomr   )r*   random_long_ints     r-   __generate_random_small_az AwsIdp.__generate_random_small_ad  s#     ++C0++r/   c                     t        | j                  | j                  | j                        }|| j                  z  dk(  rt	        d      |S )z Calculate the client's public value A = g^a%N with the generated random number a

        :return Computed large A.
        :rtype: int
        r   zSafety check for A failed)r   r   r   r   r   )r*   r   s     r-   __calculate_azAwsIdp.__calculate_am  sB     DFFD..

;DJJ1$899r/   c                 
    | dS )NxrH   )long_nums    r-   __long_to_hexzAwsIdp.__long_to_hexz  s    1r/   c                     t        | d      S )Nr   )int
hex_strings    r-   __hex_to_longzAwsIdp.__hex_to_long~  s    :r""r/   c                 R    t         j                  t        j                  |             S N)r'   r   r   r   r	  s    r-   
__hex_hashzAwsIdp.__hex_hash  s    ##I$5$5j$ABBr/   c                 p    t        j                  |       j                         }dt        |      z
  dz  |z   S )zAuthenticationHelper.hash@   r   )r   r   	hexdigestlen)bufr   s     r-   __hash_sha256zAwsIdp.__hash_sha256  s4     $..0S[ C'&00r/   c                     t        | t              st        j                  |       }n| }t	        |      dz  dk(  rd| }|S |d   dv rd| }|S )z Converts a Long integer (or hex string) to hex format padded with zeroes for hashing

        :param int|str long_int:    Number or string to pad.

        :return Padded hex string.
        :rtype: str
        r   r   r   r   89ABCDEFabcdefr   )
isinstancer]   r'   r   r  )long_inthash_strs     r-   	__pad_hexzAwsIdp.__pad_hex  se     (C(++H5HHx=1!8*~H  a[,,H:Hr/   c                 |    t        j                  t        j                  |             }t        j                  |      S r  )binasciihexlifyosurandomr'   r   )nbytes
random_hexs     r-   __get_randomzAwsIdp.__get_random  s,    %%bjj&89
##J//r/   c                     g d} g d}t         j                  j                  t         j                  j                        }||j                             d| |j                      d|j                   d}|j                  |      S )z Creates a timestamp with the correct English format.

        :return: timestamp in format 'Sun Jan 27 19:00:04 UTC 2019'
        :rtype: str
        )NJanFebMarAprMayJunJulAugSepOctNovDec)MonTueWedThuFriSatSun z %H:%M:%S UTC %Y)	dtdatetimenowtimezoneutcweekdaymonthdaystrftime)monthsdaystime_nowformat_strings       r-   __get_current_timestampzAwsIdp.__get_current_timestamp  sp     l@;;??2;;??3 0 0 234AfX^^6L5MQx||n\lm  //r/   c                     dj                  | j                  | j                  j                  d      d   | j                        S )Nz4AWS IDP Client for:
Region: {}
PoolId: {}
AppId:  {}r,   r   )r   r   r"   r   r#   r3   s    r-   __str__zAwsIdp.__str__  s8    HOOKK++C03T^^
 	
r/   N)ru   rv   rw   r   r   r(   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   rG  rH   r/   r-   r'   r'      s    !.2`"
H
"+
Z
6.,   # # C C 1 1
  & 0 0 0 0 
r/   r'   )r   r  r9  r8  r   r   r   r  urllib.parser:   commonr   utilsr   r   utils.traversalr   r	   r   r~   r   r'   rH   r/   r-   <module>rM     sY          	  ! / 5}} }F.N .2n 2_
 _
r/   