a
    hS                     @   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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 )NebulaBaseIEZwatchnebulaNc              
   C   s   z.| j dd ddt||d ddid}W nJ tyx } z2t|jtrb|jjdkrbtd	d
d W Y d }~n
d }~0 0 t	|dt
hf| _| jst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)   L/mnt/pikpak/tmp/myenv/lib/python3.9/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 } zpt|jtrX|jjdvrZ | d|jj  | 	  | j rd| j  |di d< | j|i |W  Y d }~S d }~0 0 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 Nebular9   )r   noter   token)r$   r   r   r/   r7   r)   r7   r*   r2   ;   s    
zNebulaBaseIE._real_initializec                 C   s   dD ]}zB| j d|dd  d| d|d| jdd	d
d\}}W  qW q ty } zht|jtrz|jjdkrz|   |st|jtr|jjdkr| 	d | 
  W Y d }~q W Y d }~qd }~0 0 q| || ||dS )N)FThttps://content.api.nebula.app/:r   s/z/manifest.m3u8mp4z23.10.0Zios)r;   Zapp_versionplatform)queryr-   r.   zPReauthorizing with Nebula and retrying, because fetching video resulted in error)formatsZ	subtitles)Z#_extract_m3u8_formats_and_subtitlessplitr/   r   r    r!   r   r"   raise_login_requiredr1   r2   mark_watched)r%   
content_idslugretryZ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   rG   titledescriptionpublished_atdurationrK   channel_titleZimages	thumbnailsrcorderZzype_idc                 S   s   | rt t| t t| gS d S )N)r
   NebulaIENebulaSubscriptionsIE)xr)   r)   r*   r8   o   s
    z6NebulaBaseIE._extract_video_metadata.<locals>.<lambda>)
display_idrP   rQ   	timestamprS   
channel_iduploader_idchanneluploaderseriescreatorrU   episode_number_old_archive_ids)channel_urluploader_url)r   r   	partitionr   r	   r   )r%   episodere   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 watchedFZ	completedTr   r   )fatalr   r   )r5   r   rC   r   r   r   )r%   rF   rG   r)   r)   r*   _mark_watchedv   s    zNebulaBaseIE._mark_watched)__name__
__module____qualname__Z_NETRC_MACHINEr/   r$   r+   r5   r2   rJ   ri   rk   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 )JrX   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)rO   extrP   rQ   upload_dater\   r_   r]   r`   r^   rf   ra   r[   re   rb   rS   rU   rd   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)rO   rq   rP   rQ   rr   r\   r_   r]   r`   r^   ra   r[   rb   rS   re   rf   rU   rd   )rv   md5rw   rx   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)rO   rq   rP   rQ   rr   r\   r_   r]   r`   r^   rf   rS   re   ra   r[   rU   rb   rd   z7https://watchnebula.com/videos/money-episode-1-the-drawT)rv   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)rO   rq   r[   rP   rQ   rr   r\   r_   r]   r`   r^   rf   rS   re   ra   rU   rb   rd   z6https://beta.nebula.tv/videos/money-episode-1-the-drawc                 C   sv   |  |}t|i \}}|drB|d |dd| |d |S | jd| |dd}i | || |d |S )NrO    rO   r[   rP   z.https://content.api.nebula.app/content/videos/zFetching video metadatar:   )	_match_idr   getrJ   r5   ri   )r%   rv   rG   smuggled_datametadatar)   r)   r*   _real_extract   s$    

zNebulaIE._real_extractNrl   rm   rn   IE_NAME_BASE_URL_RE
_VALID_URL_TESTSr   r)   r)   r)   r*   rX   ~   s   
frX   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)
rO   rq   r[   re   rc   rU   rf   rS   rh   rP   rs   rt   ru   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)rq   rO   rQ   	series_idmodified_timestamp
episode_idra   modified_daterr   rP   release_timestamprU   rS   r\   release_date)rv   rw   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)rq   rO   rc   rU   r   r   ra   r   r   rS   r   rP   r   rr   rh   r\   rQ   c                 C   sR  |  |dd\}}t|i \}}|drN|d |dd| |d |S | jd| d| d|dd	}|d
}|dkri | || |d |S |dkr@|d }|s|dr|   | |d | 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 )NrO   epr}   r~   'https://content.api.nebula.app/content///?include=lessonszFetching class/podcast metadatar   typelessonZpodcast_episodeepisode_urlZpremiumrP   rQ   rR   rS   r]   rT   ZassetsZregular)	rO   rv   rP   rQ   r\   rS   r]   ZchnanelrU   zUnexpected content type )Z_match_valid_urlgroupr   r   rJ   r5   ri   rD   rE   r   Zsuitable
url_resultr   r#   r   r   r	   r   )r%   rv   rG   rh   r   r   content_typer   r)   r)   r*   r   7  sN    




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 )rY   znebula:subscriptionsz2/(?P<id>myshows|library/latest-videos)/?(?:$|[?#])zhttps://nebula.tv/myshowsr   rO   myshows)rv   playlist_mincountrw   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 r   resultshttps://nebula.tv/videos/r[   rO   url_transparentTnext)	r   	itertoolscountr5   ri   r   r   rX   r   )r%   next_urlpage_numr_   rh   r   r)   r)   r*   _generate_playlist_entriesm  s0    



z0NebulaSubscriptionsIE._generate_playlist_entriesc                 C   s   |  |  dS )Nr   )playlist_resultr   )r%   rv   r)   r)   r*   r     s    z#NebulaSubscriptionsIE._real_extractN)	rl   rm   rn   r   r   r   r   r   r   r)   r)   r)   r*   rY   b  s   
rY   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{   rz   z@Tom Scott hosts a series all about trust, negotiation and money.)rO   rP   rQ      )rv   rw   Zplaylist_countzhttps://nebula.tv/lindsayellisrp   ro   zCEnjoy these hottest of takes on Disney, Transformers, and Musicals.r   )rv   rw   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 r   r   	share_urlr   r[   rO   r   Tr   )r   r   r5   ri   r   r   r   rX   )r%   collection_idcollection_slugr   r   episodesrh   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   rM   rL   r   rG   rO   r   T)ri   r   r   r   r   )r%   r_   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 r   r   c                 S   s   t |d S )Nr   )r   )_vr)   r)   r*   r8     r9   z;NebulaChannelIE._generate_podcast_entries.<locals>.<lambda>r   r   )r   r   r5   r   r   r   r   )r%   r   r   r   r   r   rh   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 channelr   r   classZpodcast_channelrO   rP   rQ   )entriesZplaylist_idZplaylist_titleZplaylist_description)r   r5   r   r   r   r   r   )r%   rv   r   r_   r   r)   r)   r*   r     s     

zNebulaChannelIE._real_extractN)rl   rm   rn   r   r   r   r   r   r   r   r   r)   r)   r)   r*   r     sT   
				*r   )r   r   Zart19r   commonr   Z
networkingr   Znetworking.exceptionsr   utilsr   r	   r
   r   r   r   r   r   r   r   Zutils.traversalr   r   r   rX   r   rY   r   r)   r)   r)   r*   <module>   s   0e}g!