
    g7                        d dl 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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 d dlZd dlmZ  ej        e          Zej                            d          Z G d de          ZdS )	    )LooseVersionN)urlopen)urlretrieve)Lock)darwincygwinlinuxlinux2c                      e Zd Z e            ZdZej        Z ej        d          rdZ	n4de
j        v rdZ	n( ej        d          rdZ	n ej        d          rd	Z	nd
Z	e
j                            e
j                            e	                    Z	 	 	 	 d!defdZd Zd"dZd#dedefdZd Zd Zd Zd Zd Zd Zed             Zed             Z d#dZ!d Z"d Z#d  Z$dS )$Patcherzchromedriver%swin32z)~/appdata/roaming/undetected_chromedriverLAMBDA_TASK_ROOTz/tmp/undetected_chromedriverr	   r
   z&~/.local/share/undetected_chromedriverr   z5~/Library/Application Support/undetected_chromedriverz~/.undetected_chromedriverNFr   version_mainc                    || _         d| _        d}|| _        |o|dk    | _        |                                  t
          j                            | j                  st          j	        | j        d           |sDt
          j        
                    | j        d
                    || j        g                    | _        t          s|r|dd         d	k    s|d	z  }t
          j        
                    | j        |          | _        |sN| j        sGt
          j                            t
          j        
                    d
| j                            | _        |rd| _        || _        | j        rd| _        nd| _        || _        d| _        dS )aK  
        Args:
            executable_path: None = automatic
                             a full file path to the chromedriver executable
            force: False
                    terminate processes which are holding lock
            version_main: 0 = auto
                specify main chrome version (rounded, ex: 82)
        F
undetectedr   T)exist_ok_N.exe.z+https://chromedriver.storage.googleapis.comz5https://googlechromelabs.github.io/chrome-for-testing)force_custom_exe_pathuser_multi_procsis_old_chromedriver_set_platform_nameospathexists	data_pathmakedirsjoinexe_nameexecutable_pathIS_POSIXzip_pathabspathurl_repor   version_full)selfr%   r   r   r   prefixs         k/var/www/html/nodeJS/PythonScripts/venv3.11/lib/python3.11/site-packages/undetected_chromedriver/patcher.py__init__zPatcher.__init__,   s     
 % 0#/#GLC4G !!!w~~dn-- 	7K6666 	#%7<<&$-)@ A A$ $D   	. .&rss+v55#v-OT^V<< 	( ')wGLLd&:;;( ($  	3$(D!#2D  # 	TIDMMSDM(     c                 N   | j                             d          rd| _        | xj        dz  c_        | j                             d          rd| _        | xj        dz  c_        | j                             d          r(| j        rd| _        nd| _        | xj        dz  c_        d	S d	S )
z
        Set the platform and exe name based on the platform undetected_chromedriver is running on
        in order to download the correct chromedriver.
        r   r   r   linux64 r   mac64zmac-x64N)platformendswithplatform_namer$   r   r+   s    r-   r   zPatcher._set_platform_nameg   s    
 =!!'** 	$!(DMMV#MM=!!"566 	 !*DMMRMM=!!(++ 	 ' /%,""%."MMRMMMM	  	 r/   c                    t          j        | j                  }| j        rt	                      5  t          |                    d                    }t          |d           }|                    |           t          t          d |                     | 
                    |          r"t          |          | _        	 ddd           dS 	 ddd           n# 1 swxY w Y   |r|| _        d| _        | j        r2| 
                    | j                  }|s|                                 S dS |r|| _        |du r|| _        	 t#          j        | j                   n# t&          $ rk | j        r8|                     | j                   |                     | j                   cY S 	 | 
                                rY dS n# t&          $ r Y nw xY wY nt,          $ r Y nw xY w|                                 }	|	j        d         | _        |	| _        |                     |                                            |                                 S )	zu

        Args:
            executable_path:
            force:
            version_main:

        Returns:

        z*chromedriver*c                 4    |                                  j        S N)statst_mtimefs    r-   <lambda>zPatcher.auto.<locals>.<lambda>   s    qvvxx7H r/   )keyc                 *    |                                  S r:   )unlinkr=   s    r-   r?   zPatcher.auto.<locals>.<lambda>   s    188:: r/   NT)r   r   )pathlibPathr!   r   r   listrglobmaxremovemapis_binary_patchedstrr%   r   	patch_exer   r   r   rB   PermissionErrorforce_kill_instancesautoFileNotFoundErrorfetch_release_numberversionr*   unzip_packagefetch_packagepatch)
