
    ,h7                        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                  j/                  d      Z G d de      Zy)	    )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	n=d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                  j                  e
j                  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$y)"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_chromedriverNversion_mainc                 H   || _         d| _        d}|| _        |xr |dk  | _        | j	                          t
        j                  j                  | j                        s!t        j                  | j                  d       |sJt
        j                  j                  | j                  dj                  || j                  g            | _        t        s|r|dd d	k(  s|d	z  }t
        j                  j                  | j                  |      | _        |sX| j                  sLt
        j                  j                  t
        j                  j                  d
| j                              | _        |rd| _        || _        | j                  rd| _        nd| _        || _        d| _        y)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         a/var/www/html/Resume-Scraper/venv/lib/python3.12/site-packages/undetected_chromedriver/patcher.py__init__zPatcher.__init__,   sI     
 % 0#/#GLC4G !ww~~dnn-KK6#%77<<&$--)@ A$D  &rs+v5#v-OT^^V<((')wwGGLLd&:&:;($ $(D!#2D  ##IDMSDM(     c                 x   | j                   j                  d      rd| _        | xj                  dz  c_        | j                   j                  d      rd| _        | xj                  dz  c_        | j                   j                  d      r1| j                  rd| _        nd| _        | xj                  dz  c_        y	y	)
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#M==!!"56!*DMMRM==!!(+''%,"%."MMRM ,r/   c                 @   t        j                  | j                        }| j                  rt	               5  t        |j                  d            }t        |d       }|j                  |       t        t        d |             | j                  |      rt        |      | _        	 ddd       y	 ddd       |r|| _        d| _        | j                  r.| j                  | j                        }|s| j                         S y|r|| _        |du r|| _        	 t#        j$                  | j                         | j/                         }	|	j0                  d   | _        |	| _        | j5                  | j7                                | j9                         S # 1 sw Y   xY w# t&        $ rl | j                   r:| j)                  | j                         | j+                  | j                          cY S 	 | j                         rY yn# t&        $ r Y nw xY wY t,        $ r Y w xY w)	zu

        Args:
            executable_path:
            force:
            version_main:

        Returns:

        z*chromedriver*c                 6    | j                         j                  S N)statst_mtimefs    r-   <lambda>zPatcher.auto.<locals>.<lambda>   s    qvvx7H7H r/   )keyc                 "    | j                         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    LL(    QWW%567!%-HI[)S-u56))+6+.{+;D(   
 7  #2D $(D!  ..t/C/CDI~~'' ,DD=DJ	IId**+ ++-#OOA.#4--/0zz|[   6  		zz))$*>*>?yy4::~y66))+ , #  ! 		sI   A2FF FAH.H H	H
