
    i]	                     `    d dl Z d dlZd dlmZ d dlmZ  e       Zdedee   fdZdedefdZ	y)    N)Optional)Consolesubtitle_pathreturnc           	         	 t        | ddd      5 }dj                  t        d      D cg c]  }|j                          c}      j	                         }d|v r
	 ddd       y	d
|v sd|v sd|v rd|v sd|v r
	 ddd       y	 ddd       y|j                  d      }t        |      D ]J  \  }}|j                         j                         s%|dz   t        |      k  s7d||dz      v sB ddd       y d|v r
	 ddd       y	 ddd       yc c}w # 1 sw Y   yxY w# t        $ r.}t        j                  d|  dt        |              Y d}~yd}~ww xY w)zFDetects the actual format of a subtitle file by examining its content.rzutf-8ignore)encodingerrors 
   webvttNvttz[script info]z[v4+ styles]z[v4 styles]zformat: namezformat: markedassssa
   z-->srtz)[red]Error detecting subtitle format for z: )openjoinrangereadlinelowersplit	enumeratestripisdigitlen	Exceptionconsoleprintstr)r   f_first_lineslinesilinees           e/home/nidran/Documenti/script/streaming-community/StreamingCommunity/core/processors/helper/ex_sub.pydetect_subtitle_formatr+      s   ]-wxH 	A''r"CA1::<"CDJJLK;&		 	 +-;1NR_cnRn![04D4S 	 	 	 	  %%d+E$U+ %4::<'')a!ec%j.@a!e,$	 	%
 #%	 	" $#	. - #D	.   ]A-PRSVWXSYRZ[\\]s   D DD
DD $D9D DD A DD
D*D+D 4D:D D DDD D 	E'$EEc                    t        |       }|t        j                  d|  d       | S t        j                  j                  |       \  }}|j                         j                  d      }||k(  r| S | d| }	 t        j                  | |       t        j                  d| d|        |S # t        $ r-}t        j                  dt        |              | cY d}~S d}~ww xY w)zSDetects the actual subtitle format and renames the file with the correct extension.Nz1[yellow]    Warning: Could not detect format for z, keeping original extension.z$[yellow]    Renamed subtitle: [cyan]z [yellow]-> [cyan]z"[red]    Error renaming subtitle: )r+   r    r!   ospathsplitextr   lstripshutilmover   r"   )r   detected_format	base_namecurrent_extnew_pathr)   s         r*   fix_subtitle_extensionr8   -   s    ,];OI-Xtuv  WW--m<I{##%,,S1K o% Ao./HM8,<[MI[\k[lmn :3q6(CDs   92B, ,	C"5"CC"C")
r.   r2   typingr   rich.consoler   r    r"   r+   r8        r*   <module>r=      sH    
   ! )# (3- :# # r<   