U
    ¦y^ô(  ã                   @   s|   d dl mZm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mZmZmZ G dd	„ d	e	ƒZG d
d„ deƒZdS )é    )ÚdivisionÚunicode_literalsNé   )ÚFileDownloader)ÚHttpFDé   )Úerror_to_compat_strÚencodeFilenameÚsanitize_openÚsanitized_Requestc                   @   s   e Zd Zdd„ ZdS )ÚHttpQuietDownloaderc                 O   s   d S ©N© )ÚselfÚargsÚkargsr   r   úQC:\Users\TaGoRe\Desktop\beinconnect-dl\binaries\youtube_dl\downloader\fragment.pyÚ	to_screen   s    zHttpQuietDownloader.to_screenN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @   sv   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zed
d„ ƒZ	dd„ Z
dd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
FragmentFDa^  
    A base file downloader class for fragmented media (e.g. f4m/m3u8 manifests).

    Available options:

    fragment_retries:   Number of times to retry a fragment for HTTP error (DASH
                        and hlsnative only)
    skip_unavailable_fragments:
                        Skip unavailable fragments (DASH and hlsnative only)
    keep_fragments:     Keep downloaded fragments on disk after downloading is
                        finished

    For each incomplete fragment download youtube-dl keeps on disk a special
    bookkeeping file with download state and metadata (in future such files will
    be used for any incomplete download handled by youtube-dl). This file is
    used to properly handle resuming, check download file consistency and detect
    potential errors. The file has a .ytdl extension and represents a standard
    JSON file of the following format:

    extractor:
        Dictionary of extractor related data. TBD.

    downloader:
        Dictionary of downloader related data. May contain following data:
            current_fragment:
                Dictionary with current (being downloaded) fragment data:
                index:  0-based index of current fragment among all fragments
            fragment_count:
                Total count of fragments

    This feature is experimental and file format may change in future.
    c              	   C   s$   |   dt|ƒ|||  |¡f ¡ d S )NzP[download] Got server HTTP error: %s. Retrying fragment %d (attempt %d of %s)...)r   r   Úformat_retries)r   ÚerrÚ
frag_indexÚcountÚretriesr   r   r   Úreport_retry_fragment8   s
    ÿÿz FragmentFD.report_retry_fragmentc                 C   s   |   d| ¡ d S )Nz"[download] Skipping fragment %d...)r   )r   r   r   r   r   Úreport_skip_fragment=   s    zFragmentFD.report_skip_fragmentc                 C   s   |  d¡}|rt|d |ƒS |S )NÚhttp_headers)Úgetr   )r   Ú	info_dictÚurlÚheadersr   r   r   Ú_prepare_url@   s    
zFragmentFD._prepare_urlc                 C   s   |   |¡ |  |¡ d S r   )Ú_prepare_frag_downloadÚ_start_frag_download)r   Úctxr   r   r   Ú _prepare_and_start_frag_downloadD   s    
z+FragmentFD._prepare_and_start_frag_downloadc                 C   s   | d  o| d dk S )NÚliveÚtmpfilenameú-r   )r'   r   r   r   Z__do_ytdl_fileH   s    zFragmentFD.__do_ytdl_filec                 C   sx   d|kst ‚t|  |d ¡dƒ\}}zDz"t | ¡ ¡d d d |d< W n tk
rd   d|d< Y nX W 5 | ¡  X d S )	NÚytdl_corruptÚfilenameÚrÚ
downloaderÚcurrent_fragmentÚindexÚfragment_indexT)ÚAssertionErrorr
   Úytdl_filenameÚcloseÚjsonÚloadsÚreadÚ	Exception)r   r'   ÚstreamÚ_r   r   r   Ú_read_ytdl_fileL   s    "zFragmentFD._read_ytdl_filec                 C   sb   t |  |d ¡dƒ\}}dd|d ii}| d¡d k	rB|d |d< | t d|i¡¡ | ¡  d S )Nr-   Úwr0   r1   r2   Úfragment_countr/   )r
   r4   r    Úwriter6   Údumpsr5   )r   r'   Zfrag_index_streamr;   r/   r   r   r   Ú_write_ytdl_fileV   s     ÿÿzFragmentFD._write_ytdl_fileNc           
      C   sj   d|d |d f }|d   |||p,| d¡dœ¡}|s<dS t|dƒ\}}||d	< | ¡ }	| ¡  d
|	fS )Nz	%s-Frag%dr*   r2   Údlr   )r"   r   )FNÚrbÚfragment_filename_sanitizedT)Údownloadr    r
   r8   r5   )
