U
    gQ                     @   s   d dl Z d dlZddlmZ ddlmZ ddl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 ddlmZ d	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   )Art19IE)InfoExtractor   )	HTTPError)
ExtractorErrorint_or_nonemake_archive_idparse_iso8601smuggle_urltry_callunsmuggle_urlupdate_url_queryurl_or_noneurljoin)traverse_objzEhttps?://(?:www\.|beta\.)?(?:watchnebula\.com|nebula\.app|nebula\.tv)c                   @   s@   e Zd ZdZd ZZdd Zdd Zdd Zd	d
 Z	dd Z
dS )NebulaBaseIEZwatchnebulaNc              
   C   s   z.| j dd ddt||d ddid}W nH tk
rv } z*t|jtrd|jjdkrdtd	d
d W 5 d }~X Y nX t	|dt
hf| _| jstdd S )Nzhttps://nebula.tv/auth/login/zLogging in to NebulazLogin failed)emailpasswordzcontent-typezapplication/json)dataheadersi  z*Login failed: Invalid username or passwordT)expectedkeyzLogin failed: No token)_download_jsonjsondumpsencoder   
isinstancecauser   statusr   str
_api_token)selfusernamer   responsee r&   ;/tmp/pip-unpacked-wheel-q5ljy6pj/yt_dlp/extractor/nebula.py_perform_login   s       
zNebulaBaseIE._perform_loginc              
   O   s   | j rd| j  |di d< z| j||W S  tk
r } zht|jtrV|jjdkrX | d|jj  | 	  | j rd| j  |di d< | j|| W Y S d }~X Y nX d S )NzBearer r   Authorization)    zPReauthorizing with Nebula and retrying, because last API call resulted in error )
_token
setdefaultr   r   r   r   r   r   	to_screen_real_initialize)r"   argskwargsr%   r&   r&   r'   	_call_api+   s    zNebulaBaseIE._call_apic                    sL    j st fdd _  jdd  j r6dd j  ind dddd	  _d S )
Nc                      s     dd jS )Nzhttps://nebula.tvznebula_auth.apiToken)Z_get_cookiesvaluer&   r"   r&   r'   <lambda>=       z/NebulaBaseIE._real_initialize.<locals>.<lambda>z2https://users.api.nebula.app/api/v1/authorization/r)   zToken zAuthorizing to Nebular6   )r   noter   token)r!   r   r   r,   r4   r&   r4   r'   r/   :   s    
  zNebulaBaseIE._real_initializec                 C   s   dD ]}zH| j d|dd  d| d|d| jdd	d
d\}}||dW   S  tk
r } z\t|jtr|jjdkr|   |st|jtr|jjdkr| 	d | 
  W Y q W 5 d }~X Y qX qd S )N)FTzhttps://content.api.nebula.app/:r   zs/z/manifest.m3u8mp4z23.10.0Zios)r8   Zapp_versionplatform)query)formatsZ	subtitlesr*   r+   zPReauthorizing with Nebula and retrying, because fetching video resulted in error)Z#_extract_m3u8_formats_and_subtitlessplitr,   r   r   r   r   r   raise_login_requiredr.   r/   )r"   Z
content_idslugretryZfmtssubsr%   r&   r&   r'   _extract_formatsC   s(     

zNebulaBaseIE._extract_formatsc                 C   s   t |ddd hfdd}d|d dd it |d	d
ddthfdthfdddddddddthfdthfddd hfd||dS )N)channel_slug
class_slugc                 S   s
   t d| S )Nhttps://nebula.tv/)r   xr&   r&   r'   r5   Y   r6   z6NebulaBaseIE._extract_video_metadata.<locals>.<lambda>F)get_allidr9   r   r@   titledescriptionpublished_atdurationrD   channel_titleZimages	thumbnailsrcorderZzype_idc                 S   s   | rt t| t t| gS d S )N)r	   NebulaIENebulaSubscriptionsIErG   r&   r&   r'   r5   k   s     )
display_idrK   rL   	timestamprN   
channel_iduploader_idchanneluploaderseriescreatorrP   episode_number_old_archive_ids)channel_urluploader_url)r   	partitionr
   r   r   )r"   episoder_   r&   r&   r'   _extract_video_metadataW   s6       z$NebulaBaseIE._extract_video_metadata)__name__
