U
    g:                     @   s   d dl Z ddlmZ ddlmZ ddl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G dd deZdS )    N   )InfoExtractor   )	HTTPError)ExtractorErrordetermine_extfloat_or_noneint_or_nonesmuggle_urltry_getunsmuggle_urlc                   @   s8   e Zd ZdZedd ZdddZddd	Zd
d ZdS )LimelightBaseIEz<http://production-ps.lvp.llnw.net/r/PlaylistService/%s/%s/%sc           	   
   C   s   dddd}dd }g }t d|D ]6\}}|| |d||  d	| d
| | q$t d|D ]H}|d|d }}|| |d| d	| d
|  | qht d|D ]&}|| |d| t | q|S )Nmediachannelchannel_list)ZMediaZChannelZChannelListc                 S   s   t | d| iS )N
source_url)r
   )url r   >/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/limelight.pysmuggle   s    z4LimelightBaseIE._extract_embed_urls.<locals>.smugglezMLimelightPlayer\.doLoad(Media|Channel|ChannelList)\(["\'](?P<id>[a-z0-9]{32})z
limelight::Z	LimelightaQ  (?sx)
                    <object[^>]+class=(["\'])(?:(?!\1).)*\bLimelightEmbeddedPlayerFlash\b(?:(?!\1).)*\1[^>]*>.*?
                        <param[^>]+
                            name=(["\'])flashVars\2[^>]+
                            value=(["\'])(?:(?!\3).)*(?P<kind>media|channel(?:List)?)Id=(?P<id>[a-z0-9]{32})
                kindidzX(?s)LimelightPlayerUtil\.embed\s*\(\s*{.*?\bmediaId["\']\s*:\s*["\'](?P<id>[a-z0-9]{32})zlimelight:media:)	refindallappend
url_resultfinditergroup
capitalizeLimelightMediaIEZie_key)	clsr   ZwebpageZlmr   entriesr   video_idZmobjr   r   r   _extract_embed_urls   sF       z#LimelightBaseIE._extract_embed_urlsTNc              
   C   s   i }|r||d< z,| j | j| j||f |d| d||dW S  tk
r } zZt|jtr|jjdkr| |jj	
  |d d }|dkr|   t|d	d
 W 5 d }~X Y nX d S )NZRefererzDownloading PlaylistService z JSON)fatalheadersi  ZdetailZcontentAccessPermissionZCountryDisabledT)expected)Z_download_json_PLAYLIST_SERVICE_URL_PLAYLIST_SERVICE_PATHr   
isinstancecauser   statusZ_parse_jsonresponsereaddecodeZraise_geo_restricted)selfitem_idmethodr%   refererr&   eerrorr   r   r   _call_playlist_service>   s$     
  z&LimelightBaseIE._call_playlist_servicec                 C   s*   | j |||d}| j ||d|d}||fS )N)r3   F)r%   r3   )r6   )r0   r1   Z	pc_methodZmobile_methodr3   pcmobiler   r   r   _extractO   s       zLimelightBaseIE._extractc           "         s   fdd}||d||d dp0d } dpBd }g }g }	 dg D ]}
|
 d}|rX||	krvqX| d	s|
 d
rqX|	| t|}|dkr|| j||ddd qX|t|
 dt|
 d|d}t|
 d}t|
 d}t|
 d}|s|s|r2||||d nd|d< t	
d|}|r,d}|
 drp|dt|
