o
    iG                     @   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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mZmZ dZdd	 ZG d
d deZG dd deZG dd deZdS )    N   )AdobePassIE   )HEADRequestRequest)ExtractorErrordetermine_extfind_xpath_attrfloat_or_noneint_or_nonemimetype2extparse_age_limitparse_qstraverse_objunsmuggle_url
update_urlupdate_url_queryurl_or_noneurlhandle_detect_extxpath_with_nsz&http://www.w3.org/2005/SMIL21/Languagec                 C   s   t | dtiS )Nsmil)r   
default_ns)p r   P/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/theplatform.py<lambda>       r   c                   @   s8   e Zd ZdZdddZdddZedd	 Zd
d ZdS )ThePlatformBaseIEcomDownloading SMIL datac                 C   s  | j |||ddi|  d}t|tdd}|d urMt|tddd}|d urM|d	d
kr7| |jd  n|jd d| j drMt	|jd dd| j
|||tddddd d\}}g }	|D ]%}
|
d }t|dkr| |d}|rt|d|ji|
d< |	|
 qc|	|fS )NformatSMIL)notequeryheadersz.//smil:refsrcz.//smil:paramname	exceptionvalueGeoLocationBlockedabstractzhttp://link.theplatform.z/s/errorFiles/Unavailable.T)expectedUXWGVKRWHFSPz3.0.3)ghdcorec                 S   s   | d| fS )Nzmp4:r   )streamerr%   r   r   r   r   6       z=ThePlatformBaseIE._extract_theplatform_smil.<locals>.<lambda>)	namespace
f4m_paramstransform_rtmp_urlurlm3u8hdnea2hdnea3)_download_xmlgeo_verification_headersr	   _xgetraise_geo_restrictedattrib
startswith_TP_TLDr   !_parse_smil_formats_and_subtitlesr   r   _get_cookiesr   r(   append)selfsmil_urlvideo_idr"   metaerror_elementr'   smil_formats	subtitlesformats_format	media_urlr6   r   r   r   _extract_theplatform_smil!   s@   


z+ThePlatformBaseIE._extract_theplatform_smilTc                 C   s(   | j d| j d| ||ddidpi S )Nzhttps://link.theplatform.z/s/r    preview)fatalr#   )_download_jsonr?   )rC   pathrE   rO   r   r   r   _download_theplatform_metadataD   s   z0ThePlatformBaseIE._download_theplatform_metadatac                 C   s  dd }t | i ddthfddthfddthfddthfddtd	d
hfddtd	d
hfddthfddthtttfdddd dthtttfddthtdd htfddddthtfd|dthtfd|dd thtfd!|d"d#d$d%thtfd&|d'thtfd(|d)d*thtf}t | d+dd,td	d
hfd-td	d
hfd.f}t	|d/kst |d0r||d+< i |d1< t | d2d3d fD ]}|d1 
|d4pd5g |d6 t|d*d7 q|S )8Nc                     s    fddS )Nc                    s   |o|  tdd  D S )Nc                 s   s    | ]}d | V  qdS )$Nr   ).0fr   r   r   	<genexpr>L   s    zpThePlatformBaseIE._parse_theplatform_metadata.<locals>.site_specific_filter.<locals>.<lambda>.<locals>.<genexpr>)endswithtuple)kvfieldsr   r   r   L   s    z]ThePlatformBaseIE._parse_theplatform_metadata.<locals>.site_specific_filter.<locals>.<lambda>r   r[   r   r[   r   site_specific_filterK   s   zKThePlatformBaseIE._parse_theplatform_metadata.<locals>.site_specific_filtertitleepisodedescription	thumbnaildefaultThumbnailUrlduration  scale	timestamppubDateuploaderbillingCodecreatorsauthor
categoriesc                 S   s   | ddv S )Nlabel)categoryN)r;   _rZ   r   r   r   r   X   r   z?ThePlatformBaseIE._parse_theplatform_metadata.<locals>.<lambda>r&   tagskeywordsc                 S   s   t d| S )Nz[;,]\s?)resplit)xr   r   r   r   Z   r0   	age_limitratings.ratingseason_numberseasonNumberepisode_numberepisodeNumberairOrderseriesshowseriesTitleseriesShortTitle)N.locationregion
media_typeprogrammingTypetypechapters	startTimeendTime)
start_timeend_timer   )r   r   rI   captionsc                 S   s   t |d S )Nr%   )r   rp   r   r   r   r   l   r0   langenr%   )r4   ext)r   strr   r
   filterallr   anyr   len
