U
    gI                     @   s   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 ddlm	Z	m
Z
 ddlmZmZmZmZmZmZmZmZmZmZmZmZmZ dZd	d
 ZG dd deZG dd deeZG dd deZdS )    N   )AdobePassIE)OnceIE   )HEADRequestRequest)ExtractorErrordetermine_extfind_xpath_attrfloat_or_noneint_or_nonemimetype2extparse_qstraverse_objunsmuggle_url
update_urlupdate_url_queryurlhandle_detect_extxpath_with_nsz&http://www.w3.org/2005/SMIL21/Languagec                 C   s   t | dtiS )NZsmil)r   
default_ns)p r   @/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/theplatform.py<lambda>       r   c                   @   s2   e Zd ZdZdddZdd Zdd Zd	d
 ZdS )ThePlatformBaseIEcomDownloading SMIL datac                 C   sB  | j |||ddi|  d}t|tdd}|d k	rt|tddd}|d k	r|d	d
krn| |jd  n,|jd d| j drt	|jd dd| j
|||tddddd d\}}g }	|D ]r}
t|
d r|	| |
d  q|
d }t|dkr.| |d}|r.t|d|ji|
d< |	|
 q|	|fS )NformatZSMIL)notequeryheadersz.//smil:refsrcz.//smil:paramname	exceptionvalueZGeoLocationBlockedZabstracthttp://link.theplatform.z/s/errorFiles/Unavailable.T)expectedZUXWGVKRWHFSPz3.0.3)gZhdcorec                 S   s   | d| fS )Nzmp4:r   )Zstreamerr"   r   r   r   r   5   r   z=ThePlatformBaseIE._extract_theplatform_smil.<locals>.<lambda>)	namespaceZ
f4m_paramsZtransform_rtmp_urlurlm3u8hdnea2Zhdnea3)Z_download_xmlZgeo_verification_headersr
   _xgetZraise_geo_restrictedattrib
startswith_TP_TLDr   Z!_parse_smil_formats_and_subtitlesr   r   ZsuitableextendZ_extract_once_formatsr	   Z_get_cookiesr   r%   append)selfsmil_urlvideo_idr   metaZerror_elementr$   Zsmil_formats	subtitlesformats_formatZ	media_urlr,   r   r   r   _extract_theplatform_smil    sX          
    
z+ThePlatformBaseIE._extract_theplatform_smilc                 C   s    d| j  d| d}| ||S )Nr&   z/s/z?format=preview)r1   _download_json)r4   pathr6   Zinfo_urlr   r   r   _download_theplatform_metadataF   s    z0ThePlatformBaseIE._download_theplatform_metadatac                    s  i } d}t|trb|D ]D}| dd| d| d  }}}||g t||d q d} dg }	g  |	rև fd	d
}
|	d d D ]}|
| d| d q|
|	d  d|	d  dp| fdd}d |d d t|dt ddpd  d tdt	hfp0d tddd dt	hfpLd tddd hf|d|dt|d|d p|dd!S )"Ncaptionslangenr"   type)extr*   durationchaptersc                    s<   t | d} t |d}| d ks$|d kr(d S  | |d d S )N  
start_timeZend_time)r   r3   rG   )rE   r   r   _add_chapterY   s    

zCThePlatformBaseIE._parse_theplatform_metadata.<locals>._add_chapterZ	startTimeZendTimec                    s   t  fddddS )Nc                    s   |o|  d  S )N$)endswith)kvfieldr   r   r   i   r   zdThePlatformBaseIE._parse_theplatform_metadata.<locals>.extract_site_specific_field.<locals>.<lambda>F)get_all)r   rO   )inforO   r   extract_site_specific_fieldg   s    zRThePlatformBaseIE._parse_theplatform_metadata.<locals>.extract_site_specific_fieldtitledescriptionZdefaultThumbnailUrlrF   ZpubDateZbillingCodeauthor
categoriesc                 S   s   | ddkS )Nlabel)categoryN)r.   _rN   r   r   r   r   v   r   z?ThePlatformBaseIE._parse_theplatform_metadata.<locals>.<lambda>r#   keywordsc                 S   s   | rt d| S d S )Nz[;,]\s?)resplit)xr   r   r   r   w   r   ZregionshowZseasonNumberZprogrammingType)rT   r8   rU   	thumbnailrD   	timestampuploaderrE   ZcreatorrW   tagslocationZseriesZseason_number
media_type)
r.   
isinstancelist
setdefaultr3   r   r   r   r   str)r4   rR   r8   r?   captionr@   r"   mimerD   Ztp_chaptersrI   ZchapterrS   r   )rE   rR   r   _parse_theplatform_metadataJ   sP    

