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   JC:\Users\Broad\Desktop\All-Down\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|rd}tj	jt|}|j|dd | j|rt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st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   