r+   r%   r   r   r   pfilesmost_recent	ispatchedreleases
             r-   rO   zPatcher.autoy   s    L((  	     QWW%56677!%-H-HIII[)))S--u55666))+66  +.{+;+;D(               
                                	)#2D $(D!  	..t/CDDI ~~''' 	- ,DD==DJ	Id*++++ 		 		 		z 7))$*>???yy4:~y66666))++  44  #    ! 	 	 	D	 ++--#OA.#4--//000zz||sO   BCC"CE AG(G ?G 
G
GGG	GGr   returnc                 D   |s| j         }t          j        |          }|                                st	          d|z            	 t          |d          5 }g }	 |                    dd           n,# t          $ r}|                    |           Y d}~nd}~ww xY w	 |	                                 n,# t          $ r}|                    |           Y d}~nd}~ww xY w|r	 ddd           dS 	 ddd           dS # 1 swxY w Y   dS # t          $ r}Y d}~dS d}~ww xY w)a  
        naive test to check if a found chromedriver binary is
        currently in use

        Args:
            path: a string or PathLike object to the binary to check.
                  if not specified, we check use this object's executable_path
        zfile does not exist: %sza+bmoder   NTF)r%   rC   rD   r    OSErroropenseekrM   appendreadline	Exception)r+   r   rV   fsexces         r-   driver_binary_in_usezPatcher.driver_binary_in_use   s     	('DLxxzz 	93a7888	ae$$$ "GGAqMMMM& " " "JJqMMMMMMMM""KKMMMM& " " "JJqMMMMMMMM"            !                 $  	 	 	DDDDD	s   D
 C=A10C=1
B;BC=BC=B32C=3
C=CC=CC="D
 /C=0D
 =DD
 DD
 

DDc                     t          j        | j                  }t          |                    d                    }|D ]}	 |                                 #  Y xY wd S )Nz*undetected*)rC   rD   r!   rE   globrB   )r+   rV   itemsitems       r-   cleanup_unused_fileszPatcher.cleanup_unused_files   sg    L((QVVN++,, 	 	D		 	s   AAc                 R    |                                   |                                 S r:   )rL   rJ   r7   s    r-   rU   zPatcher.patch   s$    %%'''r/   c                    | j         rd| j         }|                                }t                              d|z             t          t          | j        |z                                             	                                          S | j        sd}t                              d|z             t          | j        |z             5 }|                                	                                }ddd           n# 1 swxY w Y   t          j        |          }t          |d         d         d                   S d}t                              d|z             t          | j        |z             5 }|                                	                                }ddd           n# 1 swxY w Y   t          j        |          }t          |d	         t          | j                           d                   S )
z
        Gets the latest major version available, or the latest major version of self.target_version if set explicitly.
        :return: version string
        :rtype: LooseVersion
        z/latest_release_zgetting release number from %sz-/last-known-good-versions-with-downloads.jsonNchannelsStablerR   z2/latest-versions-per-milestone-with-downloads.json
milestones)r   r   upperloggerdebugr   r   r)   readdecodejsonloadsrK   )r+   r   connresponselast_versionsmajor_versionss         r-   rQ   zPatcher.fetch_release_number   s-    # 	O9d&799D::<<DLL9D@AAA(< = = B B D D K K M MNNN   	PBDLL9D@AAA-.. 0$99;;--//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 !Jx00Mj 9( CI NOOO D5<===T]T)** 	,dyy{{))++H	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, H--N<8T=N9O9OPQZ[\\\s$   'C;;C?C?1'F$$F(+F(c                 4   t          j        | j        d          5 t          fdd          D ]N}t	          j        d|          }|r5t          |d                                                   c cd d d            S O	 d d d            d S # 1 swxY w Y   d S )Nrbc                  ,                                      S r:   )rc   r=   s   r-   r?   z+Patcher.parse_exe_version.<locals>.<lambda>  s    QZZ\\ r/   r/   s'   platform_handle\x00content\x00([0-9.]*)   )ior`   r%   iterresearchr   rw   )r+   linematchr>   s      @r-   parse_exe_versionzPatcher.parse_exe_version	  s   WT)400 	;A1111377 ; ;	"MtTT ;'a(9(9::::		; 	; 	; 	; 	; 	; 	; 	;;;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   AB>BBBc                    d| j          d}| j        r| j        d| j        j        d|}n0|                    ddd          }d}|| j        j        | j         |fz  }t                              d|z             t          |          d	         S )
z^
        Downloads ChromeDriver from source

        :return: path to downloaded file
        chromedriver_z.zip/r   -r   zAhttps://storage.googleapis.com/chrome-for-testing-public/%s/%s/%szdownloading from %sr   )	r6   r   r)   r*   vstringreplacert   ru   r   )r+   zip_namedownload_urls      r-   rT   zPatcher.fetch_package  s     <4#5;;;# 	V)-8I8Q8Q8QS[S[\LL''S!44H^LT.68JHUUL*\9:::<((++r/   c                    | j         }| j        s/d| j         }t          j                            || j                   }t                              d|z             	 t          j        | j	                   n# t          t          f$ r Y nw xY wt          j        | j	        dd           t          j        |d          5 }|                    | j	                   ddd           n# 1 swxY w Y   t          j        t          j                            | j	        |          | j                   t          j        |           t'          j        | j	                   t          j        | j        d           | j        S )	zQ
        Does what it says

        :return: path to unpacked executable
        zchromedriver-zunzipping %si  T)r^   r   rr]   N)r$   r   r6   r   r   r#   rt   ru   rB   r'   rP   r_   r"   zipfileZipFile
