U
    g                      @   s   d dl Z d dlZddlmZ ddl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G dd deZdS )    N   )InfoExtractor   )ExtractorErrorint_or_noneurlencode_postdatac                   @   s.   e Zd ZdZdZdd Zd	ddZdd ZdS )
CuriosityStreamBaseIEcuriositystreamNc                 C   sJ   | di  d}|rFt|tr.d| }t| j d| ddd S )Nerrormessagez, z said: T)expected)get
isinstancedictjoinvaluesr   IE_NAME)selfresultr
    r   D/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/curiositystream.py_handle_errors   s    
 z$CuriosityStreamBaseIE._handle_errorsc                 C   sr   i }| j s8| dd}|r8| d tj|j| _ | j rH| j |d< | j| j	| |||d}| 
| |d S )Nzhttps://curiositystream.com
auth_tokenzObtained auth_token cookiezX-Auth-Token)headersquerydata)_auth_tokenZ_get_cookiesr   Zwrite_debugurllibparseunquotevalue_download_json_API_BASE_URLr   )r   pathvideo_idr   r   Zauth_cookier   r   r   r   	_call_api   s     

   
zCuriosityStreamBaseIE._call_apic                 C   s8   | j dd dt||dd}| | |d d t_d S )Nz(https://api.curiositystream.com/v1/loginz
Logging in)emailpassword)Znoter   r   r   )r!   r   r   r   r   )r   usernamer'   r   r   r   r   _perform_login"   s      
z$CuriosityStreamBaseIE._perform_login)N)__name__
__module____qualname__Z_NETRC_MACHINEr   r   r%   r)   r   r   r   r   r      s
   
r   c                   @   sN   e Zd ZdZdZddddddd	d
geddg ddddidgZdZdd ZdS )CuriosityStreamIEr	   z9https?://(?:app\.)?curiositystream\.com/video/(?P<id>\d+)z&http://app.curiositystream.com/video/22mp4z!How Did You Develop The Internet?zbVint Cerf, Google's Chief Internet Evangelist, describes how he and Bob Kahn created the internet.zCuriosity StreamZ
TechnologyZ	Interviewz*re:https://img.curiositystream.com/.+\.jpg   )idexttitledescriptionchannel
categoriesaverage_rating	series_id	thumbnailtagsdurationZskip_downloadT)url	info_dictparamsz)https://api.curiositystream.com/v1/media/c                 C   sl  |  |}g }dD ]l}| j||d|dd}|dg D ]B}|d}|dkrn|| j||dd	d
d n |dkr|| j||dd
d |d}|d}	|s|	sq:t|dt|dt|dt|dt|d|d|d|dd}
||	fD ]n}|sq|
 }t	d|}|r`|
|d|d|dddd n|
|d d! || qq:q|d" }i }|d#g D ]L}|d$}|sq|d%p|d&pd'}||g d|i q||||d(|d)p|d*p|d+t|d,|d-||d.|d/|d0g|d1t|d2p^d3pfd d4S )5N)m3u8mpdtrue)ZencodingsNewZencodingsFormat)r   	encodingsZmaster_playlist_urlr?   r/   ZhlsF)Zm3u8_idfatalr@   Zdash)Zmpd_idrC   r<   file_urlwidthheightZvideo_bitrateZaudio_bitrateZsize_in_bytesZvideo_codecZaudio_codecZcontainer_type)rE   rF   ZvbrZabrfilesizeZvcodecZacodec	containerzG^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$ZplaypathappZflvrtmp)r<   Z	play_pathrI   r2   	format_idhttp)r<   rK   r3   Zclosed_captionsfilecodelanguageenr4   Zimage_largeZimage_mediumZimage_smallr;   r:   ZproducerZprimary_categorytypeZrating_percentagecollection_id )r1   formatsr3   r4   r9   r;   r:   	subtitlesr5   r6   r7   r8   )	_match_idr%   r   extendZ_extract_m3u8_formatsZ_extract_mpd_formatsr   copyresearchupdategroupappend
setdefaultstr)r   r<   r$   rT   Zencoding_formatmediaencodingZplaylist_urlZencoding_urlrD   fZf_urlfmtrJ   r3   rU   Zclosed_captionZsub_urllangr   r   r   _real_extractG   s    



   
   




 
 zCuriosityStreamIE._real_extractN)	r*   r+   r,   r   
_VALID_URLfloat_TESTSr"   re   r   r   r   r   r-   -   s*    r-   c                   @   s   e Zd Zdd ZdS )CuriosityStreamCollectionBaseIEc           	      C   s   |  |}| ||}g }|dg D ]V}t|d}|drJdtfndtf\}}|| jd| d| | |d q&| 	|||d	|d
S )Nr`   r1   Zis_collectionZseriesvideozhttps://curiositystream.com//)ier$   r3   r4   )
rV   r%   r   r_   CuriosityStreamSeriesIEr-   r]   Z
url_resultZie_keyZplaylist_result)	r   r<   rR   Z
collectionentriesr`   Zmedia_id
media_typerl   r   r   r   re      s"    
 
  z-CuriosityStreamCollectionBaseIE._real_extractN)r*   r+   r,   re   r   r   r   r   ri      s   ri   c                   @   s6   e Zd ZdZdZdZdddddd	d
dddgZdS )CuriosityStreamCollectionsIEzcuriositystream:collectionsz?https?://(?:app\.)?curiositystream\.com/collections/(?P<id>\d+)z/https://api.curiositystream.com/v2/collections/z*https://curiositystream.com/collections/86Z86zStaff PickszeWondering where to start? Here are a few of our favorite series and films... from our couch to yours.r1   r3   r4      r<   r=   Zplaylist_mincountz*https://curiositystream.com/collections/36Tr<   Zonly_matchingNr*   r+   r,   r   rf   r"   rh   r   r   r   r   rp      s   	rp   c                   @   s6   e Zd ZdZdZdZdddddd	d
dddgZdS )rm   zcuriositystream:serieszIhttps?://(?:app\.)?curiositystream\.com/(?:series|collection)/(?P<id>\d+)z*https://api.curiositystream.com/v2/series/z$https://curiositystream.com/series/2r.   zCurious Minds: The Internetz:How is the internet shaping our lives in the 21st Century?rq      rs   z(https://curiositystream.com/collection/2Trt   Nru   r   r   r   r   rm      s   	rm   )rY   urllib.parser   commonr   utilsr   r   r   r   r-   ri   rp   rm   r   r   r   r   <module>   s   %k