a
    ;hN                     @   s   d dl Z d dlZd dl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	m
Z
mZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZ G dd dZ dS )    N   )BreaklineStatusPrinterMultilineLoggerMultilinePrinterQuietMultilinePrinter)IDENTITY
NO_DEFAULTLockingUnsupportedError	NamespaceRetryManagerclasspropertydeprecation_warningformat_bytesjoin_nonemptyparse_bytesremove_startsanitize_openshell_quotetimeconverttimetuple_from_msectry_call)_ProgressStatec                	   @   s  e Zd ZdZdZdZdd Zdd Zdd	 ZeZ	e
d
d Zedd Zedd Zedd Zedd Zeeef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+d,d-Zed.d/d+d0d1 Zed2d3d4 Z ed5d6d7 Z!d8d9 Z"d:d; Z#dad=d>Z$d?d@ Z%e&dAdAdBdCdDdEdEdFZ'dGdH Z(dIdJ Z)dKdL Z*dMdN Z+ed/fdOdPZ,dQdR Z-edSdT Z.dbdUdVZ/dWdX Z0dYdZ Z1d[d\ Z2dcd]d^Z3d_d` Z4dS )dFileDownloaderaN	  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.
    throttledratelimit: Assume the download is being throttled below this speed (bytes/sec)
    retries:            Number of times to retry for expected network errors.
                        Default is 0 for API, but 10 for CLI
    file_access_retries:   Number of times to retry on file access error (default: 3)
    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.
    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.
    progress_delta:     The minimum time between progress output, in seconds
    external_downloader_args:  A dictionary of downloader keys (in lower case)
                        and a list of additional command-line arguments for the
                        executable. Use 'default' as the name for arguments to be
                        passed to all downloaders. For compatibility with youtube-dl,
                        a single list of args can also be used
    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)
    progress_template:  See YoutubeDL.py
    retry_sleep_functions: See YoutubeDL.py

    Subclasses of this one must re-define the real_download method.
    i(  Nc                 C   sN   |  | g | _|| _|   | | j | jdrJt | _	t
 | _dS )z6Create a FileDownloader object with the given options.progress_deltaN)_set_ydl_progress_hooksparams_prepare_multiline_statusadd_progress_hookreport_progressget	threadingLock_progress_delta_locktime	monotonic_progress_delta_time)selfydlr    r)   ]/var/www/html/swiplay.fr/scambot/venv/lib/python3.9/site-packages/yt_dlp/downloader/common.py__init__U   s    

zFileDownloader.__init__c                 C   s0   || _ dD ] }t| |s
t| |t|| q
d S )N)	r   Zdeprecated_featurereport_errorreport_file_already_downloadedreport_warningto_console_titleZ	to_stderrZtroublewrite_debug)r(   hasattrsetattrgetattr)r'   r(   funcr)   r)   r*   r   `   s    
zFileDownloader._set_ydlc                 O   s"   | j j|d| jdi| d S )Nquiet)r(   	to_screenr   r    )r'   argsZkargsr)   r)   r*   r6   q   s    zFileDownloader.to_screenc                 C   s   t dd| jd d  S )Nz(?<=[a-z])(?=[A-Z])_)resub__name__lower)clsr)   r)   r*   FD_NAMEv   s    zFileDownloader.FD_NAMEc                 C   s6   | d u rdS t | d }|jdkr&dS d|d d  S )Nz Unknowni  c   z--:--:--z%02d:%02d:%02d)r   hours)secondsr$   r)   r)   r*   format_secondsz   s    
zFileDownloader.format_secondsc                 C   s   t | |ddS )Nz00:z>8s)r   rD   )r>   rC   r)   r)   r*   
format_eta   s    zFileDownloader.format_etac                 C   s    |d u rd S t | t | d S )N      Y@float)byte_counterZdata_lenr)   r)   r*   calc_percent   s    zFileDownloader.calc_percentc                 C   s   | d u rdS | ddS )Nz  N/A%z>5.1f%r)   )percentr)   r)   r*   format_percent   s    zFileDownloader.format_percentc           	      C   s   |t u r2|| }}d ||fv r"d S tt|| S || }}|d u rHd S |d u rXt }| |||}|ott|t| | S N)r   intrH   r$   
calc_speed)	r>   Zstart_or_rateZnow_or_remainingtotalcurrentrate	remainingstartnowr)   r)   r*   calc_eta   s    