$


"   
z-ThePlatformBaseIE._parse_theplatform_metadatac                 C   s   |  ||}| |S )N)r>   rm   )r4   r=   r6   rR   r   r   r   _extract_theplatform_metadata~   s    z/ThePlatformBaseIE._extract_theplatform_metadataN)r   )__name__
__module____qualname__r1   r;   r>   rm   rn   r   r   r   r   r      s
   
&4r   c                       s   e Zd ZdZddgZ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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/	d0d1d2dd$gZe fd3d4Zed;d7d8Z	d9d: Z
  ZS )<ThePlatformIEz(?x)
        (?:https?://(?:link|player)\.theplatform\.com/[sp]/(?P<provider_id>[^/]+)/
           (?:(?:(?:[^/]+/)+select/)?(?P<media>media/(?:guid/\d+/)?)?|(?P<config>(?:[^/\?]+/(?:swf|config)|onsite)/select/))?
         |theplatform:)(?P<id>[^/\?&]+)z(?x)
            <meta\s+
                property=(["'])(?:og:video(?::(?:secure_)?url)?|twitter:player)\1\s+
                content=(["'])(?P<url>https?://player\.theplatform\.com/p/.+?)\2z](?s)<(?:iframe|script)[^>]+src=(["\'])(?P<url>(?:https?:)?//player\.theplatform\.com/p/.+?)\1z^http://link.theplatform.com/s/dJ5BDC/e9I_cZgTgIPd/meta.smil?format=smil&Tracking=true&mbr=trueZe9I_cZgTgIPdZflvzBlackberry's big, bold Z30zEThe Z30 is Blackberry's biggest, baddest mobile messaging device yet.   irRZ20131031zCBSI-NEW)idrC   rT   rU   rD   rb   upload_daterc   Zskip_downloadTz404 Not Found)r*   	info_dictparamsskipz1http://link.theplatform.com/s/kYEXFC/22d_qsQ6MIRTZ22d_qsQ6MIRTz$md5:ac330c9258c04f9d7512cf26b9595409z>Tesla Model S: A second step towards a cleaner motoring futureiUZ20150312)rt   rC   rU   rT   rb   ru   rc   zCNet no longer uses ThePlatformzUhttps://player.theplatform.com/p/D6x-PC/pulse_preview/embed/select/media/yMBg9E8KFxZDZyMBg9E8KFxZDmp4z$md5:644ad9188d655b742f942bf2e06b002dz-HIGHLIGHTS: USA bag first ever series Cup winZEGSM)rt   rC   rU   rT   rc   z	Dead link)r*   rv   rx   zGhttp://player.theplatform.com/p/NnzsPC/widget/select/media/4Y0TlYUr_ZT7r*   Zonly_matchingzMhttp://player.theplatform.com/p/2E2eJC/nbcNewsOffsite?guid=tdy_or_siri_150701Z fb96bb3d85118930a5b055783a3bd992Ztdy_or_siri_150701uD   iPhone Siri’s sassy response to a math question has people talkingz$md5:a565d1deadd5086f3331d57298ec6333g     T@re:^https?://.*\.jpg$iؓUZ20150701	NBCU-NEWS)	rt   rC   rT   rU   rD   ra   rb   ru   rc   z.Error: Player PID "nbcNewsOffsite" is disabled)r*   md5rv   rx   zhttp://player.theplatform.com/p/NnzsPC/onsite_universal/select/media/guid/2410887629/2928790?fwsitesection=nbc_the_blacklist_video_library&autoPlay=true&carouselID=137781c                 #   s(   t  ||D ]}tdd|V  qd S )Nz\s )super_extract_embed_urlsr]   sub)clsr*   webpageZ	embed_url	__class__r   r   r      s    z!ThePlatformIE._extract_embed_urlsX  Fc                 C   s   |rdnd}dt t |  }dd }td| d}t|| || }	t|	d|	t
j }
|| |
 || }|  d	| S )
N10Z00z%xc                 S   s   |  d S )Nascii)encodehex)Zstr_datar   r   r   
str_to_hex   s    z+ThePlatformIE._sign_url.<locals>.str_to_hexz)https?://link\.theplatform\.com/s/([^?]+)r   r   z&sig=)inttimer]   matchgroupbytesfromhexhmacnewr   hashlibsha1	hexdigest)r*   Zsig_keyZ
sig_secretZlifeZ
include_qsflagsZexpiration_dater   relative_pathZ
clear_textZchecksumsigr   r   r   	_sign_url   s    zThePlatformIE._sign_urlc                 C   s  t |i \}}| d|di | |}|d}|d}|sHd}|d }|drh||d7 }||7 }t|}d|kr| ||}td	|}	d }
t	|	D ]8}| | 
|d
|d}| jd|dd d}
|
d k	r qq|
d krtd| d||
|d d S |ddr |}nd|kri }|d}|rF||d< t||d}| ||}| jd|ddd}| d|d}|d|krdnd7 }nh|d r|d! }|d"d#}|d$d%}| ||d&}|d'pd(| d)}|d* }nd(| d)}|d+}|r$| ||d, |d- }| ||\}}t|d.d/ st|d0d d1}| jt||d2d3dd4}|rt|d5kr| j||d6dd7\}}|| | j||d8 | ||}| |d9i |}||||d: |S );NZ	countriesZgeo_countriesprovider_idrt   ZdJ5BDC/Zmediaguidz<script[^>]+src="([^"]+)"zhttp:zDownloading feed scriptzdefaultFeedId\s*:\s*"([^"]+)"zdefault feed id)defaultzUnable to find feed idz-http://feed.theplatform.com/f/{}/{}?byGuid={}r   Zforce_smil_urlFz/guid/
source_urlZReferer)r!   zH<link[^>]+href=(["\'])(?P<url>.+?)\1[^>]+type=["\']application/smil\+xmlzsmil urlr*   )r   z1link\.theplatform\.com/s/((?:[^/?#&]+/)+[^/?#&]+)r=   ?z&formats=m3u,mpeg4configz
&form=jsonzswf/zconfig/zonsite/zonsite/config/zDownloading configZ
releaseUrlzhttp://link.theplatform.com/s/z	?mbr=truez&formats=MPEG4&manifest=f4mr   keysecretc                 S   s   |d  dS )NZ	format_idhls)r0   rZ   r   r   r   r   4  r   z-ThePlatformIE._real_extract.<locals>.<lambda>zmbr=true&manifest=m3u)r    fragmentzChecking for HLS formatszNo HLS formats found)fatalr+   r   )Zm3u8_idr   )targetr8   )rt   r9   r8   )r   Z_initialize_geo_bypassr.   _match_valid_urlr   r   Z_download_webpager]   findallreversedZ_proto_relative_urlZ_search_regexr   Z
url_resultr   r   replacer<   r   r;   r   r   Z_request_webpager   r   Z#_extract_m3u8_formats_and_subtitlesr2   _merge_subtitlesrn   update)r4   r*   Zsmuggled_datamobjr   r6   r=   Zqs_dictr   scriptsfeed_idscriptZfeed_scriptr5   r!   r   requestZ
config_urlr   Zrelease_urlr   r9   r8   Zm3u8_urlZurlhZ	m3u8_fmtsZ	m3u8_subsretZcombined_subtitlesr   r   r   _real_extract   s     





     


    

       

zThePlatformIE._real_extract)r   F)ro   rp   rq   
_VALID_URLZ_EMBED_REGEX_TESTSclassmethodr   staticmethodr   r   __classcell__r   r   r   r   rr      s   
  Frr   c                   @   s\   e Zd ZdZdZddddddd	d
