U
    $Pf*                     @   s|   d dl m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 G dd	 d	eZG d
d deZdS )    )unicode_literalsN   )InfoExtractor   )
compat_strcompat_urlparse)encode_data_uriExtractorErrorint_or_nonefloat_or_nonemimetype2extstr_or_nonec                   @   s   e Zd ZdZdZdddddddd	d
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%id&d'd%d(gZed)d* Zd4d,d-Zd.d/ Z	d0d1 Z
d2d3 Zd+S )5	UstreamIEzfhttps?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/(?P<type>recorded|embed|embed/recorded)/(?P<id>\d+)Zustreamz'http://www.ustream.tv/recorded/20274954Z 088f151799e8f572f84eb62f17d73e5cZ20274954Zflvz4Young Americans for Liberty February 7, 2012 2:28 AMiz0OZ20120207Z	yalibertyZ6780869)idexttitledescription	timestampupload_dateuploaderuploader_id)urlmd5	info_dictzfhttp://www.ustream.tv/embed/recorded/59307601?ub=ff0000&lc=ff0000&oc=ffffff&uc=ffffff&v=3&wmode=directZ 5a2abf40babeac9812ed20ae12d34e10Z59307601z"-CG11- Canada Games Figure SkatingZsportscanadatv)r   r   r   r   zNThis Pro Broadcaster has chosen to remove this video from the ustream.tv site.)r   r   r   skipz$http://www.ustream.tv/embed/10299409r   Z10299409   )r   r   Zplaylist_countz'http://www.ustream.tv/recorded/91343263Z91343263mp4z)GitHub Universe - General Session - Day 1Z20160914iZWZwa0dnskeqkrZ38977840)r   r   r   r   r   r   r   r   Zskip_downloadT)r   r   paramszVhttps://video.ibm.com/embed/recorded/128240221?&autoplay=true&controls=true&volume=100)r   Zonly_matchingc                 C   s"   t d| }|d k	r|dS d S )Nz_<iframe[^>]+?src=(["\'])(?P<url>https?://(?:www\.)?(?:ustream\.tv|video\.ibm\.com)/embed/.+?)\1r   )researchgroup)webpageZmobj r"   @/tmp/pip-unpacked-wheel-7zdooeg3/youtube_dl/extractor/ustream.py_extract_urlK   s     zUstreamIE._extract_urlNc           
      C   s   dd }t j}|sd}| jd|d|f |d| d|d |d	 d
