U
    g?!                     @   sx   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mZmZ G dd deZG dd	 d	eZG d
d deZdS )    N   )InfoExtractor   )ExtractorErrordetermine_extextract_attributesint_or_noneparse_qssmuggle_urlunescapeHTMLunsmuggle_urlc                   @   sP   e Zd ZdZdZedd ZedddZdd	 Z	dd
dZ
dd Zdd ZdS )GlomexBaseIEzhttps://player.glomex.com/z:https://integration-cloudfront-eu-west-1.mes.glomex.cloud/c                 C   s   |d kr| S t | d|iS )Norigin)r
   )url
origin_url r   ;/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/glomex.py_smuggle_origin_url   s    z GlomexBaseIE._smuggle_origin_urlNc                 C   s*   d|p
| j i}t||d\}}||d fS )Nr   )default)_DEFAULT_ORIGIN_URLr   )clsr   Zfallback_origin_urldefaultsZunsmuggled_urldatar   r   r   _unsmuggle_origin_url   s    z"GlomexBaseIE._unsmuggle_origin_urlc                 C   s(   ddddd}| dd }||dS )	NvideoZplaylistzrelated videos playlistzcurated playlist)vplZrlZcl-r   zunknown type)splitget)selfvideo_idZ_VIDEOID_TYPESprefixr   r   r   _get_videoid_type!   s    zGlomexBaseIE._get_videoid_typec                 C   sB   |||p| j d}| |}| j| j|d| dd| d|dS )N)integration_idplaylist_idcurrent_urlzDownloading z JSONzUnable to download )query)r   r#   Z_download_json_API_URL)r    r!   integrationr&   r'   Zvideo_id_typer   r   r   _download_api_data+   s    
 

zGlomexBaseIE._download_api_datac                    s\     ||}|d }|s(td  fdd|D }t|dkrP|d S  |S )Nvideoszno videos found for c                    s   g | ]}  |qS r   )_extract_api_data).0r   r    r!   r   r   
<listcomp>=   s     z?GlomexBaseIE._download_and_extract_api_data.<locals>.<listcomp>r   r   )r*   r   lenZplaylist_result)r    r!   r)   r&   Zapi_datar+   r   r.   r   _download_and_extract_api_data8   s    z+GlomexBaseIE._download_and_extract_api_datac              	   C   s"  | ddkr| j|d d g i  }}|d  D ]Z\}}t|}|dkr~| j||d|dd	\}}	|| | j|	|d
 q4|||d q4| dr|D ]}
|d |
d< q| dpg | dpi g }dd |D }| | | dp|| d| d|t	| d| d||dS )NZ
error_codeZcontentGeoblockedZgeo_locations)Z	countriessourceZm3u8mp4F)Zm3u8_idfatal)target)r   	format_idlanguageimagesimagec                 S   s4   g | ],}| d r| d|d   ddddqS )r   idz/profile:player-960x540i  i  )r:   r   widthheight)r   )r-   r9   r   r   r   r/   W   s    
z2GlomexBaseIE._extract_api_data.<locals>.<listcomp>Zclip_idtitledescriptionZclip_durationZ
created_at)r:   r=   r>   
thumbnailsduration	timestampformatsZ	subtitles)
r   Zraise_geo_restricteditemsr   Z#_extract_m3u8_formats_and_subtitlesextendZ_merge_subtitlesappendZ_remove_duplicate_formatsr   )r    r   r!   rB   subsr6   Z
format_urlextZformats_Zsubs_fmtr8   r?   r   r   r   r,   @   sH    
   




zGlomexBaseIE._extract_api_data)N)N)__name__
__module____qualname__r   r(   staticmethodr   classmethodr   r#   r*   r1   r,   r   r   r   r   r      s   


r   c                
   @   sB   e Zd ZdZdZdZdZddddd	d
dddddgZdd ZdS )GlomexIEZglomexzGlomex videosz1https?://video\.glomex\.com/[^/]+/(?P<id>v-[^-]+)Z19syy24xjn1oqlpczehttps://video.glomex.com/sport/v-cb24uwg77hgh-nach-2-0-sieg-guardiola-mit-mancity-vor-naechstem-titelZ cec33a943c4240c9cb33abea8c26242ezv-cb24uwg77hghr3   z$md5:38a90cedcfadd72982c81acf13556e0cz$md5:1ea6b6caff1443fcbbba159e432eedb8is  i颍`Z20210501)r:   rG   r=   r>   r@   rA   upload_dater   md5	info_dictc                 C   s(   |  |}| t|| j|t |S )N)	_match_idZ
url_resultGlomexEmbedIEbuild_player_url_INTEGRATION_IDZie_key)r    r   r!   r   r   r   _real_extract   s    
 zGlomexIE._real_extractN)	rI   rJ   rK   IE_NAMEIE_DESC