zFileDownloader.calc_etac                 C   s(   ||  }|dks|dk rd S t || S )Nr   MbP?rG   )rU   rV   bytesZdifr)   r)   r*   rP      s    zFileDownloader.calc_speedc                 C   s   | d u rdS t | ddS )Nz Unknown B/s>10sz/s)r   )speedr)   r)   r*   format_speed   s    zFileDownloader.format_speedc                 C   s   | t dkrdS t| S )Ninf)rH   rO   )retriesr)   r)   r*   format_retries   s    zFileDownloader.format_retriesc                 C   s   t j| rt j| S dS )Nr   )ospathisfilegetsize)Zunencoded_filenamer)   r)   r*   filesize_or_none   s    zFileDownloader.filesize_or_nonec                 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  @ rX   )maxminrO   )Zelapsed_timerY   Znew_minZnew_maxrS   r)   r)   r*   best_block_size   s    zFileDownloader.best_block_sizec                 C   s   t d t| S )z:Parse a string indicating a byte quantity into an integer.zvyt_dlp.FileDownloader.parse_bytes is deprecated and may be removed in the future. Use yt_dlp.utils.parse_bytes instead)r   r   )Zbytestrr)   r)   r*   r      s    zFileDownloader.parse_bytesc                 C   s~   | j d}|du s|dkr dS |du r0t }|| }|dkrDdS t|| }||krzt|| | }|dkrzt| dS )z3Sleep if the download speed is over the rate limit.Z	ratelimitNr   g        )r   r    r$   rH   sleep)r'   
start_timerV   rI   Z
rate_limitelapsedr[   Z
sleep_timer)   r)   r*   	slow_down   s    zFileDownloader.slow_downc                 C   s:   | j dds.|dks.tj|r2tj|s2|S |d S )z4Returns a temporary filename for the given filename.nopartF-.part)r   r    r`   ra   existsrb   r'   filenamer)   r)   r*   	temp_name   s    

zFileDownloader.temp_namec                 C   s    | dr|d td  S |S )Nrn   )endswithlenrp   r)   r)   r*   undo_temp_name   s    
zFileDownloader.undo_temp_namec                 C   s   |d S )Nz.ytdlr)   rp   r)   r)   r*   ytdl_filename   s    zFileDownloader.ytdl_filenameF)fatalc                   s(    fddfdd}t t j|S )Nc                   sF   t j| || j fddr"d n fdd jdi ddS )Nc                    s    t dd  d|  fS )Ng{Gz?z[download] Unable to  file: )r$   rh   r6   eactionfdr)   r*   <lambda>       zIFileDownloader.wrap_file_access.<locals>.error_callback.<locals>.<lambda>c                    s    d  d|  S )Nz
Unable to rx   r,   ry   r{   r)   r*   r~      r   retry_sleep_functionsZfile_access)infowarnerror
sleep_func)r   report_retry_FileDownloader__to_screenr   r    )errcountr^   r}   )r|   rw   r}   r*   error_callback   s    
z7FileDownloader.wrap_file_access.<locals>.error_callbackc                    s   t | jdd | dD ]x}z|| g|R i |W   S  ty } z>|jtjtjfv rl||_W Y d }~q||dd W Y d }~qd }~0 0 qd S )NZfile_access_retries   r      r   )	r   r   r    OSErrorerrnoEACCESEINVALr   r   )r'   r4   r7   kwargsretryr   )r   r)   r*   wrapper   s    z0FileDownloader.wrap_file_access.<locals>.wrapper)	functoolspartialpartialmethod)r|   rw   r   r)   )r|   r   rw   r*   wrap_file_access   s    
zFileDownloader.wrap_file_accessopenTc                 C   s8   t ||\}}t|dd s0| jtj ddd ||fS )Nlockedz. Proceeding without lockingT)Z	only_once)r   r3   r0   r	   msg)r'   rq   Z	open_modefr)   r)   r*   r      s    zFileDownloader.sanitize_openremovec                 C   s   t j|rt | d S rN   )r`   ra   rb   r   rp   r)   r)   r*   
try_remove  s    zFileDownloader.try_removerenamec                 C   s   ||krd S t || d S rN   )r`   replace)r'   Zold_filenameZnew_filenamer)   r)   r*   
try_rename	  s    zFileDownloader.try_renamec                 C   s   |du rdS t j|sdS |}|du r,dS t|}|du r@|S |dkrLdS tt$ t |t |f W d   n1 s0    Y  |S )z4Try to set the last-modified time of the given file.Nr   )	r`   ra   rb   r   
contextlibsuppress	Exceptionutimer$   )r'   rq   Zlast_modified_hdrZtimestrZfiletimer)   r)   r*   	try_utime  s    2zFileDownloader.try_utimec                 C   s   |  d|  dS )zReport destination filename.z[download] Destination: Nr6   rp   r)   r)   r*   report_destination"  s    z!FileDownloader.report_destinationr   c                 C   s   | j drt | _nb| jj dr:t| jj d || _n>| j drZt| jjj|| _nt	| jjj|| j d | _| jj
jo| jj
jdk| j_| jj
j| j_d S )N
noprogressloggerZprogress_with_newliner5   no_color)r   r    r   
_multiliner(   r   r   Z
_out_filesoutr   Z_allow_colorsallow_colorsZ_HAVE_FULLCAP)r'   linesr)   r)   r*   r   &  s    
z(FileDownloader._prepare_multiline_statusc                 C   s   | j   d S rN   )r   endr'   r)   r)   r*   _finish_multiline_status2  s    z'FileDownloader._finish_multiline_statusz
light blueZyellowZgreenz
bold white )downloaded_bytesrL   etar[   rj   total_bytestotal_bytes_estimatec                 C   s   | j jD ]2\}}d| d}||vr&q| || |||< q|| |d< | }|d |d |d}| jdi }| j| j	
|dpd||d	pd
 | | j	