d  7 }|dd}d}|D ]~\}}||d krqd||dd d  }|	| | |||r| }|||d! ||  qq||d|d|d"d#|d$ || qX d%g D ]}| d&}| d'}|rD||	krpqD|d(kr| d	dsqD|	| t|}|d)kr|| j||d*d+|dd, n8|dkr|| j|||dd n|||d-|d. qDi } d/D ]}|d0kr| |d1d|p2g }|D ]N}| d2}|sPq8| d3pl| jd4|d5d6d7} || g d|i q8 qqfd8d}!|||!d9|t|!d:d;|!d<p|!d=|d>S )?Nc                    s   t |  fddtpi S )Nc                    s   |    S Nr   x)iyr   r   <lambda>V       zALimelightBaseIE._extract_info.<locals>.<lambda>.<locals>.<lambda>)r   dict)r<   r>   )r=   )r>   r   r?   V   r@   z/LimelightBaseIE._extract_info.<locals>.<lambda>playlistItemsZ	mediaListZmediaIdtitleZstreamsr   Zallow_unplayable_formatsZdrmProtectedZf4mZhdsF)Zf4m_idr%   ZaudioBitRateZvideoFrameRate)r   ZabrZfpsextZvideoWidthInPixelsZvideoHeightInPixelsZvideoBitRate)widthheightvbrnoneZvcodeczG^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$rtmpz-%dhttp))zdelvenetworks.comzcpl.delvenetworks.com)zvideo.llnw.netzs2.content.video.llnw.nethostzhttp://{}/{}Zplaypath   )r   	format_idappZflv)r   Z	play_pathrN   rD   rM   Z
mobileUrlsZ	mobileUrlZtargetMediaPlatform)ZWidevineZSmoothStreamingZm3u8mp4Zm3u8_native)Zm3u8_idr%   i)r   rM   ZqualityrD   flagsZClosedCaptionsZ!getClosedCaptionsDetailsByMediaIdZwebvttFileUrlZlanguageCodez/([a-z]{2})\.vttlangen)defaultc                    s    | p  | S r:   )getr;   )mobile_itempc_itemr   r   r?      r@   descriptionZdurationInMillisecondsi  ZpreviewImageUrlZthumbnailImageUrl)r   rC   rW   formatsduration	thumbnail	subtitles)rT   	get_paramr   r   extendZ_extract_f4m_formatsr   r	   updater   searchreplacer   lowerformatZ_is_valid_urlcopyZ_extract_m3u8_formatsr6   Z_search_regex
setdefault)"r0   r7   r8   r=   r3   Zget_itemr#   rC   rX   urlsstreamZ
stream_urlrD   fmtrE   rF   rG   rI   rM   Zhttp_format_idZ	CDN_HOSTSZcdn_hostZ	http_hostZhttp_urlZhttp_fmtZ
mobile_urlZ	media_urlr[   flagZclosed_captionsccZcc_urlrQ   Zget_metar   )r=   rU   rV   r   _extract_infoU   s    



   










    

   

  
 

