U
    od                     @   s   d dl Z d dlZd dlZd dlZd dlZ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 Zdd Zed	d
 Zedd Zedd Zdd Zdd ZdS )    N)logger)urlparse)VideoFileClipc                    s    fdd}|S )Nc                     sb   t j  } | |}t j  }td j d|  d|  td j d||   d |S )Nz
Function 'z' called with arguments:  zTime taken by z: z seconds)datetimenowr   debug__name__total_seconds)argskwargs
start_timeresultend_timefunc (/var/www/html/BCA_API/app/utils/media.pywrapper   s    


"zexecute_time.<locals>.wrapperr   )r   r   r   r   r   execute_time   s    r   c                  C   sF   t  } t j| d}t j|r&|S t j| }|| kr<qB|} qd S )Nstatic)osgetcwdpathjoinisdirdirname)Zcurrent_dirZstatic_folder_pathZ
parent_dirr   r   r   find_static_folder_path   s    r   c                 C   s,   t  j}tj d}| d| |  S )Nz%Y_%m_%d_%H_%M_%S_)uuiduuid4hexr   r   strftime)	extension	unique_idcurrent_dater   r   r   generate_unique_filename+   s    
r&   c                 C   sJ   t | }|jd}d|kr&tdt| jdd }t| }||fS )Nzcontent-typevideoz*The provided URL does not point to a video/)	requestsheadheadersget
ValueErrorr   r   splitget_file_duration)urlresponsecontent_typefilenamedurationr   r   r   extract_title_and_duration2   s    
r6   c                 C   s   t | }|j}|  |S )N)r   r5   close)	file_pathclipr5   r   r   r   r0   ?   s    r0   c                 C   s(   t  dddt j| d dd d S )N   secondsz%H:%M:%S.%f)r   	timedeltar"   r;   r   r   r   seconds_to_hhmmssH   s    r?   c           
   
   C   s  g }t | D ]>}|drtt j| |d}|t| W 5 Q R X qt	||| d kgrd| 
ddd  }t|dd d	}t  d| d}t|d
}t|| W 5 Q R X |rt|}	t|d d}||	 W 5 Q R X qq td q |S )Nz.jsonrr:   r   r(   c                 S   s   | d S )NZframe_idr   )xr   r   r   <lambda>]       z$combine_json_files.<locals>.<lambda>)keywz.xmlwbgMbP?)r   listdirendswithopenr   r   appendjsonloadlenr/   sortedr   dump	dicttoxmlwritetimesleep)
Zfolder_pathtotal_framesintervalsave_xmlZ
json_filesr4   file	file_nameZ
final_filexmlr   r   r   combine_json_filesM   s$    

r[   )r   r   rL   rS   r*   r   rQ   logurur   urllib.parser   Zmoviepy.editorr   r   r   r&   r6   r0   r?   r[   r   r   r   r   <module>   s&   


