
    }i
E                         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Zd dl	Z	d dl
mZmZmZmZmZmZmZmZ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mZ de defd	Z!de fd
Z" G d de      Z#y)    N)Path)TestCase)patch)YouTubeTranscriptApiTranscriptsDisabledNoTranscriptFoundVideoUnavailable	IpBlockedNotTranslatableTranslationLanguageNotAvailableCookiePathInvalidCookieInvalidFailedToCreateConsentCookieYouTubeRequestFailedInvalidVideoIdFetchedTranscriptFetchedTranscriptSnippetAgeRestrictedRequestBlockedVideoUnplayablePoTokenRequired)GenericProxyConfigWebshareProxyConfigfilenamereturnc                 |    t        dj                  t        j                  j	                  t
              |             S )Nz{dirname}/assets/{filename})dirnamer   )r   formatospathr   __file__)r   s    O/usr/local/lib/python3.12/dist-packages/youtube_transcript_api/test/test_api.pyget_asset_pathr#   "   s4    %,,GGOOH- 	- 	
     c                 z    t        t        |       d      5 }|j                         cd d d        S # 1 sw Y   y xY w)Nrb)mode)openr#   read)r   files     r"   
load_assetr+   *   s1    	nX&T	2 dyy{  s   1:c                   P   e Zd Zd Zd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z ed      d        Z  ed      d        Z! ed      d         Z" ed      d!        Z# ed      d"        Z$e%jL                  jO                  d#$      d%        Z(e%jL                  jO                  d#$      d&        Z)e%jL                  jO                  d#$      d'        Z*e%jL                  jO                  d#$      d(        Z+y))*TestYouTubeTranscriptApic           	         t        t        ddd      t        ddd      t        ddd	      gd
ddd      | _        | j                  j                         | _        t        j                          t        j                  t
        j                  dt        d             t        j                  t
        j                  dt        d             t        j                  t
        j                  dt        d             y )NzHey, this is just a testg        gp=
ף?)textstartdurationz#this is not the original transcriptgp=
ף@z-just something shorter, I made up for testingg@gx	@EnglishenFGJLlxj_dtq8)snippetslanguagelanguage_codeis_generatedvideo_id*https://www.youtube.com/youtubei/v1/playerzyoutube.innertube.json.staticbodyhttps://www.youtube.com/watchzyoutube.html.static%https://www.youtube.com/api/timedtextztranscript.xml.static)r   r   ref_transcriptto_raw_dataref_transcript_raw	httprettyenableregister_uriPOSTr+   GETselfs    r"   setUpzTestYouTubeTranscriptApi.setUp0   s    /(3!
 )>!
 )H"" "+
. #'"5"5"A"A"CNN8;<	

 	MM+12	

 	MM334	
r$   c                 T    t        j                          t        j                          y )N)rB   resetdisablerG   s    r"   tearDownz!TestYouTubeTranscriptApi.tearDownZ   s    r$   c                 n    t               j                  d      }| j                  || j                         y )Nr4   )r   fetchassertEqualr?   rH   
transcripts     r"   
test_fetchz#TestYouTubeTranscriptApi.test_fetch^   s/    )+11-@
	
r$   c                     t               j                  dd      }d| j                  d   _        | j	                  || j                         y )Nr4   T)preserve_formattingz*this is <i>not</i> the original transcript   )r   rO   r?   r/   rP   rQ   s     r"   test_fetch_formattedz-TestYouTubeTranscriptApi.test_fetch_formattedf   sO    )+11t 2 

 'SA#	
r$   c                     t        j                  t         j                  dt        d             t	               j                  d      }| j                  || j                         y )Nr:   z0youtube_altered_user_agent.innertube.json.staticr;   r4   )rB   rD   rE   r+   r   rO   rP   r?   rQ   s     r"   #test_fetch__with_altered_user_agentz<TestYouTubeTranscriptApi.test_fetch__with_altered_user_agentr   sR    NN8NO	
 *+11-@
	
