
    yfB                         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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traverse_objunescapeHTMLc                   ~    e Zd ZdZdZddddddd	d
dddddddd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)#GoPlayIEzGhttps?://(www\.)?goplay\.be/video/([^/]+/[^/]+/|)(?P<display_id>[^/#]+)goplayzjhttps://www.goplay.be/video/de-container-cup/de-container-cup-s3/de-container-cup-s3-aflevering-2#autoplayz$9c4214b8-e55d-4e4b-a446-f015f6c6f811mp4zS3 - Aflevering 2zDe Container CupzSeason 3   z	Episode 2r   )idexttitleseriesseasonseason_numberepisodeepisode_numberz1This video is only available for registered users)url	info_dictskipzNhttps://www.goplay.be/video/a-family-for-thr-holidays-s1-aflevering-1#autoplayz$74e3ed07-748c-49e4-85a0-393a93337dbfzA Family for the Holidays)r   r   r   zNhttps://www.goplay.be/video/de-mol/de-mol-s11/de-mol-s11-aflevering-1#autoplayz$03eb8f2f-153e-41cb-9805-0d3a29dab656zS11 - Aflevering 1z	Episode 1zDe Mol   r   z	Season 11)r   r   r   r   r   r   r   r   skip_downloadT)r   r   paramsr   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        9/usr/lib/python3/dist-packages/yt_dlp/extractor/goplay.py_perform_loginzGoPlayIE._perform_login?   s:    &;Gcd,,h,R    c                 @    | j                   s| j                  d      y )Nr"   )method)r&   raise_login_requiredr'   s    r*   _real_initializezGoPlayIE._real_initializeD   s"    ~~++:+>> r,   c                    | j                        j                  dd      \  }| j                  |      }| j                  d|d      }| j	                  t        |      |      j                  d      }|j                  d      }|r|d   }d|j                  d      i}nXt        |d	d
dfdfd      }	|	d   }|	j                  d      t        |	d      |	j                  d      |	j                  d      d}| j                  d| |dd| j                  z  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 ),Nr   
display_idz<div\s+data-hero="([^"]+)"
video_datadatamovie	videoUuidr   	playlists.episodesc                     |d   d   k(  S )NpageInfor    )r)   vr   s     r*   <lambda>z(GoPlayIE._real_extract.<locals>.<lambda>U   s    [\]g[hin[osv[v r,   F)get_allepisodeTitle)programr   seasonNumberepisodeNumber)r   r   r   r   z.https://api.goplay.be/web/v1/videos/long-form/Authorizationz	Bearer %s)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)r5   rE   stream_manifestz-ad-r   )r   formats	subtitles)_match_valid_urlgroup_download_webpage_html_search_regex_parse_jsonr   getr   _download_jsonr&   geo_verification_headers#_extract_m3u8_formats_and_subtitlesr   _extract_mpd_periods_merge_mpd_periodsupdate)r'   r   r3   webpagevideo_data_jsonr4   r6   video_idr   r   apirN   rO   ssai_content_source_idssai_video_iddaiperiodsps    `                r*   _real_extractzGoPlayIE._real_extractH   sl   //4::1lKZ((j9112OQXZfg%%l?&CZPTTU[\
w'[)H7+I #:S*Nv/w  BG  HG{+H ^4&w0DE!(^!<")++o">	I !!<XJGt~~!=//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   (G&5G&)
__name__
__module____qualname__
_VALID_URL_NETRC_MACHINE_TESTSr&   r+   r1   re   r<   r,   r*   r
   r
      s    [JN |8(( "	
 D `80

 D `8)"!	
 T
 D+%FN IS
?9r,   r
   c                       e Zd ZdZy)InvalidLoginExceptionz# The login credentials are invalid Nrf   rg   rh   __doc__r<   r,   r*   rm   rm      s    -r,   rm   c                       e Zd ZdZy)AuthenticationExceptionz' Something went wrong while logging in Nrn   r<   r,   r*   rq   rq      s    1r,   rq   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_hashk _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>BkkKH
$ 
"#8'B''

3
##DJJ/##DOOD4::4E4Kdjj4X$YZ!;;=!//1r,   c                 D   | j                  |      }t        j                  |      j                  d      }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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. rv   z.AWSCognitoIdentityProviderService.InitiateAuthidentityzapplication/x-amz-json-1.1)X-Amz-TargetzAccept-EncodingContent-TypeNzAuthenticating usernamezInvalid username)r5   rE   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   rV   r   rU   rq   '_AwsIdp__get_challenge_response_requestrm   )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   sX    ::8DJJ~.55g>	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==gFV8
 "WW33HHd9J*4F 4 H **'(:9(EFF56yA56~F
 	
r,   c                 ^    || 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!   auth_requests      r*   __get_authentication_requestz#AwsIdp.__get_authentication_request   s;     %++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   rv   )	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   rz   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_stringr   s                  r*    __get_challenge_response_requestz'AwsIdp.__get_challenge_response_request   s>    '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      | j                  j	                  d      d   |d|}| j                  |j                  d            }| 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.r)   r   :rv   )_AwsIdp__calculate_ur   ry   r   rz   _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_valuer   s                r*   __get_hkdf_key_for_passwordz"AwsIdp.__get_hkdf_key_for_password%  s0    $$T%7%7Ha<011)-););C)@)CXxX!%!3!34E4L4LW4U!V$$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   rv   N   )r   r   r   r   r   r   r~   chr)r'   ikmr   prkinfo_bits_update	hmac_hashs         r*   __compute_hkdfzAwsIdp.__compute_hkdfA  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_uN  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_a[  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   ry   )r'   r   s     r*   __calculate_azAwsIdp.__calculate_ad  sB     DFFD..

;DJJ1$899r,   c                     d| z  S )Nz%xr<   )long_nums    r*   __long_to_hexzAwsIdp.__long_to_hexq  s    hr,   c                     t        | d      S )Nr   )int
hex_strings    r*   __hex_to_longzAwsIdp.__hex_to_longu  s    :r""r,   c                 R    t         j                  t        j                  |             S N)r$   r   r~   r   r   s    r*   
__hex_hashzAwsIdp.__hex_hashy  s    ##I$5$5j$ABBr,   c                 p    t        j                  |       j                         }dt        |      z
  dz  |z   S )zAuthenticationHelper.hash@   rx   )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|z  }|S |d   dv rd|z  }|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   z0%sr   89ABCDEFabcdefz00%s)
