
    jDJ                        d dl Z d dlmZ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 d dlmZ d dlZd dlmZmZ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$m%Z%m&Z&m'Z' d	dl(m)Z) e G d d             Z*e G d d             Z+e G d d             Z, G d de-e      Z. G d de-e      Z/dede-defdZ0 G d d      Z1 G d d      Z2 G d d       Z3 G d! d"      Z4 G d# d$      Z5y)%    N)	dataclassasdict)Enum)chain)unescape)ListDictIteratorIterablePatternOptional)ElementTree)	HTTPErrorSessionResponse   )ProxyConfig)VideoUnavailableYouTubeRequestFailedNoTranscriptFoundTranscriptsDisabledNotTranslatableTranslationLanguageNotAvailableFailedToCreateConsentCookieInvalidVideoId	IpBlockedRequestBlockedAgeRestrictedVideoUnplayableYouTubeDataUnparsable)	WATCH_URLc                   .    e Zd ZU eed<   eed<   	 eed<   y)FetchedTranscriptSnippettextstartdurationN)__name__
__module____qualname__str__annotations__float     d/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/youtube_transcript_api/_transcripts.pyr#   r#   "   s    
IL Or.   r#   c                       e Zd ZU dZee   ed<   eed<   eed<   eed<   eed<   de	e   fdZ
defd	Zdefd
Zdee   fdZy)FetchedTranscriptz
    Represents a fetched transcript. This object is iterable, which allows you to
    iterate over the transcript snippets.
    snippetsvideo_idlanguagelanguage_codeis_generatedreturnc                 ,    t        | j                        S N)iterr2   selfs    r/   __iter__zFetchedTranscript.__iter__>   s    DMM""r.   c                      | j                   |   S r9   )r2   )r<   indexs     r/   __getitem__zFetchedTranscript.__getitem__A   s    }}U##r.   c                 ,    t        | j                        S r9   )lenr2   r;   s    r/   __len__zFetchedTranscript.__len__D   s    4==!!r.   c                 >    | D cg c]  }t        |       c}S c c}w r9   )r   )r<   snippets     r/   to_raw_datazFetchedTranscript.to_raw_dataG   s    /34Gw444s   N)r'   r(   r)   __doc__r   r#   r+   r*   boolr
   r=   r@   intrC   r	   rF   r-   r.   r/   r1   r1   1   sf    
 +,,MM#(#;< #$$< $" "5T$Z 5r.   r1   c                   "    e Zd ZU eed<   eed<   y)_TranslationLanguager4   r5   N)r'   r(   r)   r*   r+   r-   r.   r/   rK   rK   K   s    Mr.   rK   c                       e Zd ZdZdZdZy)_PlayabilityStatusOKERRORLOGIN_REQUIREDN)r'   r(   r)   rN   rO   rP   r-   r.   r/   rM   rM   Q   s    	BE%Nr.   rM   c                       e Zd ZdZdZdZy)_PlayabilityFailedReasonu%   Sign in to confirm you’re not a botzSign in to confirm your agezVideo unavailableN)r'   r(   r)   BOT_DETECTEDAGE_RESTRICTEDVIDEO_UNAVAILABLEr-   r.   r/   rR   rR   W   s    :L2N+r.   rR   responser3   r7   c                 b    	 | j                          | S # t        $ r}t        ||      d }~ww xY wr9   )raise_for_statusr   r   )rV   r3   errors      r/   _raise_http_errorsrZ   ]   s5    4!!# 4"8U334s    	.).c                   z    e Zd Zdededededededee   fdZdd	ed
e	fdZ
d
efdZed
efd       Zded
d fdZy)
Transcripthttp_clientr3   urlr4   r5   r6   translation_languagesc                     || _         || _        || _        || _        || _        || _        || _        |D ci c]  }|j                  |j                   c}| _        yc c}w )z
        You probably don't want to initialize this directly. Usually you'll access Transcript objects using a
        TranscriptList.
        N)_http_clientr3   _urlr4   r5   r6   r_   _translation_languages_dict)	r<   r]   r3   r^   r4   r5   r6   r_   translation_languages	            r/   __init__zTranscript.__init__f   sm     ( 	 *(%:" )>,
$ !..0D0M0MM,
( ,
s   Apreserve_formattingr7   c                 .   | j                   j                  | j                        }t        |      j	                  t        || j                        j                        }t        || j                  | j                  | j                  | j                        S )z
        Loads the actual transcript data.
        :param preserve_formatting: whether to keep select HTML text formatting
        )rf   )r2   r3   r4   r5   r6   )ra   getrb   _TranscriptParserparserZ   r3   r$   r1   r4   r5   r6   )r<   rf   rV   r2   s       r/   fetchzTranscript.fetch   sz    
 $$((3$9LMSSx7<<
 !]]]],,**
 	