HHHHHr   returnc                    |s| j                   }t        j                  |      }|j                         st	        d|z        	 t        |d      5 }g }	 |j                  dd       	 |j                          |r
	 ddd       y	 ddd       y# t        $ r}|j                  |       Y d}~Fd}~ww xY w# t        $ r}|j                  |       Y d}~\d}~ww xY w# 1 sw Y   yxY w# t        $ r
}Y d}~y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LLxxz3a788	ae$ "GGAqM"KKM    ! 
 ' "JJqMM"
 ' "JJqMM" $  		s   C' CB'B47C:C' CC' 	B1B,'C,B11C4	C=CCCCC$ C' $C' '	C:5C:c                     t        j                  | j                        }t        |j	                  d            }|D ]  }	 |j                           y #  Y xY w)Nz*undetected*)rC   rD   r!   rE   globrB   )r+   rV   itemsitems       r-   cleanup_unused_fileszPatcher.cleanup_unused_files   sN    LL(QVVN+, 	D	s    AAc                 B    | j                          | j                         S r:   )rL   rJ   r7   s    r-   rU   zPatcher.patch   s    %%''r/   c                 x   | j                   rtd| j                   }|j                         }t        j	                  d|z         t        t        | j                  |z         j                         j                               S | j                  sd}t        j	                  d|z         t        | j                  |z         5 }|j                         j                         }ddd       t        j                        }t        |d   d   d         S d}t        j	                  d|z         t        | j                  |z         5 }|j                         j                         }ddd       t        j                        }t        |d	   t        | j                           d         S # 1 sw Y   xY w# 1 sw Y   QxY w)
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|    ##%d&7&7%89D::<DLL9D@A(< = B B D K K MNN   BDLL9D@A-. 0$99;--/0 !JJx0Mj 9( CI NOO D5<=T]]T)* 	,dyy{))+H	, H-N<8T=N=N9OPQZ[\\0 0	, 	,s   ?F$F0$F-0F9c                    t        j                  | j                  d      5 t        fdd      D ]A  }t	        j
                  d|      }|st        |d   j                               c cd d d        S  	 d d d        y # 1 sw Y   y xY w)Nrbc                  $     j                         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	  sy    WWT))40 	;A137 ;		"MtT'a(9::		; 	;;	; 	; 	;s   +B B 5B  B	c                 N   d| j                    d}| j                  r)| j                  d| j                  j                  d|}n;|j                  ddd      }d}|| j                  j                  | j                   |fz  }t        j                  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#5"6d;##)-8I8I8Q8QS[\L''S!4H^LT..668J8JHUUL*\9:<(++r/   c                 b   | j                   }| j                  s9d| j                   }t        j                  j                  || j                         }t        j                  d|z         	 t        j                  | j                         t        j                  | j                  dd       t        j                  |d      5 }|j                  | j                         ddd       t        j                   t        j                  j                  | j                  |      | j"                         t        j$                  |       t'        j(                  | j                         t        j*                  | j"                  d       | j"                  S # t        t        f$ r Y w xY w# 1 sw Y   xY w)	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'9&:;Hww||Hdmm<H^b()	IIdmm$ 	DMM=__Rc* 	)bMM$--(	)
		"'',,t}}h79M9MN
		"dmm$
%%u-### "7+ 			) 	)s   +F F%F"!F"%F.c                     t         j                  j                  |       } t        rt        j                  d| z        }| S 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<  sU     77##H-		2X=>A u 		.9:Aur/   c                      t        j                  t        j                  d      } dj	                  |       j                         S )N   )kr2   )randomchoicesstringascii_lettersr#   encode)cdcs    r-   gen_random_cdczPatcher.gen_random_cdcK  s/    nnV11R8wws|""$$r/   c                     |xs | j                   }	 t        j                  |d      5 }|j                         j	                  d      dk7  cd d d        S # 1 sw Y   y xY w# t
        $ r Y yw xY w)Nr   s   undetected chromedriverF)r%   r   r`   rv   findrP   )r+   r%   fhs      r-   rJ   zPatcher.is_binary_patchedP  si    )AT-A-A	$/ H2wwy~~&@ARGH H H  		s.   A  "A
	A  AA  A   	A,+A,c                    t        j                         }t        j                  d| j                  z         t        j                  | j                  d      5 }|j                         }t        j                  d|      }|r|d   }dj                  t        |      d      }|j                  ||      }||k(  rt        j                  d       nt        j                  d|d	|       |j                  d       |j!                  |       d d d        t        j                  d
j#                  t        j                         |z
               y # 1 sw Y   CxY w)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    !!#3d6J6JJKWWT))51 	&RggiG')yy1H''R$'7:EKKL)4 !
 &ool<LM')NNn LL')9; 
%-	&. 	-44T5F5F5H55PQ	
/	& 	&s   B3EEc                 b    dj                  | j                  j                  | j                        S )Nz{0:s}({1:s}))r   	__class____name__r%   r7   s    r-   __repr__zPatcher.__repr__v  s+    $$NN##  
 	
r/   c                 |   | j                   ry d}t        j                         }d } |       |z
  |kD  rP	 | j                  ry t	        j
                  | j                         t        j                  d| j                  z         y y # t        t        t        f$ r t        j                  d       Y t        $ r Y y w xY w)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       G A*C%!)g%
,,IId223LL!;d>R>R!RS &  ? JJt$( s   B AB )B;0B;:B;)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'   6DH||Hx!7	rzz	)*			0	14			8	$C( 2 21 56I 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      so    + 	   	    	   
  " &   			8	$<<""#JKvf vr/   