U
    $P‘fÍ*  ã                   @   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ÚargsZkargsr   r   úB/tmp/pip-unpacked-wheel-7zdooeg3/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   Z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+   Úwr.   r/   r0   Úfragment_countr-   )r
   r2   r   Úwriter4   Údumpsr3   )r   r%   Zfrag_index_streamr9   r-   r   r   r   Ú_write_ytdl_fileV   s     ÿÿzFragmentFD._write_ytdl_fileNc                 C   s†   d|d |d f }||p"|  d¡dœ}|d  ||¡}|s@dS |  d¡rX|  d¡|d	< t|d
ƒ\}}	|	|d< | ¡ }
| ¡  d|
fS )Nz	%s-Frag%dr(   r0   r   )r    r   Údl)FNÚfiletimeÚfragment_filetimeÚrbÚfragment_filename_sanitizedT)r   Údownloadr
   r6   r3   )r   r%   Zfrag_urlr   r!   Zfragment_filenameZfragment_info_dictÚ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 )NZkeep_fragmentsFrD   Údest_stream)	Ú_FragmentFD__do_ytdl_filer?   Úparamsr   ÚosÚremover	   r=   Úflush)r   r%   rG   r   r   r   Ú_append_fragments   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)Z
continuedlÚquietZ
noprogressrR   r   rS   rT   ÚwbÚab)r(   r0   r*   r0   z.ytdl file is corruptz2Inconsistent state of incomplete fragment downloadz$%s. Restarting from the beginning...)r@   rI   r(   Úcomplete_frags_downloaded_bytes)r   r   ZFD_NAMEZreport_destinationr   ZydlrK   Z	temp_namerL   ÚpathÚisfiler	   ÚgetsizeÚupdaterJ   r2   r:   Zreport_warningr?   r1   r
   )r   r%   Ztotal_frags_strrQ   r@   r(   Z	open_modeÚ
resume_lenZ
is_corruptZis_inconsistentÚmessagerI   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 )NrX   rP   Údownloadingr0   r+   r(   )ÚstatusÚdownloaded_bytesr0   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 )Nr`   )r_   ÚfinishedÚelapsedÚtotal_bytesr   r'   rX   r0   r   Ztotal_bytes_estimaterd   ra   rc   ÚetaÚspeed)Útimer   Zcalc_etaÚ_hook_progress)ÚsZtime_nowZfrag_total_bytesZestimated_sizeZfrag_downloaded_bytes©r%   r]   r   ÚstartÚstaterP   r   r   Úfrag_progress_hookÙ   s@    

ÿÿÿ
  
þz;FragmentFD._start_frag_download.<locals>.frag_progress_hookr@   )ri   r\   Zadd_progress_hook)r   r%   ro   r   rl   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 }nv|  	|d |d ¡ | j
 dd¡rÆ| d	¡}|rÆzt |d t ¡ |f¡ W n tk
rÄ   Y nX tj t|d ƒ¡}|  |||d d
|dœ¡ d S )NrI   r+   rb   r(   r)   rX   Z
updatetimeTrB   rd   )ra   rf   r+   r`   re   )r3   rJ   r	   r2   rL   rY   rZ   rM   ri   Z
try_renamerK   r   Úutimer7   r[   rj   )r   r%   r2   re   ra   rA   r   r   r   Ú_finish_frag_downloadü   s0    



ûz FragmentFD._finish_frag_download)N)r   r   r   Ú__doc__r   r   r"   r&   ÚstaticmethodrJ   r:   r?   rH   rO   r#   r$   rq   r   r   r   r   r      s   !


E9r   )Ú
__future__r   r   rL   ri   r4   Úcommonr   Úhttpr   Úutilsr   r	   r
   r   r   r   r   r   r   r   Ú<module>   s   