U
    g.                     @   s`   d dl Z d dlZddlmZ ddlmZmZmZmZm	Z	m
Z
 G dd deZG dd	 d	eZdS )
    N   )InfoExtractor   )ExtractorErrordetermine_ext
js_to_jsonparse_qstraverse_objurlencode_postdatac                   @   s   e Zd 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ddd
ddi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gZd'd( Z	d)d* Z
d+d, Zd-d. ZdS )/IPrimaIEzAhttps?://(?!cnn)(?:[^/]+)\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)FZiprimazhttps://auth.iprima.czNz+https://prima.iprima.cz/particka/92-epizodaZp51388mp4u   Partička (92)z$md5:57943f6a50d6188288c3a579d2fd5f01u	   Partičkau   Prima Partička\   zkhttps://d31b9s05ygj54s.cloudfront.net/prima-plus/image/video-ef6cf9de-c980-4443-92e4-17fe8bccd45c-16x9.jpeg)	idexttitledescriptionepisodeZseasonZseriesepisode_number	thumbnailskip_downloadTurlZ	info_dictparamszKhttps://zoom.iprima.cz/porady/krasy-kanarskych-ostrovu/tenerife-v-risi-ohneZp1412199   u   Tenerife: V říši ohněz$md5:4b4a05c574b5eaef130e68d4811c3f2cg     N@zhttps://d31b9s05ygj54s.cloudfront.net/prima-plus/image/video-f66dd7fb-c1a0-47d1-b3bc-7db328d566c5-16x9-1711636518.jpg/t_16x9_medium_1366_768icfZ20240330)
r   r   r   r   r   durationr   r   	timestampZupload_datez*http://play.iprima.cz/particka/particka-92)r   Zonly_matchingzBhttp://play.iprima.cz/closer-nove-pripady/closer-nove-pripady-iv-1z7https://prima.iprima.cz/my-little-pony/mapa-znameni-2-2zGhttps://prima.iprima.cz/porady/jak-se-stavi-sen/rodina-rathousova-prahaz&http://www.iprima.cz/filmy/desne-randezLhttps://zoom.iprima.cz/10-nejvetsich-tajemstvi-zahad/posvatna-mista-a-stavbyz)https://krimi.iprima.cz/mraz-0/sebevrazdyz,https://cool.iprima.cz/derava-silnice-nevadiz:https://love.iprima.cz/laska-az-za-hrob/slib-dany-bratrovic                 C   s  | j r
d S | j| j dd ddd}| |}|||d | j| j dd t|dd\}}d|jkr| d	|d
}| j	| j d| d ddidd}t
|jthddf}|stdddddd|| j dd}	| j| j dd ddt|	d}
|
d| _ | j d krtdddd S ) Nz/oauth2/loginzDownloading login pagezDownloading login page failed)noteerrnote)Z_emailZ	_passwordz
Logging in)datar   z/profile-selectz!data-identifier\s*=\s*["\']?(\w+)z
profile idz/user/profile-select-perform/ZcontinueUrlz/user/login?redirect_uri=/user/zSelecting profile)queryr   coder   zLogin failedTexpectedz1openid+email+profile+phone+address+offline_accessZ	prima_ssoZauthorization_codez/sso/auth-check)ZscopeZ	client_idZ
grant_typer    Zredirect_uriz/oauth2/tokenzDownloading tokenzDownloading token failed)r   r   r   access_tokenzGetting token failed)r#   _download_webpage
_AUTH_ROOTZ_hidden_inputsupdateZ_download_webpage_handler
   r   _search_regexZ_request_webpager	   r   r   _download_jsonget)selfusernamepasswordZ
login_pageZ
login_formZprofile_select_htmlZlogin_handleZ
profile_idr    Ztoken_request_dataZ
token_data r-   ;/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/iprima.py_perform_loginU   sd    
  

  

    

  zIPrimaIE._perform_loginc                 C   s   | j s| jddd d S )Nz.Login is required to access any iPrima contentr,   )method)r#   Zraise_login_required)r*   r-   r-   r.   _real_initialize   s    zIPrimaIE._real_initializec                 C   s4   |dkr| j dgdd n|d k	r0| jddd d S )NZPLAY_GEOIP_DENIEDCZTZ	countriesZmetadata_availablez Access to stream infos forbiddenr!   )raise_geo_restrictedraise_no_formats)r*   Z
error_coder-   r-   r.   _raise_access_error   s    zIPrimaIE._raise_access_errorc                 C   s  |  |}| ||}| |p4| jddg|dd d}| jd|ddd d}|sz| j||d	d
d}t|ddddthfd
d}|s| jd|dd ddd}t|dd d
d}|s| 	d | j
d| d|ddd| jidd}| |d  |d!}g }|d kr| j	d"d#d$ n|D ]|}	|	d%}
|	d&}t|}|
d'ksH|d(krd|| j||d)d*d+d
d,7 }n*|
d-ksx|d.kr|| j||d/d
d07 }q| j||i d}|||dp|| jd1d2d3g|d1d d|| jd4d5d6g|d4d dd7 |S )8Nzog:titleztwitter:titler   default)z%productId\s*=\s*([\'"])(?P<id>p\d+)\1z'pproduct_id\s*=\s*([\'"])(?P<id>p\d+)\1z(let\s+videos\s*=\s*([\'"])(?P<id>p\d+)\1real idr   )groupr8   r   F)Ztraversefatal.contentZadditionalsZvideoPlayId)get_allz.<script[^>]+\bid=["\']__NUXT_DATA__["\'][^>]*>z	nuxt dataz	</script>z\[(?s:.+)\])Zend_patternZcontains_patternc                 S   s   t d|S )Nzp\d+)re	fullmatch)_vr-   r-   r.   <lambda>       z(IPrimaIE._real_extract.<locals>.<lambda>z'Unable to extract video ID from webpagez7https://api.play-backend.iprima.cz/api/v1//products/id-z/playzGetting manifest URLszFailed to get manifest URLszX-OTT-Access-Tokeni  )r   r   headersZexpected_statusZ	errorCodeZstreamInfoszReading stream infos failedTr!   typer   ZHLSm3u8r   m3u8_nativehlsZentry_protocolZm3u8_idr;   ZDASHmpddashZmpd_idr;   r   zog:imageztwitter:imager   zog:descriptionztwitter:descriptionr   r   r   formatsr   )	_match_idr$   Z_html_extract_titleZ_html_search_metar'   Z_search_nuxt_datar	   strZ_search_jsonr5   r(   r#   r6   r)   r   _extract_m3u8_formats_extract_mpd_formatsZ_search_json_ldr&   )r*   r   video_idwebpager   Z	nuxt_datametadataZstream_infosrN   manifestZmanifest_typeZmanifest_urlr   Zfinal_resultr-   r-   r.   _real_extract   s    
           

  



    
       zIPrimaIE._real_extract)__name__
__module____qualname__
_VALID_URL_GEO_BYPASSZ_NETRC_MACHINEr%   r#   _TESTSr/   r1   r6   rW   r-   r-   r-   r.   r      s     ?/r   c                   @   s6   e Zd ZdZdZddddddd	id
gZdd ZdS )IPrimaCNNIEz4https?://cnn\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)Fzhttps://cnn.iprima.cz/porady/strunc/24072020-koronaviru-mam-plne-zuby-strasit-druhou-vlnou-je-absurdni-rika-senatorka-dernerovaZp716177r   z$md5:277c6b1ed0577e51b40ddd35602ff43e)r   r   r   r   rF   r   c                    sp   |ddd |}j|d dp>d|d}d|djd	d
dtt dd|id}g  d fdd	}jjd|dddtdd}|r|	di 
 D ]B\}}t|tsq|D ](}	|		d}
|
r||
| |		d qq s,td|D ]\}}
||
 q sLd|krLjdgdd |j|d d j|d ddS ) Nzplay.iprima.czZott_adult_confirmed1r7   z<h1>([^<]+)r   )z<iframe[^>]+\bsrc=["\'](?:https?:)?//(?:api\.play-backend\.iprima\.cz/prehravac/embedded|prima\.iprima\.cz/[^/]+/[^/]+)\?.*?\bid=(p\d+)zdata-product="([^"]+)">zid=["\']player-(p\d+)"z!playerId\s*:\s*["\']player-(p\d+)z\bvideos\s*=\s*["\'](p\d+)r9   z$http://play.iprima.cz/prehravac/initzDownloading playerr   )Z_infuse_tsZ	productIdZReferer)r   r   rD   c                    sv   t | }g }|dks|dkr4j| ddddd}n|dksD|dkrHd S |rh|D ]}|d
sP||d
< qP | d S )NrH   rF   r   rG   FrI   rK   rJ   rL   language)r   rQ   rR   r)   extend)Z
format_urlZ
format_keylangr   Znew_formatsfrN   r*   rS   r-   r.   extract_formats  s0                 

z2IPrimaCNNIE._real_extract.<locals>.extract_formatsz<(?s)(?:TDIPlayerOptions|playerOptions)\s*=\s*({.+?});\s*\]\]zplayer optionsz{}F)Ztransform_sourcer;   trackssrcrc   zsrc["\']\s*:\s*(["\'])(.+?)\1z>GEO_IP_NOT_ALLOWED<r2   Tr3   rM   )NN)rO   Z_set_cookier$   Z_og_search_titler'   roundtimeZ_parse_jsonr   r)   items
isinstancelistlowerr>   findallr4   Z_og_search_thumbnailZ_og_search_description)r*   r   rT   r   Z
playerpagerf   optionskeyrg   trackrh   r@   r-   re   r.   rW      sx    
     
    

zIPrimaCNNIE._real_extractN)rX   rY   rZ   r[   r\   r]   rW   r-   r-   r-   r.   r^      s    r^   )r>   rj   commonr   utilsr   r   r   r   r	   r
   r   r^   r-   r-   r-   r.   <module>   s    
 F