
    ѫfi                          d Z ddlZddlZddlmZmZ ddlmZmZ ddl	m
Z
  G d de      Z G d	 d
e      Z G d de      Z G d de      Zy)z:Selection algorithms for choosing proxies from a provider.    N)ABCabstractmethod)DictList   )Proxyc                   0    e Zd ZdZedee   defd       Zy)	Algorithma  Base class for proxy selection algorithms.

    Users can extend this class to create custom proxy selection logic.

    Example:
        >>> class MyAlgorithm(Algorithm):
        ...     def select(self, proxies):
        ...         return proxies[0]  # Always return first proxy
        ...
        >>> algorithm = MyAlgorithm()
        >>> proxy = provider.get_proxy(algorithm)
    proxiesreturnc                      y)zSelect a proxy from the list.

        :param proxies: List of available proxies
        :return: Selected proxy
        :raises ValueError: If proxy list is empty
        N selfr   s     Y/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/proxyproviders/algorithms.pyselectzAlgorithm.select   s     	    N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r
   r
      s-     d5k e  r   r
   c                   &    e Zd ZdZdee   defdZy)RandomzRandom proxy selection algorithm.

    Selects a random proxy from the available list on each call.

    Example:
        >>> algorithm = Random()
        >>> proxy = provider.get_proxy(algorithm)
    r   r   c                 F    |st        d      t        j                  |      S )zSelect a random proxy from the list.

        :param proxies: List of available proxies
        :return: Randomly selected proxy
        :raises ValueError: If proxy list is empty
        #Cannot select from empty proxy list)
ValueErrorrandomchoicer   s     r   r   zRandom.select.   s"     BCC}}W%%r   Nr   r   r   r   r   r   r   r   r   r   r   r   $   s    	&d5k 	&e 	&r   r   c                   ,    e Zd ZdZd Zdee   defdZy)
RoundRobinaz  Round-robin proxy selection algorithm with state tracking.

    Cycles through proxies in order, maintaining state across calls.
    Each RoundRobin instance maintains its own independent state.

    Example:
        >>> algorithm = RoundRobin()
        >>> proxy1 = provider.get_proxy(algorithm)
        >>> proxy2 = provider.get_proxy(algorithm)  # Next proxy in sequence
    c                 D    d| _         t        j                         | _        y)z5Initialize round-robin algorithm with state tracking.r   N)_current_index	threadingLock_lock)r   s    r   __init__zRoundRobin.__init__F   s    ^^%
r   r   r   c                     |st        d      | j                  5  || j                  t        |      z     }| j                  dz   t        |      z  | _        ddd       |S # 1 sw Y   S xY w)zSelect next proxy in round-robin sequence.

        :param proxies: List of available proxies
        :return: Next proxy in round-robin sequence
        :raises ValueError: If proxy list is empty
        r   r   N)r   r&   r#   len)r   r   selected_proxys      r   r   zRoundRobin.selectK   sr     BCCZZ 	K$T%8%83w<%GHN#'#6#6#:c'l"JD	K 		K s   <A  A*N)r   r   r   r   r'   r   r   r   r   r   r   r!   r!   :   s#    	&
d5k e r   r!   c                   &    e Zd ZdZdee   defdZy)Firsta  First proxy selection algorithm.

    Always selects the first proxy from the list.
    Useful for deterministic behavior or when proxies are pre-sorted.

    Example:
        >>> algorithm = First()
        >>> proxy = provider.get_proxy(algorithm)  # Always first proxy
    r   r   c                 &    |st        d      |d   S )zSelect the first proxy from the list.

        :param proxies: List of available proxies
        :return: First proxy in the list
        :raises ValueError: If proxy list is empty
        r   r   )r   r   s     r   r   zFirst.selectg   s     BCCqzr   Nr   r   r   r   r,   r,   \   s    	d5k 	e 	r   r,   )r   r   r$   abcr   r   typingr   r   models.proxyr   r
   r   r!   r,   r   r   r   <module>r1      sH    @   #   2&Y &, DI r   