3
Õb`ô(  ã               @   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   úOC:\Users\Broad\Desktop\BeinConnetFix\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$   | j dt|ƒ||| j|ƒ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   | j 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   |j 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   | j |ƒ | j|ƒ d S )N)Ú_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| j|d ƒdƒ\}}zDy"tj|jƒ ƒd d d |d< W n tk
rd   d|d< Y nX W d |jƒ  X d S )	NÚytdl_corruptÚfilenameÚrÚ
downloaderÚcurrent_fragmentÚindexÚfragment_indexT)ÚAssertionErrorr
   Úytdl_filenameÚjsonÚloadsÚreadÚ	ExceptionÚclose)r   r&   ÚstreamÚ_r   r   r   Ú_read_ytdl_fileL   s    "zFragmentFD._read_ytdl_filec             C   sb   t | j|d ƒdƒ\}}dd|d ii}|jdƒd k	rB|d |d< |jtjd|iƒƒ |jƒ  d S )Nr,   Úwr/   r0   r1   Úfragment_countr.   )r
   r3   r   Úwriter4   Údumpsr8   )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 j |||p,|jdƒdœƒ}|s<dS t|dƒ\}}||d	< |jƒ }	|jƒ  d
|	fS )Nz	%s-Frag%dr)   r1   Údlr   )r!   r   FÚrbÚfragment_filename_sanitizedT)FN)Údownloadr   r
   r6   r8   )
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 j |ƒ |d jƒ  W d | j|ƒr4| j|ƒ | jjddƒsTtjt|d ƒƒ |d= X d S )NÚdest_streamÚkeep_fragmentsFrC   )	r>   ÚflushÚ_FragmentFD__do_ytdl_filer@   Úparamsr   ÚosÚremover	   )r   r&   rF   r   r   r   Ú_append_fragmentp   s    

zFragmentFD._append_fragmentc             C   sÜ  d|krd|d< |d sBd|d  }|j ddƒ}|rF|d| 7 }nd}| jd	| j|f ƒ | j|d
 ƒ t| jddd| jj dƒ| jj ddƒ| jj ddƒ| jj ddƒdœƒ}| j|d
 ƒ}d}d}tj	j
t|ƒƒräd}tj	jt|ƒƒ}|j|ddœƒ | j|ƒ�r¶tj	j
t| j|d
 ƒƒƒ�rš| j|ƒ |j dƒdk}|d dk�oH|dk}	|�sV|	�r¶|�r`dnd}
| jd|
 ƒ d |d< }d|k�rŽ|d= | j|ƒ n| j|ƒ |d dk�s¶t‚t||ƒ\}}|j||||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Ú
noprogressrR   r   rS   rT   ÚwbÚab)r)   r1   r+   r1   z.ytdl file is corruptz2Inconsistent state of incomplete fragment downloadz$%s. Restarting from the beginning...)rA   rH   r)   Úcomplete_frags_downloaded_bytes)r   r   ÚFD_NAMEÚreport_destinationr   ÚydlrL   Ú	temp_namerM   ÚpathÚisfiler	   ÚgetsizeÚupdaterK   r3   r;   Úreport_warningr@   r2   r
   )r   r&   Ztotal_frags_strrQ   rA   r)   Ú	open_modeÚ
resume_lenZ
is_corruptZis_inconsistentÚmessagerH   r   r   r   r$   {   sf    






z!FragmentFD._prepare_frag_downloadc                sn   ˆ d ‰ˆ d ‰dˆˆ d ˆˆ d ˆ d dœ‰t j ƒ ‰ˆ jˆdd	œƒ ‡ ‡‡‡‡‡fd
d„}ˆ d j|ƒ ˆS )NrZ   rP   Údownloadingr1   r,   r)   )ÚstatusÚdownloaded_bytesr1   r=   r,   r)   r   )ÚstartedÚprev_frag_downloaded_bytesc                s:  | d dkrd S t j ƒ }|ˆ ˆd< | jdƒp0d}ˆ d s^ˆ d | ˆd	 d
  ˆ }|ˆd< | d dkr´ˆd	  d
7  < ˆd	 ˆ d	< ˆd  |ˆ d  7  < ˆd ˆ d< dˆ d< nx| d }ˆd  |ˆ d  7  < ˆ d � sþˆjˆ||ˆ ˆd ˆ ƒˆd< | jdƒ�pˆ jdƒˆd< ˆd ˆ d< |ˆ d< ˆjˆƒ d S )Nrh   rg   ÚfinishedÚelapsedÚtotal_bytesr   r(   rZ   r1   r   Útotal_bytes_estimateri   rk   ÚetaÚspeed)rg   rl   )Útimer   Úcalc_etaÚ_hook_progress)ÚsZtime_nowZfrag_total_bytesZestimated_sizeZfrag_downloaded_bytes)r&   re   r   ÚstartÚstaterP   r   r   Úfrag_progress_hookÖ   s0    


z;FragmentFD._start_frag_download.<locals>.frag_progress_hookrA   )rr   rb   Úadd_progress_hook)r   r&   rx   r   )r&   re   r   rv   rw   rP   r   r%   À   s    
zFragmentFD._start_frag_downloadc             C   sª   |d j ƒ  | j|ƒr>t| j|d ƒƒ}tjj|ƒr>tj|ƒ tjƒ |d  }|d dkrd|d }n(| j	|d |d ƒ tjj
t|d ƒƒ}| j|||d d|dœƒ d S )	NrH   r,   rj   r)   r*   rZ   rl   )ri   rn   r,   rh   rm   )r8   rK   r	   r3   rM   r_   r`   rN   rr   Ú
try_renamera   rt   )r   r&   r3   rm   ri   r   r   r   Ú_finish_frag_downloadù   s     


z FragmentFD._finish_frag_download)N)r   r   r   Ú__doc__r   r   r#   r'   ÚstaticmethodrK   r;   r@   rG   rO   r$   r%   r{   r   r   r   r   r      s    

E9r   )Ú
__future__r   r   rM   rr   r4   Úcommonr   Úhttpr   Úutilsr   r	   r
   r   r   r   r   r   r   r   Ú<module>   s   