
    }i                         d dl Z d dlZd dlmZmZ ddlmZmZ  G d 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 G d de      Z G d d      Zy)    N)ListIterable   )FetchedTranscriptFetchedTranscriptSnippetc                   2    e Zd ZdZdedefdZdee   fdZy)	FormatterzFormatter should be used as an abstract base class.

    Formatter classes should inherit from this class and implement
    their own .format() method which should return a string. A
    transcript is represented by a List of Dictionary items.
    
transcriptreturnc                     t        d      )NzMA subclass of Formatter must implement their own .format_transcript() method.NotImplementedErrorselfr
   kwargss      L/usr/local/lib/python3.12/dist-packages/youtube_transcript_api/formatters.pyformat_transcriptzFormatter.format_transcript   s    !5
 	
    transcriptsc                     t        d      )NzNA subclass of Formatter must implement their own .format_transcripts() method.r   )r   r   r   s      r   format_transcriptszFormatter.format_transcripts   s    !6
 	
r   N)	__name__
__module____qualname____doc__r   strr   r   r    r   r   r	   r	   	   s-    
,= 
C 

d3D.E 
r   r	   c                   2    e Zd ZdedefdZdee   defdZy)PrettyPrintFormatterr
   r   c                 J    t        j                  |j                         fi |S )zPretty prints a transcript.

        :param transcript:
        :return: A pretty printed string representation of the transcript.
        pprintpformatto_raw_datar   s      r   r   z&PrettyPrintFormatter.format_transcript   s!     ~~j446A&AAr   r   c                 p    t        j                  |D cg c]  }|j                          c}fi |S c c}w zConverts a list of transcripts into a JSON string.

        :param transcripts:
        :return: A JSON string representation of the transcript.
        r!   r   r   r   r
   s       r   r   z'PrettyPrintFormatter.format_transcripts'   s7     ~~8CD*Z##%D
HN
 	
D   3Nr   r   r   r   r   r   r   r   r   r   r   r   r      s3    B,= BC B
d3D.E 
TW 
r   r   c                   2    e Zd ZdedefdZdee   defdZy)JSONFormatterr
   r   c                 J    t        j                  |j                         fi |S )zConverts a transcript into a JSON string.

        :param transcript:
        :return: A JSON string representation of the transcript.
        jsondumpsr$   r   s      r   r   zJSONFormatter.format_transcript3   s!     zz*002=f==r   r   c                 p    t        j                  |D cg c]  }|j                          c}fi |S c c}w r&   r-   r'   s       r   r   z JSONFormatter.format_transcripts;   s7     zz8CD*Z##%D
HN
 	
Dr(   Nr)   r   r   r   r+   r+   2   s0    >,= >C >
d3D.E 
TW 
r   r+   c                   2    e Zd ZdedefdZdee   defdZy)TextFormatterr
   r   c                 2    dj                  d |D              S )zConverts a transcript into plain text with no timestamps.

        :param transcript:
        :return: all transcript text lines separated by newline breaks.
        
c              3   4   K   | ]  }|j                     y w)N)text).0lines     r   	<genexpr>z2TextFormatter.format_transcript.<locals>.<genexpr>M   s     :t:s   joinr   s      r   r   zTextFormatter.format_transcriptG   s     yy:z:::r   r   c           
      l    dj                  |D cg c]  } | j                  |fi | c}      S c c}w )zConverts a list of transcripts into plain text with no timestamps.

        :param transcripts:
        :return: all transcript text lines separated by newline breaks.
        z


)r;   r   r'   s       r   r   z TextFormatter.format_transcriptsO   s9     }}LWXj#T##J9&9X
 	
Xs   1Nr)   r   r   r   r2   r2   F   s0    ;,= ;C ;
d3D.E 
TW 
r   r2   c            
       v    e Zd Zdededededef
dZdee   defdZd	ed
ededefdZ	de
defdZdedefdZy)_TextBasedFormatterhoursminssecsmsr   c                     t        d      )NzVA subclass of _TextBasedFormatter must implement their own .format_timestamp() method.r   r   r?   r@   rA   rB   s        r   _format_timestampz%_TextBasedFormatter._format_timestamp[   s    !4
 	
r   linesc                     t        d      )Nz\A subclass of _TextBasedFormatter must implement their own _format_transcript_header method.r   r   rF   s     r   _format_transcript_headerz-_TextBasedFormatter._format_transcript_headera   s    !:
 	
r   i	time_textsnippetc                     t        d      )Nz\A subclass of _TextBasedFormatter must implement their own _format_transcript_helper method.r   r   rJ   rK   rL   s       r   _format_transcript_helperz-_TextBasedFormatter._format_transcript_helperg   s     ":
 	
r   timec                    t        |      }t        |d      \  }}t        |d      \  }}t        |      t        |      t        |      }}}t        t        |t        |      z
  dz  d            }	| j	                  ||||	      S )a|  Helper that converts `time` into a transcript cue timestamp.

        :reference: https://www.w3.org/TR/webvtt1/#webvtt-timestamp

        :param time: a float representing time in seconds.
        :type time: float
        :return: a string formatted as a cue timestamp, 'HH:MM:SS.MS'
        :example:
        >>> self._seconds_to_timestamp(6.93)
        '00:00:06.930'
        i  <   i     )floatdivmodintroundrE   )
