
    ѫfic                     r    d dl m Z  d dlmZmZmZ d dlZddlmZmZm	Z	 ddl
mZ ddlmZmZ  G d d	e      Zy)
    )datetime)DictListOptionalN   )ProxyConversionExceptionProxyFetchExceptionProxyInvalidResponseException)Proxy)ProxyConfigProxyProviderc                        e Zd ZdZdZddgZ	 	 ddedee   dee	   f fdZ
d	ee   fd
Zded	efdZdee   d	ee   fdZ xZS )Webshareaa  Webshare is a proxy provider that offers residential and datacenter proxies.

    Create an account for webshare `here <https://www.webshare.io/?referral_code=3x5812idzzzp>`_ (affiliate link) to get started with 10 free data center proxies.

    You can find your API key in the Webshare dashboard `here <https://dashboard.webshare.io/userapi/keys>`_

    You can find a list of all available parameters in the Webshare API documentation `here <https://apidocs.webshare.io/proxy-list/list#parameters>`_

    :param api_key: Your Webshare API key
    :param search_params: Optional parameters to include in the API requests
    :param config: Configuration for the proxy provider. View the ProxyConfig class docs for more information.

    Example:

    .. code-block:: python

        from proxyproviders import Webshare

        # Initialize the Webshare API client with an API key and optional parameters
        proxy_provider = Webshare(api_key="your-api-key", params={"country_code_in": "US"})

        # Fetch proxies
        proxies = proxy_provider.list_proxies()

        # With config
        from proxyproviders import ProxyConfig

        config = ProxyConfig(refresh_interval=60)
        proxy_provider = Webshare(api_key="your-api-key", params={"country_code_in": "US"}, config=config)

        # Fetch proxies
        proxies = proxy_provider.list_proxies()
    z https://proxy.webshare.io/api/v2httphttpsapi_keysearch_paramsconfigc                 H    t         |   |       || _        |xs i | _        y )N)super__init__r   r   )selfr   r   r   	__class__s       a/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/proxyproviders/providers/webshare.pyr   zWebshare.__init__5   s&     	 *0b    returnc                 J   dd| j                    i}g }d}	 |dz  }d|dd}i || j                  }	 t        j                  | j                   d||	      }|j                          |j                         }|j                  d      }	|	t        |j                        |j                  |	D 
cg c]  }
| j                  |
       c}
       |j                  d      	 |S # t        j                  j                  $ r}t        d
t        |             |d}~ww xY wc c}
w )zUFetches proxies from Webshare API and converts them to the standardized Proxy format.AuthorizationzToken r      directd   )modepage	page_sizez/proxy/list)paramsheaderszFailed to fetch proxies: Nresultsnext)r   r   requestsget	_BASE_URLraise_for_status
exceptionsRequestExceptionr	   strjsonr
   textextend_convert_to_proxy)r   r&   all_proxiesr#   default_paramsr%   responseedata
proxy_dataproxys              r   _fetch_proxieszWebshare._fetch_proxies?   s9   "fT\\N$;<AID  N >=$*<*<=FW#<<~~&k267 ))+ ==?D),J!3HMMBB:V% 6 6u =VWxx'9  &&77 W),Ec!fX*NOUVVW  Ws   5C# /D #D DDr8   c                 F   	 t        |d   |d   |d   |d   t        |d         |j                  d      |j                  d      | j                  |j                  d            | j                  		      S # t
        t        f$ r}t        d
t        |             |d}~ww xY w)z:Converts Webshare's proxy data to the shared Proxy format.idusernamepasswordproxy_addressportcountry_code	city_name
created_at)	r=   r>   r?   r@   rA   rB   rC   rD   	protocolszFailed to convert proxy data: N)	r   intr*   _parse_timestamp
_PROTOCOLSKeyError
ValueErrorr   r/   )r   r8   r7   s      r   r3   zWebshare._convert_to_proxyd   s    	:j)j)"?3f&!XXn5((;/00,1GH//
 
 *% 	*0Q9	s   A1A4 4B BB 	timestampc                 R    |r	 t        j                  |      S y# t        $ r Y yw xY w)z;Parses an ISO 8601 timestamp string into a datetime object.N)r   fromisoformatrJ   )r   rK   s     r   rG   zWebshare._parse_timestampw   s4    --i88   s    	&&)NN)__name__
__module____qualname____doc__r+   rH   r/   r   dictr   r   r   r   r;   r   r3   r   rG   __classcell__)r   s   @r   r   r      s     D 3I'"J
 )-(,	11  ~1 %	1#U #Jd u &(3- HX<N r   r   )r   typingr   r   r   r)   r-   r   r	   r
   models.proxyr   proxy_providerr   r   r    r   r   <module>rX      s1     ' '  
 ! 7o} or   