
    hڑ              
          S r SSKrSSKrSSKrSSKrSSKrSSKrSSKJr  SSK	J
r
JrJrJr  SSKJr  SSKJr  SSKJr  SS	KJrJrJrJrJr  SS
KJrJr  SSKJr  \R>                  " S5      r S\!4S jr"S\S\\#\4   4S jr$S\S\\#   4S jr%S\S\\#   4S jr&  S/S\S\
\   S\
\   S\\#   4S jjr'S0S\S\(S\!4S jjr)\" SSSS9S/S\#S\#S\#4S  jj5       r*  S/S\S\
\   S\
\   S\4S! jjr+  S/S\S\
\   S\
\   S\
\   4S" jjr,\S#\#4S$ j5       r-S\S\!4S% jr.\S&\#4S' j5       r/\S( 5       r0S1S\S)\
\(   S\(4S* jjr1S\S\!4S+ jr2\S, 5       r3\S\#4S- j5       r4\S. 5       r5g)2z
Celery Tasks for Stream Processing

This module contains all stream processing functionality as Celery tasks
for better task management. All FFmpeg operations, HLS processing,
and stream management are handled as background tasks.
    N)Path)OptionalDictAnyList)shared_task)timezone)settings   )ChannelStreamSession
HLSSegmentVideoConfigurationAudioConfiguration)JingleDetectorAdBreakAnalyzer)NotificationServicezstream_processor.tasksreturnc                       [         R                  " SS/SSSS9n U R                  S:X  a  [        R	                  S5        g[        SU R                   35      e! [         a    [        S	5      e[         R                   a    [        S
5      ef = f)z
Validate that FFmpeg is available and accessible.

Returns:
    bool: True if FFmpeg is available, raises exception otherwise
    
Raises:
    RuntimeError: If FFmpeg is not found or not executable
ffmpegz-versionT
   )capture_outputtexttimeoutr   zFFmpeg validation successfulzFFmpeg test failed: z0FFmpeg not found in PATH. Please install FFmpeg.zFFmpeg validation timed out)	
subprocessrun
returncodeloggerinfoRuntimeErrorstderrFileNotFoundErrorTimeoutExpired)results    >C:\Users\brahi\OneDrive\Desktop\Code\src\apps\streams\tasks.py_validate_ffmpegr&      s    :z"	
 !KK67!5fmm_EFF OMNN$$ :899:s   >A A 4Bchannelc           
         [        [        R                  S   5      U R                  -  nUS-  nUS-  nUS-  nUUUUS.nUR	                  5        H?  u  pg UR                  SSS9  UR                  S5        [        R                  S	U 35        MA     U$ ! [         a$  n[        R                  S
U SU SU 35        e SnAff = f)z
Create the necessary output directories for a channel.

Args:
    channel (Channel): Channel model instance
    
Returns:
    Dict[str, Path]: Dictionary containing paths to created directories

OUTPUT_DIRhlslogsiframes)baser*   r+   r,   T)parentsexist_oki  zCreated directory: zFailed to create z directory : N)r   r
   STREAM_CONFIGslugitemsmkdirchmodr   r   OSErrorerror)	r'   	base_pathhls_path	logs_pathiframes_pathdirectoriesnamepathes	            r%   _create_output_directoriesr@   <   s     X++L9:W\\II5 HF"Iy(L 	K "'')
	JJtdJ3JJuKK-dV45 * 	  	LL,TF+dV2aSIJ	s   9B
