o
    iP                     @   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FileDownloadera 	  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
    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   sR   |  | g | _|| _|   | | j | jdr't | _	t
 | _dS 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)   L/home/ubuntu/.local/lib/python3.10/site-packages/yt_dlp/downloader/common.py__init__T   s   

zFileDownloader.__init__c                 C   s0   || _ dD ]}t| |st| |t|| qd S )N)	r   deprecated_featurereport_errorreport_file_already_downloadedreport_warningto_console_title	to_stderr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'   argskargsr)   r)   r*   r9   p   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_NAMEu      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_secondsy   s   
zFileDownloader.format_secondsc                 C   s   t | |ddS )Nz00:z>8s)r   rI   )rB   rH   r)   r)   r*   
format_eta      zFileDownloader.format_etac                 C   s    |d u rd S t | t | d S )N      Y@float)byte_counter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      zFileDownloader.format_percentc           	      C   s   |t u r||}}d ||fv rd S tt|| S ||}}|d u r$d S |d u r,t }| |||}|o@tt|t| | S N)r   intrN   r$   
calc_speed)	rB   start_or_rate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?rM   )r_   r`   bytesdifr)   r)   r*   rX      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   rD   zFileDownloader.format_speedc                 C   s   | t dkrdS t| S )Ninf)rN   rW   )retriesr)   r)   r*   format_retries   rU   zFileDownloader.format_retriesc                 C   s   t j| rt j| S dS )Nr   )ospathisfilegetsize)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 rt|S ||  }||kr%t|S ||k r-t|S t|S )Ng       @g      ?i  @ rb   )maxminrW   )elapsed_timerc   new_minnew_maxr]   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   )bytestrr)   r)   r*   r      s   zFileDownloader.parse_bytesc                 C   s   | j d}|du s|dkrdS |du rt }|| }|dkr"dS t|| }||kr?t|| | }|dkrAt| dS dS dS )z3Sleep if the download speed is over the rate limit.	ratelimitNr   g        )r   r    r$   rN   sleep)r'   
start_timer`   rO   
rate_limitelapsedrf   
sleep_timer)   r)   r*   	slow_down   s   zFileDownloader.slow_downc                 C   s:   | j dds|dkstj|rtj|s|S |d S )z4Returns a temporary filename for the given filename.nopartF-.part)r   r    rk   rl   existsrm   r'   filenamer)   r)   r*   	temp_name   s   

zFileDownloader.temp_namec                 C   s    | dr|d td  S |S )Nr   )endswithlenr   r)   r)   r*   undo_temp_name   s   
zFileDownloader.undo_temp_namec                 C   s   |d S )Nz.ytdlr)   r   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$   ry   r9   eactionfdr)   r*   <lambda>   s     zIFileDownloader.wrap_file_access.<locals>.error_callback.<locals>.<lambda>c                    s    d  d|  S )Nz
Unable to r   r-   r   r   r)   r*   r      s    retry_sleep_functionsfile_access)infowarnerror
sleep_func)r   report_retry_FileDownloader__to_screenr   r    )errcountri   r   )r   r   r   r*   error_callback   s   
z7FileDownloader.wrap_file_access.<locals>.error_callbackc                    s   t | jdd | dD ];}z|| g|R i |W   S  tyG } z|jtjtjfv r6||_W Y d }~q||dd W Y d }~qd }~ww d S )Nfile_access_retries   r      r   )	r   r   r    OSErrorerrnoEACCESEINVALr   r   )r'   r7   r:   kwargsretryr   )r   r)   r*   wrapper   s   z0FileDownloader.wrap_file_access.<locals>.wrapper)	functoolspartialpartialmethod)r   r   r   r)   )r   r   r   r*   wrap_file_access   s   
zFileDownloader.wrap_file_accessopenTc                 C   s8   t ||\}}t|dd s| jtj ddd ||fS )Nlockedz. Proceeding without lockingT)	only_once)r   r6   r3   r	   msg)r'   r   	open_modefr)   r)   r*   r      s   zFileDownloader.sanitize_openremovec                 C   s   t j|rt | d S d S rV   )rk   rl   rm   r   r   r)   r)   r*   
try_remove  s   zFileDownloader.try_removerenamec                 C   s   ||krd S t || d S rV   )rk   replace)r'   old_filename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r&dS tt t |t |f W d   |S 1 sAw   Y  |S )z4Try to set the last-modified time of the given file.Nr   )	rk   rl   rm   r   
contextlibsuppress	Exceptionutimer$   )r'   r   last_modified_hdrtimestrfiletimer)   r)   r*   	try_utime  s$   
zFileDownloader.try_utimec                 C   s   |  d|  dS )zReport destination filename.z[download] Destination: Nr9   r   r)   r)   r*   report_destination!  s   z!FileDownloader.report_destinationr   c                 C   s   | j drt | _n1| jj drt| jj d || _n| j dr-t| jjj|| _nt	| jjj|| j d | _| jj
joG| jj
jdk| j_| jj
j| j_d S )N
noprogressloggerprogress_with_newliner8   no_color)r   r    r   
_multiliner(   r   r   
_out_filesoutr   _allow_colorsallow_colors_HAVE_FULLCAP)r'   linesr)   r)   r*   r   %  s   
z(FileDownloader._prepare_multiline_statusc                 C   s   | j   d S rV   )r   endr'   r)   r)   r*   _finish_multiline_status1  s   z'FileDownloader._finish_multiline_statusz
light blueyellowgreenz
bold white )downloaded_bytesrS   etarf   r|   total_bytestotal_bytes_estimatec                 C   s   | j jD ]\}}d| d}||vrq| || |||< q|| |d< | }|d |d |d}| jdi }| j| j	
|dpGd||d	pOd
 | | j	
