
    yfQ                         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e      Z G d de      Z G d de      Z G d de      Z G d de      Zy)    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                   6    e Zd ZdZdxZZd Zd Zd Zd Z	d Z
y)NebulaBaseIEwatchnebulaNc                    	 | j                  dd ddt        j                  ||d      j                         ddi      }t        |dt        hf      | _        | j                  st	        d      y # t        $ rF}t        |j                  t              r&|j                  j                  dk(  rt	        d	d
       d }~ww xY w)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es        9/usr/lib/python3/dist-packages/yt_dlp/extractor/nebula.py_perform_loginzNebulaBaseIE._perform_login   s    		**/&ZZ( IJQQS');<	 + >H 'x%#@ !9::   	!''9-!''..C2G$%Q\`aa	s   =A/ /	B>8AB99B>c                    | j                   r"d| j                    |j                  di       d<   	  | j                  |i |S # t        $ r}t	        |j
                  t              r|j
                  j                  dvr | j                  d|j
                  j                          | j                          | j                   r"d| j                    |j                  di       d<    | j                  |i |cY d }~S d }~ww xY 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)   s       r*   	_call_apizNebulaBaseIE._call_api+   s    ;;BI$++@WFi,_=
	8&4&&777 	8aggy1QWW^^:5UNNbcdcjcjcqcqbrsu!!#{{FMdkk]D[!!)R0A&4&&777	8s   A 	D B*C;5D ;D c                       j                   st         fd       _          j                  dd  j                   rdd j                    ind dd      d    _        y )	Nc                  @     j                  d      d   j                  S )Nzhttps://nebula.tvznebula_auth.apiToken)_get_cookiesvaluer&   s   r*   <lambda>z/NebulaBaseIE._real_initialize.<locals>.<lambda>=   s    ))*=>?UV\\     z2https://users.api.nebula.app/api/v1/authorization/r-   zToken zAuthorizing to Nebular=   )r   noter   token)r%   r   r   r0   r;   s   `r*   r3   zNebulaBaseIE._real_initialize:   sb    &\^DO))@$EI___t.?&@AZ^(s * 4 5<=r=   c           
         dD ]H  }	 | j                  d|j                  d      d    d| d|d| j                  dd	d
      \  }}||dc S  y # t        $ r}t	        |j
                  t              r)|j
                  j                  dk(  r| j                          |sYt	        |j
                  t              r?|j
                  j                  dk(  r&| j                  d       | j                          Y d }~ d }~ww xY w)N)FTzhttps://content.api.nebula.app/:r   zs/z/manifest.m3u8mp4z23.10.0ios)r?   app_versionplatform)query)formats	subtitlesr.   r/   zPReauthorizing with Nebula and retrying, because fetching video resulted in error)#_extract_m3u8_formats_and_subtitlessplitr0   r   r!   r"   r   r#   raise_login_requiredr2   r3   )r&   