r.   c                 x    dj                  | j                  | j                  | j                  rd      S d      S )Nz7{language_code} ("{language}"){translation_description}z[TRANSLATABLE] )r4   r5   translation_description)formatr4   r5   is_translatabler;   s    r/   __str__zTranscript.__str__   sN    HOO]],,8<8L8L$4 P 
 	
 SU P 
 	
r.   c                 2    t        | j                        dkD  S )Nr   )rB   r_   r;   s    r/   rp   zTranscript.is_translatable   s    4--.22r.   c           	      $   | j                   st        | j                        || j                  vrt	        | j                        t        | j                  | j                  dj                  | j                  |      | j                  |   |dg       S )Nz{url}&tlang={language_code})r^   r5   T)	rp   r   r3   rc   r   r\   ra   ro   rb   )r<   r5   s     r/   	translatezTranscript.translate   s    ##!$--00 @ @@1$--@@MM)00II] 1  ,,];

 
	
r.   NF)r'   r(   r)   r   r*   rH   r   rK   re   r1   rk   rq   propertyrp   rt   r-   r.   r/   r\   r\   e   s    

 
 	

 
 
 
  $$89
4
 
:K 
"
 
 3 3 3
s 
| 
r.   r\   c            	          e Zd ZdZdedeeef   deeef   dee   fdZ	e
dededed	d fd
       Zd	ee   fdZdee   d	efdZdee   d	efdZdee   d	efdZdee   deeeef      d	efdZd	efdZdee   d	efdZy)TranscriptListz
    This object represents a list of transcripts. It can be iterated over to list all transcripts which are available
    for a given YouTube video. Also it provides functionality to search for a transcript in a given language.
    r3   manually_created_transcriptsgenerated_transcriptsr_   c                 <    || _         || _        || _        || _        y)a  
        The constructor is only for internal use. Use the static build method instead.

        :param video_id: the id of the video this TranscriptList is for
        :param manually_created_transcripts: dict mapping language codes to the manually created transcripts
        :param generated_transcripts: dict mapping language codes to the generated transcripts
        :param translation_languages: list of languages which can be used for translatable languages
        N)r3   _manually_created_transcripts_generated_transcripts_translation_languages)r<   r3   ry   rz   r_   s        r/   re   zTranscriptList.__init__   s#     !-I*&;#&;#r.   r]   captions_jsonr7   c                 r   |j                  dg       D cg c]  }t        |d   d   |d          }}i }i }|d   D ]f  }|j                  dd      d	k(  r|}n|}t        | ||d
   |d   d   |d   |j                  dd      d	k(  |j                  dd      r|ng       ||d   <   h t        ||||      S c c}w )a]  
        Factory method for TranscriptList.

        :param http_client: http client which is used to make the transcript retrieving http calls
        :param video_id: the id of the video this TranscriptList is for
        :param captions_json: the JSON parsed from the YouTube pages static HTML
        :return: the created TranscriptList
        translationLanguageslanguageName
simpleTextlanguageCoder4   r5   captionTrackskindrm   asrbaseUrlnameisTranslatableF)rh   rK   r\   rx   )	r]   r3   r   rd   r_   ry   rz   captiontranscript_dicts	            r/   buildzTranscriptList.build   s
   " )6(9(9:PRT(U!

 %	 !-n=lK2>B!
 !
 (*$ "$_5 	G{{62&%/"7">7A	"-'FB'50)05Eu)M%SU8OGN34	  (!!	
 	
7!
s   B4c                 z    t        | j                  j                         | j                  j                               S r9   )r   r|   valuesr}   r;   s    r/   r=   zTranscriptList.__iter__   s3    ..557''..0
 	
