
     Si                     @    d dl Z d dlmZ d dlZd dlmZ  G d d      Zy)    N)FreeProxyExceptionc                   8    e Zd ZdZd	dZd Zd Zd Zd
dZd Z	y)	FreeProxya  
    FreeProxy class scrapes proxies from <https://www.sslproxies.org/>,
    <https://www.us-proxy.org/>, <https://free-proxy-list.net/uk-proxy.html>,
    and <https://free-proxy-list.net> and checks if proxy is working. 
    There is possibility to filter proxies by country and acceptable timeout. 
    You can also randomize list of proxies from where script would get first 
    working proxy.
    Nc	                 |    || _         || _        || _        || _        || _        || _        |rdnd| _        || _        y )Nhttpshttp)
country_idtimeoutrandomanonymelitegoogleschemaurl)	selfr	   r
   randr   r   r   r   r   s	            =/home/agent/.friday_env/lib/python3.12/site-packages/fp/fp.py__init__zFreeProxy.__init__   s>    $
!&gF    c                 B   	 t        j                  | j                  |            }t        j                  |j
                        }	 |j                  d      }t        dt        |            D cg c]E  }| j                  ||         r/||   d   j                          d||   d   j                          G c}S # t         j                  j                  $ r$}t        d| j                  |       d      |d }~ww xY wc c}w # t        $ r}t        d      |d }~ww xY w)NzRequest to z failedz//*[@id="list"]//tr   r   :zFailed to get list of proxies)requestsget_FreeProxy__websitelh
fromstringcontent
exceptionsRequestExceptionr   xpathrangelen_FreeProxy__criteriatext_content	Exception)r   repeatpagedocetr_elementsis          r   get_proxy_listzFreeProxy.get_proxy_list   s   	F<<v 67D---C	M))$9:K"1c+&67\4??;WX>;Z #1~a(5578+a.:K:X:X:Z9[\ \ \ ""33 	F$dnnV45W=?DEF	F
\ 	M$%DE1L	MsC   AB; (D .A
C?8D ;C<C77C<?D 	DDDc                 N    |ry| j                   dgk(  ry| j                   dgk(  ryy)Nzhttps://free-proxy-list.netUSzhttps://www.us-proxy.orgGBz)https://free-proxy-list.net/uk-proxy.htmlzhttps://www.sslproxies.org)r	   )r   r'   s     r   	__websitezFreeProxy.__website-   s-    0__&-__&>/r   c                    | j                   sdn|d   j                         | j                   v }| j                  sdnd|d   j                         v }| j                  r| j                  rdnd|d   j                         k(  }ddd}| j                  dn.| j                  |j                  |d   j                               k(  }| j                  d	k(  rdn#|d
   j                         j                         dk(  }|xr |xr
 |xr |xr |S )NT   r      	anonymousF)yesno   r      r6   )r	   r%   r   r   r   r   r   lower)r   row_elementscountry_criteriaelite_criteriaanonym_criteriaswitchgoogle_criteriahttps_criterias           r   
__criteriazFreeProxy.__criteria7   s   '+4LO<X<X =
__=%)ZZWQ@\@\ A
 6
 

 0;|A?[?[?]0] 	U+"&++"5$4;;&**O((*K, <,!%!6LO<X<X =

%'U=mNmm?m_mmr   c                 x   | j                  |      }| j                  rt        j                  |       d}|D ]+  }| j                  d| i}	 | j	                  |      }|r|c S - |s'|s%| j                  d| _        | j                  d      S t        d      # t
        j                  j                  $ r Y w xY w)z>Returns a working proxy that matches the specified parameters.Nzhttp://T)r'   z*There are no working proxies at this time.)r-   r   shuffler   %_FreeProxy__check_if_proxy_is_workingr   r   r    r	   r   r   )r   r'   
proxy_listworking_proxyproxy_addressproxiess         r   r   zFreeProxy.getE   s    ((0
;;NN:&' 	M{{gm_$=>G $ @ @ I (( !		 V*"&8848(( 8: 	: &&77 s   BB98B9c                    | j                    d| j                   }|| j                      j                  d      d   dd  }t        j                  ||| j
                  d      5 }|j                  j                  j                  rL|j                  j                  j                  j                         d   |k(  r|| j                      cd d d        S d d d        y # 1 sw Y   y xY w)Nz://r   r   r3   T)rI   r
   streamr   )
r   r   splitr   r   r
   raw
connectionsockgetpeername)r   rI   r   iprs        r   __check_if_proxy_is_workingz%FreeProxy.__check_if_proxy_is_workingZ   s    S
+T[[!'',Q/3\\#wTR 	,VWuu$$)9)9)>)>)J)J)LQ)OSU)Ut{{+	, 	, 	, 		, 	s   #A#CC")Ng      ?FFFNFzhttps://www.google.com)F)
__name__
__module____qualname____doc__r   r-   r   r$   r   rE    r   r   r   r      s'    M0n:*r   r   )r   	lxml.htmlhtmlr   r   	fp.errorsr   r   rX   r   r   <module>r\      s       (U Ur   