isinstancestrr$   r   r  )long_inthash_strs     r*   	__pad_hexzAwsIdp.__pad_hex  sc     (C(++H5HHx=1!x'H  a[,,(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                        }dj                  ||j                            | |j                     |j                        }|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Sunz{} {} {} %H:%M:%S UTC %Y)
dtdatetimenowtimezoneutcformatweekdaymonthdaystrftime)monthsdaystime_nowformat_stringtime_strings        r*   __get_current_timestampzAwsIdp.__get_current_timestamp  su     l@;;??2;;??3299$x?O?O?Q:RTZ[c[i[iTjltlxlxy''6r,   c                 x    d| j                   d| j                  j                  d      d   d| j                  S )NzAWS IDP Client for:
Region: z	
PoolId: r)   r   z	
AppId:  )r{   r   rz   r    r0   s    r*   __str__zAwsIdp.__str__  s/    KK++C03T^^
 	
r,   N)rf   rg   rh   ro   r   r%   r   r   r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r9  r<   r,   r*   r$   r$      s    !-2^"
H$,!\8.,   # # C C 1 1
  & 0 0  "
r,   r$   )r   r  r)  r(  r   r   r   r  commonr   utilsr   r   r   r
   rm   rq   r$   r<   r,   r*   <module>r=     sZ          	 ! p} pl.N .2n 2b
 b
r,   