o
    i\                     @   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	 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G dd deZdS )    N   )Art19IE)InfoExtractor   )PATCHRequest)	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                   @   sH   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d ZdS )NebulaBaseIEwatchnebulaNc              
   C   s   z| j dd ddt||d ddid}W n ty6 } zt|jtr1|jjdkr1td	d
d d }~ww t	|dt
hf| _| jsGtdd S )Nzhttps://nebula.tv/auth/login/zLogging in to NebulazLogin failed)emailpasswordcontent-type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*   K/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/extractor/nebula.py_perform_login   s"   
zNebulaBaseIE._perform_loginc              
   O   s   | j rd| j  |di d< z	| j|i |W S  ty\ } z8t|jtr,|jjdvr- | d|jj  | 	  | j rJd| j  |di d< | j|i |W  Y d }~S d }~ww )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 rdd j  ind dddd	  _d S )
Nc                      s     dd jS )Nzhttps://nebula.tvznebula_auth.apiToken)_get_cookiesvaluer*   r&   r*   r+   <lambda>>   s    z/NebulaBaseIE._real_initialize.<locals>.<lambda>z2https://users.api.nebula.app/api/v1/authorization/r-   zToken zAuthorizing to Nebula    )r   noter   token)r%   r   r   r0   r9   r*   r9   r+   r3   ;   s   

zNebulaBaseIE._real_initializec                 C   s   dD ][}z| j d|dd  d| d|d| jdd	d
d\}}W  n: ty] } z.t|jtr;|jjdkr;|   |sXt|jtrX|jjdkrX| 	d | 
  W Y d }~q d }~ww | || ||dS )N)FThttps://content.api.nebula.app/:r   s/z/manifest.m3u8mp4z23.10.0ios)r=   app_versionplatform)queryr.   r/   zPReauthorizing with Nebula and retrying, because fetching video resulted in error)formats	subtitles)#_extract_m3u8_formats_and_subtitlessplitr0   r   r!   r"   r   r#   raise_login_requiredr2   r3   mark_watched)r&   
content_idslugretryfmtssubsr)   r*   r*   r+   _extract_formatsD   s.   

	
zNebulaBaseIE._extract_formatsc                 C   s   t |dt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_slughttps://nebula.tv/F)get_allidr?   r   rM   titledescriptionpublished_atdurationrR   channel_titleimages	thumbnailsrcorderzype_idc                 S   s   | rt t| t t| gS d S )N)r
   NebulaIENebulaSubscriptionsIE)xr*   r*   r+   r:   o   s
   z6NebulaBaseIE._extract_video_metadata.<locals>.<lambda>)
display_idrW   rX   	timestamprZ   
channel_iduploader_idchanneluploaderseriescreatorr]   episode_number_old_archive_ids)channel_urluploader_url)r   r   	partitionr   r	   r   )r&   episodern   r*   r*   r+   _extract_video_metadata[   s2   z$NebulaBaseIE._extract_video_metadatac              
   C   sJ   | j td|dd  d| d|dddtd	d
i ddid d S )Nr>   r?   r   r@   z
/progress/zMarking watchedzUnable to mark watchedF	completedTr   r   )fatalr   r   )r6   r   rI   r   r   r    )r&   rL   rM   r*   r*   r+   _mark_watchedv   s   
zNebulaBaseIE._mark_watched)__name__
__module____qualname___NETRC_MACHINEr0   r%   r,   r6   r3   rQ   rr   ru   r*   r*   r*   r+   r      s    	r   c                	   @   s:  e Zd ZdZe dZ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"d#gid$d%i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d0dd.dd1dd2dd2dd d!d3d4gid$d%id5d6d7i dd8dddd9d
d:dd;dd<dd=dd>dd=dd>dd?dd@dd?dd=ddAdd dd=d!dBdCgid$d%id5dDdEdFdGi ddHddddIddJd
dKddLddMddNddOddNddOddPddQddPddNdd ddNd!dRdSgid$d%id&dTdEdFgZdUdV ZdWS )Xra   znebula:videoz/videos/(?P<id>[\w-]+)zEhttps://nebula.tv/videos/that-time-disney-remade-beauty-and-the-beastrV   z$84ed544d-4afd-4723-8cd5-2b95261f0abfextrA   rW   z,That Time Disney Remade Beauty and the BeastrX   z$md5:2aae3c4cfc5ee09a1ecdff0909618cf4upload_date20180731re   i_[rh   Lindsay Ellisrf   lindsayellisri   rg   ro   z(re:https://nebula\.(tv|app)/lindsayellisrj   rd   z,that-time-disney-remade-beauty-and-the-beastrn   rk   rZ   i  r]   z&re:https://\w+\.cloudfront\.net/[\w-]+rm   znebula 5c271b40b13fd613090034fdz,nebulasubscriptions 5c271b40b13fd613090034fdskip_downloadm3u8url	info_dictparamszWhttps://nebula.tv/videos/the-logistics-of-d-day-landing-craft-how-the-allies-got-ashore 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.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)r   md5r   r   z1https://nebula.tv/videos/money-episode-1-the-draw ebe28a7ad822b9ee172387d860487868z$b96c5714-9e2b-4ec3-b3f1-20f6e89cc553zEpisode 1: The DrawuO   contains:There’s free money on offer… if the players can all work together.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 5e779ebdd157bc0001d1c75az7https://watchnebula.com/videos/money-episode-1-the-drawT)r   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:b4e2a14e3ff08f546a3209c75261e78920230223i0czTLDR News EU
tldrnewseuz&re:https://nebula\.(tv|app)/tldrnewseui  znebula 63f64c74366fcd00017c1513z,nebulasubscriptions 63f64c74366fcd00017c1513z6https://beta.nebula.tv/videos/money-episode-1-the-drawc                 C   sv   |  |}t|i \}}|dr!|d |dd| |d |S | jd| |dd}i | || |d |S )NrV    rV   rd   rW   z.https://content.api.nebula.app/content/videos/zFetching video metadatar<   )	_match_idr   getrQ   r6   rr   )r&   r   rM   smuggled_datametadatar*   r*   r+   _real_extract   s$   

