3
˜b`¶9  ã               @   sx   d dl mZmZ d dlZd dlZd dlZd dlZd dlZddlm	Z	 ddl
mZmZmZmZmZmZ G dd„ deƒZdS )é    )ÚdivisionÚunicode_literalsNé   )Úcompat_os_name)ÚdecodeArgumentÚencodeFilenameÚerror_to_compat_strÚformat_bytesÚshell_quoteÚtimeconvertc               @   s\  e Zd ZdZdZdZdd„ Zedd„ ƒZedd	„ ƒZ	ed
d„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZe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(d)„ Zd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ ZdKd5d6„Zd7d8„ Z d9d:„ Z!d;d<„ Z"d=d>„ Z#d?d@„ Z$dAdB„ Z%dCdD„ Z&dEdF„ Z'dGdH„ Z(dLdIdJ„Z)dS )MÚFileDownloadera6  File Downloader class.

    File downloader objects are the ones responsible of downloading the
    actual video file and writing it to disk.

    File downloaders accept a lot of parameters. In order not to saturate
    the object constructor with arguments, it receives a dictionary of
    options instead.

    Available options:

    verbose:            Print additional info to stdout.
    quiet:              Do not print messages to stdout.
    ratelimit:          Download speed limit, in bytes/sec.
    retries:            Number of times to retry for HTTP error 5xx
    buffersize:         Size of download buffer in bytes.
    noresizebuffer:     Do not automatically resize the download buffer.
    continuedl:         Try to continue downloads if possible.
    noprogress:         Do not print the progress bar.
    logtostderr:        Log messages to stderr instead of stdout.
    consoletitle:       Display progress in console window's titlebar.
    nopart:             Do not use temporary .part files.
    updatetime:         Use the Last-modified header to set output file timestamps.
    test:               Download only first bytes to test the downloader.
    min_filesize:       Skip files smaller than this size
    max_filesize:       Skip files larger than this size
    xattr_set_filesize: Set ytdl.filesize user xattribute with expected size.
    external_downloader_args:  A list of additional command-line arguments for the
                        external downloader.
    hls_use_mpegts:     Use the mpegts container for HLS videos.
    http_chunk_size:    Size of a chunk for chunk-based HTTP downloading. May be
                        useful for bypassing bandwidth throttling imposed by
                        a webserver (experimental)

    Subclasses of this one must re-define the real_download method.
    i(  Nc             C   s"   || _ g | _|| _| j| jƒ dS )z6Create a FileDownloader object with the given options.N)ÚydlÚ_progress_hooksÚparamsÚadd_progress_hookÚreport_progress)Úselfr   r   © r   úHC:\Users\Broad\Desktop\All-Down\binaries\youtube_dl\downloader\common.pyÚ__init__=   s    zFileDownloader.__init__c             C   sN   t | dƒ\}}t |dƒ\}}|dkr(dS |dkr<d||f S d|||f S d S )Né<   éc   z--:--:--r   z	%02d:%02dz%02d:%02d:%02d)Údivmod)ÚsecondsZminsZsecsÚhoursr   r   r   Úformat_secondsD   s    zFileDownloader.format_secondsc             C   s    |d krd S t | ƒt |ƒ d S )Ng      Y@)Úfloat)Úbyte_counterZdata_lenr   r   r   Úcalc_percentO   s    zFileDownloader.calc_percentc             C   s   | d krdS dd|   S )Nz---.-%z%6sz%3.1f%%r   )Úpercentr   r   r   Úformat_percentU   s    zFileDownloader.format_percentc             C   s\   |d krd S |d krt j ƒ }||  }|dks4|dk r8d S t|ƒ| }tt|ƒt|ƒ | ƒS )Nr   gü©ñÒMbP?)Útimer   Úint)ÚstartÚnowÚtotalÚcurrentÚdifÚrater   r   r   Úcalc_eta[   s    zFileDownloader.calc_etac             C   s   | d krdS t j| ƒS )Nz--:--)r   r   )Úetar   r   r   Ú
format_etag   s    zFileDownloader.format_etac             C   s(   ||  }|dks|dk rd S t |ƒ| S )Nr   gü©ñÒMbP?)r   )r#   r$   Úbytesr'   r   r   r   Ú
calc_speedm   s    zFileDownloader.calc_speedc             C   s    | d krdd S ddt | ƒ  S )Nz%10sz---b/sz%s/s)r	   )Úspeedr   r   r   Úformat_speedt   s    zFileDownloader.format_speedc             C   s   | t dƒkrdS d|  S )NÚinfz%.0f)r   )Úretriesr   r   r   Úformat_retriesz   s    zFileDownloader.format_retriesc             C   sb   t |d dƒ}tt |d dƒdƒ}| dk r2t|ƒS ||  }||krJt|ƒS ||k rZt|ƒS t|ƒS )Ng       @g      ð?i  @ gü©ñÒMbP?)ÚmaxÚminr"   )Zelapsed_timer,   Znew_minZnew_maxr(   r   r   r   Úbest_block_size~   s    zFileDownloader.best_block_sizec             C   sN   t jd| ƒ}|dkrdS t|jdƒƒ}ddj|jdƒjƒ ƒ }tt|| ƒƒS )z:Parse a string indicating a byte quantity into an integer.z"(?i)^(\d+(?:\.\d+)?)([kMGTPEZY]?)$Né   g      �@Z	bkmgtpezyr   )ÚreÚmatchr   ÚgroupÚindexÚlowerr"   Úround)ZbytestrZmatchobjÚnumberZ
multiplierr   r   r   Úparse_bytes‹   s    zFileDownloader.parse_bytesc             O   s   | j j||Ž d S )N)r   Ú	to_screen)r   ÚargsÚkargsr   r   r   r?   •   s    zFileDownloader.to_screenc             C   s   | j j|ƒ d S )N)r   r?   )r   Úmessager   r   r   Ú	to_stderr˜   s    zFileDownloader.to_stderrc             C   s   | j j|ƒ d S )N)r   Úto_console_title)r   rB   r   r   r   rD   ›   s    zFileDownloader.to_console_titlec             O   s   | j j||Ž d S )N)r   Útrouble)r   r@   rA   r   r   r   rE   ž   s    zFileDownloader.troublec             O   s   | j j||Ž d S )N)r   Úreport_warning)r   r@   rA   r   r   r   rF   ¡   s    zFileDownloader.report_warningc             O   s   | j j||Ž d S )N)r   Úreport_error)r   r@   rA   r   r   r   rG   ¤   s    zFileDownloader.report_errorc             C   s~   | j jdƒ}|dks|dkr dS |dkr0tjƒ }|| }|dkrDdS t|ƒ| }||krzt|ƒ| | }|dkrztj|ƒ dS )z3Sleep if the download speed is over the rate limit.Ú	ratelimitNr   g        )r   Úgetr!   r   Úsleep)r   Z
start_timer$   r   Z
rate_limitÚelapsedr.   Z
sleep_timer   r   r   Ú	slow_down§   s    zFileDownloader.slow_downc             C   sD   | j jddƒs8|dks8tjjt|ƒƒr<tjjt|ƒƒ r<|S |d S )z4Returns a temporary filename for the given filename.ÚnopartFú-z.part)r   rI   ÚosÚpathÚexistsr   Úisfile)r   Úfilenamer   r   r   Ú	temp_name·   s    "zFileDownloader.temp_namec             C   s    |j dƒr|d tdƒ … S |S )Nz.part)ÚendswithÚlen)r   rS   r   r   r   Úundo_temp_name¾   s    
zFileDownloader.undo_temp_namec             C   s   |d S )Nz.ytdlr   )r   rS   r   r   r   Úytdl_filenameÃ   s    zFileDownloader.ytdl_filenamec             C   sb   y$||krd S t jt|ƒt|ƒƒ W n8 ttfk
r\ } z| jdt|ƒ ƒ W Y d d }~X nX d S )Nzunable to rename file: %s)rO   Úrenamer   ÚIOErrorÚOSErrorrG   r   )r   Zold_filenameZnew_filenameÚerrr   r   r   Ú
try_renameÆ   s    zFileDownloader.try_renamec             C   s‚   |dkrdS t jjt|ƒƒs dS |}|dkr0dS t|ƒ}|dkrD|S |dkrPdS yt j|tjƒ |fƒ W n tk
r|   Y nX |S )z4Try to set the last-modified time of the given file.Nr   )rO   rP   rR   r   r   Úutimer!   Ú	Exception)r   rS   Zlast_modified_hdrZtimestrZfiletimer   r   r   Ú	try_utimeÎ   s"    zFileDownloader.try_utimec             C   s   | j d| ƒ dS )zReport destination filename.z[download] Destination: N)r?   )r   rS   r   r   r   Úreport_destinationã   s    z!FileDownloader.report_destinationFc             C   sž   d| }| j jddƒr"| j|ƒ njtdkrft| ddƒ}|t|ƒkrV|d|t|ƒ  7 }t|ƒ| _d}ntjj	ƒ rtd	nd}| j|| | d
� | j
d| ƒ d S )Nz[download] Úprogress_with_newlineFÚntÚ!_report_progress_prev_line_lengthr   ú úz[K)Zskip_eolzyoutube-dl )r   rI   r?   r   ÚgetattrrV   rd   ÚsysÚstderrÚisattyrD   )r   ÚmsgÚis_last_lineZfullmsgZprev_lenZ
clear_liner   r   r   Ú_report_progress_statusç   s    
z&FileDownloader._report_progress_statusc             C   sJ  |d dkrŠ| j jddƒr&| jdƒ ndd}|jdƒd k	rPt|d ƒ|d< |d	7 }|jd
ƒd k	rx| j|d
 ƒ|d< |d7 }| j|| dd� | j jdƒršd S |d dkrªd S |jdƒd k	rÌ| j|d ƒ|d< nd|d< |jdƒoê|jdƒd k	�r| jd|d  |d  ƒ|d< nd|jdƒ�rJ|jdƒd k	�rJ| jd|d  |d  ƒ|d< n(|jdƒdk�rj| jdƒ|d< nd|d< |jdƒd k	�r–| j|d ƒ|d< nd|d< |jdƒd k	�rÄt|d ƒ|d< d}nt|jdƒd k	�rêt|d ƒ|d< d}nN|jdƒd k	�r4t|d ƒ|d< |jd
ƒ�r.| j|d
 ƒ|d< d }nd!}nd"}| j|| ƒ d S )#NÚstatusÚfinishedÚ
noprogressFz[download] Download completedz100%%Útotal_bytesZ_total_bytes_strz of %(_total_bytes_str)srK   Z_elapsed_strz in %(_elapsed_str)sT)rl   Zdownloadingr*   Z_eta_strzUnknown ETAZdownloaded_byteséd   Z_percent_strZtotal_bytes_estimater   z	Unknown %r.   Z
_speed_strzUnknown speedzK%(_percent_str)s of %(_total_bytes_str)s at %(_speed_str)s ETA %(_eta_str)sZ_total_bytes_estimate_strzU%(_percent_str)s of ~%(_total_bytes_estimate_str)s at %(_speed_str)s ETA %(_eta_str)sZ_downloaded_bytes_strz>%(_downloaded_bytes_str)s at %(_speed_str)s (%(_elapsed_str)s)z+%(_downloaded_bytes_str)s at %(_speed_str)sz5%(_percent_str)s % at %(_speed_str)s ETA %(_eta_str)s)	r   rI   r?   r	   r   rm   r+   r    r/   )r   ÚsZmsg_templater   r   r   r   ø   sV      zFileDownloader.report_progressc             C   s   | j d| ƒ dS )z'Report attempt to resume at given byte.z'[download] Resuming download at byte %sN)r?   )r   Z
resume_lenr   r   r   Úreport_resuming_byte4  s    z#FileDownloader.report_resuming_bytec             C   s"   | j dt|ƒ|| j|ƒf ƒ dS )z&Report retry in case of HTTP error 5xxzD[download] Got server HTTP error: %s. Retrying (attempt %d of %s)...N)r?   r   r2   )r   r\   Úcountr1   r   r   r   Úreport_retry8  s    zFileDownloader.report_retryc             C   s6   y| j d| ƒ W n tk
r0   | j dƒ Y nX dS )z.Report file has already been fully downloaded.z)[download] %s has already been downloadedz/[download] The file has already been downloadedN)r?   ÚUnicodeEncodeError)r   Ú	file_namer   r   r   Úreport_file_already_downloaded>  s    z-FileDownloader.report_file_already_downloadedc             C   s   | j dƒ dS )z,Report it was impossible to resume download.z[download] Unable to resumeN)r?   )r   r   r   r   Úreport_unable_to_resumeE  s    z&FileDownloader.report_unable_to_resumec             C   sô   | j jddƒotjjt|ƒƒ}t|dƒs’| j jddƒoTtjjt|ƒƒoT| j jddƒ }|dkr’|sf|r’| j|ƒ | j	|dtjj
t|ƒƒd	œƒ dS | j jd
ƒ}|rè| j jd|ƒ}tj||ƒ}| jd|jƒ rÒt|ƒnd|  ƒ tj|ƒ | j||ƒS )zpDownload to a filename using the info from info_dict
        Return True on success and False otherwise
        ÚnooverwritesFÚwriteÚ
continuedlTrM   rN   ro   )rS   rn   rq   Úsleep_intervalÚmax_sleep_intervalz![download] Sleeping %s seconds...z%.2f)r   rI   rO   rP   rQ   r   ÚhasattrrR   ry   Ú_hook_progressÚgetsizeÚrandomÚuniformr?   Ú
is_integerr"   r!   rJ   Úreal_download)r   rS   Ú	info_dictZnooverwrites_and_existsZcontinuedl_and_existsZmin_sleep_intervalr   r~   r   r   r   ÚdownloadI  s.    


zFileDownloader.downloadc             C   s   t dƒ‚dS )z.Real download process. Redefine in subclasses.z-This method must be implemented by subclassesN)ÚNotImplementedError)r   rS   r‡   r   r   r   r†   p  s    zFileDownloader.real_downloadc             C   s   x| j D ]}||ƒ qW d S )N)r   )r   rn   Úphr   r   r   r�   t  s    zFileDownloader._hook_progressc             C   s   | j j|ƒ d S )N)r   Úappend)r   rŠ   r   r   r   r   x  s    z FileDownloader.add_progress_hookc             C   sR   | j jddƒsd S dd„ |D ƒ}|d kr8tjj|d ƒ}| jd|t|ƒf ƒ d S )NÚverboseFc             S   s   g | ]}t |ƒ‘qS r   )r   )Ú.0Úar   r   r   ú
<listcomp>�  s    z-FileDownloader._debug_cmd.<locals>.<listcomp>r   z[debug] %s command line: %s)r   rI   rO   rP   Úbasenamer?   r
   )r   r@   ZexeZstr_argsr   r   r   Ú
_debug_cmd}  s    zFileDownloader._debug_cmd)F)N)*Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_TEST_FILE_SIZEr   r   Ústaticmethodr   r   r    r)   r+   r-   r/   r2   r5   r>   r?   rC   rD   rE   rF   rG   rL   rT   rW   rX   r]   r`   ra   rm   r   rt   rv   ry   rz   rˆ   r†   r�   r   r‘   r   r   r   r   r      sL   $

<'r   )Ú
__future__r   r   rO   r7   rh   r!   rƒ   Úcompatr   Úutilsr   r   r   r	   r
   r   Úobjectr   r   r   r   r   Ú<module>   s    