ddddgdd
ddddgZdi dfddZdd ZdS )ThePlatformFeedIEz-%s//feed.theplatform.com/f/%s/%s?form=json&%szhttps?://feed\.theplatform\.com/f/(?P<provider_id>[^/]+)/(?P<feed_id>[^?/]+)\?(?:[^&]+&)*(?P<filter>by(?:Gui|I)d=(?P<id>[^&]+))zwhttp://feed.theplatform.com/f/7wvmTC/msnbc_video-p-test?form=json&pretty=true&range=-40&byGuid=n_hardball_5biden_140207Z 6e32495b5073ab414471b615c5ded394Zn_hardball_5biden_140207ry   z'The Biden factor: will Joe run in 2016?zbCould Vice President Joe Biden be preparing a 2016 campaign? Mark Halperin and Sam Stein weigh in.r{   Z20140208iRg     0}@zMSNBC/Issues/Democratsz$MSNBC/Issues/Elections/Election 2016r|   )
rt   rC   rT   rU   ra   ru   rb   rD   rW   rc   )r*   r}   rv   zQhttp://feed.theplatform.com/f/2E2eJC/nnd_NBCNews?byGuid=nn_netcast_180306.Copy.01Trz   Nc              
   C   s  | j |  |||f }| ||d d }	|r@d|||	d f n|	d}