r   r'   Zfrag_urlr!   r#   Zfragment_filenameÚsuccessZdownZfrag_sanitizedÚfrag_contentr   r   r   Ú_download_fragmentb   s    
þzFragmentFD._download_fragmentc                 C   s`   z|d  |¡ |d  ¡  W 5 |   |¡r4|  |¡ | j dd¡sTt t|d ƒ¡ |d= X d S )NÚkeep_fragmentsFrD   Údest_stream)	Ú_FragmentFD__do_ytdl_filerA   Úparamsr    ÚosÚremover	   r?   Úflush)r   r'   rG   r   r   r   Ú_append_fragmentp   s    

zFragmentFD._append_fragmentc                 C   sÜ  d|krd|d< |d sBd|d  }|  dd¡}|rF|d| 7 }nd}|  d	| j|f ¡ |  |d
 ¡ t| jddd| j  d¡| j  dd¡| j  dd¡| j  dd¡dœƒ}|  |d
 ¡}d}d}tj	 
t|ƒ¡räd}tj	 t|ƒ¡}| |ddœ¡ |  |¡r¶tj	 
t|  |d
 ¡ƒ¡rš|  |¡ |  d¡dk}|d dkoH|dk}	|sV|	r¶|r`dnd}
|  d|
 ¡ d |d< }d|krŽ|d= |  |¡ n|  |¡ |d dks¶t‚t||ƒ\}}| ||||dœ¡ d S )Nr)   Fz%dÚtotal_fragsÚad_fragsr   z (not including %d ad)zunknown (live)z[%s] Total fragments: %sr-   TÚ	ratelimitr   ÚnopartÚtest)Ú
continuedlÚquietÚ
noprogressrS   r   rT   rU   ÚwbÚab)r*   r2   r,   r2   z.ytdl file is corruptz2Inconsistent state of incomplete fragment downloadz$%s. Restarting from the beginning...)rB   rJ   r*   Úcomplete_frags_downloaded_bytes)r    r   ÚFD_NAMEÚreport_destinationr   ÚydlrL   Ú	temp_namerM   ÚpathÚisfiler	   ÚgetsizeÚupdaterK   r4   r<   Úreport_warningrA   r3   r
   )r   r'   Ztotal_frags_strrR   rB   r*   Ú	open_modeÚ
resume_lenZ
is_corruptZis_inconsistentÚmessagerJ   r   r   r   r%   {   st    ÿ
ùþþ

þÿ

ûz!FragmentFD._prepare_frag_downloadc                    sn   ˆ d ‰ˆ d ‰dˆˆ d ˆˆ d ˆ d dœ‰t   ¡ ‰ˆ  ˆdd	œ¡ ‡ ‡‡‡‡‡fd
d„}ˆ d  |¡ ˆS )Nr[   rQ   Údownloadingr2   r-   r*   )ÚstatusÚdownloaded_bytesr2   r>   r-   r*   r   )ÚstartedÚprev_frag_downloaded_bytesc                    s8  | d dkrd S t   ¡ }|ˆ ˆd< |  d¡p0d}ˆ d s^ˆ d | ˆd d	  ˆ }|ˆd
< | d dkr´ˆd  d	7  < ˆd ˆ d< ˆd  |ˆ d  7  < ˆd ˆ d< dˆ d< nv| d }ˆd  |ˆ d  7  < ˆ d süˆ ˆ||ˆ ˆd ˆ ¡ˆd< |  d¡pˆ  d¡ˆd< ˆd ˆ d< |ˆ d< ˆ ˆ¡ d S )Nri   )rh   ÚfinishedÚelapsedÚtotal_bytesr   r)   r[   r2   r   Útotal_bytes_estimaterm   rj   rl   ÚetaÚspeed)Útimer    Úcalc_etaÚ_hook_progress)ÚsZtime_nowZfrag_total_bytesZestimated_sizeZfrag_downloaded_bytes©r'   rf   r   ÚstartÚstaterQ   r   r   Úfrag_progress_hookÖ   s@    

ÿÿÿ
  
þz;FragmentFD._start_frag_download.<locals>.frag_progress_hookrB   )rs   rc   Úadd_progress_hook)r   r'   rz   r   rw   r   r&   À   s"    ú	üzFragmentFD._start_frag_downloadc                 C   sª   |d   ¡  |  |¡r>t|  |d ¡ƒ}tj |¡r>t |¡ t ¡ |d  }|d dkrd|d }n(|  	|d |d ¡ tj 
t|d ƒ¡}|  |||d d|dœ¡ d S )	NrJ   r-   rk   r*   r+   r[   rm   )rj   ro   r-   ri   rn   )r5   rK   r	   r4   rM   r`   ra   rN   rs   Ú
try_renamerb   ru   )r   r'   r4   rn   rj   r   r   r   Ú_finish_frag_downloadù   s"    


ûz FragmentFD._finish_frag_download)N)r   r   r   Ú__doc__r   r   r$   r(   ÚstaticmethodrK   r<   rA   rH   rP   r%   r&   r}   r   r   r   r   r      s   !


E9r   )Ú
__future__r   r   rM   rs   r6   Úcommonr   Úhttpr   Úutilsr   r	   r
   r   r   r   r   r   r   r   Ú<module>   s   