r.   language_codesc                 R    | j                  || j                  | j                  g      S )a>  
        Finds a transcript for a given language code. Manually created transcripts are returned first and only if none
        are found, generated transcripts are used. If you only want generated transcripts use
        `find_manually_created_transcript` instead.

        :param language_codes: A list of language codes in a descending priority. For example, if this is set to
        ['de', 'en'] it will first try to fetch the german transcript (de) and then fetch the english transcript (en) if
        it fails to do so.
        :return: the found Transcript
        )_find_transcriptr|   r}   r<   r   s     r/   find_transcriptzTranscriptList.find_transcript   s.     $$//1L1LM
 	
r.   c                 <    | j                  || j                  g      S )a  
        Finds an automatically generated transcript for a given language code.

        :param language_codes: A list of language codes in a descending priority. For example, if this is set to
        ['de', 'en'] it will first try to fetch the german transcript (de) and then fetch the english transcript (en) if
        it fails to do so.
        :return: the found Transcript
        )r   r}   r   s     r/   find_generated_transcriptz(TranscriptList.find_generated_transcript  s      $$^d6Q6Q5RSSr.   c                 <    | j                  || j                  g      S )a|  
        Finds a manually created transcript for a given language code.

        :param language_codes: A list of language codes in a descending priority. For example, if this is set to
        ['de', 'en'] it will first try to fetch the german transcript (de) and then fetch the english transcript (en) if
        it fails to do so.
        :return: the found Transcript
        )r   r|   r   s     r/    find_manually_created_transcriptz/TranscriptList.find_manually_created_transcript  s%     $$T??@
 	
r.   transcript_dictsc                 f    |D ]  }|D ]  }||v s||   c c S   t        | j                  ||       r9   )r   r3   )r<   r   r   r5   r   s        r/   r   zTranscriptList._find_transcript(  sJ    
 , 	:M#3 : O3*=99:	:
  ~tDDr.   c           	      8   dj                  | j                  | j                  d | j                  j	                         D              | j                  d | j
                  j	                         D              | j                  d | j                  D                    S )Na  For this video ({video_id}) transcripts are available in the following languages:

(MANUALLY CREATED)
{available_manually_created_transcript_languages}

(GENERATED)
{available_generated_transcripts}

(TRANSLATION LANGUAGES)
{available_translation_languages}c              3   2   K   | ]  }t        |        y wr9   r*   .0
transcripts     r/   	<genexpr>z)TranscriptList.__str__.<locals>.<genexpr>?  s      [ J[   c              3   2   K   | ]  }t        |        y wr9   r   r   s     r/   r   z)TranscriptList.__str__.<locals>.<genexpr>C  s      K$.JKr   c              3   j   K   | ]+  }d j                  |j                  |j                         - yw)z{language_code} ("{language}")r   N)ro   r4   r5   )r   rd   s     r/   r   z)TranscriptList.__str__.<locals>.<genexpr>F  s>      K
 )	 1771::"6"D"D 8 Ks   13)r3   /available_manually_created_transcript_languagesavailable_generated_transcriptsavailable_translation_languages)ro   r3   _get_language_descriptionr|   r   r}   r~   r;   s    r/   rq   zTranscriptList.__str__4  s    0 &]]<@<Z<Z ["&"D"D"K"K"M[ = -1,J,J K262M2M2T2T2VK - -1,J,J K
 -1,G,GK -  
	
r.   transcript_stringsc                 >    dj                  d |D              }|r|S dS )N
c              3   @   K   | ]  }d j                  |        yw)z - {transcript})r   N)ro   r   s     r/   r   z;TranscriptList._get_language_description.<locals>.<genexpr>P  s&       
 $$
