
    ѫfi                     b    d dl mZmZmZ d dlZddlmZmZ ddlm	Z	 ddl
mZmZ  G d de      Zy)	    )DictListOptionalN   )ProxyFetchExceptionProxyInvalidResponseException)Proxy)ProxyConfigProxyProviderc                        e Zd ZU dZdZdZdZddgZee	   e
d<   	 	 	 dde	d	e	d
ee	   dee   dee   f
 fdZdee   fdZdefdZd	e	de	fdZd	e	defdZdd	e	dee	   defdZ	 	 dde	de	dee   dee   def
dZ xZS )
BrightDataap  BrightData (formerly luminati) is a proxy provider that offers residential and datacenter proxies.

    Create an account `here <https://get.brightdata.com/davidteather>`_ (affiliate link).

    You can find your API key in the account settings `here <https://brightdata.com/cp/setting/users>`_ then create "add token" with scope "limit" (`BrightData article <https://docs.brightdata.com/general/account/api-token>`_ for more info)

    The BrightData API documentation is `here <https://docs.brightdata.com/api-reference/account-management-api/Get_active_Zones?playground=open>`_

    :param api_key: Your BrightData API key
    :param zone: The zone ID/name to fetch proxies for (you can get this from the BrightData dashboard)
    :param username_suffix: Optional suffix to append to the username `more info <https://docs.brightdata.com/proxy-networks/config-options>`_ allows you to target region, city, etc. (requires use_super_proxy=True)
    :param use_super_proxy: Optional flag to use super proxy instead of targeting specific IPs, this is enabled by default. If you want to target specific IPs or have consistent IPs for a session, set this to False.
    :param config: Configuration for the proxy provider. View the ProxyConfig class docs for more information.

    Example:

    .. code-block:: python

        from proxyproviders import BrightData

        # Initialize the BrightData API client with an API key and a zone
        proxy_provider = BrightData(api_key="your-api-key", zone="my_zone")

        # Fetch proxies
        proxies = proxy_provider.list_proxies() # returns one proxy for super proxy by default

        # If you want to manage specific IPs
        proxy_provider = BrightData(api_key="your-api-key", zone="my_zone", use_super_proxy=False)
        proxies = proxy_provider.list_proxies() # returns multiple proxies for each IP in the zone (potentially thousands)
    zhttps://api.brightdata.comzbrd.superproxy.ioi7  httphttps
_PROTOCOLSapi_keyzoneusername_suffixuse_super_proxyconfigc                 \    t         |   |       || _        || _        || _        || _        y N)super__init__r   r   r   r   )selfr   r   r   r   r   	__class__s         c/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/proxyproviders/providers/brightdata.pyr   zBrightData.__init__/   s0     	 	..    returnc                     | j                  | j                        }| j                  r|| j                  z  }| j                  | j                        }| j                  r6t        d||d   d   | j                  | j                  | j                        gS g }| j                  | j                        }|D ]X  }|d|d    z   }|j                  t        |d   ||d   d   | j                  | j                  |d   | j                               Z |S )	Nr   	passwordsr   )idusernamepasswordproxy_addressport	protocolsz-ip-ipcountry)r!   r"   r#   r$   r%   country_coder&   )get_zone_usernamer   r   get_zone_passwordsr   r	   _SUPER_PROXY_ADDRESS_SUPER_PROXY_PORTr   list_all_ips_in_zoneappend)r   r"   r    proxiesipsr'   ip_targeted_usernames          r   _fetch_proxieszBrightData._fetch_proxies=   s    ))$))4,,,H++DII6	 %&{3A6"&";";//"oo	 	  ''		2 	B#+RXJ.?#? NN$x1&{3A6"&";";//!#I"oo
	 r   c                 &    | j                  dd      S )zFetches active zones from BrightData API.

        Response:

        .. code-block:: json

            [
                {
                    "name": "zone1",
                    "type": "dc",
                }
            ]

        z/zone/get_active_zonesGET_make_request)r   s    r   get_active_zoneszBrightData.get_active_zonesh   s     !!":EBBr   c                 z    | j                  ddd|i      }|j                  d      }|st        d      d| d| S )	a  Fetches zone username for the given zone ID from BrightData API.

        Note: this isn't directly an API endpoint, I'm sort of reconstructing some things here and it seems to behave a little weird.

        :param zone: The zone ID to fetch username for
        z/statusr5   r   paramscustomerzFailed to fetch customer datazbrd-customer-z-zone-)r7   getr   )r   r   datar<   s       r   r*   zBrightData.get_zone_usernamey   sO     !!GeVTN!K88J'/0OPPxjtf55r   c                 .    | j                  ddd|i      S )a0  Fetches zone passwords from BrightData API.

        :param zone: The zone ID to fetch passwords for

        Response:


        .. code-block:: json

            {
                "passwords": [
                    "password1",
                    "password2",
                ]
            }

        z/zone/passwordsr5   r   r:   r6   )r   r   s     r   r+   zBrightData.get_zone_passwords   s     $ !!OeVTN!SSr   r(   c                 2    | j                  dd|d|d      S )at  Fetches all IPs in a zone from BrightData API.

        :param zone: The zone ID to fetch IPs for
        :param country: Optional 2-letter country code to filter IPs by

        Response:


        .. code-block:: json

            [
                {
                    "ip": "192.168.1.1",
                    "country": "US",
                }
            ]

        z/zone/route_ipsr5   T)r   list_countriesr(   r:   r6   )r   r   r(   s      r   r.   zBrightData.list_all_ips_in_zone   s,    & !! DWM " 
 	
r   pathmethodr;   jsonc                 X   dd| j                    i}| j                   | }t        j                  |||||      }|j                  dk7  r%t        d|j                   d|j                         	 |j                         }|S # t        $ r}	t        dt        |	             |	d}	~	ww xY w)	a  Makes a request to the BrightData API.

        :param path: The path to the endpoint
        :param method: The HTTP method to use
        :param params: Optional parameters to include in the request
        :param json: Optional JSON data to include in the request
        AuthorizationzBearer )headersr;   rD      z1Failed to fetch from BrightData, got status code z, text: zFailed to parse response: N)r   	_BASE_URLrequestsrequeststatus_coder   textrD   	Exceptionr   str)
r   rB   rC   r;   rD   rG   urlresponser>   es
             r   r7   zBrightData._make_request   s      wt||n5
  '##Cd
 3&%CHDXDXCYYabjboboapq 	==?D   	/,SVH5	s   1B 	B)B$$B))NTNr   )NN)__name__
__module____qualname____doc__rI   r,   r-   r   r   rO   __annotations__r   boolr
   r   r	   r3   r   r8   r*   r+   r.   r7   __classcell__)r   s   @r   r   r   
   s'   > -I.#W-JS	- *.*.(,// / "#	/
 "$/ %/)U )VC$ C"6c 6c 6 Ts Tt T(
 
x} 
PT 
: "&#$$ $ 	$
 tn$ 
$r   r   )typingr   r   r   rJ   
exceptionsr   r   models.proxyr	   proxy_providerr
   r   r    r   r   <module>r_      s%    ' '  K   7P Pr   