||d||df d|d ||ddd}|d d d d }|d d d d }	| jd||	f |d| dS )Nc                 S   s   t | dd  S )Nr   )hex)nr"   r"   r#   
num_to_hexS   s    z.UstreamIE._get_stream_info.<locals>.num_to_hex z9http://r%d-1-%s-recorded-lp-live.ums.ustream.tv/1/ustreamg    חAzDownloading connection infoZviewerr   r   z%s:%sz_rpin.%dg  4&kCZrecorded)typeZappIdZ
appVersionZrsidZrpinZreferrerZmediaZapplication)notequeryargshostZconnectionIdz#http://%s/1/ustream?connectionId=%szDownloading stream infor*   )random	randrange_download_json)
selfr   video_id
app_id_ver
extra_noter'   ZrndZ	conn_infor-   Zconnection_idr"   r"   r#   _get_stream_infoR   s2     

 zUstreamIE._get_stream_infoc                 C   sd   t dD ]V}| j||||dkr*d|d  ndd}d|d d d kr|d d d d   S qg S )	Nr   r   z	 (try %d)r   r(   )r5   streamr,   )ranger6   )r2   r   r3   r4   Ztrial_countZstream_infor"   r"   r#   _get_streamso   s      zUstreamIE._get_streamsc                 C   sP  dd }g }|d D ]4}|d d }d||d |d  d|d d	 ig}t |d
 |d  D ]:}|d||d |d  ||d t|d d  i q`|d }|dd }	dtd d|	t|dgdtddt	||d|d|d}
|	dkr"|

|dd|dd n|

d|d|dd ||
 q|S )Nc                 S   s   |  dt|d d|S )N%r   )replacer   )templateidxZ
chunk_hashr"   r"   r#   resolve_dash_templatez   s    z=UstreamIE._parse_segmented_mp4.<locals>.resolve_dash_templateZstreamsZ	providersr   r   ZinitUrlhashes0ZvideoLengthZ	chunkTimeZ
segmentUrl
   contentType/-ZdashZbitrateZhttp_dash_segmentss   <MPD/>ztext/xmlheightwidth)	format_idprotocolr   r   rE   rF   	fragmentsvideocodecnone)vcodecacodecZvbr)rM   rN   Zabr)r8   appendr   splitjoinfilterr   getr   r   update)r2   Zdash_stream_infor>   formatsr7   ZproviderrI   r=   content_typekindfr"   r"   r#   _parse_segmented_mp4y   s^       
    

zUstreamIE._parse_segmented_mp4c                    s  t j|}|d|ddkrD|dd }|dS |ddkr|d|}d|d}t	fd	d
|S 
d }|d}|rtdj|f dd|d }|d }	t|d  fdd|d  D }
|
sBj|dd}|rB|
j|d d ddd |
 |d}t|d}t|d}t|d }|d!i d"}|d!i d}d#d |d$i  D }|	||||||||
d%
S )&Nr   r)   zembed/recordedhttp://www.ustream.tv/recorded/Ustreamembedz-ustream\.vars\.offAirContentVideoIds=([^;]+);zcontent video IDsc                    s     d|  dS )NrZ   r[   
url_result)ur2   r"   r#   <lambda>       z)UstreamIE._real_extract.<locals>.<lambda>z%https://api.ustream.tv/videos/%s.jsonerrorz%s returned error: %sT)expectedrJ   r   	file_sizec                    s"   g | ]\}}|r|| d qS ))r   r   r   filesizer"   ).0rG   Z	video_url)rf   r3   r"   r#   
<listcomp>   s    z+UstreamIE._real_extract.<locals>.<listcomp>Z
media_urls)   r   )r4   r   r   r   Zhls)r   Zm3u8_idr   Z
created_atlengthZviewsownerusernamec                 S   s   g | ]\}}||d qS ))r   r   r"   )rg   Zthumbnail_idZthumbnail_urlr"   r"   r#   rh      s   Z	thumbnail)
r   r   r   
thumbnailsr   duration
view_countr   r   rU   )r   match
_VALID_URLr    r^   _download_webpageZ_parse_jsonZ_search_regexZplaylist_resultmapr1   rS   r	   IE_NAMEr   itemsr9   extendZ_extract_m3u8_formatsZ_sort_formatsr
   )r2   r   mZdesktop_urlr!   Zcontent_video_idsr   rc   rJ   r   rU   Zhls_streamsr   r   rn   ro   r   r   rm   r"   )rf   r2   r3   r#   _real_extract   s    


  
 

   


zUstreamIE._real_extract)N)__name__
__module____qualname__rq   rt   Z_TESTSstaticmethodr$   r6   r9   rY   rx   r"   r"   r"   r#   r      sj     3


.r   c                   @   s,   e Zd ZdZdZdddiddZdd	 Zd
S )UstreamChannelIEz3https?://(?:www\.)?ustream\.tv/channel/(?P<slug>.+)zustream:channelz*http://www.ustream.tv/channel/channeljapanr   Z10874166   )r   r   Zplaylist_mincountc                    s   t  j|}|d} ||} d|}d}d| }g }|r jt|||dt	|d  d}	|
t d|	d	  |	d
 }q@ fdd|D }
d|||
dS )NZslugzustream:channel_idzhttp://www.ustream.tvz#/ajax/socialstream/videos/%s/1.jsonz(Downloading video information (next: %d)r   r.   zdata-content-id="(\d.*)"dataZnextUrlc                    s   g | ]}  d | dqS )rZ   r[   r]   )rg   Zvidr`   r"   r#   rh     s   z2UstreamChannelIE._real_extract.<locals>.<listcomp>Zplaylist)_typer   
display_identries)r   rp   rq   r    rr   Z_html_search_metar1   r   urljoinlenrv   findall)r2   r   rw   r   r!   Z
channel_idZBASEZnext_urlZ	video_idsZreplyr   r"   r`   r#   rx     s.    

 

zUstreamChannelIE._real_extractN)ry   rz   r{   rq   rt   Z_TESTrx   r"   r"   r"   r#   r}      s    r}   )
__future__r   r/   r   commonr   compatr   r   utilsr   r	   r
   r   r   r   r   r}   r"   r"   r"   r#   <module>   s    
 e