|dp\d|t||d d S )Nr<   _str_default_template	info_dict)r   progressprogress_templatedownloadz)[download] %(progress._default_template)sprogress_idxr   zdownload-titlez%yt-dlp %(progress._default_template)s_percent)ProgressStylesitems__format_progresscopypopr   r    r   print_at_liner(   evaluate_outtmplr0   r   	from_dict)r'   sdefault_templatenamestyle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 rV   )r(   _format_textr   streamr   )r'   r:   r   r)   r)   r*   r   R  s   zFileDownloader._format_progressc                    s  dd fdd
} fdd} d dkrW| j d	r | d
 t fdd} || | |d|  dd| dd | 	 t
d|d|d|ddd  d dkr_d S | j d }r| j t | jk r{	 W d    d S |  j|7  _W d    n1 sw   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 rV   r    .0r   r   r)   r*   	<genexpr>Y      zFFileDownloader.report_progress.<locals>.with_fields.<locals>.<genexpr>)all)r   tupsfieldstmplr   r)   r*   with_fieldsW  s
   z3FileDownloader.report_progress.<locals>.with_fieldsc                    s   t  | dS )Nre   )r   r    )kr   r)   r*   r   ]  s    z0FileDownloader.report_progress.<locals>.<lambda>statusfinishedr   z[download] Download completedc                      s    d  d  S )Nr   r|   r)   r)   r   r)   r*   r   b      r   r|   rL   d   )rf   
_speed_str_total_bytes_str_elapsed_strr   _percent_strz100%%)r   zof %(_total_bytes_str)s)r|   zin %(_elapsed_str)s)rf   zat %(_speed_str)s )delimdownloadingr   c                         d d   d  S )Nr  r   r   r)   r)   r   r)   r*   r   |      c                      r	  )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   rf   r   r   )_eta_strr  r   r  r  _total_bytes_estimate_str_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   r|   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_indexfragment_countz- (frag %(fragment_index)s/%(fragment_count)s))r  z (frag %(fragment_index)s))r   r    r9   r   updaterg   striprI   rT   r   r   r#   r$   r%   r&   rJ   )r'   r   r   _format_bytesrf   update_deltar   msg_templater)   r   r*   r   V  sr   



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'   
resume_lenr)   r)   r*   report_resuming_byte  rK   z#FileDownloader.report_resuming_bytec                    s|   |t u rdnd}tj||| j fdd|stn fdd jdi |p'd|r8d|du r1d	nd
|  ndd dS )zReport retryFfragmentc                         d|  S )Nz[download] Got error: )r   )r   r   r)   r*   r     r   z-FileDownloader.report_retry.<locals>.<lambda>c                    r  )Nz[download] Got error: r   r   r   r)   r*   r     r   r   httpNr   r  )r   r   r   r   suffix)r   r   r   r   r   r   r    )r'   r   r   ri   
frag_indexr   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   sv  | j dd otj|}t|dsI| j ddo'tj|o'| j dd }|dkrI|s0|rI| | | |dtj	|d	| | 
  d
S d}|rV| j dpTd}nA| j dp]d}| j dped}	|dpm|g}
tdd |
D  }r|tt  }||krd}|}||	kr|}	t||	p|}|dkr| d|dd| d t| | ||}| 
  |dfS )zpDownload to a filename using the info from info_dict
        Return True on success and False otherwise
        
overwritesTwrite
continuedlr   Fr   r   )r   r   r   )TFr   sleep_interval_subtitlesr   sleep_intervalmax_sleep_intervalrequested_formatsc                 s   s    | ]
}| d p
dV  qdS )available_atr   Nr   r   r)   r)   r*   r     r   z*FileDownloader.download.<locals>.<genexpr>zas required by the sitez[download] Sleeping z.2fz	 seconds z...)r   r    rk   rl   r   r4   rm   r.   _hook_progressrn   r   rq   rW   r$   randomuniformr9   ry   real_download)r'   r   r   subtitlenooverwrites_and_existscontinuedl_and_exists
sleep_noter$  min_sleep_intervalr%  r&  r'  forced_sleep_intervalretr)   r)   r*   r     sT   





zFileDownloader.downloadc                 C   s   t d)z.Real download process. Redefine in subclasses.z-This method must be implemented by subclasses)NotImplementedError)r'   r   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 rV   )r   append)r'   r4  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    rk   rl   basenamer3   r   )r'   r:   exer)   r)   r*   
_debug_cmd  s
   zFileDownloader._debug_cmdc                 C   sH   | d}|d u rd S | j|\}}|r|S |r"| | j| d S )Nimpersonate)r    r(   _parse_impersonate_targetsr/   _unavailable_targets_message)r'   r   r:  available_targetrequested_targetsr)   r)   r*   _get_impersonate_target  s   
z&FileDownloader._get_impersonate_target)r   )FrV   )5r@   
__module____qualname____doc___TEST_FILE_SIZEr   r+   r   r9   r   r   rC   staticmethodrI   classmethodrJ   rQ   rT   r   ra   rX   rg   rj   rp   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   r9  r?  r)   r)   r)   r*   r   %   s    +
















@


6	
	r   )!r   r   r   rk   r)  r>   r!   r$   
minicursesr   r   r   r   utilsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   utils._utilsr   r   r)   r)   r)   r*   <module>   s    H