r   rP   hours_float	remainder
mins_float
secs_floatr?   r@   rA   rB   s
             r   _seconds_to_timestampz)_TextBasedFormatter._seconds_to_timestampo   s     T{!'d!3Y!'	2!6
J,c*os:Tts4y(D0!45%%eT4<<r   r
   c           
         g }t        |      D ]  \  }}|j                  |j                  z   }dj                  | j	                  |j                        | j	                  |t        |      dz
  k  r'||dz      j                  |k  r||dz      j                  n|            }|j                  | j                  |||              | j                  |      S )zA basic implementation of WEBVTT/SRT formatting.

        :param transcript:
        :reference:
        https://www.w3.org/TR/webvtt1/#introduction-caption
        https://www.3playmedia.com/blog/create-srt-file/
        z	{} --> {}r   )		enumeratestartdurationformatr\   lenappendrO   rI   )r   r
   r   rF   rJ   r8   endrK   s           r   r   z%_TextBasedFormatter.format_transcript   s      , 
	MGAt**t}},C#****4::6**3z?Q..:a!e3D3J3JS3P q1u%++I LL779dKL
	M --e44r   N)r   r   r   rV   r   rE   r   rI   r   rO   rT   r\   r   r   r   r   r   r>   r>   Z   s    
s 
# 
S 
c 
c 

x} 
 


!$
/G
	
=% =C =&5,= 5C 5r   r>   c            
       V    e Zd Zdededededef
dZdee   defdZd	ed
ededefdZ	y)SRTFormatterr?   r@   rA   rB   r   c                 *    dj                  ||||      S )Nz{:02d}:{:02d}:{:02d},{:03d}ra   rD   s        r   rE   zSRTFormatter._format_timestamp       ,33E4rJJr   rF   c                 *    dj                  |      dz   S )N

r4   r:   rH   s     r   rI   z&SRTFormatter._format_transcript_header   s    {{5!D((r   rJ   rK   rL   c                 B    dj                  |dz   ||j                        S )Nz{}
{}
{}r   ra   r6   rN   s       r   rO   z&SRTFormatter._format_transcript_helper   s!     ""1q5)W\\BBr   N
r   r   r   rV   r   rE   r   rI   r   rO   r   r   r   rf   rf      sq    Ks K# KS Kc Kc K)x} ) )CC!$C/GC	Cr   rf   c            
       V    e Zd Zdededededef
dZdee   defdZd	ed
ededefdZ	y)WebVTTFormatterr?   r@   rA   rB   r   c                 *    dj                  ||||      S )Nz{:02d}:{:02d}:{:02d}.{:03d}rh   rD   s        r   rE   z!WebVTTFormatter._format_timestamp   ri   r   rF   c                 0    ddj                  |      z   dz   S )NzWEBVTT

rk   r4   r:   rH   s     r   rI   z)WebVTTFormatter._format_transcript_header   s    fkk%00477r   rJ   rK   rL   c                 :    dj                  ||j                        S )Nz{}
{}rm   rN   s       r   rO   z)WebVTTFormatter._format_transcript_helper   s     y',,77r   Nrn   r   r   r   rp   rp      sl    Ks K# KS Kc Kc K8x} 8 888!$8/G8	8r   rp   c                   D    e Zd ZeeeeedZ G d de	      Z
ddedefdZy)	FormatterLoader)r.   prettyr6   webvttsrtc                   $     e Zd Zdef fdZ xZS )$FormatterLoader.UnknownFormatterTypeformatter_typec           	          t         |   dj                  |dj                  t        j
                  j                                            y )NzpThe format '{formatter_type}' is not supported. Choose one of the following formats: {supported_formatter_types}z, )r{   supported_formatter_types)super__init__ra   r;   ru   TYPESkeys)r   r{   	__class__s     r   r   z-FormatterLoader.UnknownFormatterType.__init__   sG    GSSYSY#1.2ii8M8M8R8R8T.U TZ Tr   )r   r   r   r   r   __classcell__)r   s   @r   UnknownFormatterTyperz      s    	3 	 	r   r   r{   r   c                     |t         j                  j                         vrt         j                  |      t        j                  |          S )z
        Loads the Formatter for the given formatter type.

        :param formatter_type:
        :return: Formatter object
        )ru   r   r   r   )r   r{   s     r   loadzFormatterLoader.load   s@     !6!6!;!;!==!66~FF$$^466r   N)rv   )r   r   r   r+   r   r2   rp   rf   r   	Exceptionr   r   r	   r   r   r   r   ru   ru      s7    &!Ey 	73 	7i 	7r   ru   )r.   r"   typingr   r   _transcriptsr   r   r	   r   r+   r2   r>   rf   rp   ru   r   r   r   <module>r      sr      ! E
 
*
9 
(
I 
(
I 
(=5- =5@
C& 
C
8) 
87 7r   