r$   c                     t               j                  d      }|D ch c]  }|j                   }}| j                  |h d       y c c}w )Nr4   >   csder3   eshijakozh)r   listr7   rP   )rH   transcript_listrR   language_codess       r"   	test_listz"TestYouTubeTranscriptApi.test_list   sH    .055mDETUz*22UUR	
 Vs   Ac                     t               j                  d      }|j                  dg      }| j                  |j                         y )Nr4   r[   )r   rb    find_manually_created_transcriptassertFalser8   rH   rc   rR   s      r"    test_list__find_manually_createdz9TestYouTubeTranscriptApi.test_list__find_manually_created   s;    .055mD$EEtfM
001r$   c                    t               j                  d      }| j                  t              5  |j	                  dg       d d d        |j	                  dg      }| j                  |j                         y # 1 sw Y   7xY w)Nr4   r[   r3   )r   rb   assertRaisesr   find_generated_transcript
assertTruer8   ri   s      r"   test_list__find_generatedz2TestYouTubeTranscriptApi.test_list__find_generated   so    .055mD01 	>55tf=	> %>>vF

//0	> 	>s   A88Bc                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY w)Nr:   /youtube_video_unavailable.innertube.json.staticr;   z8https://www.youtube.com/youtubei/v1/player?v=GJLlxj_dtq8)rB   rD   rE   r+   rl   r   r   rb   rG   s    r"   test_list__url_as_video_idz3TestYouTubeTranscriptApi.test_list__url_as_video_id   s\    NN8MN	
 ~. 	 "''J	 	 	   A((A1c                     t               j                  d      j                  dg      }|j                  d      }| j	                  |j
                  d       | j                  d|j                         y )Nr4   r3   arz	&tlang=ar)r   rb   find_transcript	translaterP   r7   assertIn_url)rH   rR   translated_transcripts      r"   test_translate_transcriptz2TestYouTubeTranscriptApi.test_translate_transcript   sa    )+00?OOQUPVW
 * 4 4T :.<<dCk#8#=#=>r$   c                     t               j                  d      j                  dg      }| j                  t              5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr4   r3   xyz)r   rb   rv   rl   r   rw   rQ   s     r"   =test_translate_transcript__translation_language_not_availablezVTestYouTubeTranscriptApi.test_translate_transcript__translation_language_not_available   sV    )+00?OOQUPVW
>? 	(  '	( 	( 	(s   AA#c                     t               j                  d      j                  dg      }g |_        | j	                  t
              5  |j                  d       d d d        y # 1 sw Y   y xY w)Nr4   r3   af)r   rb   rv   translation_languagesrl   r   rw   rQ   s     r"   +test_translate_transcript__not_translatablezDTestYouTubeTranscriptApi.test_translate_transcript__not_translatable   s]    )+00?OOQUPVW
+-
(/ 	'  &	' 	' 	's   A!!A*c                    t               j                  dddg       t        j                         j                  }| j                  d|       | j                  t        |d         d       | j                  |d   d   d       y )Nr4   r\   r3   langrV   r   )r   rO   rB   last_requestquerystringrx   rP   lenrH   query_strings     r"   $test_fetch__correct_language_is_usedz=TestYouTubeTranscriptApi.test_fetch__correct_language_is_used   so    $$]T4LA --/;;fl+\&12A6f-a0$7r$   c                 f   t        j                  t         j                  dt        d             t	               j                  dddg       t        j                         j                  }| j                  d|       | j                  t        |d         d       | j                  |d   d	   d       y )
Nr:   z'youtube_ww1_nl_en.innertube.json.staticr;   F1xioXWb8CYr\   r3   r   rV   r   )rB   rD   rE   r+   r   rO   r   r   rx   rP   r   r   s     r"   %test_fetch__fallback_language_is_usedz>TestYouTubeTranscriptApi.test_fetch__fallback_language_is_used   s    NN8EF	
 	$$]T4LA --/;;fl+\&12A6f-a0$7r$   c                 d   t        j                  t         j                  dt        d             t	               j                  d       | j                  t        t        j                               d       t        j                         dd  D ]!  }| j                  |j                  d   d       # y )	Nr=    youtube_consent_page.html.staticr;   r      rV   cookiez'CONSENT=YES+cb.20210328-17-p0.de+FX+119)
