
    vKg7                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
r
S SKrS SKrS SKrS SKJr  S SKJr  S SKrS SKJr  \R(                  " \5      r\R                  R/                  S5      r " S S\5      rg)	    )LooseVersionN)urlopen)urlretrieve)Lock)darwincygwinlinuxlinux2c                      \ rS rSr\" 5       rSr\R                  r\R                  " S5      (       a  Sr
OIS\R                  ;   a  Sr
O6\R                  " S5      (       a  Sr
O\R                  " S	5      (       a  S
r
OSr
\R                  R                  \R                  R!                  \
5      5      r    S!S\4S jjrS rS"S jrS#S\S\4S jjrS rS rS rS rS rS r\S 5       r \S 5       r!S#S jr"S r#S r$S r%S r&g)$Patcher   z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                    X l         SU l        SnX@l        U=(       a    US:*  U l        U R	                  5         [
        R                  R                  U R                  5      (       d  [
        R                  " U R                  SS9  U(       dI  [
        R                  R                  U R                  SR                  XPR                  /5      5      U l        [        (       d  U(       a  USS S	:X  d  US	-  n[
        R                  R                  U R                  U5      U l        U(       d]  U R                  (       dL  [
        R                  R                  [
        R                  R                  S
U R                  5      5      U l        U(       a  SU l        Xl        U R                  (       a  SU l        OSU l        X0l        SU l        g)a  
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         _/var/www/highfloat_scraper/venv/lib/python3.13/site-packages/undetected_chromedriver/patcher.py__init__Patcher.__init__,   sD     
 % 0#/#GLC4G !ww~~dnn--KK6#%77<<&--)@ A$D  x&rs+v5#v-OT^^V<((')wwGGLLd&:&:;($ $(D!#2  ##IDMSDM(     c                    U R                   R                  S5      (       a  SU l        U =R                  S-  sl        U R                   R                  S5      (       a  SU l        U =R                  S-  sl        U R                   R                  S5      (       a6  U R                  (       a  SU l        OSU l        U =R                  S-  sl        g	g	)
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   Patcher._set_platform_nameg   s    
 ==!!'**!(DMMV#M==!!"566!*DMMRM==!!(++''%,"%."MMRM ,r1   c                    [         R                  " U R                  5      nU R                  (       a  [	        5          [        UR                  S5      5      n[        US S9nUR                  U5        [        [        S U5      5        U R                  U5      (       a  [        U5      U l         SSS5        g SSS5        U(       a  Xl        SU l        U R                  (       a3  U R                  U R                  5      nU(       d  U R                  5       $ gU(       a  X0l        USL a  X l         ["        R$                  " U R                  5        U R/                  5       n	U	R0                  S   U l        Xl        U R5                  U R7                  5       5        U R9                  5       $ ! , (       d  f       N= f! [&         ax    U R                   (       a;  U R)                  U R                  5        U R+                  U R                   (       + S9s $  U R                  5       (       a   g N! [&         a      Nf = f[,         a     Nf = f)	zE

Args:
    executable_path:
    force:
    version_main:

Returns:

z*chromedriver*c                 6    U R                  5       R                  $ N)statst_mtimefs    r.   <lambda>Patcher.auto.<locals>.<lambda>   s    qvvx7H7Hr1   )keyc                 "    U R                  5       $ r=   )unlinkr@   s    r.   rB   rC      s
    188:r1   NT)r   r   )pathlibPathr"   r   r   listrglobmaxremovemapis_binary_patchedstrr&   r   	patch_exer   r   r   rF   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.   rS   Patcher.autoy   s    LL(  QWW%567!%-HI[)S-u56))+66+.{+;D( 
 7  #2 $(D!  ..t/C/CDI~~'' ,D=J	IId**+ ++-#OOA.#4--/0zz|[ 6  		zz))$*>*>?yy4::~y66))++ , #  ! 		s=   A4F,3 F= ,
F:=AI
H--
H;7I
:H;;I
	I
r    returnc                 $   U(       d  U R                   n[        R                  " U5      nUR                  5       (       d  [	        SU-  5      e [        USS9 n/ n UR                  SS5         UR                  5         U(       a
   SSS5        g SSS5        g! [         a  nUR                  U5         SnANLSnAff = f! [         a  nUR                  U5         SnANcSnAff = f! , (       d  f       g= f! [         a
  n SnAgSnAff = f)z
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&   rG   rH   r!   OSErroropenseekrQ   appendreadline	Exception)r,   r    rZ   fsexces         r.   driver_binary_in_usePatcher.driver_binary_in_use   s     ''DLLxxzz3a788	ae$"GGAqM"KKM  %$  ! %$
 ' "JJqMM"
 ' "JJqMM" %$$  		s   
C; C*B/C?C*C; C*C; 
B?$B:5C*:B??C*
C'C"C*"C''C**
C84C; 8C; ;
D
Dc                     [         R                  " U R                  5      n[        UR	                  S5      5      nU H  n UR                  5         M     g !    M  = f)Nz*undetected*)rG   rH   r"   rI   globrF   )r,   rZ   itemsitems       r.   cleanup_unused_filesPatcher.cleanup_unused_files   sI    LL(QVVN+,D s   AAc                 B    U R                  5         U R                  5       $ r=   )rP   rN   r9   s    r.   rY   Patcher.patch   s    %%''r1   c                    U R                   (       at  SU R                   3nUR                  5       n[        R	                  SU-  5        [        [        U R                  U-   5      R                  5       R                  5       5      $ U R                  (       d  Sn[        R	                  SU-  5        [        U R                  U-   5       nUR                  5       R                  5       nSSS5        [        R                  " W5      n[        US   S   S   5      $ Sn[        R	                  SU-  5        [        U R                  U-   5       nUR                  5       R                  5       nSSS5        [        R                  " W5      n[        US	   [        U R                  5         S   5      $ ! , (       d  f       N= f! , (       d  f       N\= f)
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StablerV   z2/latest-versions-per-milestone-with-downloads.json
milestones)r   r   upperloggerdebugr   r   r*   readdecodejsonloadsrO   )r,   r    connresponselast_versionsmajor_versionss         r.   rU   Patcher.fetch_release_number   sn    ##%d&7&7%89D::<DLL9D@A(< = B B D K K MNN   BDLL9D@A-.$99;--/ / !JJx0Mj 9( CI NOO D5<=T]]T)*dyy{))+H + H-N<8T=N=N9OPQZ[\\ /. +*s   	F0G0
F>
Gc                 4  ^ [         R                  " U R                  S5       m[        U4S jS5       HH  n[        R
                  " SU5      nU(       d  M#  [        US   R                  5       5      s  sS S S 5        $    S S S 5        g ! , (       d  f       g = f)Nrbc                  $   > T R                  5       $ r=   )rh   r@   s   r.   rB   +Patcher.parse_exe_version.<locals>.<lambda>  s    QZZ\r1   r1   s'   platform_handle\x00content\x00([0-9.]*)   )iore   r&   iterresearchr   r   )r,   linematchrA   s      @r.   parse_exe_versionPatcher.parse_exe_version	  si    WWT))40A137		"MtT5'a(9::	 107 100s   0B	B	>B		
Bc                 \   SU R                    S3nU R                  (       a,  U R                  < SU R                  R                  < SU< 3nO:UR                  SSS5      nSnX R                  R                  U R                   U4-  n[        R                  SU-  5        [        U5      S	   $ )
zF
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   )	r8   r   r*   r+   vstringreplacer|   r}   r   )r,   zip_namedownload_urls      r.   rX   Patcher.fetch_package  s     #4#5#5"6d;##)-8I8I8Q8QS[\L''S!4H^L..668J8JHUUL*\9:<(++r1   c                 x   U R                   nU R                  (       d8  SU R                   3n[        R                  R                  X0R                   5      n[        R                  SU-  5         [        R                  " U R                  5        [        R                  " U R                  SSS9  [        R                  " USS9 nUR                  U R                  5        SSS5        [        R                   " [        R                  R                  U R                  U5      U R"                  5        [        R$                  " U5        [&        R(                  " U R                  5        [        R*                  " U R"                  S5        U R"                  $ ! [        [        4 a     GNf = f! , (       d  f       N= f)	z9
Does what it says

:return: path to unpacked executable
zchromedriver-zunzipping %si  T)rc   r   rrb   N)r%   r   r8   r   r    r$   r|   r}   rF   r(   rT   rd   r#   zipfileZipFile
extractallrenamer&   rL   shutilrmtreechmod)r,   fpexe_pathr   zfs        r.   rW   Patcher.unzip_package!  s$    ==''&t'9'9&:;Hww||Hmm<H^b()	IIdmm$ 	DMM=__Rc*bMM$--( +
		"'',,t}}h79M9MN
		"dmm$
%%u-### "7+ 		 +*s   / F F+F('F(+
F9c                     [         R                  R                  U 5      n [        (       a   [         R                  " SU -  5      nU(       + $ [         R                  " SU -  5      nU(       + $ )zv
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.   rR   Patcher.force_kill_instances<  sW     77##H-8		2X=>A u 		.9:Aur1   c                      [         R                  " [        R                  SS9n SR	                  U 5      R                  5       $ )Nr   )kr4   )randomchoicesstringascii_lettersr$   encode)cdcs    r.   gen_random_cdcPatcher.gen_random_cdcK  s/    nnV11R8wws|""$$r1   c                     U=(       d    U R                   n [        R                  " US5       nUR                  5       R	                  S5      S:g  sS S S 5        $ ! , (       d  f       g = f! [
         a     gf = f)Nr   s   undetected chromedriverF)r&   r   re   r~   findrT   )r,   r&   fhs      r.   rN   Patcher.is_binary_patchedP  s\    )AT-A-A	$/2wwy~~&@ARG 0//  		s.   A+ "A	A+ 
A($A+ (A+ +
A87A8c                    [         R                  " 5       n[        R                  SU R                  -  5        [
        R                  " U R                  S5       nUR                  5       n[        R                  " SU5      nU(       a  US   nSR                  [        U5      S5      nUR                  XV5      nXs:X  a  [        R                  S5        O[        R                  SU< S	U< 35        UR                  S5        UR!                  U5        S S S 5        [        R                  S
R#                  [         R                  " 5       U-
  5      5        g ! , (       d  f       NI= f)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_counterr|   infor&   r   re   r~   r   r   ljustlenr   warningr}   rf   writeformat)r,   startr   contentmatch_injected_codeblocktarget_bytesnew_target_bytesnew_contents           r.   rP   Patcher.patch_exeX  s   !!#3d6J6JJKWWT))51RggiG')yy1H''R$'7:EKKL)4 !
 &oolM)NNn LL')9; 
%- 2. 	-44T5F5F5H55PQ	
/ 21s   B:E
E$c                 b    SR                  U R                  R                  U R                  5      $ )Nz{0:s}({1:s}))r   	__class____name__r&   r9   s    r.   __repr__Patcher.__repr__v  s+    $$NN##  
 	
r1   c                    U R                   (       a  g Sn[        R                  " 5       nS nU" 5       U-
  U:  aV   U R                  (       a  g [        R
                  " U R                  5        [        R                  SU R                  -  5        g g ! [        [        [        4 a    [        R                  " S5         M  [         a     g f = f)N   c                  ,    [         R                  " 5       $ r=   )r   	monotonic r1   r.   rB   !Patcher.__del__.<locals>.<lambda>  s    $..*r1   zsuccessfully unlinked %sg{Gz?)r   r   r   r   r   rF   r&   r|   r}   rd   RuntimeErrorrQ   sleeprT   )r,   timeouttnows       r.   __del__Patcher.__del__|  s       G A*C%!)g%
,,IId223LL!;d>R>R!RS &  ? JJt$( s   B AB +C 	CC)
r   r&   r   r   r8   r*   r   r+   r   r(   )NFr   F)NFNNr=   )'r   
__module____qualname____firstlineno__r   lockr%   sysr6   r7   dr   environ
startswithr    r)   
expanduserr"   intr/   r   rS   rO   boolrm   rs   rY   rU   r   rX   rW   staticmethodrR   r   rN   rP   r   r   __static_attributes__r   r1   r.   r   r      s*   6DH||H!!7	rzz	)*			0	1	14			8	$	$C( 2 21 56I 9! 	9!v $:x$ $ $L(]B;,"$6   % %
<
r1   r   )distutils.versionr   r   r   loggingr   rG   r6   r   r   r   r   r   r   urllib.requestr   r   r   multiprocessingr   	getLoggerr   r|   r   r'   objectr   r   r1   r.   <module>r      sm    + 	   	    	   
  " &   			8	$<<""#JKvf vr1   