content_idslugretryfmtssubsr)   s          r*   _extract_formatszNebulaBaseIE._extract_formatsC   s    " 	E!EE5j6F6Fs6KA6N5OrR\Q]]kl%!%'0$)( F 
d $(d;;	 " aggy1aggnn6K--/AGGY!?AGGNNVYDYNN#uv))+s   AA	C<BC76C77C<c                     t        |dd hfd      }d|d   j                  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 hfd      ||dS )N)channel_slug
class_slugc                     t        d|       S )Nhttps://nebula.tv/)r   xs    r*   r<   z6NebulaBaseIE._extract_video_metadata.<locals>.<lambda>Y   s    I]_`Aa r=   F)get_allidrA   r   rM   titledescriptionpublished_atdurationrS   channel_titleimages	thumbnailsrcorderzype_idc                 J    | r t        t        |       t        t        |       gS d S )N)r
   NebulaIENebulaSubscriptionsIErW   s    r*   r<   z6NebulaBaseIE._extract_video_metadata.<locals>.<lambda>k   s)    `aOHa0/BWYZ2[<] gk r=   )
display_idr[   r\   	timestampr^   
channel_iduploader_idchanneluploaderseriescreatorra   episode_number_old_archive_ids)channel_urluploader_url)r   	partitionr   r	   r   )r&   episoderr   s      r*   _extract_video_metadataz$NebulaBaseIE._extract_video_metadataW   s    "47a6bcmrt '$-))#.q1
7$ ,,}o>'+7,-*+)*&U[MJ#*[M":%. 2l 1m %n% 
( ''+
 	
r=   )__name__
__module____qualname___NETRC_MACHINEr0   r%   r+   r6   r3   rQ   rv    r=   r*   r   r      s*    "NFZ;8=(
r=   r   c            	       8   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dU ZyV)Wrf   znebula:videoz/videos/(?P<id>[\w-]+)zEhttps://nebula.tv/videos/that-time-disney-remade-beauty-and-the-beastrZ   z$84ed544d-4afd-4723-8cd5-2b95261f0abfextrB   r[   z,That Time Disney Remade Beauty and the Beastr\   z$md5:2aae3c4cfc5ee09a1ecdff0909618cf4upload_date20180731ri   i_[rl   Lindsay Ellisrj   lindsayellisrm   rk   rs   z(re:https://nebula\.(tv|app)/lindsayellisrn   rh   z,that-time-disney-remade-beauty-and-the-beastrr   ro   r^   i  ra   z&re:https://\w+\.cloudfront\.net/[\w-]+rq   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                    | j                  |      }t        |i       \  }}|j                  d      r|d   |dd| j                  |d   |      S | j	                  d| |d      }i | j                  |      | j                  |d   |      S )NrZ    rZ   rh   r[   z.https://content.api.nebula.app/content/videos/zFetching video metadatar>   )	_match_idr   getrQ   r6   rv   )r&   r   rM   smuggled_datametadatas        r*   _real_extractzNebulaIE._real_extract   s    ~~c"*33]T"#D)" ''d(;TB	  >><TFC0 " 2
**84
##HTND9
 	
r=   Nrw   rx   ry   IE_NAME_BASE_URL_RE
_VALID_URL_TESTSr   r{   r=   r*   rf   rf   s   s<   G!N"89JV
8
5
 C
 A	

 :
 
 
 .
 
 >
 G
 o
 H
 F
 
  !
" B#
$ !BDr s%
( #F+-0 i1
8
5
 @
 k	

 :
 
 D
 '
 E
 7
 C
 Z
 D
 
 4
  5!
" B#
$ !BDr s%
( #F+/2 C1
8
5
 *
 m	

 :
 
 2
 4
 3
 5
 H
 
 G
 1
 4
  B!
" 2#
$ !BDr s%
( #F+/2 I h
8
5
 Y
 J	

 A
 :
 
 ~
 ,
 
 <
 E
 
 D
 n
  B!
" ~#
$ !BDr s%
( #F+-0 HCdFL
r=   rf   c                       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dB ZyC)D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-f74345daed7brB   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)
rZ   r}   rh   rr   rp   ra   rs   r^   ru   r[   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)r}   rZ   r\   	series_idmodified_timestamp
episode_idrn   modified_dater~   r[   release_timestampra   r^   ri   release_date)r   r   z2https://nebula.tv/thelayover/the-layover-episode-1r}   rZ   z$9d74a762-00bb-45a8-9e8d-9ed47c04a1d0rp   r   ra   r   20230304r   20230403rn   zThe Layoverr   r   iF:+dr^   g	t@r   icdr[   u   The Layover — Episode 1r   z$874303a5-4900-4626-a4b6-2aacac34466ar~   20230303ru   z	Episode 1ri   iydr\   z$md5:002cca89258e3bc7c268d5b8c24ba482c                    | j                  |      j                  dd      \  }}t        |i       \  }}|j                  d      r|d   |dd| j	                  |d   |      S | j                  d| d| d|d	      }|j                  d
      }|dk(  r(i | j                  |      | j	                  |d   |      S |dk(  r|d   }|s!|j                  d      r| j                          t        j                  |      r| j                  |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|      )NrZ   epr   r   'https://content.api.nebula.app/content///?include=lessonszFetching class/podcast metadatar   typelessonpodcast_episodeepisode_urlpremiumr[   r\   r]   r^   rj   r_   assetsregular)	rZ   r   r[   r\   ri   r^   rj   chnanelra   zUnexpected content type )_match_valid_urlgroupr   r   rQ   r6   rv   rK   r   suitable
url_resultr   r$   r   r   r	   r   )r&   r   rM   ru   r   r   content_typer   s           r*   r   zNebulaClassIE._real_extract,  s   --c288tDg*33]T"#D)" ''d(;TB	  >>5dV1WIEVW8 " :  ||F+8#..x8''=  .."=1K8<<	#:))+,{G<<cUm%}5!C5) -u5,}o>'+7+cU3+cU3&	K=A
+ 
 
 77GHIIr=   Nr   r{   r=   r*   r   r      ss   G!N"klJB8K dL#3
 #F+  b8A?",@#'%C!+V"#&
( D
5
8
 a
 V	

 J
 Z
 m
 @
 !*
 

  
 0
 ?
 :
 {
  !
" A#
E7Fr'Jr=   r   c                   8    e Zd ZdZe dZddddidgZd Zd	 Zy
)rg   znebula:subscriptionsz2/(?P<id>myshows|library/latest-videos)/?(?:$|[?#])zhttps://nebula.tv/myshowsr   rZ   myshows)r   playlist_mincountr   c           
   #   P  K   t        ddddd      }t        j                  d      D ]z  }| j                  |dd| 	      }|d
   D ]D  }| j	                  |      } | j
                  t        d|d    d|d   i      t        fddi| F |j                  d      }|rz y  y w)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/rh   rZ   url_transparentTnext)	r   	itertoolscountr6   rv   r   r   rf   r   )r&   next_urlpage_numrl   ru   r   s         r*   _generate_playlist_entriesz0NebulaSubscriptionsIE._generate_playlist_entriesa  s     #$T#'W
 
 "* 
	Hnn),J8**U % WG"9- X77@%dook/0F/GH74=)'+,4XFJXNVX XX
 {{6*H
	s   BB&"B&c                 B    | j                  | j                         d      S )Nr   )playlist_resultr   )r&   r   s     r*   r   z#NebulaSubscriptionsIE._real_extracts  s    ##D$C$C$EyQQr=   N)	rw   rx   ry   r   r   r   r   r   r   r{   r=   r*   rg   rg   V  s;    $G!N"TUJ*)
 F$Rr=   rg   c            	           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 Zd  Zd! Z	d" Z
y#)$NebulaChannelIEznebula:channelz5/(?!myshows|library|videos)(?P<id>[\w-]+)/?(?:$|[?#])r   r   r   z@Tom Scott hosts a series all about trust, negotiation and money.)rZ   r[   r\      )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           
   #   b  K   d| d}t        j                  d      D ]  }| j                  ||d|       }|d   D ]W  }| j                  |      } | j                  t        |j                  d      xs d|d	    d
|d
   i      t        fddi| Y |j                  d      }|r y  y w)Nz.https://content.api.nebula.app/video_channels/z'/video_episodes/?ordering=-published_atr   zRetrieving channel page r   r   	share_urlr   rh   rZ   r   Tr   )r   r   r6   rv   r   r   r   rf   )r&   collection_idcollection_slugr   r   episodesru   r   s           r*   r   z*NebulaChannelIE._generate_playlist_entries  s     CM?Ryz!* 		H~~hH`ai`jFk~lH#I. X77@%dookKK,d2KHUaLbKc0d74=)'+,4XFJXNVX XX
  ||F+H		s   B(B/+B/c           
   #      K   |d   D ]]  }| j                  |      } | j                  t        |j                  d      xs d|d    d|d    d|d   i      t        fdd	i| _ y w)
Nlessonsr   rV   rT   r   rM   rZ   r   T)rv   r   r   r   r   )r&   rl   r   r   s       r*   _generate_class_entriesz'NebulaChannelIE._generate_class_entries  s     i( 	XF33F;H!$//+

;'k-?@V?WWXYabhYiXj+kvd|$#&'4XFJXNVX X	Xs   A'A)c              #     K   d| d}t        j                  d      D ]]  }| j                  ||d|       }t        |dd f      D ]  }| j	                  |d   t
                |j                  d	      }|r] y  y w)
Nz0https://content.api.nebula.app/podcast_channels/z6/podcast_episodes/?ordering=-published_at&premium=truer   zRetrieving podcast page r   r   c                     t        |d         S )Nr   )r   )_vs     r*   r<   z;NebulaChannelIE._generate_podcast_entries.<locals>.<lambda>  s    ;WXYdWeKf r=   r   r   )r   r   r6   r   r   r   r   )r&   r   r   r   r   r   ru   s          r*   _generate_podcast_entriesz)NebulaChannelIE._generate_podcast_entries  s     Em_  UK  L!* 	H~~hH`ai`jFk~lH'9>f2gh Koogk&:MJJK||F+H	s   A8A?;A?c                    | j                  |      }| j                  d| d|d      }|j                  d      dk(  r| j                  |      }n?|j                  d      dk(  r| j	                  |d   |      }n| j                  |d   |      }| j                  |||j                  d	      |j                  d
            S )Nr   r   zRetrieving channelr   r   classpodcast_channelrZ   r[   r\   )entriesplaylist_idplaylist_titleplaylist_description)r   r6   r   r   r   r   r   )r&   r   r   rl   r   s        r*   r   zNebulaChannelIE._real_extract  s    ..-..5o5FFWX"6 ! 8 ;;v')227;G[[ $5544WT]OTG55gdm_UG##'";;w/!(]!;	 $ = 	=r=   N)rw   rx   ry   r   r   r   r   r   r   r   r   r{   r=   r*   r   r   w  s    G!N"WXJ;,0]

  0 $`

  0 $L

   @03A

  6&,A

  A(FTX	=r=   r   )r   r   art19r   commonr   networking.exceptionsr   utilsr   r	   r
   r   r   r   r   r   r   r   utils.traversalr   r   r   rf   r   rg   r   r{   r=   r*   <module>r      sy       ! -   +WX
= X
vz
| z
zcJL cJLRL RB]=l ]=r=   