CB>>Caudio_configc           
          / nU R                   (       a  UR                  SS/5        UR                  SU R                  S[        U R                  5      S[        U R
                  5      SU R                  /5        U$ )z
Build audio encoding options from configuration.

Args:
    audio_config (AudioConfiguration): Audio configuration object
    
Returns:
    List[str]: Audio encoding options for FFmpeg
-afloudnorm=I=-16:LRA=11:TP=-1-c:a-ar-ac-b:a)	normalizeextendcodecstrsample_ratechannelsbitrate)rA   optionss     r%   _build_audio_optionsrQ   a   sv     G <=> NN""s<++,s<(()$$	  N    video_configc                 ^   SU R                   SU R                  SU R                  SU R                  SU R                  S[        U R                  5      SU R                  SU R                  S	U R                  /nU R                   S
:X  a%  UR                  SU R                  SSSSSSSS/
5        U$ )z
Build video encoding options from configuration.

Args:
    video_config (VideoConfiguration): Video configuration object
    
Returns:
    List[str]: Video encoding options for FFmpeg
-c:v
-profile:v-level-s-aspectz-r-b:v-maxrate-bufsizeh264z-preset-crf20-sc_threshold0-g48-keyint_min)rK   profilelevel
resolutionaspect_ratiorL   
frame_ratemin_bitratemax_bitraterJ   preset)rS   rP   s     r%   _build_video_optionsrm   |   s     	""l**,$$l%%<,,c,))*((L,,L,,
G V#|**DS$4
 	 NrR   c                 ^   [        U 5      nUS   nSSSSSSSS	S
U R                  SSSS/nU(       a  UR                  [        U5      5        O`UR                  SSSSS[	        [
        R                  S   5      S[	        [
        R                  S   5      S[
        R                  S   /
5        U(       a  UR                  [        U5      5        O~UR                  SSSSSSS[
        R                  S   S [
        R                  S!   S"[
        R                  S#   S$[
        R                  S%   S&[
        R                  S#   S'S(S)S*S+S,S-S,/5        UR                  S.SS/[	        U R                  5      S0[	        U R                  5      S1S2S3S4S5S6S7S2S8[	        US9-  5      [	        US:-  5      /5        U$ );a5  
Build the FFmpeg command for stream capture.

Args:
    channel (Channel): Channel configuration
    video_config (VideoConfiguration, optional): Video encoding settings
    audio_config (AudioConfiguration, optional): Audio encoding settings
    
Returns:
    List[str]: FFmpeg command as list of arguments
r*   r   z-hide_bannerz	-loglevelr   z-user_agentz<Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36z-headerszReferer: http://www.radio2m.ma/z-iz-mapz0:v:0z0:a:0rC   rD   rE   aacrF   SAMPLE_RATErG   CHANNELSrH   BITRATErU   r]   rV   mainrW   z3.1rX   
RESOLUTIONrY   ASPECT_RATIOrZ   MIN_BITRATEr[   MAX_BITRATEr\   r^   r_   r`   ra   rb   rc   rd   z-fz	-hls_timez-hls_list_sizez-hls_delete_threshold1z
-hls_flagsdelete_segmentsz-hls_start_number_sourcedatetimez	-strftimez-hls_segment_filenamezindex_%Y_%m_%d_%H_%M_%S.ts
index.m3u8)r@   hls_urlrJ   rQ   rL   r
   AUDIO_CONFIGrm   VIDEO_CONFIGsegment_durationmax_segments)r'   rS   rA   r<   r9   cmds         r%   _build_ffmpeg_commandr      s   " -W5K5!H 	V 	U 	5 	goo 	!C( 

'56 	

0E3x,,];<3x,,Z89H)))4
 	 

'56 	