extractallrenamer%   rH   shutilrmtreechmod)r+   fpexe_pathr   zfs        r-   rS   zPatcher.unzip_package!  s    =' 	=;t'9;;Hw||Hdm<<H^b()))	Idm$$$$!7+ 	 	 	D	 	DM====_Rc*** 	)bMM$-(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)
	"',,t}h779MNNN
	"dm$$$
%u---##s$   A6 6B
	B
?C&&C*-C*c                     t           j                            |           } t          rt          j        d| z            }nt          j        d| z            }| S )z
        kills running instances.
        :param: executable name to kill, may be a path as well

        :return: True on success else False
        zkill -f -9 $(pidof %s)ztaskkill /f /im %s)r   r   basenamer&   system)r$   r   s     r-   rN   zPatcher.force_kill_instances<  sT     7##H-- 	;	2X=>>AA	.9::Aur/   c                      t          j        t          j        d          } d                    |                                           S )N   )kr2   )randomchoicesstringascii_lettersr#   encode)cdcs    r-   gen_random_cdczPatcher.gen_random_cdcK  s5    nV1R888wws||""$$$r/   c                     |p| j         }	 t          j        |d          5 }|                                                    d          dk    cd d d            S # 1 swxY w Y   d S # t
          $ r Y dS w xY w)Nr   s   undetected chromedriverF)r%   r   r`   rv   findrP   )r+   r%   fhs      r-   rJ   zPatcher.is_binary_patchedP  s    )AT-A	$// H2wwyy~~&@AARGH H H H H H H H H H H H H H H H H H  	 	 	55	s4   A% +AA% AA% A A% %
A32A3c                    t          j                    }t                              d| j        z             t          j        | j        d          5 }|                                }t          j	        d|          }|r|d         }d
                    t          |          d          }|                    ||          }||k    rt                              d           n t                              d|d	|           |                    d           |                    |           d d d            n# 1 swxY w Y   t                              d
                    t          j                    |z
                       d S )Nzpatching driver executable %szr+bs   \{window\.cdc.*?;\}r   s.   {console.log("undetected chromedriver 1337!")}    zTsomething went wrong patching the driver binary. could not find injection code blockzfound block:
z
replacing with:
zpatching took us {:.2f} seconds)timeperf_counterrt   infor%   r   r`   rv   r   r   ljustlenr   warningru   ra   writeformat)r+   startr   contentmatch_injected_codeblocktarget_bytesnew_target_bytesnew_contents           r-   rL   zPatcher.patch_exeX  s   !##3d6JJKKKWT)511 	&RggiiG')y1H''R'R$' &7:EKKL))4  !
 &ool<LMM'))NNn    LLL'<<)9)9;   


%%%-	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&. 	-44T5F5H5H55PQQ	
 	
 	
 	
 	
s   CD44D8;D8c                 L    d                     | j        j        | j                  S )Nz{0:s}({1:s}))r   	__class____name__r%   r7   s    r-   __repr__zPatcher.__repr__v  s(    $$N# 
 
 	
r/   c                 r   | j         rd S d}t          j                    }d } |            |z
  |k    r	 | j        rd S t	          j        | j                   t                              d| j        z             d S # t          t          t          f$ r t          j        d           Y t          $ r Y d S w xY wd S )N   c                  (    t          j                    S r:   )r   	monotonic r/   r-   r?   z!Patcher.__del__.<locals>.<lambda>  s    $.** r/   zsuccessfully unlinked %sg{Gz?)r   r   r   r   r   rB   r%   rt   ru   r_   RuntimeErrorrM   sleeprP   )r+   timeouttnows       r-   __del__zPatcher.__del__|  s      	 FG  A**C#%%!)g%%
, Id2333LL!;d>R!RSSSE?   Jt$$$H(   EE &%s   A: ;A: :+B4'	B43B4)NFr   F)NFNNr:   )%r   
__module____qualname__r   lockr$   sysr4   r5   dr   environ
startswithr   r(   
expanduserr!   intr.   r   rO   rK   boolrh   rm   rU   rQ   r   rT   rS   staticmethodrN   r   rJ   rL   r   r   r   r/   r-   r   r      s       466DH|Hx!! 	)7	rz	)	)*		0	1	1 )4		8	$	$ )C( 2 21 5 566I 9! 9! 	9! 9! 9! 9!v     $: : : :x$ $ $ $ $ $ $L  ( ( (] ] ]B; ; ;, , ,"$ $ $6   \ % % \%   
 
 
<
 
 
    r/   r   )distutils.versionr   r   rx   loggingr   rC   r4   r   r   r   r   r   r   urllib.requestr   r   r   multiprocessingr   	getLoggerr   rt   r   r&   objectr   r   r/   r-   <module>r      s*   + * * * * * 				   				    				   



  " " " " " " & & & & & &             		8	$	$<""#JKKv v v v vf v v v v vr/   