|dpd|t||d d S )Nr8   _strZ_default_template	info_dict)r   progressprogress_templatedownloadz)[download] %(progress._default_template)sZprogress_idxr   zdownload-titlez%yt-dlp %(progress._default_template)s_percent)ProgressStylesZitems__format_progresscopypopr   r    r   Zprint_at_liner(   Zevaluate_outtmplr/   r   	from_dict)r'   sZdefault_templatenamestyleZprogress_dictr   r)   r)   r*   _report_progress_status?  s,    

z&FileDownloader._report_progress_statusc                 O   s$   | j j| jj| jjg|R i |S rN   )r(   _format_textr   streamr   )r'   r7   r   r)   r)   r*   r   S  s    zFileDownloader._format_progressc                    s  dd fdd
} fdd} d dkr| j d	r@| d
 t fdd} || | |d|  dd| dd | 	 t
d|d|d|ddd  d dkrd S | j d }r$| j< t | jk rW d    d S |  j|7  _W d    n1 s0    Y  t fdd fdd fdd} |  d |  d|| ||d|d|d|  dd  |d!d"d#d$d%d}||d&d'7 }| 	 | d S )(Nr   )defaultc                    s2   |D ](^ }}t  fdd|D r|  S q| S )Nc                 3   s   | ]}  |d uV  qd S rN   )r    ).0r   r   r)   r*   	<genexpr>Z  r   zFFileDownloader.report_progress.<locals>.with_fields.<locals>.<genexpr>)all)r   Ztupsfieldstmplr   r)   r*   with_fieldsX  s    
z3FileDownloader.report_progress.<locals>.with_fieldsc                    s   t  | dS )NrZ   )r   r    )kr   r)   r*   r~   ^  r   z0FileDownloader.report_progress.<locals>.<lambda>statusfinishedr   z[download] Download completedc                      s    d  d  S )Nr   rj   r)   r)   r   r)   r*   r~   c  r   r   rj   rF   d   )r[   
_speed_str_total_bytes_str_elapsed_strr   _percent_strz100%%)r   zof %(_total_bytes_str)s)rj   zin %(_elapsed_str)s)r[   zat %(_speed_str)s )delimZdownloadingr   c                      s   d d   d  S )Nr   r   r   r)   r)   r   r)   r*   r~   }  r   c                      s   d d   d  S )Nr   r   r   r)   r)   r   r)   r*   r~   ~  r   c                      s    d dkodS )Nr   r   r)   r)   r   r)   r*   r~     r   r   r[   r   r   )Z_eta_strr   r   r   r   Z_total_bytes_estimate_strZ_downloaded_bytes_strr   )r   zK%(_percent_str)s of %(_total_bytes_str)s at %(_speed_str)s ETA %(_eta_str)s)r   zU%(_percent_str)s of ~%(_total_bytes_estimate_str)s at %(_speed_str)s ETA %(_eta_str)s)r   rj   z>%(_downloaded_bytes_str)s at %(_speed_str)s (%(_elapsed_str)s))r   z+%(_downloaded_bytes_str)s at %(_speed_str)sz3%(_percent_str)s at %(_speed_str)s ETA %(_eta_str)s)fragment_indexZfragment_countz- (frag %(fragment_index)s/%(fragment_count)s))r   z (frag %(fragment_index)s))r   r    r6   r   updater\   striprD   rM   r   r   r#   r$   r%   r&   rE   )r'   r   r   Z_format_bytesr[   Zupdate_deltar   Zmsg_templater)   r   r*   r   W  sn    
.


