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   MC:\Users\Broad\Desktop\BeinConnetFix\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)ZsecondsZminsZsecsZ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   gMbP?)timer   int)startnowtotalZ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   gMbP?)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  @ gMbP?)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   r?   r   r   r   rA      s    zFileDownloader.to_console_titlec             O   s   | j j|| d S )N)r   trouble)r   r=   r>   r   r   r   rB      s    zFileDownloader.troublec             O   s   | j j|| d S )N)r   report_warning)r   r=   r>   r   r   r   rC      s    zFileDownloader.report_warningc             O   s   | j j|| d S )N)r   report_error)r   r=   r>   r   r   r   rD      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   rF   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   rP   r   r   r   undo_temp_name   s    
zFileDownloader.undo_temp_namec             C   s   |d S )Nz.ytdlr   )r   rP   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)rL   renamer   IOErrorOSErrorrD   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   )rL   rM   rO   r   r   utimer   	Exception)r   rP   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   rP   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   rF   r<   r   getattrrS   ra   sysstderrisattyrA   )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rd S |d dkrd 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	rt|d |d< d}nt|jdd k	rt|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)srH   Z_elapsed_strz in %(_elapsed_str)sT)ri   Zdownloadingr'   Z_eta_strzUnknown ETAZdownloaded_bytesd   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   rF   r<   r	   r   rj   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   r/   )r   rY   countr.   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 rt|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
continuedlTrJ   rK   rl   )rP   rk   rn   sleep_intervalmax_sleep_intervalz![download] Sleeping %s seconds...z%.2f)r   rF   rL   rM   rN   r   hasattrrO   rv   _hook_progressgetsizerandomuniformr<   
is_integerr    r   rG   real_download)r   rP   	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   rP   r   r   r   r   r   p  s    zFileDownloader.real_downloadc             C   s   x| j D ]}|| qW d S )N)r   )r   rk   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   rF   rL   rM   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,   r/   r2   r;   r<   r@   rA   rB   rC   rD   rI   rQ   rT   rU   rZ   r]   r^   rj   r   rq   rs   rv   rw   r   r   r~   r   r   r   r   r   r   r      sL   $

<'r   )
__future__r   r   rL   r4   re   r   r   compatr   utilsr   r   r   r	   r
   r   objectr   r   r   r   r   <module>   s    
