o
    i                      @   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}|r#t|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   T/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/curiositystream.py_handle_errors   s   
z$CuriosityStreamBaseIE._handle_errorsc                 C   sr   i }| j s| dd}|r| d tj|j| _ | j r$| j |d< | j| j	| |||d}| 
| |d S )Nzhttps://curiositystream.com
auth_tokenzObtained auth_token cookiezX-Auth-Token)headersquerydata)_auth_token_get_cookiesr   write_debugurllibparseunquotevalue_download_json_API_BASE_URLr   )r   pathvideo_idr   r   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)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___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 Stream
Technology	Interviewz*re:https://img.curiositystream.com/.+\.jpg   )idexttitledescriptionchannel
categoriesaverage_rating	series_id	thumbnailtagsdurationskip_downloadT)url	info_dictparamsz)https://api.curiositystream.com/v1/media/c                 C   sR  |  |}g }dD ]}| j||d|dd}|dg D ]}|d}|dkr5|| j||dd	d
d n|dkrE|| j||dd
d |d}|d}	|sT|	sTqt|dt|dt|dt|dt|d|d|d|dd}
||	fD ]3}|sq|
 }t	d|}|r|
|d|d|dddd n|
|d d! || qqq	|d" }i }|d#g D ]!}|d$}|sq|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p&d d4S )5N)m3u8mpdtrue)encodingsNewencodingsFormat)r   	encodingsmaster_playlist_urlrG   r4   hlsF)m3u8_idfatalrH   dash)mpd_idrP   rD   file_urlwidthheightvideo_bitrateaudio_bitratesize_in_bytesvideo_codecaudio_codeccontainer_type)rT   rU   vbrabrfilesizevcodecacodec	containerzG^(?P<url>rtmpe?://(?P<host>[^/]+)/(?P<app>.+))/(?P<playpath>mp[34]:.+)$playpathappflvrtmp)rD   	play_pathrc   r9   	format_idhttp)rD   rg   r:   closed_captionsfilecodelanguageenr;   image_largeimage_mediumimage_smallrB   rA   producerprimary_categorytyperating_percentagecollection_id )r8   formatsr:   r;   r@   rB   rA   	subtitlesr<   r=   r>   r?   )	_match_idr(   r   extend_extract_m3u8_formats_extract_mpd_formatsr   copyresearchupdategroupappend
setdefaultstr)r   rD   r&   rw   encoding_formatmediaencodingplaylist_urlencoding_urlrS   ff_urlfmtre   r:   rx   closed_captionsub_urllangr   r   r   _real_extractG   s   







,
zCuriosityStreamIE._real_extractN)	r.   r/   r0   r   
_VALID_URLfloat_TESTSr$   r   r   r   r   r   r2   -   s*    r2   c                   @   s   e Zd Zdd ZdS )CuriosityStreamCollectionBaseIEc           	      C   s   |  |}| ||}g }|dg D ]+}t|d}|dr%dtfndtf\}}|| jd| d| | |d q| 	|||d	|d
S )Nr   r8   is_collectionseriesvideozhttps://curiositystream.com//)ier&   r:   r;   )
ry   r(   r   r   CuriosityStreamSeriesIEr2   r   
url_resultie_keyplaylist_result)	r   rD   ru   
collectionentriesr   media_id
media_typer   r   r   r   r      s   

z-CuriosityStreamCollectionBaseIE._real_extractN)r.   r/   r0   r   r   r   r   r   r      s    r   c                   @   6   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/8686zStaff PickszeWondering where to start? Here are a few of our favorite series and films... from our couch to yours.r8   r:   r;      rD   rE   playlist_mincountz*https://curiositystream.com/collections/36TrD   only_matchingNr.   r/   r0   r   r   r$   r   r   r   r   r   r          	r   c                   @   r   )r   zcuriositystream:serieszIhttps?://(?:app\.)?curiositystream\.com/(?:series|collection)/(?P<id>\d+)z*https://api.curiositystream.com/v2/series/z$https://curiositystream.com/series/2r3   zCurious Minds: The Internetz:How is the internet shaping our lives in the 21st Century?r      r   z(https://curiositystream.com/collection/2Tr   Nr   r   r   r   r   r      r   r   )r~   urllib.parser   commonr   utilsr   r   r   r   r2   r   r   r   r   r   r   r   <module>   s    %k