3
b]c              	   @   sJ   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 d	ddZ	dS )
    N   )utils   T   c          /   C   s  |	p
t j }	|si }n
tj|}|r|rtjj|rtjj|}|	jdj	|| ||7 }|rl| j
|7  _
|d |kr|	jdj	| dS ||kr|	jdj	|| d}|r| j
|8  _
|	jd||f  d||f |d	 d
< n|	jd d}|	jdj	| | tjj| f|}ytjj||d}W n tjjk
r } z`|jdkr|
dkr|	jdj	|
d  tjd t| |||||||||	|
d S  n W Y dd}~X nX | rtjj|r|	jdj	| tj| t|d}|r|| }nRy*|j }t|jd }|	jdj	| W n& tttfk
rJ   |	jd Y nX d}tj }d}d}xb|r2d|kr|	jd t d|krtjd qdd|kr2tj }|| }|dkr2|| | |d kr*|| |d  }|	jdj	t j|| t j |ddd t j|d  tj| qdn|}|}y|j!|}W nH t"k
r } z*|	jt#| |rv| j
|8  _
 W Y dd}~X nX |sP |t$|7 }|r| j
t$|7  _
|j%| qdW W dQ R X |j&  dS )!z5The basic download function that runs at each thread.z1Found partial chunks of file {} (found {} bytes).r   z#This chunk is 100% downloaded ({}).NzKstartByte ({}) > endByte ({}). Chunks are corrupted. Overriding old chunks.FzDownloading bytes=%d-%dzbytes=%d-%dheadersRangez3without endByte, attempt_resume cannot be supportedzDownloading '{}' to '{}'...)timeouti  r   zHThread didn't got the file it was expecting. Retrying ({} times left)...   zDeleting old {}...abzContent-LengthzContent-Length is {}.z#Server did not send Content-Length.i    stopz stop command received. Stopping.pauseg?limitg?z>Thread has downloaded {} in {}. Limit is {}/s. Slowing down...T)	fmt_shortshow_ms)'r   DummyLoggercopydeepcopyospathexistsgetsizeinfoformatvaluewarningdebugurllibrequestRequesturlopenerror	HTTPErrorcodetimesleepdownloadunlinkopenintr   
IndexErrorKeyError	TypeErrorCanceledExceptionsizeof_human
time_humanread	Exceptionstrlenwriteclose)urldestr   	startByteendByter   Zattempt_resume
shared_varthread_shared_cmdsloggerretriesZdownload_headersZpartial_sizerequrlObjeffilesizemetaZfilesize_dlZlimitspeed_timestampZlimitspeed_filesizeblock_sznowZtime_passedZtime_to_sleepZbuff rE   HC:\Users\ibrahim.fayad\Desktop\sentSatProg_v.0.1.2\pySmartDL\download.pyr%      s    

	

 







4
r%   )	Nr   Nr   TNNNr   )
r   urllib.requestr   urllib.errorurllib.parser#   r    r   r%   rE   rE   rE   rF   <module>   s
   