_VALID_URLrV   _TESTSrW   r   r   r   r   rN   k   s    rN   c                
   @   s   e Zd ZdZdZdZeeddZ	de	 dZ
dd	d
dddddddddddidddddiddgZed#ddZedd  Zd!d" ZdS )$rT   zglomex:embedzGlomex embedded videosz4//player.glomex.com/integration/1/iframe-player.htmlz/1/z/[^/]/zhttps?:z$\?([^#]+&)?playlistId=(?P<id>[^#&]+)zvhttps://player.glomex.com/integration/1/iframe-player.html?integrationId=4059a013k56vb2yd&playlistId=v-cfa6lye0dkdd-sfZ 68f259b98cc01918ac34180142fce287zv-cfa6lye0dkdd-sfr3   iCyai Z20211027z$md5:e741185fc309310ff5d0c789b437be66z$md5:35647293513a6c92363817a0fb0a7961)r:   rG   rA   r@   rO   r>   r=   rP   zhttps://player.glomex.com/integration/1/iframe-player.html?origin=fullpage&integrationId=19syy24xjn1oqlpc&playlistId=rl-vcb49w1fb592p&playlistIndex=0r:   zrl-vcb49w1fb592pd   )r   rR   Zplaylist_countzthttps://player.glomex.com/integration/1/iframe-player.html?playlistId=cl-bgqaata6aw8x&integrationId=19syy24xjn1oqlpczcl-bgqaata6aw8xr   )r   rR   Zplaylist_mincountNc                 C   s,   t j||d}| d| j d| |S )N)
playlistIdintegrationIdzhttps:?)urllibparse	urlencoder   _BASE_PLAYER_URL)r   r!   r)   r   Zquery_stringr   r   r   rU      s
    zGlomexEmbedIE.build_player_urlc                 c   s  d}d| d| j  d}t||D ]*}t|d}| |r$| ||V  q$d| d}t||D ]>}t|d}|d	rh|d
rh| 	|d
 |d	 |V  qhd| d}td|D ]H}|d}t
|d |}	|	sqt
|d |}
|
r| 	|
|	|V  qd S )Nz["\']z((?x)
            <iframe[^>]+?src=(?P<q>z&)(?P<url>
                (?:https?:)?z&\?(?:(?!(?P=q)).)+
            )(?P=q)r   zX(?x)
            <glomex-player [^>]+?>|
            <div[^>]* data-glomex-player=(?P<q>z)true(?P=q)[^>]*>r   zdata-integration-idzdata-playlist-idzN(?x)
            (?P<is_js>dataset\.)?%s\s*(?(is_js)=|:)\s*
            (?P<q>z!)(?P<id>(?:(?!(?P=q)).)+)(?P=q)\sz(?x)<script[^<]*>.+?</script>r^   r]   )_BASE_PLAYER_URL_RErefinditerr   groupZsuitabler   r   r   rU   search)r   r   ZwebpageZquot_reregexZmobjZ	embed_urlattrsscriptr$   r%   r   r   r   _extract_embed_urls   s8    

z!GlomexEmbedIE._extract_embed_urlsc                 C   sL   |  |\}}| |}t|dd gd }|s>tddd| |||S )Nr^   r   zNo integrationId in URLT)expected)r   rS   r	   r   r   r1   )r    r   r   r%   r)   r   r   r   rW      s    
zGlomexEmbedIE._real_extract)N)rI   rJ   rK   rX   rY   rc   re   escapereplacerd   rZ   r[   rM   rU   rl   rW   r   r   r   r   rT      sD     
"rT   )re   urllib.parser`   commonr   utilsr   r   r   r   r	   r
   r   r   r   rN   rT   r   r   r   r   <module>   s   (Z