rB   rD   rF   r+   r   rO   rP   r   latest_requestsheaders)rH   requests     r"   +test_fetch__create_consent_cookie_if_neededzDTestYouTubeTranscriptApi.test_fetch__create_consent_cookie_if_needed   s    MM+>?	
 	$$]3Y66891= 002126 	G)+T	r$   c                    t        d      D ]1  }t        j                  t        j                  dt	        d             3 | j                  t              5  t               j                  d       d d d        y # 1 sw Y   y xY w)N   r=   r   r;   r   )	rangerB   rD   rF   r+   rl   r   r   rO   )rH   _s     r"   5test_fetch__exception_if_create_consent_cookie_failedzNTestYouTubeTranscriptApi.test_fetch__exception_if_create_consent_cookie_failed   sm    q 	A""/ BC	 :; 	8 "((7	8 	8 	8s   A88Bc                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY w)Nr=   z(youtube_consent_page_invalid.html.staticr;   r   )rB   rD   rF   r+   rl   r   r   rO   rG   s    r"   3test_fetch__exception_if_consent_cookie_age_invalidzLTestYouTubeTranscriptApi.test_fetch__exception_if_consent_cookie_age_invalid   sZ    MM+FG	
 :; 	8 "((7	8 	8 	8rs   c                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY w)Nr:   rq   r;   abc)rB   rD   rE   r+   rl   r	   r   rO   rG   s    r"   *test_fetch__exception_if_video_unavailablezCTestYouTubeTranscriptApi.test_fetch__exception_if_video_unavailable   sZ    NN8MN	
 /0 	0 "((/	0 	0 	0rs   c                     t        j                  t         j                  dd       | j                  t              5 }t               j                  d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr:   i  statusr   zRequest to YouTube failed: )
