U
    Gdd                     @   s   d dl m Z  d dlZd dlZd dlZd dlZd dlZeeZd dl	m
Z
 d dlmZ d dlmZ d dlZdZdd Zd	d
 Zdd Zdd Zdd Zdd Zdd Zdd ZdS )    )	unidecodeN)Pool)partial)BeautifulSoupzstatic/subtitles/c                 C   s"   t | } tdd| } d|  d S )Nz[^0-9a-zA-Z._-]+ z^[0-9a-zA-Z._-]+$)r   resub)filename r   +/var/www/html/transcript/interface/utils.pyfilename_to_regex   s    r   c                 C   sX   t j| \}}ttj dd dddddd}t	 j
 d| | S )N.r    _:-)ospathsplitextstrdatetimenowsplitreplaceuuiduuid4hex)r
   Zfilecharextr   r   r   r   generate_name   s    0r   c              	   C   sV   t |t| d:}t| tkr0||   n|  D ]}|| q8W 5 Q R X d S )Nzwb+)openr   typewriteencodechunks)filer   r
   destinationchunkr   r   r   handle_uploaded_file    s
    r(   c                 C   sZ   |  d}g }tt|D ]0}|dkr<|| ||d  kr<q|||  qd|}|S )N
r      )r   rangelenappendjoin)Zhtml_contentlinesZdeduplicated_linesiZdeduplicated_htmlr   r   r   remove_duplicate_lines/   s    

r1   c                 C   s   t |ddd}|d |d t| D ]\}}|dd }|| d|d	d
 d|d t| k r| |d  dd d	d
n| d d	d
  |d|dd  d q,W 5 Q R X dS )zConvert a list of subtitle strings to a Webvtt (.vtt) file.

    Args:
        subtitles (list): A list of subtitle strings in the format
            "00:00:00.000 subtitle text".
        file_path (str): The file path to write the SubRip file to.
    wutf-8encodingzWEBVTT

r   N   r)    .r    --> r*   r      

)r    r"   	enumerater   r,   r   )lst	file_pathfr0   item
start_timer   r   r   list_to_vtt9   s    

^rA   c                 C   s   t |ddd}t| D ]~\}}|dd dd}|d t| k r`| |d  dd ddn
| d	 }|| d
| d| d
|dd  d qW 5 Q R X dS )zConvert a list of subtitle strings to a SubRip (.srt) file.

    Args:
        subtitles (list): A list of subtitle strings in the format
            "00:00:00.000 subtitle text".
        file_path (str): The file path to write the SubRip file to.
    r2   r3   r4   Nr6   r7   ,r*   r   r)   r8   r9   r:   )r    r;   r   r,   r   r"   )r<   r=   r>   r0   r?   r@   Znext_start_timer   r   r   list_to_srtI   s
    8rC   c           	   	   C   s   t jtdt jt j|d  d}t jtdt jt j|d  d| d}| jd| }|dk	r|d	krt| t	|d
}|
d}dd |D }t|| t|| ntd dS )zS
    Process a single caption file, converting it to both SRT and VTT formats.
    srtr   z.srtvttr   z.vttZ	file_srt_Nr   zhtml.parserpc                 S   s   g | ]}|j  qS r   )textstrip).0rF   r   r   r   
<listcomp>j   s     z#process_caption.<locals>.<listcomp>zHTML file is None.)r   r   r.   STATIC_FILESr   basenamedatagetr1   r   Zfind_allrC   rA   print)	requestkeycaptionZsrt_pathvtt_pathZ	html_fileZsoupZp_tagsZ	text_listr   r   r   process_captionW   s&        "


rT   c                 C   sp   | j rt| j nd| jr"t| jnd| jr4t| jnd| jrFt| jndd}| D ]\}}t||| qVdS )zF
    Download all caption files for a video instance in parallel.
    N)fresenpo)path_srt_frr   path_srt_espath_srt_enpath_srt_poitemsrT   )instancerP   Z	srt_filesrQ   rR   r   r   r   download_captionsr   s    r_   )r   r   r   r   r   logging	getLogger__file__loggermultiprocessingr   	functoolsr   Zbs4r   rK   r   r   r(   r1   rA   rC   rT   r_   r   r   r   r   <module>   s&   
	