$; 
s   None)join)r<   r   descriptions      r/   r   z(TranscriptList._get_language_descriptionO  s,    ii  
0 
 
 *{5v5r.   N)r'   r(   r)   rG   r*   r	   r\   r   rK   re   staticmethodr   r   r
   r=   r   r   r   r   r   rq   r   r-   r.   r/   rx   rx      s4   
<< '+3
?&;<  $CO4	<
  $$89<( +
+
(++
<@+
	+
 +
Z
(:. 

hsm 

 
 	T 	T* 	T
&sm
	

E 
E tCO45
E 
	
E
 
66HSM 6c 6r.   rx   c                       e Zd Zdedee   fdZdedefdZ	ddede
defdZd	ededefd
ZdededdfdZd	ededdfdZdedefdZdedefdZy)TranscriptListFetcherr]   proxy_configc                      || _         || _        y r9   )ra   _proxy_config)r<   r]   r   s      r/   re   zTranscriptListFetcher.__init__X  s    ')r.   r3   r7   c                 b    t         j                  | j                  || j                  |            S r9   )rx   r   ra   _fetch_captions_json)r<   r3   s     r/   rk   zTranscriptListFetcher.fetch\  s/    ##%%h/
 	
r.   
try_numberc                 ,   	 | j                  | j                  |      |      S # t        $ rg}| j                  dn| j                  j                  }|dz   |k  r| j                  ||dz         cY d }~S |j                  | j                        d }~ww xY w)Nr   r   )r   )_extract_captions_json_fetch_video_htmlr   r   retries_when_blockedr   with_proxy_config)r<   r3   r   	exceptionretriess        r/   r   z*TranscriptListFetcher._fetch_captions_jsonc  s    	B..&&x0(   	B %%- ''<< 
 A~'00jSTn0UU--d.@.@AA	Bs!    # 	BAB-B3BBhtmlc                 ,   t        d      }	 |j                  ||      }| j	                  |j                  d      |       |j                  di       j                  d      }|d|vrt        |      |S # t        $ r}d|v rt        |      |d }~ww xY w)NytInitialPlayerResponsezclass="g-recaptcha"playabilityStatuscaptionsplayerCaptionsTracklistRendererr   )_JsVarParserrj   r    r   _assert_playabilityrh   r   )r<   r   r3   
var_parser
video_dataer   s          r/   r   z,TranscriptListFetcher._extract_captions_jsonr  s    !";<
	#))$9J 	  0C!DhO"z26::-
  O=$H%h// % 	$,))G		s   A4 4	B=BBplayability_status_dataNc                 .   |j                  d      }|t        j                  j                  k7  ra|]|j                  d      }|t        j                  j                  k(  rP|t
        j                  j                  k(  rt        |      |t
        j                  j                  k(  rt        |      |t        j                  j                  k(  rU|t
        j                  j                  k(  r8|j                  d      s|j                  d      rt        |      t        |      |j                  di       j                  di       j                  di       j                  dg       }t        |||D cg c]  }|j                  d	d
       c}      y y c c}w )Nstatusreasonzhttp://zhttps://errorScreenplayerErrorMessageRenderer	subreasonrunsr$   rm   )rh   rM   rN   valuerP   rR   rS   r   rT   r   rO   rU   
startswithr   r   r   )r<   r   r3   playability_statusr   
subreasonsruns          r/   r   z)TranscriptListFetcher._assert_playability  sc   488B"4"7"7"="==".,00:F!%7%F%F%L%LL5BBHHH(225DDJJJ'11"&8&>&>&D&DD6HHNNN&&y1X5H5H5T(22&x00'++M2>126["%VR	  "&*"M337762#6"M + / >. #Ns   0Fc                     t        j                  d|      }|t        |      | j                  j                  j                  dd|j                  d      z   d       y )Nzname="v" value="(.*?)"CONSENTzYES+r   z.youtube.com)domain)researchr   ra   cookiessetgroup)r<   r   r3   matchs       r/   _create_consent_cookiez,TranscriptListFetcher._create_consent_cookie  sV    		2D9=-h77!!%%vA.~ 	& 	
r.   c                     | j                  |      }d|v r2| j                  ||       | j                  |      }d|v rt        |      |S )Nz&action="https://consent.youtube.com/s")_fetch_htmlr   r   )r<   r3   r   s      r/   r   z'TranscriptListFetcher._fetch_video_html  sR    )3t;''h7##H-D74?1(;;r.   c                     | j                   j                  t        j                  |            }t	        t        ||      j                        S )N)r3   )ra   rh   r!   ro   r   rZ   r$   )r<   r3   rV   s      r/   r   z!TranscriptListFetcher._fetch_html  s<    $$(()9)98)LM*8X>CCDDr.   )r   )r'   r(   r)   r   r   r   re   r*   rx   rk   rI   r	   r   r   r   r   r   r   r-   r.   r/   r   r   W  s    *G *8K;P *
c 
n 
BS Bc B$ B3 # $ (4 3 SW :
3 
# 
$ 
# # EC EC Er.   r   c                   N    e Zd Zg dZd	defdZdedee   fdZdede	e
   fdZy)