rB   rD   rE   rl   r   r   rO   rx   str	exceptionrH   cms     r"   .test_fetch__exception_if_youtube_request_failszGTestYouTubeTranscriptApi.test_fetch__exception_if_youtube_request_fails   sp    NNHQT	
 34 	0 "((/	0 	3S5FG	0 	0s   BBc                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY wNr=   z%youtube_too_many_requests.html.staticr;   r   rB   rD   rF   r+   rl   r
   r   rO   rG   s    r"   6test_fetch__exception_if_youtube_request_limit_reachedzOTestYouTubeTranscriptApi.test_fetch__exception_if_youtube_request_limit_reached  s[     	MM+CD	
 y) 	0 "((/	0 	0 	0rs   c                     t        j                  t         j                  dd       | j                  t              5  t               j                  d       d d d        y # 1 sw Y   y xY w)Nr>   i  r   r   )rB   rD   rF   rl   r
   r   rO   rG   s    r"   8test_fetch__exception_if_timedtext_request_limit_reachedzQTestYouTubeTranscriptApi.test_fetch__exception_if_timedtext_request_limit_reached  sU     	MM3	
 y) 	0 "((/	0 	0 	0s   AA(c                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY w)Nr:   z,youtube_age_restricted.innertube.json.staticr;   Njp5uhTorCo)rB   rD   rE   r+   rl   r   r   rO   rG   s    r"   'test_fetch__exception_if_age_restrictedz@TestYouTubeTranscriptApi.test_fetch__exception_if_age_restricted  sY    NN8JK	
 }- 	8 "((7	8 	8 	8rs   c                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY wr   r   rG   s    r"   #test_fetch__exception_if_ip_blockedz<TestYouTubeTranscriptApi.test_fetch__exception_if_ip_blocked)  sY    MM+CD	
 y) 	0 "((/	0 	0 	0rs   c                     t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   y xY w)Nr:   z/youtube_po_token_required.innertube.json.staticr;   r4   )rB   rD   rE   r+   rl   r   r   rO   rG   s    r"   *test_fetch__exception_if_po_token_requiredzCTestYouTubeTranscriptApi.test_fetch__exception_if_po_token_required3  sY    NN8MN	
 / 	8 "((7	8 	8 	8rs   c                 2   t        j                  t         j                  dt        d             | j	                  t
              5 }t               j                  d       d d d        | j                  dt        j                               y # 1 sw Y   /xY w)Nr:   -youtube_request_blocked.innertube.json.staticr;   r   z)YouTube is blocking requests from your IP)rB   rD   rE   r+   rl   r   r   rO   rx   r   r   r   s     r"   %test_fetch__exception_request_blockedz>TestYouTubeTranscriptApi.test_fetch__exception_request_blocked=  su    NN8KL	
 ~. 	8" "((7	8 	A3r||CTU	8 	8s   BBc                    t        j                  t         j                  dt        d             | j	                  t
              5 }t               j                  d       d d d        j                  }| j                  |j                  d       | j                  |j                  ddg       | j                  dt        |             y # 1 sw Y   kxY w)Nr:   z(youtube_unplayable.innertube.json.staticr;   r   zCustom ReasonzSub Reason 1zSub Reason 2)rB   rD   rE   r+   rl   r   r   rO   r   rP   reasonsub_reasonsrx   r   )rH   r   r   s      r"    test_fetch__exception_unplayablez9TestYouTubeTranscriptApi.test_fetch__exception_unplayableI  s    NN8FG	
 / 	82 "((7	8LL	))?;..0PQos9~6	8 	8s   C		Cc                    t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        t        j                  t         j                  dt        d             | j	                  t
              5  t               j                  d       d d d        y # 1 sw Y   pxY w# 1 sw Y   y xY w)Nr:   z2youtube_transcripts_disabled.innertube.json.staticr;   dsMFmonKDD4z3youtube_transcripts_disabled2.innertube.json.staticFjg5lYqvzUs)rB   rD   rE   r+   rl   r   r   rO   rG   s    r"   -test_fetch__exception_if_transcripts_disabledzFTestYouTubeTranscriptApi.test_fetch__exception_if_transcripts_disabledW  s    NN8PQ	
 23 	8 "((7	8 	NN8QR	

 23 	8 "((7	8 	8	8 	8	8 	8s   C+CCC#c                     | j                  t              5 }t               j                  ddg       d d d        | j	                  dt        j                               y # 1 sw Y   /xY w)Nr4   cz)	languageszNo transcripts were found for)rl   r   r   rO   rx   r   r   r   s     r"   -test_fetch__exception_if_language_unavailablezFTestYouTubeTranscriptApi.test_fetch__exception_if_language_unavailablei  s^    01 	JR "((4&(I	J 	5s2<<7HI	J 	Js   A!!A*zByoutube_transcript_api.proxies.GenericProxyConfig.to_requests_dictc                     t        dd      }t        |      j                  d      }| j                  || j                         |j                          y )Nhttp://localhost:8080http_url	https_urlproxy_configr4   )r   r   rO   rP   r?   assert_any_call)rH   to_requests_dictr   rR   s       r"   test_fetch__with_proxyz/TestYouTubeTranscriptApi.test_fetch__with_proxyo  sV    ),-
 *|DJJ

 		
 	((*r$   c                     t        dd      }t        |      j                  d       t        j                         }| j                  |j                  j                  d      d       y )Nusernamepasswordproxy_usernameproxy_passwordr   r4   
Connectionclose)r   r   rO   rB   r   rP   r   get)rH   r   r   r   s       r"   0test_fetch__with_proxy_prevent_alive_connectionszITestYouTubeTranscriptApi.test_fetch__with_proxy_prevent_alive_connections~  sS    *%j
 	,7==mL((*,,\:GDr$   c                 ,   t        d      D ]1  }t        j                  t        j                  dt	        d             3 t        dd      }t        |      j                  d	       | j                  t        t        j                               d
       y )N   r:   r   r;   r   r   r   r   r   	   )r   rB   rD   rE   r+   r   r   rO   rP   r   r   )rH   r   r   r   s       r"   )test_fetch__with_proxy_retry_when_blockedzBTestYouTubeTranscriptApi.test_fetch__with_proxy_retry_when_blocked  s{    q 	A""< OP	 +%%

 	,7==mLY66899Er$   c                 "   d}t        |      D ]1  }t        j                  t        j                  dt	        d             3 t        dd|      }| j                  t              5 }t        |      j                  d	       d d d        | j                  t        t        j                               |d