__module____qualname__Z_NETRC_MACHINEr,   r!   r(   r2   r/   rC   rc   r&   r&   r&   r'   r      s   	r   c                   @   s  e Zd ZdZe dZddddddd	d
dd
ddd
ddd
ddddg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#gd$ddid%d&d'd(dd)d*d+d,d-d.d-d.d/d0d/d-d1dd-d2d3gd4ddid%d5d6d7d8d9dd:d;d<d=d>d?d@d?d@dAdBdAd?dd?dCdDgdEddiddFd6d7gZdGdH ZdIS )JrS   znebula:videoz/videos/(?P<id>[\w-]+)zEhttps://nebula.tv/videos/that-time-disney-remade-beauty-and-the-beastz$84ed544d-4afd-4723-8cd5-2b95261f0abfr:   z,That Time Disney Remade Beauty and the Beastz$md5:2aae3c4cfc5ee09a1ecdff0909618cf4Z20180731i_[Lindsay Ellislindsayellisz(re:https://nebula\.(tv|app)/lindsayellisz,that-time-disney-remade-beauty-and-the-beasti  z&re:https://\w+\.cloudfront\.net/[\w-]+znebula 5c271b40b13fd613090034fdz,nebulasubscriptions 5c271b40b13fd613090034fd)rJ   extrK   rL   upload_daterV   rY   rW   rZ   rX   r`   r[   rU   r_   r\   rN   rP   r^   skip_downloadm3u8url	info_dictparamszWhttps://nebula.tv/videos/the-logistics-of-d-day-landing-craft-how-the-allies-got-ashoreZ d05739cf6c38c09322422f696b569c23z$7e623145-1b44-4ca3-aa0b-ed25a247ea34z)Landing Craft - How The Allies Got AshorezMre:^In this episode we explore the unsung heroes of D-Day, the landing craft.Z20200327i,~~^u+   Real Engineering — The Logistics of D-Dayzd-dayz>the-logistics-of-d-day-landing-craft-how-the-allies-got-ashoreiI  zhttps://nebula.tv/d-dayznebula 5e7e78171aaf320001fbd6bez,nebulasubscriptions 5e7e78171aaf320001fbd6be)rJ   ri   rK   rL   rj   rV   rY   rW   rZ   rX   r[   rU   r\   rN   r_   r`   rP   r^   )rn   md5ro   rp   z1https://nebula.tv/videos/money-episode-1-the-drawZ ebe28a7ad822b9ee172387d860487868z$b96c5714-9e2b-4ec3-b3f1-20f6e89cc553zEpisode 1: The DrawuO   contains:There’s free money on offer… if the players can all work together.Z20200323ix^Tom Scott Presents: Moneytom-scott-presents-money*https://nebula.tv/tom-scott-presents-moneyi9  zmoney-episode-1-the-drawznebula 5e779ebdd157bc0001d1c75az,nebulasubscriptions 5e779ebdd157bc0001d1c75a)rJ   ri   rK   rL   rj   rV   rY   rW   rZ   rX   r`   rN   r_   r[   rU   rP   r\   r^   z7https://watchnebula.com/videos/money-episode-1-the-drawT)rn   Zonly_matchingzVhttps://nebula.tv/videos/tldrnewseu-did-the-us-really-blow-up-the-nordstream-pipelinesz$e389af9d-1dab-44f2-8788-ee24deb7ff0dz=tldrnewseu-did-the-us-really-blow-up-the-nordstream-pipelinesz3Did the US Really Blow Up the NordStream Pipelines?z$md5:b4e2a14e3ff08f546a3209c75261e789Z20230223i0czTLDR News EUZ
tldrnewseuz&re:https://nebula\.(tv|app)/tldrnewseui  znebula 63f64c74366fcd00017c1513z,nebulasubscriptions 63f64c74366fcd00017c1513)rJ   ri   rU   rK   rL   rj   rV   rY   rW   rZ   rX   r`   rN   r_   r[   rP   r\   r^   z6https://beta.nebula.tv/videos/money-episode-1-the-drawc                 C   sr   |  |}t|i \}}|drB|d |dd| |d |S | jd| |dd}| || |d |S )NrJ    rJ   rU   rK   z.https://content.api.nebula.app/content/videos/zFetching video metadatar7   )	_match_idr   getrC   r2   rc   )r"   rn   r@   smuggled_datametadatar&   r&   r'   _real_extract   s"    

 zNebulaIE._real_extractNrd   re   rf   IE_NAME_BASE_URL_RE
_VALID_URL_TESTSr|   r&   r&   r&   r'   rS   s   s   
frS   c                   @   s   e Zd ZdZe dZ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dddd d!d"dd#d$dd%d&d'd#d(d)d*d+d,d-d.d/d0d1d!gZd2d3 Zd4S )5NebulaClassIEznebula:mediazI/(?!(?:myshows|library|videos)/)(?P<id>[\w-]+)/(?P<ep>[\w-]+)/?(?:$|[?#])z1https://nebula.tv/copyright-for-fun-and-profit/14z$d7432cdc-c608-474d-942c-f74345daed7br:   Z14.https://nebula.tv/copyright-for-fun-and-profit   zIhttps://dj423fildxgac.cloudfront.net/d533718d-9307-42d4-8fb0-e283285e99c9i  z
Episode 14zPhotos, Sculpture, and Video)
rJ   ri   rU   r_   r]   rP   r`   rN   rb   rK   rk   rl   rm   zPhttps://nebula.tv/extremitiespodcast/pyramiden-the-high-arctic-soviet-ghost-townZmp3z$018f65f0-0033-4021-8f87-2d132beb19aaz$md5:05d2b23ab780c955e2511a2b9127acffz$335e8159-d663-491a-888f-1732285706aci03P_ZExtremitiesZ20200903Z20200902z,Pyramiden: The High-Arctic Soviet Ghost TowniF0]z:re:^https?://content\.production\.cdn\.art19\.com.*\.jpeg$g,}:(@i(P_Z20191016)ri   rJ   rL   	series_idmodified_timestamp
episode_idr[   modified_daterj   rK   release_timestamprP   rN   rV   release_date)rn   ro   z2https://nebula.tv/thelayover/the-layover-episode-1z$9d74a762-00bb-45a8-9e8d-9ed47c04a1d0r   Z20230304Z20230403zThe LayoveriF:+dg	t@icdu   The Layover — Episode 1z$874303a5-4900-4626-a4b6-2aacac34466aZ20230303z	Episode 1iydz$md5:002cca89258e3bc7c268d5b8c24ba482)ri   rJ   r]   rP   r   r   r[   r   r   rN   r   rK   r   rj   rb   rV   rL   c                 C   s>  |  |dd\}}t|i \}}|drN|d |dd| |d |S | jd| d| d|dd	}|d
}|dkr| || |d |S |dkr,|d }|s|dr|   t	|r| 
|tS t|dthfdthfdthfdthfdthfdthfdthfdthfddthfd	S td|d S )NrJ   epru   rv   'https://content.api.nebula.app/content///?include=lessonszFetching class/podcast metadatarw   typelessonZpodcast_episodeepisode_urlZpremiumrK   rL   rM   rN   rW   rO   ZassetsZregular)	rJ   rn   rK   rL   rV   rN   rW   ZchnanelrP   zUnexpected content type )Z_match_valid_urlgroupr   ry   rC   r2   rc   r?   r   Zsuitable
url_resultr   r    r   r
   r   r   )r"   rn   r@   rb   rz   r{   content_typer   r&   r&   r'   r|   ,  sJ    
 



zNebulaClassIE._real_extractNr}   r&   r&   r&   r'   r      sp   
9r   c                   @   s<   e Zd ZdZe dZddddidgZdd	 Zd
d ZdS )rT   znebula:subscriptionsz2/(?P<id>myshows|library/latest-videos)/?(?:$|[?#])zhttps://nebula.tv/myshowsr   rJ   myshows)rn   playlist_mincountro   c                 c   s   t ddddd}tdD ]x}| j|dd| d	}|d
 D ]@}| |}| jtd|d  d|d itfddi|V  q>|d}|s d S qd S )Nz.https://content.api.nebula.app/video_episodes/trueZ
engagementz-published_at)Z	followingincludeZorderingr   r   zRetrieving subscriptions page rw   resultshttps://nebula.tv/videos/rU   rJ   url_transparentTnext)	r   	itertoolscountr2   rc   r   r   rS   ry   )r"   next_urlpage_numrY   rb   r{   r&   r&   r'   _generate_playlist_entriesa  s4      



z0NebulaSubscriptionsIE._generate_playlist_entriesc                 C   s   |  |  dS )Nr   )playlist_resultr   )r"   rn   r&   r&   r'   r|   s  s    z#NebulaSubscriptionsIE._real_extractN)	rd   re   rf   r~   r   r   r   r   r|   r&   r&   r&   r'   rT   V  s   
 rT   c                	   @   s   e Zd ZdZe 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dddd	dddddddgZdd  Zd!d" Zd#d$ Z	d%d& Z
d'S )(NebulaChannelIEznebula:channelz5/(?!myshows|library|videos)(?P<id>[\w-]+)/?(?:$|[?#])rt   rs   rr   z@Tom Scott hosts a series all about trust, negotiation and money.)rJ   rK   rL      )rn   ro   Zplaylist_countzhttps://nebula.tv/lindsayellisrh   rg   zCEnjoy these hottest of takes on Disney, Transformers, and Musicals.r   )rn   ro   r   zhttps://nebula.tv/johnnyharrisZjohnnyharriszJohnny Harrisz/I make videos about maps and many other things.Z   r   zcopyright-for-fun-and-profitzCopyright for Fun and Profitz$md5:6690248223eed044a9f11cd5a24f9742   z$https://nebula.tv/trussissuespodcastZtrussissuespodcastzThe TLDR News Podcastz$md5:a08c4483bc0b705881d3e0199e721385P   c                 c   s   d| d}t dD ]}| j||d| d}|d D ]J}| |}| jt|dpbd|d	  d
|d
 itfddi|V  q8|d}|s qqd S )Nz.https://content.api.nebula.app/video_channels/z'/video_episodes/?ordering=-published_atr   zRetrieving channel page rw   r   	share_urlr   rU   rJ   r   Tr   )r   r   r2   rc   r   r   ry   rS   )r"   collection_idcollection_slugr   r   episodesrb   r{   r&   r&   r'   r     s$    



z*NebulaChannelIE._generate_playlist_entriesc                 c   sb   |d D ]T}|  |}| jt|dp<d|d  d|d  d|d itfdd	i|V  qd S )
NZlessonsr   rF   rE   r   r@   rJ   r   T)rc   r   r   ry   r   )r"   rY   r   r{   r&   r&   r'   _generate_class_entries  s    
 
z'NebulaChannelIE._generate_class_entriesc                 c   sr   d| d}t dD ]V}| j||d| d}t|ddd fD ]}| |d	 tV  qB|d
}|s qnqd S )Nz0https://content.api.nebula.app/podcast_channels/z6/podcast_episodes/?ordering=-published_at&premium=truer   zRetrieving podcast page rw   r   c                 S   s   t |d S )Nr   )r   )_vr&   r&   r'   r5     r6   z;NebulaChannelIE._generate_podcast_entries.<locals>.<lambda>r   r   )r   r   r2   r   r   r   ry   )r"   r   r   r   r   r   rb   r&   r&   r'   _generate_podcast_entries  s    
z)NebulaChannelIE._generate_podcast_entriesc                 C   s   |  |}| jd| d|dd}|ddkr<| |}n0|ddkr\| |d |}n| |d |}| j|||d	|d
dS )Nr   r   zRetrieving channelrw   r   classZpodcast_channelrJ   rK   rL   )entriesZplaylist_idZplaylist_titleZplaylist_description)rx   r2   ry   r   r   r   r   )r"   rn   r   rY   r   r&   r&   r'   r|     s"    

 zNebulaChannelIE._real_extractN)rd   re   rf   r~   r   r   r   r   r   r   r|   r&   r&   r&   r'   r   w  sT   
				*r   )r   r   Zart19r   commonr   Znetworking.exceptionsr   utilsr   r   r	   r
   r   r   r   r   r   r   Zutils.traversalr   r   r   rS   r   rT   r   r&   r&   r&   r'   <module>   s   0[}f!