ri   )
strongembimarksmalldelinssubsuprf   c                 0    | j                  |      | _        y r9   )_get_html_regex_html_regex)r<   rf   s     r/   re   z_TranscriptParser.__init__  s    //0CDr.   r7   c                     |rIdj                  | j                        }d|z   dz   }t        j                  |t        j                        }|S t        j                  dt        j                        }|S )N|z<\/?(?!\/?(z
)\b).*?\b>z<[^>]*>)r   _FORMATTING_TAGSr   compile
IGNORECASE)r<   rf   formats_regex
html_regexs       r/   r   z!_TranscriptParser._get_html_regex  s`    HHT%:%:;M*]:]JMM2==AJ  J>Jr.   raw_datac                 X   t        j                  |      D cg c]  }|j                  yt        t	        j
                  | j                  dt        |j                              t        |j                  d         t        |j                  j                  dd                   c}S c c}w )Nrm   r%   durz0.0)r$   r%   r&   )r   
fromstringr$   r#   r   r   r   r   r,   attribrh   )r<   r   xml_elements      r/   rj   z_TranscriptParser.parse  s      +55h?
 + %VVD,,b(;;K;K2LMK..w78{1155eUCD
 	
 
s   BB'Nru   )r'   r(   r)   r   rH   re   r   r*   r   r   r#   rj   r-   r.   r/   ri   ri     sH    ED E4 GCL 	
c 	
d+C&D 	
r.   ri   c                   `    e Zd ZdefdZdededefdZdededee   fdZdee   dedefd	Z	y
)r   var_namec                     || _         y r9   )	_var_name)r<   r  s     r/   re   z_JsVarParser.__init__  s	    !r.   raw_htmlr3   r7   c                 t    | j                  ||      }| j                  ||      }t        j                  |      S r9   )_create_var_char_iterator_find_var_substringjsonloads)r<   r  r3   char_iterator
var_strings        r/   rj   z_JsVarParser.parse  s5    66xJ--mXF
zz*%%r.   c                     |j                  d| j                         }t        |      dk  rt        |      t	        |d         }t        |      dk7  r	 t        |      dk7  r|S )Nzvar r   {)splitr  rB   r    r:   next)r<   r  r3   splitted_htmlr  s        r/   r  z&_JsVarParser._create_var_char_iterator  sh     dnn-='>?}"'11]1-.=!S( =!S(r.   r  c                     d}d}d}dg}|D ]X  }|j                  |       |rd}n(|dk(  rd}n |dk(  r| }n|s|dk(  r|dz  }n
|dk(  r|dz  }|dk(  sGd	j                  |      c S  t        |      )
NFr   r  \T"}r   rm   )appendr   r    )r<   r  r3   escaped	in_quotesdepthcharschars           r/   r  z _JsVarParser._find_var_substring  s    	! 	&DLL )M	3;QJES[QJEzwwu~%	&" $H--r.   N)
r'   r(   r)   r*   re   r	   rj   r
   r  r  r-   r.   r/   r   r     sf    " "&c &S &T &
#  RU .# .# .RU .r.   r   )6r	  dataclassesr   r   enumr   	itertoolsr   r   r   typingr   r	   r
   r   r   r   
defusedxmlr   r   requestsr   r   r   proxiesr   _errorsr   r   r   r   r   r   r   r   r   r   r   r   r    	_settingsr!   r#   r1   rK   r*   rM   rR   rZ   r\   rx   r   ri   r   r-   r.   r/   <module>r%     s    )    D D " 	 1 1      !    5 5 52   
&d &,sD ,4 4S 4X 4H
 H
Vd6 d6N_E _ED#
 #
L). ).r.   