setdefaultr;   rB   r   )tp_metadatar]   infor   captionr   r   r   _parse_theplatform_metadataI   sd   




	

z-ThePlatformBaseIE._parse_theplatform_metadatac                 C   s   |  ||}| |S )N)rR   r   )rC   rQ   rE   r   r   r   r   _extract_theplatform_metadatat   s   
z/ThePlatformBaseIE._extract_theplatform_metadataN)r   )T)	__name__
__module____qualname__r?   rM   rR   staticmethodr   r   r   r   r   r   r      s    

#
*r   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=truee9I_cZgTgIPdflvzBlackberry's big, bold Z30zEThe Z30 is Blackberry's biggest, baddest mobile messaging device yet.   irR20131031zCBSI-NEW)idr   r^   r`   rc   rg   upload_dateri   skip_downloadTz404 Not Found)r4   	info_dictparamsskipz1http://link.theplatform.com/s/kYEXFC/22d_qsQ6MIRT22d_qsQ6MIRTz$md5:ac330c9258c04f9d7512cf26b9595409z>Tesla Model S: A second step towards a cleaner motoring futureiU20150312)r   r   r`   r^   rg   r   ri   zCNet no longer uses ThePlatformzUhttps://player.theplatform.com/p/D6x-PC/pulse_preview/embed/select/media/yMBg9E8KFxZDyMBg9E8KFxZDmp4z$md5:644ad9188d655b742f942bf2e06b002dz-HIGHLIGHTS: USA bag first ever series Cup winEGSM)r   r   r`   r^   ri   z	Dead link)r4   r   r   zGhttp://player.theplatform.com/p/NnzsPC/widget/select/media/4Y0TlYUr_ZT7r4   only_matchingzMhttp://player.theplatform.com/p/2E2eJC/nbcNewsOffsite?guid=tdy_or_siri_150701 fb96bb3d85118930a5b055783a3bd992tdy_or_siri_150701uD   iPhone Siri’s sassy response to a math question has people talkingz$md5:a565d1deadd5086f3331d57298ec6333g     T@re:^https?://.*\.jpg$iؓU20150701	NBCU-NEWS)	r   r   r^   r`   rc   ra   rg   r   ri   z.Error: Player PID "nbcNewsOffsite" is disabled)r4   md5r   r   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_urlsrt   sub)clsr4   webpage	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1000z%xc                 S   s   |  d S )Nascii)encodehex)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timert   matchgroupbytesfromhexhmacnewr   hashlibsha1	hexdigest)r4   sig_key
sig_secretlife
include_qsflagsexpiration_dater   relative_path
clear_textchecksumsigr   r   r   	_sign_url   s   zThePlatformIE._sign_urlc                 C   s  t |i \}}| d|di | |}|d}|d}|s$d}|d }|dr4||d7 }||7 }t|}d|v r| ||}td	|}	d }
t	|	D ]}| | 
|d
|d}| jd|dd d}
|
d urn nqR|
d u rwtd| d||
|d d S |ddr|}njd|v ri }|d}|r||d< t||d}| ||}| jd|ddd}| d|d}|d|vrdnd7 }n2|d r|d! }|d"d#}|d$d%}| ||d&}|d'pd(| d)}|d* }nd(| d)}|d+}|r| ||d, |d- }| ||\}}t|d.d/ sOt|d0d d1}| jt||d2d3dd4}|rOt|d5krO| j||d6dd7\}}|| | j||d8 | ||}| |d9i |}||||d: |S );N	countriesgeo_countriesprovider_idr   dJ5BDC/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   force_smil_urlFz/guid/
source_urlReferer)r$   zH<link[^>]+href=(["\'])(?P<url>.+?)\1[^>]+type=["\']application/smil\+xmlzsmil urlr4   )r   z1link\.theplatform\.com/s/((?:[^/?#&]+/)+[^/?#&]+)rQ   ?z&formats=m3u,mpeg4configz
&form=jsonzswf/zconfig/zonsite/zonsite/config/zDownloading config
releaseUrlzhttp://link.theplatform.com/s/z	?mbr=truez&formats=MPEG4&manifest=f4mr   keysecretc                 S   s   |d  dS )N	format_idhls)r>   rp   r   r   r   r   *  r   z-ThePlatformIE._real_extract.<locals>.<lambda>zmbr=true&manifest=m3u)r#   fragmentzChecking for HLS formatszNo HLS formats found)rO   r5   r   )m3u8_idrO   )targetrI   )r   rJ   rI   )r   _initialize_geo_bypassr;   _match_valid_urlr   r   _download_webpagert   findallreversed_proto_relative_url_search_regexr   
url_resultr    r   replacerP   r   rM   r   r   _request_webpager   r   #_extract_m3u8_formats_and_subtitlesextend_merge_subtitlesr   update)rC   r4   smuggled_datamobjr   rE   rQ   qs_dictr   scriptsfeed_idscriptfeed_scriptrD   r$   r   request
config_urlr   release_urlr   rJ   rI   m3u8_urlurlh	m3u8_fmts	m3u8_subsretcombined_subtitlesr   r   r   _real_extract   s   











zThePlatformIE._real_extract)r   F)r   r   r   
_VALID_URL_EMBED_REGEX_TESTSclassmethodr   r   r   r  __classcell__r   r   r   r   r   y   s    
Fr   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_140207 6e32495b5073ab414471b615c5ded394n_hardball_5biden_140207r   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   20140208iRg     0}@zMSNBC/Issues/Democratsz$MSNBC/Issues/Elections/Election 2016r   )
r   r   r^   r`   ra   r   rg   rc   rm   ri   )r4   r   r   zQhttp://feed.theplatform.com/f/2E2eJC/nnd_NBCNews?byGuid=nn_netcast_180306.Copy.01Tr   Nc              
   C   s  | j |  |||f }| ||d d }	|r d|||	d f n|	d}
g }i }d }d }g }|	d D ]a}|d }t|}|d u rK|}t|d}|d	pUt|d
 }|D ];}||v r_qX|| d|d |d}||v rw|	||  | 
t|
p}|||d| \}}|| | ||}qXq3d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)mbrrJ   r  zDownloading SMIL data for c                 S   s0   g | ]}|d  t |dt |ddqS )r  zplfile$widthzplfile$height)r4   widthheight)r   r;   )rT   ra   r   r   r   
<listcomp>x  s    
z8ThePlatformFeedIE._extract_feed_info.<locals>.<listcomp>zmedia$thumbnailszmedia$availableDaterd   re   c                 S   s   g | ]}|d  qS )z
media$namer   )rT   itemr   r   r   r#    s    zmedia$categoriesr   rI   )r   rJ   rI   
thumbnailsrc   rg   rm   )_URL_TEMPLATEhttp_schemerP   r;   r   	_match_idr
   r   rB   r  rM   r   r   r   r   r   )rC   r   r  filter_queryrE   custom_fieldsasset_types_query
account_idreal_urlentrymain_smil_urlrJ   rI   first_video_idrc   asset_typesr$  rD   cur_video_idfile_asset_types
asset_typer#   cur_formatscur_subtitlesr%  rg   rm   r  r   r   r   _extract_feed_infoW  sh    




	z$ThePlatformFeedIE._extract_feed_infoc                 C   sB   |  |}|d}|d}|d}|d}| ||||S )Nr   r   r  r   )r   r   r7  )rC   r4   r  rE   r   r  r)  r   r   r   r    s   




zThePlatformFeedIE._real_extract)r   r   r   r&  r  r  r7  r  r   r   r   r   r  ?  s.    :r  )r   r   rt   r   	adobepassr   
networkingr   r   utilsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r:   r   r   r  r   r   r   r   <module>   s    D[ G