
    VGhS                         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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   )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                   <    e Zd ZdZdxZZd 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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es        `/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-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/r0   zToken zAuthorizing to Nebular@   )r   noter   token)r(   r   r    r3   r>   s   `r-   r6   zNebulaBaseIE._real_initialize;   sb    &\^DO))@$EI___t.?&@AZ^(s * 4 5<=r@   c           
      "   dD ]C  }	 | j                  d|j                  d      d    d| d|d| j                  dd	d
      \  }} n | j                  ||       dS # t        $ r}t	        |j
                  t              r)|j
                  j                  dk(  r| j                          |sZt	        |j
                  t              r@|j
                  j                  dk(  r'| j                  d       | j                          Y d }~ d }~ww xY w)N)FThttps://content.api.nebula.app/:r   s/z/manifest.m3u8mp4z23.10.0ios)rB   app_versionplatform)queryr1   r2   zPReauthorizing with Nebula and retrying, because fetching video resulted in error)formats	subtitles)#_extract_m3u8_formats_and_subtitlessplitr3   r	   r$   r%   r   r&   raise_login_requiredr5   r6   mark_watched)r)   
content_idslugretryfmtssubsr,   s          r-   _extract_formatszNebulaBaseIE._extract_formatsD   s	   " 	E!EE5j6F6Fs6KA6N5OrR\Q]]kl%!%'0$)( F 
d 	& 	*d+d33 " aggy1aggnn6K--/AGGY!?AGGNNVYDYNN#uv))+s   ?A  	D)BD	D		Dc                     t        |dt        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_slughttps://nebula.tv/F)get_allidrE   r   rS   titledescriptionpublished_atdurationrY   channel_titleimages	thumbnailsrcorderzype_idc                 J    | r t        t        |       t        t        |       gS d S )N)r   NebulaIENebulaSubscriptionsIE)xs    r-   r?   z6NebulaBaseIE._extract_video_metadata.<locals>.<lambda>o   s)    `aOHa0/BWYZ2[<] gk r@   )
display_idr^   r_   	timestampra   
channel_iduploader_idchanneluploaderseriescreatorrd   episode_number_old_archive_ids)channel_urluploader_url)r   r   	partitionr   r
   r   )r)   episoderv   s      r-   _extract_video_metadataz$NebulaBaseIE._extract_video_metadata[   s    "4w?S7T6UV`eg '$-))#.q1
7$ ,,}o>'+7,-*+)*&U[MJ#*[M":%. 2l 1m %n% 
( ''+
 	
r@   c                     | j                  t        d|j                  d      d    d| d      |dddt        j                  d	d
i      j                         ddi       y )NrD   rE   r   rF   z
/progress/zMarking watchedzUnable to mark watchedF	completedTr   r   )fatalr   r   )r9   r   rO   r!   r"   r#   )r)   rR   rS   s      r-   _mark_watchedzNebulaBaseIE._mark_watchedv   so    ::;K;KC;PQR;S:TTVWaVbblmn#%=U[$/0779#%78	 	 	:r@   )__name__
__module____qualname___NETRC_MACHINEr3   r(   r.   r9   r6   rW   rz   r~    r@   r-   r   r      s/    "NFZ;8=4.
6: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)Wri   znebula:videoz/videos/(?P<id>[\w-]+)zEhttps://nebula.tv/videos/that-time-disney-remade-beauty-and-the-beastr]   z$84ed544d-4afd-4723-8cd5-2b95261f0abfextrG   r^   z,That Time Disney Remade Beauty and the Beastr_   z$md5:2aae3c4cfc5ee09a1ecdff0909618cf4upload_date20180731rm   i_[rp   Lindsay Ellisrn   lindsayellisrq   ro   rw   z(re:https://nebula\.(tv|app)/lindsayellisrr   rl   z,that-time-disney-remade-beauty-and-the-beastrv   rs   ra   i  rd   z&re:https://\w+\.cloudfront\.net/[\w-]+ru   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 )Nr]    r]   rl   r^   z.https://content.api.nebula.app/content/videos/zFetching video metadatarA   )	_match_idr   getrW   r9   rz   )r)   r   rS   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r   r   r   IE_NAME_BASE_URL_RE
_VALID_URL_TESTSr   r   r@   r-   ri   ri   ~   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@   ri   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-f74345daed7brG   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)
r]   r   rl   rv   rt   rd   rw   ra   ry   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   r]   r_   	series_idmodified_timestamp
episode_idrr   modified_dater   r^   release_timestamprd   ra   rm   release_date)r   r   z2https://nebula.tv/thelayover/the-layover-episode-1r   r]   z$9d74a762-00bb-45a8-9e8d-9ed47c04a1d0rt   r   rd   r   20230304r   20230403rr   zThe Layoverr   r   iF:+dra   g	t@r   icdr^   u   The Layover — Episode 1r   z$874303a5-4900-4626-a4b6-2aacac34466ar   20230303ry   z	Episode 1rm   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                          | j                  |d   |       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|      )Nr]   epr   r   'https://content.api.nebula.app/content///?include=lessonszFetching class/podcast metadatar   typelessonpodcast_episodeepisode_urlpremiumr^   r_   r`   ra   rn   rb   assetsregular)	r]   r   r^   r_   rm   ra   rn   chnanelrd   zUnexpected content type )_match_valid_urlgroupr   r   rW   r9   rz   rP   rQ   r   suitable
url_resultr   r'   r   r   r
   r	   )r)   r   rS   ry   r   r   content_typer   s           r-   r   zNebulaClassIE._real_extract7  s   --c288tDg*33]T"#D)" ''d(;TB	  >>5dV1WIEVW8 " :  ||F+8#..x8''=  .."=1K8<<	#:))+htnd3,{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
)rj   znebula:subscriptionsz2/(?P<id>myshows|library/latest-videos)/?(?:$|[?#])zhttps://nebula.tv/myshowsr   r]   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/rl   r]   url_transparentTnext)	r   	itertoolscountr9   rz   r   r   ri   r   )r)   next_urlpage_numrp   ry   r   s         r-   _generate_playlist_entriesz0NebulaSubscriptionsIE._generate_playlist_entriesm  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_extract  s    ##D$C$C$EyQQr@   N)	r   r   r   r   r   r   r   r   r   r   r@   r-   rj   rj   b  s;    $G!N"TUJ*)
 F$Rr@   rj   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.)r]   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   rl   r]   r   Tr   )r   r   r9   rz   r   r   r   ri   )r)   collection_idcollection_slugr   r   episodesry   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   r[   rZ   r   rS   r]   r   T)rz   r   r   r   r   )r)   rp   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   r9   r   r   r   r   )r)   r   r   r   r   r   ry   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_channelr]   r^   r_   )entriesplaylist_idplaylist_titleplaylist_description)r   r9   r   r   r   r   r   )r)   r   r   rp   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)r   r   r   r   r   r   r   r   r   r   r   r   r@   r-   r   r     s    G!N"WXJ;,0]

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

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

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

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

  A(FTX	=r@   r   )r   r!   art19r   commonr   
networkingr   networking.exceptionsr   utilsr	   r
   r   r   r   r   r   r   r   r   utils.traversalr   r   r   ri   r   rj   r   r   r@   r-   <module>r     s|       ! % -   +Wb:= b:Jz
| z
zdJL dJNRL RB]=l ]=r@   