zFileDownloader.report_progressc                 C   s   |  d|  dS )z'Report attempt to resume at given byte.z%[download] Resuming download at byte Nr   )r'   Z
resume_lenr)   r)   r*   report_resuming_byte  s    z#FileDownloader.report_resuming_bytec                    s|   |t u rdnd}tj||| j fdd|s0tn
 fdd jdi |pNd|rpd|du rbd	nd
|  ndd dS )zReport retryFfragmentc                    s     d|  S )Nz[download] Got error: )r   )r   r   r)   r*   r~     r   z-FileDownloader.report_retry.<locals>.<lambda>c                    s     d|  S )Nz[download] Got error: r   ry   r   r)   r*   r~     r   r   httpNr   r   )r   r   r   r   suffix)r   r   r   r   r   r   r    )r'   r   r   r^   Z
frag_indexrw   Zis_fragr)   r   r*   r     s    

"zFileDownloader.report_retryc                 C   s   |  d dS )z,Report it was impossible to resume download.z[download] Unable to resumeNr   r   r)   r)   r*   report_unable_to_resume  s    z&FileDownloader.report_unable_to_resumec                 C   s   dS )zp Whether the downloader can download the fragments from the manifest.
        Redefine in subclasses if needed. Nr)   )manifestr)   r)   r*   supports_manifest  s    z FileDownloader.supports_manifestc           	      C   s  | j dd otj|}t|ds| j ddoNtj|oN| j dd }|dkr|s`|r| | | |dtj	|d	| | 
  d
S |r| j dpd}n(| j dpd}t|| j dp|}|dkr| d|dd t| | ||}| 
  |dfS )zpDownload to a filename using the info from info_dict
        Return True on success and False otherwise
        Z
overwritesTwriteZ
continuedlrl   Frm   r   )rq   r   r   )TFZsleep_interval_subtitlesr   sleep_intervalZmax_sleep_intervalz[download] Sleeping z.2fz seconds ...)r   r    r`   ra   ro   r1   rb   r-   _hook_progressrc   r   randomuniformr6   r$   rh   real_download)	r'   rq   r   ZsubtitleZnooverwrites_and_existsZcontinuedl_and_existsr   Zmin_sleep_intervalretr)   r)   r*   r     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'   rq   r   r)   r)   r*   r     s    zFileDownloader.real_downloadc                 C   s    ||d< | j D ]}|| qd S )Nr   )r   )r'   r   r   phr)   r)   r*   r     s    
zFileDownloader._hook_progressc                 C   s   | j | d S rN   )r   append)r'   r   r)   r)   r*   r     s    z FileDownloader.add_progress_hookc                 C   sF   | j ddsd S |d u r*tj|d }| | dt|  d S )NverboseFr   z command line: )r   r    r`   ra   basenamer0   r   )r'   r7   Zexer)   r)   r*   
_debug_cmd  s
    zFileDownloader._debug_cmdc                 C   sH   | d}|d u rd S | j|\}}|r.|S |rD| | j| d S )Nimpersonate)r    r(   Z_parse_impersonate_targetsr.   Z_unavailable_targets_message)r'   r   r   Zavailable_targetZrequested_targetsr)   r)   r*   _get_impersonate_target  s    
z&FileDownloader._get_impersonate_target)r   )F)N)5r<   
__module____qualname____doc__Z_TEST_FILE_SIZEr   r+   r   r6   r   r   r?   staticmethodrD   classmethodrE   rJ   rM   r   rW   rP   r\   r_   rd   rg   r   rk   rr   ru   rv   r   r   r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   r   %   s   ,
















@


)	
	r   )!r   r   r   r`   r   r:   r!   r$   Z
minicursesr   r   r   r   utilsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   Zutils._utilsr   r   r)   r)   r)   r*   <module>   s   H