F&e(''5x,,^<H))-8--m<--m<DS$4
 	  JJeS112#g223'"JS 	 H334 	H|#$ " JrR   <   sessionr   c                    [         R                   " 5       n[         R                   " 5       U-
  U:  Ga   U R                  (       aH  [        R                  " SSU R                  /SS9nUR                  S:w  a  [
        R                  S5        g[        U R                  R                  5       5      nUS-  nUR                  5       (       a?  [        UR                  S	5      5      n[        U5      S:  a  [
        R                  S
5        g[         R                  " S5        [         R                   " 5       U-
  U:  a  GM  [
        R#                  S5        g! [          a"  n[
        R                  SU 35         SnAgSnAff = f)z
Monitor a capture session for initial success.

Args:
    session (StreamSession): Session to monitor
    timeout (int): Timeout in seconds for initial success check
    
Returns:
    bool: True if capture appears successful, False otherwise
ps-pT)r   r   zFFmpeg process has terminatedFr{   *.tsz!Stream capture appears successful   zError monitoring capture: NzCapture monitoring timed out)time
process_idr   r   r   r   r7   r   r'   get_hls_pathexistslistgloblenr   sleep	Exceptionwarning)r   r   
start_timer$   r9   playlist_filesegmentsr?   s           r%   _monitor_capturer      s/    J
))+

"W
,	!!#4!3!34#' $$)LL!@A  GOO88:;H$|3M##%%f 56x=1$KK CD JJqM1 ))+

"W
,< NN12  	LL5aS9:	s%   AE A;E 	E 
F E;;F T   )bindmax_retriesdefault_retry_delay
channel_idvideo_config_idaudio_config_idc           	          [         R                  SU 35        [        R                  R	                  US9nU(       a  [
        R                  R	                  US9OSnU(       a  [        R                  R	                  US9OSn[        5         [        UUUS9nU(       a  [         R                  SUR                   35        [        5       nUR                  SUR                  [        UR                  5      UR                  R                  5       S.S9  S	[        UR                  5      UR                  UR                  R                  5       S
.$ SUR                   S3n	[         R!                  U	5        [        5       nUR                  SUR                  U	["        R$                  " 5       R                  5       S.S9  ['        U	5      e! [        R(                   a'    SU S3n	[         R!                  U	5        ['        U	5      e[&         a  n
[         R!                  SU
 35        U R*                  R,                  U R.                  :  a?  [         R                  SU R*                  R,                  S-    S35        U R1                  U
S9e [        5       nUR                  SU[        U
5      ["        R$                  " 5       R                  5       S	S.S9  e !    e = fSn
A
ff = f)a  
Start stream capture for a specific channel.

This task initiates the FFmpeg stream capture process and creates
the necessary directory structure and configuration files.

Args:
    channel_id (str): UUID of the channel to capture
    video_config_id (str, optional): UUID of video configuration to use
    audio_config_id (str, optional): UUID of audio configuration to use
    
Returns:
    dict: Task result with session information
    
Raises:
    Exception: If stream capture fails to start
z*Starting stream capture task for channel: idN)r'   rS   rA   z0Stream capture started successfully for channel stream_started)channel_name
session_idr   template_typecontextT)successr   r   
started_atz"Stream capture failed for channel z after all retry attemptsstream_error)r   error_message	timestampChannel with ID 
 not foundzStream capture task failed: z&Retrying stream capture task (attempt r   ))exc)r   r   r   retries_exhausted)r   r   r   objectsgetr   r   r&   _capture_with_retryr=   r   send_notificationrL   r   r   	isoformatr7   r	   nowr   DoesNotExistrequestretriesr   retry)selfr   r   r   r'   rS   rA   r   notification_service	error_msgr?   s              r%   start_stream_capturer   ,  s   &T@MN //%%%4M\)11555IbfM\)11555Ibf 	 &%%
 KKJ7<<.YZ $7#8  22.$+LL"%gjj/")"4"4">">"@ 3   !'**o '%00::<	  =W\\NJcdILL# $7#8  22,$+LL%.!)!9!9!; 3  I&& #&zl*=	Y	"" 3A378 <<$"2"22KK@AUAUXYAY@ZZ[\]***##':'<$$66"0(2),Q%-\\^%=%=%?-1	 7  /s:   EG	 A9G	 	AKA;KA	KKKKKc           	         [         R                  SU R                   35        [        R                  R                  U UUS[        R                  " 5       S9n [        XU5      n[         R                  SSR                  U5       35        [        R                  " U[        R                  [        R                  SSSS9n[        UR                  5      Ul        S	Ul        UR#                  5         [         R                  S
UR                   35        U$ ! [$         al  nSUl        [        U5      Ul        [        R                  " 5       Ul        UR#                  5         [         R+                  SU 35        [-        SU 35      eSnAff = f)aY  
Start stream capture for a channel.

Args:
    channel (Channel): Channel to capture
    video_config (VideoConfiguration, optional): Video encoding settings
    audio_config (AudioConfiguration, optional): Audio encoding settings
    
Returns:
    StreamSession: Created session object
    
Raises:
    RuntimeError: If capture fails to start
z%Starting stream capture for channel: pending)r'   rS   rA   statusr   zFFmpeg command:  Tr   )stdoutr!   r   bufsizeuniversal_newlinesactivez!Stream capture started with PID: failedz Failed to start stream capture: zStream capture failed: N)r   r   r=   r   r   creater	   r   r   joinr   PopenPIPErL   pidr   r   saver   
last_errorended_atr7   r    )r'   rS   rA   r   r   processr?   s          r%   _start_capturer     sJ   & KK7~FG ##**!!<<> + G":#G<H 	&sxx}o67 ""????#
 !-!7}EF :! V#<<>7s;<4QC899:s   B<D 
FA'FFc                 b   SnU R                   nU R                  nX4:  a}   [        R                  SUS-    SU 35        [	        XU5      n[        U5      (       a  U$ US-  nX4:  a/  [        R                  SU S35        [        R                  " U5        X4:  a  M}  [        R                  SU SU R                   35        g
! [         aa  n[        R                  SUS-    SU 35        US-  nX4:  a/  [        R                  S	U S35        [        R                  " U5         S
nANS
nAff = f)aR  
Start stream capture with automatic retry logic.

Args:
    channel (Channel): Channel to capture
    video_config (VideoConfiguration, optional): Video encoding settings
    audio_config (AudioConfiguration, optional): Audio encoding settings
    
Returns:
    Optional[StreamSession]: Session if successful, None if all retries failed
r   zStream capture attempt r   /zCapture failed, retrying in z seconds...zCapture attempt z	 failed: zRetrying in NzAll z capture attempts failed for )retry_attemptsretry_intervalr   r   r   r   r   r   r   r   r7   r=   )r'   rS   rA   r   r   r   r   r?   s           r%   r   r     s2     G((K++N

	+KK1'A+a}MN %WLIG  (( 1(NN6~6FkR JJ~.# 
< LL4}$A',,PQ  	+LL+GaK=	!EFqLG$">"2+> 

>*	+s   ;C 9C 
D.AD))D.r   c           	          [         R                  SU  35        [        R                  R	                  U S9n[        U5      nU(       a  [         R                  SU  35        [        5       nUR                  SUR                  R                  [        UR                  5      UR                  5       (       a  [        UR                  5       5      OSUR                  S.S9  S[        UR                  5      UR                  R                  UR                  (       a  UR                  R                  5       S
.$ S	S
.$ SU  3n[         R!                  U5        [#        U5      e! [        R$                   a'    SU  S3n[         R!                  U5        [#        U5      e["         a  n[         R!                  SU 35        e S	nAff = f)z
Stop an active stream capture session.

Args:
    session_id (str): UUID of the stream session to stop
    
Returns:
    dict: Task result with session information
z%Stopping stream capture for session: r   z0Stream capture stopped successfully for session stream_stoppedUnknown)r   r   durationsegments_processedr   TN)r   r   r   r   z*Failed to stop stream capture for session zStream session with ID r   z!Stop stream capture task failed: )r   r   r   r   r   _stop_capturer   r   r'   r=   rL   r   r   r   r   r   r7   r   r   )r   r   r   r   r   r?   s         r%   stop_stream_capturer     s   *;J<HI  ''++z+:  (KKJ:,WX $7#8  22.$+OO$8$8"%gjj/;B;K;K;M;MG$4$4$6 7S\*1*D*D	 3   !'**o ' 4 4<C<L<LG,,668	  SW	  EZLQILL#I&&%% #-j\D	Y	"" 8<=s%   D3E 6E :%E AG "F;;G c                    [         R                  SU R                   35         U R                  (       Ga   [        R
                  " [        U R                  5      [        R                  5        [         R                  SU R                   35        [        R                  " S5         [        R
                  " [        U R                  5      S5        [        R
                  " [        U R                  5      [        R                  5        [         R                  SU R                   35        [         R                  SU R                   35        SU l        [         R"                  " 5       U l        SU l        U R'                  5         g! [         a&    [         R                  SU R                   S35         Nf = f! [         a&    [         R                  SU R                   S35         N[         a/  n[         R                  S	U R                   S
U 35         SnANSnAff = f! [         a6  n[         R)                  SU 35        U R+                  SU 35         SnAgSnAff = f)z
Stop an active stream capture session.

Args:
    session (StreamSession): Session to stop
    
Returns:
    bool: True if successfully stopped, False otherwise
z!Stopping stream capture session: zSent SIGTERM to process PID:    r   zForce killed process PID: zProcess PID z terminated gracefullyz not found (already terminated)zError terminating process r0   NzTerminated process PID: 	completedTz Failed to stop capture session: zStop failed: F)r   r   r   r   oskillintsignalSIGTERMr   r   SIGKILLProcessLookupErrorr   r   r   r	   r   r   r   r7   	add_error)r   r?   s     r%   r   r   G  s    KK3GJJ<@A(WG../@;G<N<N;OPQ 

1[GGC 2 23Q7GGC 2 23V^^DKK"<W=O=O<P QR KK273E3E2FGH %#<<>!% * [KK,w/A/A.BBX YZ[ & `l7+=+=*>>]^_ W!;G<N<N;OrRSQTUVVW  7s;<M!-.ss   H$ A0F: (BF ,AH$ -F74F: 5H$ 6F77F: :-H!'H$ )	H!2%HH$ H!!H$ $
I$.,II$
segment_idc           	      l    [         R                  SU  35        [        R                  R	                  U S9n[
        R                  R	                  SS5      (       d  [         R                  S5        SSS.$ [        R                  R                  [
        R                  S   S	5      n[        R                  R                  UR                  R                  R                  5       S
5      n[        X#5      nUR!                  U5      nS[#        UR$                  5      SUSLS.nU(       Ga+  [         R                  SU  SUR&                  R(                   35        [+        5       nUR-                  U5      n[/        5       n	U	R1                  SUR&                  R(                  UR                  R                  R(                  UR2                  UR4                  R7                  5       UR8                  S.S9  UR;                  [#        UR$                  5      UR&                  R(                  UR2                  U(       a  [#        UR$                  5      OSS.5        UR                  =R<                  S-  sl        UR                  R?                  S/S9  U$ ! [        R@                   a'    SU  S3n
[         RC                  U
5        [E        U
5      e[D         aa  n[         RC                  SU 35         [        R                  R	                  U S9nUR                  RG                  SU 35        e !    e = fSnAff = f)a1  
Process a newly created HLS segment for jingle detection.

This task is triggered when a new segment is detected and performs
jingle detection and ad break analysis.

Args:
    segment_id (str): UUID of the HLS segment to process
    
Returns:
    dict: Processing results including any detections found
zProcessing new segment: r   USE_JINGLESFz9Jingle detection is disabled, skipping segment processingT)r   jingle_detectionr)   jinglesr,   N)r   r   r   detection_foundzJingle detected in segment r0   jingle_detected)jingle_namer   confidence_scoredetection_time
frame_pathr   )detection_idr   r   ad_break_idr   r   )update_fieldszHLS segment with ID r   z Segment processing task failed: zSegment processing failed: )$r   r   r   r   r   r
   JINGLE_CONFIGdebugr   r>   r   r1   r   r'   get_output_pathr   process_segmentrL   r   templater=   r   process_detectionr   r   r   r   r   r   updater   r   r   r7   r   r   )r   segmentjingles_folderiframes_folderdetector	detectionr$   analyzerad_breakr   r   r?   s               r%   process_new_segmentr  ~  s   K.zl;< $$((J(7 %%))-??LLTU#?? h&<&<\&JIVgoo&=&=&M&M&OQZ[!.A ,,W5	 gjj/ $(4	
 KK5j\IDVDVD[D[C\]^ '(H11)<H $7#8  22/#,#5#5#:#:$+OO$;$;$@$@(1(B(B&/&>&>&H&H&J"+"6"6 3 	 MM #ILL 1(1166$-$>$>3;s8;;/	  	**a/*,@+AB"" #*:,jA	Y	"" 
7s;<	 ((,,
,;GOO%%(CA3&GH 		
s8   A3J 6HJ AL3L.+;L'&L.'L+)L..L3c                      [         R                  S5        [        R                  R	                  SS/S9n SnSn/ nU  GHg  n UR
                  (       a  [        R                  " 5       UR
                  -
  nUR                  5       S:  aM  UR                  S:X  a=  SUR                   S3nUR                  U5        [         R                  U5        US	-  nM  UR                  (       a  SS
Kn UR                  " SSUR                  /SSS9nUR                   S:w  a  SUR                   SUR                   S3nUR                  U5        [         R                  U5        SUl        [        R                  " 5       Ul        SUl        UR)                  5         US	-  nGMa   US	-  nGMj     U(       aF  US:  a@  [1        5       n
U
R3                  SUUU[        R                  " 5       R5                  5       S.S9  [         R7                  SU SU S35        SUUUS.$ ! UR*                   a?    SUR                   3nUR                  U5        [         R                  U5        US	-  n GM*  f = f! [,         aI  n	SUR                   SU	 3nUR                  U5        [         R/                  U5        US	-  n S
n	A	GM|  S
n	A	ff = f! [,         a  n	[         R/                  SU	 35        e S
n	A	ff = f) z
Periodic task to check the health of active stream sessions.

This task monitors active streams for issues and sends alerts
if problems are detected.

Returns:
    dict: Health check results
zRunning stream health checkr   
processing)
status__inr   i,  zSession z  has no segments after 5 minutesr   Nr   r   Tr   )r   r   zProcess z for session r   r   zProcess terminated unexpectedlyz!Health check timeout for session zHealth check error for session r0   health_check)healthy_sessionsunhealthy_sessionsissuesr   r   zStream health check completed: z
 healthy, z
 unhealthy)r   r
  r  r  zStream health check failed: )r   r   r   r   filterr   r	   r   total_secondsr   r   appendr   r   r   r   r   r   r   r   r   r#   r   r7   r   r   r   r   )active_sessionshealthy_countunhealthy_countr  r   time_since_startissuer   r$   r?   r   s              r%   check_stream_healthr    s   V23 (//66 ,/ 7 
 &G/%%%'/||~8J8J'J$'557#="55:&.wzzl:Z$[E"MM%0"NN51+q0O$ %%%!!+!4););<+/$&"
 ",,1&.w/A/A.B-PWPZPZ|[e$fE"MM%0"NN51 .6GN/7||~G,1RG.#LLN+q0O$ 2( "U 'f o)#6#8  22,(5*9$!)!9!9!;	 3  	5m_JN__ijk  -"1	
 	
= &44 !"CGJJ< Pe,u-'1, !  %9'**RsKe$U#1$	%8  3A378s   A K BI,K I,-B/HK I,%A4K A
I)$I,%K (I))I,,
J?6=J:3K :J??K 
K*K%%K*
keep_countc                 B   Uc  U R                   n [        U R                  5       5      n[        UR	                  S5      S S9nSn[        U5      U:  a=  USU*  nU H1  n UR                  5         US-  n[        R                  SU 35        M3     US:  a%  [        R                  S
U SU R                   35        U$ ! [         a&  n[        R                  SU S	U 35         SnAM  SnAff = f! [         a"  n[        R                  SU 35         SnAgSnAff = f)z
Clean up old segment files beyond the configured limit.

Args:
    channel (Channel): Channel to clean up
    keep_count (int, optional): Number of segments to keep
    
Returns:
    int: Number of segments deleted
Nr   c                 6    U R                  5       R                  $ N)statst_mtime)xs    r%   <lambda>'_cleanup_old_segments.<locals>.<lambda>N  s    qvvx?P?PrR   )keyr   r   zDeleted segment: zFailed to delete segment r0   Cleaned up z old segments for zFailed to cleanup segments: )r   r   r   sortedr   r   unlinkr   r   r6   r   r   r=   r   r7   )r'   r  r9   r   deleted_countsegments_to_deleter   r?   s           r%   _cleanup_old_segmentsr%  >  s"    ))
,,./(--/5PQx=:%!),J;!7-ONN$!Q&MLL#4WI!>?	 . 1KK+m_4Fw||nUV  ONN%>wir!#MNNO  3A378sB   AC2 !-B?0C2 ?
C/	C*$C2 *C//C2 2
D<DDc                 d    [        U R                  5       5      nUS-  n/ SQn[        US5       nUR                  SR	                  U5      5        SSS5        [
        R                  SU 35        g! , (       d  f       N'= f! [         a"  n[
        R                  SU 35         SnAg	SnAff = f)
z
Create a master HLS playlist file for a channel.

Args:
    channel (Channel): Channel to create playlist for
    
Returns:
    bool: True if playlist was created successfully
zmaster.m3u8)z#EXTM3Uz#EXT-X-VERSION:3z#EXT-X-INDEPENDENT-SEGMENTSzz#EXT-X-MEDIA:TYPE=CLOSED-CAPTIONS,GROUP-ID="CC",LANGUAGE="eng",NAME="english",DEFAULT=YES,AUTOSELECT=YES,INSTREAM-ID="CC1"zy#EXT-X-STREAM-INF:BANDWIDTH=3405600,RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2",FRAME-RATE=25,CLOSED-CAPTIONS="CC"r{   w
NzCreated master playlist: Tz"Failed to create master playlist: F)	r   r   openwriter   r   r   r   r7   )r'   r9   master_pathcontentfr?   s         r%   _create_master_playlistr.  f  s    ,,./.
 +s#qGGDIIg&' $ 	/}=>	 $#  9!=>s.   .B !A2 B 2
B <B 
B/B**B/c                      [         R                  S5        [        R                  R	                  SS9n SnU  H@  n [        U5      nX-  nUS:  a'  [         R                  SU SUR                   35        M@  MB     SS
K
Jn  [        R                  " 5       U" SS9-
  n[        R                  R	                  USS9nUR                  5       nUR!                  5         [         R                  SU SU S35        SUUS.$ ! [         a0  n[         R                  SUR                   SU 35         S	nAM  S	nAff = f! [         a  n[         R                  SU 35        e S	nAff = f)z
Periodic task to clean up old segment files and maintain disk space.

This task removes old segment files beyond the configured retention
limits for each channel.

Returns:
    dict: Cleanup results
zRunning segment cleanup taskT)	is_activer   r   z segments for channel z'Failed to cleanup segments for channel r0   N)	timedelta   )daysF)processed_at__ltis_availablezSegment cleanup completed: z files, z DB records)r   files_deleteddb_records_deletedzSegment cleanup task failed: )r   r   r   r   r  r%  r   r=   r   r7   rz   r1  r	   r   r   countdelete)	rN   total_deletedr'   r#  r?   r1  cutoff_timeold_segmentsdb_deleted_counts	            r%   cleanup_old_segmentsr>    sl   +34 ??))D)9G
 5g >. 1$KK+m_<RSZS_S_R` ab %   	'llnya'88!))00( 1 

 (--/1-IYHZZefg *"2
 	
%  Fw||nTVWXVYZ[0  4QC89sA   9D; :C>6BD; >
D8%D3-D; 3D88D; ;
E#EE#c                     [         R                  SU  35        [        R                  R	                  U S9n[        U5      nU(       a#  S[        UR                  5      UR                  S.$ [        S5      e! [        R                   a'    SU  S3n[         R                  U5        [        U5      e[         a  n[         R                  SU 35        e S	nAff = f)
zz
Create HLS playlists for a channel.

Args:
    channel_id (str): UUID of the channel
    
Returns:
    dict: Task result
z Creating playlists for channel: r   T)r   r   r   z Failed to create master playlistr   r   zPlaylist creation task failed: N)r   r   r   r   r   r.  rL   r   r=   r   r   r7   )r   r'   r   r   r?   s        r%   create_playlist_for_channelr@    s    6zlCD//%%%4 *'2!'**o '  >?? #&zl*=	Y	"" 6qc:;s   A)A7 ,A7 7AC:CCc            
      >    [         R                  S5        [        R                  R	                  SS9R                  5       n [        R                  R	                  [        R                  " 5       R                  5       S9R                  5       nSSK	J
n  UR                  R	                  [        R                  " 5       R                  5       S9R                  5       nSSK	Jn  UR                  R	                  [        R                  " 5       R                  5       S	9R                  5       n[        5       nUR                  S
SU UUU[        R                  " 5       R                  5       S.S9  SU UUUS.$ ! [         a  n[         R!                  SU 35        e SnAff = f)z
Send periodic status updates about system operation.

This task provides regular status reports about stream processing
activity and system health.

Returns:
    dict: Status report data
z!Generating periodic status reportr   )r   )started_at__dater   )JingleDetection)detection_time__date)AdBreak)start_time__datesystem_alertperiodic_report)status_typer  sessions_todaydetections_todayad_breaks_todayr   r   T)r   r  rJ  rK  rL  zPeriodic status task failed: N)r   r   r   r   r  r8  r	   r   dateapps.jingles.modelsrC  rE  r   r   r   r   r7   )r  total_sessions_todayrC  rK  rE  rL  r   r?   s           r%   send_periodic_statusrP    sp   -89 (//66h6GMMO,44;;%\\^002  <  

%' 	
 	8*2299!)!4!4!6 : 

%' 	
 	0!//00%\\^002 1 

%' 	
  34..(0#2"6$4#2%\\^557 	/ 
	
 .2 0.
 	
  4QC89s   E1E4 4
F>FF)NN)r   r  )6__doc__r   r   r   loggingshutilr   pathlibr   typingr   r   r   r   celeryr   django.utilsr	   django.confr
   modelsr   r   r   r   r   apps.jingles.servicesr   r   apps.notifications.servicesr   	getLoggerr   boolr&   rL   r@   rQ   rm   r   r   r   r   r   r   r   r   r  r  r%  r.  r>  r@  rP   rR   r%   <module>r_     s   
       , ,  !   ^ ^ A ; 
		3	4:$ ::" "DdO "J'9 d3i 6 '9  d3i  J 2615[[-.[ -.[ 
#Y	[|,m ,c ,4 ,^ $A2>f3 f f^a f ?fV 2615@:@:-.@: -.@: 	@:J 261533-.3 -.3 m	3l 4C 4 4n4= 4T 4n XC X Xv ` `F%7 % %QT %P W    F 5 5p "C " "J 7 7rR   