o
    !\i                     @   sP   d Z ddlZddlZddlmZ ddlmZ ddlZee	Z
G dd dZdS )zFlashscore HTTP client    N)Optional)settingsc                   @   sF   e Zd ZdZdd Zdd Zddeded	ee	j
 fd
dZdd ZdS )FlashscoreClientz-HTTP client for Flashscore with rate limitingc                 C   s:   t j| _t j| _t j| _d| _tj	dd| jidd| _
d S )Ng        g      >@z
User-AgentT)timeoutheadersfollow_redirects)r   flashscore_base_urlbase_urlflashscore_rate_limit_secondsrate_limit_secondsflashscore_user_agent
user_agentlast_request_timehttpxClientclientself r   a/var/www/www-root/data/www/176.119.141.140/sports-predictor/app/data_sources/flashscore/client.py__init__   s   zFlashscoreClient.__init__c                 C   s:   t   | j }|| jk r| j| }t | t   | _dS )zApply rate limitingN)timer   r   sleep)r   elapsed
sleep_timer   r   r   _rate_limit   s
   


zFlashscoreClient._rate_limit   urlretriesreturnc                 C   s   | dr|n| j | }t|D ]Y}z|   | j|}|  |W   S  tjyk } z7t	
d|d  d| d|  ||d krZt	d| d| d W Y d	}~ d	S td
|  W Y d	}~qd	}~ww d	S )z*GET request with retries and rate limitinghttpzRequest failed (attempt    /z): zFailed to fetch z after z	 attemptsN   )
startswithr	   ranger   r   getraise_for_statusr   	HTTPErrorloggerwarningerrorr   r   )r   r   r   full_urlattemptresponseer   r   r   r&   !   s    
 zFlashscoreClient.getc                 C   s   | j   dS )zClose the clientN)r   closer   r   r   r   r0   4   s   zFlashscoreClient.closeN)r   )__name__
__module____qualname____doc__r   r   strintr   r   Responser&   r0   r   r   r   r   r      s    r   )r4   r   r   typingr   app.core.configr   logging	getLoggerr1   r)   r   r   r   r   r   <module>   s    