g }i }d }d }g }|	d D ]}|d }t|}|d kr|}t|d}|d	pt|d
 }|D ]v}||krq|| d|d |d}||kr|	||  | 
t|
p|||d| \}}|| | ||}qqfdd |	dg D }t|	ddd}dd |	dg D }| | d| |}| ||d }|	|||||||d |r|	||	 |S )Nentriesr   z1http://link.theplatform.com/s/%s/media/guid/%d/%sr   zplmedia$publicUrlzmedia$content
plfile$urlzplfile$durationzplfile$assetTypes
assetTypestruezplfile$format)Zmbrr9   r   zDownloading SMIL data for c                 S   s0   g | ](}|d  t |dt |ddqS )r   zplfile$widthzplfile$height)r*   widthheight)r   r.   ).0ra   r   r   r   
<listcomp>  s
   z8ThePlatformFeedIE._extract_feed_info.<locals>.<listcomp>zmedia$thumbnailszmedia$availableDaterF   )Zscalec                 S   s   g | ]}|d  qS )z
media$namer   )r   itemr   r   r   r     s     zmedia$categoriesr   r8   )rt   r9   r8   
thumbnailsrD   rb   rW   )_URL_TEMPLATEZhttp_schemer<   r.   rr   Z	_match_idr   r   r3   r   r;   r   r2   r   r   rn   )r4   r   r   filter_queryr6   Zcustom_fieldsZasset_types_queryZ
account_idZreal_urlentryZmain_smil_urlr9   r8   Zfirst_video_idrD   Zasset_typesr   r5   Zcur_video_idZfile_asset_typesZ
asset_typer    Zcur_formatsZcur_subtitlesr   rb   rW   r   r   r   r   _extract_feed_infoa  sj     

  

	z$ThePlatformFeedIE._extract_feed_infoc                 C   sB   |  |}|d}|d}|d}|d}| ||||S )Nrt   r   r   filter)r   r   r   )r4   r*   r   r6   r   r   r   r   r   r   r     s    




zThePlatformFeedIE._real_extract)ro   rp   rq   r   r   r   r   r   r   r   r   r   r   I  s,   :r   )r   r   r]   r   Z	adobepassr   oncer   Z
networkingr   r   utilsr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r-   r   rr   r   r   r   r   r   <module>   s   <f G