zNebulaIE._real_extractNrv   rw   rx   IE_NAME_BASE_URL_RE
_VALID_URL_TESTSr   r*   r*   r*   r+   ra   ~   sZ   
	
	
	
	
fra   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"i d#dd$d%d&d'd(dd)d*d+d,d-d.d/d%d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAd!gZdBdC ZdDS )E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-f74345daed7brA   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)
rV   rz   rd   rn   rl   r]   ro   rZ   rq   rW   r   r   r   zPhttps://nebula.tv/extremitiespodcast/pyramiden-the-high-arctic-soviet-ghost-townmp3z$018f65f0-0033-4021-8f87-2d132beb19aaz$md5:05d2b23ab780c955e2511a2b9127acffz$335e8159-d663-491a-888f-1732285706aci03P_Extremities2020090320200902z,Pyramiden: The High-Arctic Soviet Ghost TowniF0]z:re:^https?://content\.production\.cdn\.art19\.com.*\.jpeg$g,}:(@i(P_20191016)rz   rV   rX   	series_idmodified_timestamp
episode_idrj   modified_dater{   rW   release_timestampr]   rZ   re   release_date)r   r   z2https://nebula.tv/thelayover/the-layover-episode-1rz   rV   z$9d74a762-00bb-45a8-9e8d-9ed47c04a1d0rl   r   r]   r   20230304r   20230403rj   zThe Layoverr   r   iF:+drZ   g	t@r   icdrW   u   The Layover — Episode 1r   z$874303a5-4900-4626-a4b6-2aacac34466ar{   20230303rq   z	Episode 1re   iydrX   z$md5:002cca89258e3bc7c268d5b8c24ba482c                 C   sL  |  |dd\}}t|i \}}|dr'|d |dd| |d |S | jd| d| d|dd	}|d
}|dkrNi | || |d |S |dkr|d }|sa|dra|   | |d | t	
|rt| |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|)NrV   epr   r   'https://content.api.nebula.app/content///?include=lessonszFetching class/podcast metadatar   typelessonpodcast_episodeepisode_urlpremiumrW   rX   rY   rZ   rf   r[   assetsregular)	rV   r   rW   rX   re   rZ   rf   chnanelr]   zUnexpected content type )_match_valid_urlgroupr   r   rQ   r6   rr   rJ   rK   r   suitable
url_resultr   r$   r   r   r	   r   )r&   r   rM   rq   r   r   content_typer   r*   r*   r+   r   7  sN   



zNebulaClassIE._real_extractNr   r*   r*   r*   r+   r      s    
	
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 )rb   znebula:subscriptionsz2/(?P<id>myshows|library/latest-videos)/?(?:$|[?#])zhttps://nebula.tv/myshowsr   rV   myshows)r   playlist_mincountr   c                 c   s    t ddddd}tdD ]<}| j|dd| d	}|d
 D ] }| |}| jtd|d  d|d itfddi|V  q |d}|sK d S qd S )Nz.https://content.api.nebula.app/video_episodes/true
engagementz-published_at)	followingincludeorderingr   r   zRetrieving subscriptions page r   resultshttps://nebula.tv/videos/rd   rV   url_transparentTnext)	r   	itertoolscountr6   rr   r   r   ra   r   )r&   next_urlpage_numrh   rq   r   r*   r*   r+   _generate_playlist_entriesm  s6   