zLimelightBaseIE._extract_info)TN)N)	__name__
__module____qualname__r(   classmethodr$   r6   r9   rj   r   r   r   r   r      s   
*

r   c                
   @   s`   e Zd ZdZdZ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gZdZdd ZdS )r    Z	limelighta  (?x)
                        (?:
                            limelight:media:|
                            https?://
                                (?:
                                    link\.videoplatform\.limelight\.com/media/|
                                    assets\.delvenetworks\.com/player/loader\.swf
                                )
                                \?.*?\bmediaId=
                        )
                        (?P<id>[a-z0-9]{32})
                    zWhttp://link.videoplatform.limelight.com/media/?mediaId=3ffd040b522b4485b6d84effc750cd86Z 3ffd040b522b4485b6d84effc750cd86rO   zHaP and the HB Prince Trailerz$md5:8005b944181778e313d95c1237ddb640zre:^https?://.*\.jpeg$g(\b@)r   rD   rC   rW   rZ   rY   Zskip_downloadT)r   	info_dictparamsz0limelight:media:a3e00274d4564ec4a9b29b9466432335Z 2fa3bad9ac321e23860ca23bc2c69e3dZ a3e00274d4564ec4a9b29b9466432335z3Play Media Overview VideogX9vS@z
mincount:1)r   rD   rC   rZ   rY   r[   )r   md5ro   z[https://assets.delvenetworks.com/player/loader.swf?mediaId=8018a574f08d416e95ceaccae4ba0452r   Zonly_matchingr   c                 C   sZ   t |i \}}| |}|d}| d|di | |dd|\}}| ||d|S )Nr   Z	countriesZgeo_countriesZgetPlaylistByMediaIdZgetMobilePlaylistByMediaIdr   )r   	_match_idrT   Z_initialize_geo_bypassr9   rj   )r0   r   smuggled_datar#   r   r7   r8   r   r   r   _real_extract  s    

   zLimelightMediaIE._real_extractNrk   rl   rm   ZIE_NAMEZ
_VALID_URLZ_TESTSr)   ru   r   r   r   r   r       s<   
  r    c                   @   s>   e Zd ZdZdZddddddd	d
ddgZdZdd ZdS )LimelightChannelIEzlimelight:channela  (?x)
                        (?:
                            limelight:channel:|
                            https?://
                                (?:
                                    link\.videoplatform\.limelight\.com/media/|
                                    assets\.delvenetworks\.com/player/loader\.swf
                                )
                                \?.*?\bchannelId=
                        )
                        (?P<id>[a-z0-9]{32})
                    zYhttp://link.videoplatform.limelight.com/media/?channelId=ab6a524c379342f9b23642917020c082Z ab6a524c379342f9b23642917020c082zJavascript Sample CodezVJavascript Sample Code - http://www.delvenetworks.com/sample-code/playerCode-demo.html)r   rC   rW      r   ro   Zplaylist_mincountz\http://assets.delvenetworks.com/player/loader.swf?channelId=ab6a524c379342f9b23642917020c082Trr   r   c                    sv   t |i \}}|}|d|dd\  fddttd D }||d dS )	Nr   ZgetPlaylistByChannelIdz7getMobilePlaylistWithNItemsByChannelId?begin=0&count=-1c                    s   g | ]}  |qS r   )rj   ).0r=   r8   r7   r0   r   r   r   
<listcomp>8  s   z4LimelightChannelIE._real_extract.<locals>.<listcomp>rB   rC   rW   )r   rs   rT   r9   rangelenplaylist_result)r0   r   rt   Z
channel_idr"   r   r{   r   ru   .  s$    

    z LimelightChannelIE._real_extractNrv   r   r   r   r   rw     s   	rw   c                   @   s<   e Zd ZdZdZddddddd	d
dgZdZdd ZdS )LimelightChannelListIEzlimelight:channel_lista  (?x)
                        (?:
                            limelight:channel_list:|
                            https?://
                                (?:
                                    link\.videoplatform\.limelight\.com/media/|
                                    assets\.delvenetworks\.com/player/loader\.swf
                                )
                                \?.*?\bchannelListId=
                        )
                        (?P<id>[a-z0-9]{32})
                    z]http://link.videoplatform.limelight.com/media/?channelListId=301b117890c4465c8179ede21fd92e2bZ 301b117890c4465c8179ede21fd92e2bzWebsite - Hero Player)r   rC   r   ry   zahttps://assets.delvenetworks.com/player/loader.swf?channelListId=301b117890c4465c8179ede21fd92e2bTrr   r   c                    s>     |} |d} fdd|d D } |||d S )NZgetMobileChannelListByIdc                    s"   g | ]}  d |d dqS )zlimelight:channel:{}r   ZLimelightChannel)r   rb   )rz   r   r0   r   r   r|   a  s   z8LimelightChannelListIE._real_extract.<locals>.<listcomp>ZchannelListrC   )rs   r6   r   )r0   r   Zchannel_list_idr   r"   r   r   r   ru   [  s    
 
  z$LimelightChannelListIE._real_extractNrv   r   r   r   r   r   @  s   r   )r   commonr   Znetworking.exceptionsr   utilsr   r   r   r	   r
   r   r   r   r    rw   r   r   r   r   r   <module>   s   $ D?.