z         | j                  j                  j                  |       | j                  dt!        |j                               y # 1 sw Y   xY w)N   r:   r   r;   r   r   )r   r   retries_when_blockedr   r   r   Webshare)r   rB   rD   rE   r+   r   rl   r   r   rO   rP   r   r   r   _proxy_configrx   r   )rH   r   retriesr   r   r   s         r"   4test_fetch__with_webshare_proxy_reraise_when_blockedzMTestYouTubeTranscriptApi.test_fetch__with_webshare_proxy_reraise_when_blocked  s    w 	A""< OP	 +%%!(
 ~. 	Q" l;AA-P	Q 	Y66897Q;G33\Bj#bll"34	Q 	Qs   %DDc                    t        j                  t         j                  dt        d             t	        dd      }| j                  t              5 }t        |      j                  d       d d d        | j                  t        t        j                               d       | j                  j                  j                  |       | j                  d	t        |j                               y # 1 sw Y   xY w)
Nr:   r   r;   r   r   r   r   r   z!YouTube is blocking your requests)rB   rD   rE   r+   r   rl   r   r   rO   rP   r   r   r   r   rx   r   )rH   r   r   r   s       r"   3test_fetch__with_generic_proxy_reraise_when_blockedzLTestYouTubeTranscriptApi.test_fetch__with_generic_proxy_reraise_when_blocked  s    NN8KL	

 *,-

 ~. 	Q" l;AA-P	Q 	Y66891=33\B93r||;LM	Q 	Qs   C//C8zfThis test is temporarily disabled because cookie auth is currently not working due to YouTube changes.)r   c                     t        d      }t        |      j                  d      }| j                  || j                         y )Nexample_cookies.txtcookie_pathr4   )r#   r   rO   rP   r?   )rH   r   rR   s      r"   test_fetch__with_cookiesz1TestYouTubeTranscriptApi.test_fetch__with_cookies  s>    
 %%:;)kBHHW
	
r$   c                     t        d      }t        |      }|j                  j                  j                  }| j                  ddit        j                  j                  |             y )Nr   r   
TEST_FIELD
TEST_VALUE)	r#   r   _fetcher_http_clientcookiesrP   requestsutilsdict_from_cookiejar)rH   r   ytt_apisession_cookiess       r"   test_load_cookiesz*TestYouTubeTranscriptApi.test_load_cookies  sX    
 %%:;&;?!**77??<(NN..?	
r$   c                     t        d      }| j                  t              5  t        |       d d d        y # 1 sw Y   y xY w)Nznonexistent_cookies.txtr   )r#   rl   r   r   rH   r   s     r"    test_load_cookies__bad_file_pathz9TestYouTubeTranscriptApi.test_load_cookies__bad_file_path  s;    
 %%>?01 	: [9	: 	: 	:	   7A c                     t        d      }| j                  t              5  t        |       d d d        y # 1 sw Y   y xY w)Nzexpired_example_cookies.txtr   )r#   rl   r   r   r   s     r"   #test_load_cookies__no_valid_cookiesz<TestYouTubeTranscriptApi.test_load_cookies__no_valid_cookies  s:    
 %%BC}- 	: [9	: 	: 	:r   N),__name__
__module____qualname__rI   rM   rS   rW   rY   re   rj   ro   rr   r{   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   pytestmarkskipr   r   r   r    r$   r"   r-   r-   /   s   (
T




21
?('88	880H
0
0808
V78$J OP+ Q+ OPE QE OPF QF  OP5 Q5* OPN QN$ [[*  
	
 [[*  	
		
 [[*  :	:
 [[*  :	:r$   r-   )$r   r   pathlibr   unittestr   unittest.mockr   r   rB   youtube_transcript_apir   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   youtube_transcript_api.proxiesr   r   r   r#   r+   r-   r  r$   r"   <module>r     sf     	         ( SS T  
@:x @:r$   