z0NebulaSubscriptionsIE._generate_playlist_entriesc                 C   s   |  |  dS )Nr   )playlist_resultr   )r&   r   r*   r*   r+   r     s   z#NebulaSubscriptionsIE._real_extractN)	rv   rw   rx   r   r   r   r   r   r   r*   r*   r*   r+   rb   b  s    
rb   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-]+)/?(?:$|[?#])r   r   r   z@Tom Scott hosts a series all about trust, negotiation and money.rV   rW   rX      r   r   playlist_countzhttps://nebula.tv/lindsayellisr~   r}   zCEnjoy these hottest of takes on Disney, Transformers, and Musicals.r   )r   r   r   zhttps://nebula.tv/johnnyharris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/trussissuespodcasttrussissuespodcastzThe TLDR News Podcastz$md5:a08c4483bc0b705881d3e0199e721385P   c                 c   s    d| d}t dD ]A}| j||d| d}|d D ]%}| |}| jt|dp2d|d	  d
|d
 itfddi|V  q|d}|sM d S qd S )Nz.https://content.api.nebula.app/video_channels/z'/video_episodes/?ordering=-published_atr   zRetrieving channel page r   r   	share_urlr   rd   rV   r   Tr   )r   r   r6   rr   r   r   r   ra   )r&   collection_idcollection_slugr   r   episodesrq   r   r*   r*   r+   r     s*   



z*NebulaChannelIE._generate_playlist_entriesc                 c   sd    |d D ]*}|  |}| jt|dpd|d  d|d  d|d itfdd	i|V  qd S )
Nlessonsr   rT   rS   r   rM   rV   r   T)rr   r   r   r   r   )r&   rh   r   r   r*   r*   r+   _generate_class_entries  s   
 

z'NebulaChannelIE._generate_class_entriesc                 c   sv    d| d}t dD ],}| j||d| d}t|ddd fD ]}| |d	 tV  q"|d
}|s8 d S qd S )Nz0https://content.api.nebula.app/podcast_channels/z6/podcast_episodes/?ordering=-published_at&premium=truer   zRetrieving podcast page r   r   c                 S   s   t |d S )Nr   )r   _vr*   r*   r+   r:         z;NebulaChannelIE._generate_podcast_entries.<locals>.<lambda>r   r   )r   r   r6   r   r   r   r   )r&   r   r   r   r   r   rq   r*   r*   r+   _generate_podcast_entries  s   
z)NebulaChannelIE._generate_podcast_entriesc                 C   s   |  |}| jd| d|dd}|ddkr| |}n|ddkr.| |d |}n| |d |}| j|||d	|d
dS )Nr   r   zRetrieving channelr   r   classpodcast_channelrV   rW   rX   )entriesplaylist_idplaylist_titleplaylist_description)r   r6   r   r   r   r   r   )r&   r   r   rh   r   r*   r*   r+   r     s    

zNebulaChannelIE._real_extractN)rv   rw   rx   r   r   r   r   r   r   r   r   r*   r*   r*   r+   r     sV    
				*r   c                   @   sl   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	gZdd Zdd Zdd Z	dS )NebulaSeasonIEznebula:seasonz4/(?P<series>[\w-]+)/season/(?P<season_number>[\w-]+)z"https://nebula.tv/jetlag/season/15	jetlag_15zTag: All Starsz$md5:5aa5b8abf3de71756448dc44ffebb674r      r   z"https://nebula.tv/jetlag/season/14	jetlag_14Snakez$md5:6da9040f1c2ac559579738bfb6919d1ez$https://nebula.tv/jetlag/season/13-5zjetlag_13-5zHide + Seek Across NYCz$md5:5b87bb9acc6dcdff289bb4c71a2ad59f   c                 C   sZ   t |dthfptd|dpd|d  }| jt|d|d itfddi| |S )	Nr   rT   app_pathr   rM   rV   r   T)r   r   r   r   r   r   ra   rr   )r&   itemr   r*   r*   r+   _build_url_result   s   z NebulaSeasonIE._build_url_resultc                 c   sx    t |ddd dfD ]}| |V  qt |ddddd fD ]}| |V  qt |d	d
d fD ]}| |V  q1d S )Nr   c                 S   s   |d d S )NvideorV   r*   r   r*   r*   r+   r:     r   z)NebulaSeasonIE._entries.<locals>.<lambda>r   extras.itemsc                 S      |d S NrV   r*   r   r*   r*   r+   r:         trailersc                 S   r   r   r*   r   r*   r*   r+   r:     r   )r   r   )r&   r   rq   extratrailerr*   r*   r+   _entries
  s   zNebulaSeasonIE._entriesc                 C   sn   |  |dd\}}| d| }| d| d| |}| j| ||fi t|dthfdthfdS )	Nrj   season_numberr   r   z/season/rW   rX   )rW   rX   )r   r   r6   r   r   r   r$   )r&   r   rj   	season_idr   r   r*   r*   r+   r     s   
zNebulaSeasonIE._real_extractN)
rv   rw   rx   r   r   r   r   r   r   r   r*   r*   r*   r+   r     s8    
		
r   )r   r   art19r   commonr   
networkingr   networking.exceptionsr   utilsr   r	   r
   r   r   r   r   r   r   r   utils.traversalr   r   r   ra   r   rb   r   r   r*   r*   r*   r+   <module>   s    0e}g!`