
    Ri                        U 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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 defdZd e       iZeeef   ed<   dej0                  v r eej0                  d   j5                               d kD  rej0                  d   j5                         Z ej8                  d	e      Zet ej<                  ej>                  ejA                  d
       e!ejA                  d            dejA                  d      ejA                  d             ejD                  e_        ejF                  Z$i dddfdedeeef   dee   deejJ                     de&defdZ'ddedee   deejJ                     defdZ(y)    N)Mapping)AnyOptionalreturnc                      t        j                  ddd      } d}t         j                  j                         }||| z
  j                  dz  z   }d| d| dS )	z4 Disguise as browser to circumvent website blocking i        }      z-Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:z.0) Gecko/20100101 Firefox/z.0)datetimedatetodaydays)baseDatebaseVersionnowDate
nowVersions       $/config/qBittorrent/nova3/helpers.pygetBrowserUserAgentr   /   s`     }}T1b)HKmm!!#G8!3 9 9R ?@J::,Fablammopp    z
User-Agentheaders
sock_proxyzL^(?:(?P<username>[^:]+):(?P<password>[^@]+)@)?(?P<host>[^:]+):(?P<port>\w+)$hostportTusernamepasswordurlcustom_headersrequest_datassl_contextunescape_html_entitiesc                    t         j                  j                  | |i t        |      }	 t         j                  j	                  ||      }|j                         }|dd dk(  rMt        j                  |      5 }	t        j                  |	      5 }
|
j                         }ddd       ddd       d	}	 |j!                  d
d      j#                  dd      d   }|j'                  |d      }|rt)        j*                  |      }|S # t         j
                  j                  $ r2}t        d|j                   t        j                         Y d}~yd}~ww xY w# 1 sw Y   xY w# 1 sw Y   xY w# t$        $ r Y w xY w)z0 Return the content of the url page as a string contextzConnection error: )fileN       fileobjzutf-8zContent-Typezcharset=   replace)urllibrequestRequestr   urlopenerrorURLErrorprintreasonsysstderrreadioBytesIOgzipGzipFile	getheadersplit
IndexErrordecodehtmlunescape)r   r   r   r    r!   r.   responseerrnodatacompressedStreamgzippercharsetdataStrs                r   retrieve_urlrI   P   sI    nn$$S,8U78Un8UVG>>))';)G --/D BQx;ZZ!14==IY3Z^e<<>D 4[ G$$^R8>>z1MaP kk'9-G--(N- <<   "5<<.1

C 4[3Z  sM   !D 9E"E!E"4%E. E!(EEE	E""E+.	E:9E:refererc                 d   t         j                  j                  | t              }||j	                  d|       t         j                  j                  ||      }|j                         }|dd dk(  rMt        j                  |      5 }t        j                  |      5 }|j                         }ddd       ddd       t        j                         \  }}	t        j                  |d      5 }
|
j                  |       ddd       |	 d	|  S # 1 sw Y   _xY w# 1 sw Y   cxY w# 1 sw Y   (xY w)
zV Download file at url and write it to a file, return the path to the file and the url )r   NrJ   r#   r'   r(   r)   wb )r-   r.   r/   r   
add_headerr0   r7   r8   r9   r:   r;   tempfilemkstemposfdopenwrite)r   rJ   r    r.   rB   rD   rE   rF   
fileHandlepathr%   s              r   download_filerV   o   s     nn$$S'$:G9g.~~%%g{%CH==?D BQx;ZZ!14==IY3Z^e<<>D 4[  '')J	:t	$

4 
% V1SE? 4[3Z
 
%	$s0   DD0D-D&D	DD#&D/)NN))r   r:   r@   r8   rQ   resocketsockssslr5   rO   urllib.errorr-   urllib.requestcollections.abcr   typingr   r   strr   r   dict__annotations__environlenstrip	proxy_strmatchmsetdefaultproxyPROXY_TYPE_SOCKS5groupint
socksocketrA   htmlentitydecode
SSLContextboolrI   rV    r   r   <module>rq      s  <    	 	 	   
 
    #  qS q  ()<)>?c3h ? 2::#bjj&>&D&D&F"G!"K

<(..0I`	A}e55qwwv!!''&/2D!''*:MqwwWaOb	d(( ==  @Bae  PT  uy c 738+< QYZ]Q^ t|  ~A  ~L  ~L  uM   nr   ~A >s Xc] QTQ_Q_H` lo r   