Ошибка 429 ("Too Many Requests")

Мой Код:

import time
import re
import requests
import redis
import json
import logging
from flask import Flask, request, jsonify

app = Flask(__name__)
redis_client = redis.StrictRedis(host='localhost', port=6379, db=0, decode_responses=True)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

def get_item_from_api(m_value):
    api_url = f"https://api.csgofloat.com/?url=https://steamcommunity.com/profiles/76561198034390551/inventory/#730_2_{m_value}"
    attempt = 0
    max_attempts = 5
    backoff_factor = 10  # Увеличено начальное время задержки

    while attempt < max_attempts:
        response = requests.get(api_url)
        if response.status_code == 429:
            wait_time = int(response.headers.get('Retry-After', backoff_factor))
            logging.error(f"Attempt {attempt + 1}: 429 Too Many Requests. Retrying in {wait_time} seconds.")
            time.sleep(wait_time)
            backoff_factor *= 2  # Увеличение задержки экспоненциально
            attempt += 1
        elif response.status_code == 200:
            return response.json()
        else:
            response.raise_for_status()  # Вызов исключения для других HTTP ошибок

    logging.error("Failed to fetch data from API after maximum retries.")
    return None

@app.route('/api/item', methods=['GET'])
def get_item_details():
    link = request.args.get('link')
    if not link:
        return jsonify({"error": "Invalid link format"}), 400

    m_value = re.search(r'M(\d+)', link)
    if not m_value:
        return jsonify({"error": "Invalid link format"}), 400

    m_value = m_value.group(1)
    item_details = get_item_from_api(m_value)
    if item_details:
        redis_client.setex(m_value, 3600, json.dumps(item_details))
        return jsonify(item_details)

    error_message = "Item details could not be fetched"
    redis_client.setex(m_value, 120, json.dumps({"error": error_message}))
    return jsonify({"error": error_message}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

Вывод:

2024-06-05 17:43:34,244 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:8000
 * Running on http://192.168.1.96:8000
2024-06-05 17:43:34,245 - INFO - Press CTRL+C to quit
2024-06-05 17:43:39,643 - DEBUG - Starting new HTTPS connection (1): api.csgofloat.com:443
2024-06-05 17:43:39,950 - DEBUG - https://api.csgofloat.com:443 "GET /?url=https://steamcommunity.com/profiles/76561198034390551/inventory/ HTTP/1.1" 429 132
2024-06-05 17:43:39,951 - ERROR - Attempt 1: 429 Too Many Requests. Retrying in 10 seconds.
2024-06-05 17:43:49,956 - DEBUG - Starting new HTTPS connection (1): api.csgofloat.com:443
2024-06-05 17:43:50,296 - DEBUG - https://api.csgofloat.com:443 "GET /?url=https://steamcommunity.com/profiles/76561198034390551/inventory/ HTTP/1.1" 429 132
2024-06-05 17:43:50,297 - ERROR - Attempt 2: 429 Too Many Requests. Retrying in 20 seconds.
2024-06-05 17:44:10,302 - DEBUG - Starting new HTTPS connection (1): api.csgofloat.com:443
2024-06-05 17:44:10,612 - DEBUG - https://api.csgofloat.com:443 "GET /?url=https://steamcommunity.com/profiles/76561198034390551/inventory/ HTTP/1.1" 429 132
2024-06-05 17:44:10,613 - ERROR - Attempt 3: 429 Too Many Requests. Retrying in 40 seconds.
2024-06-05 17:44:50,619 - DEBUG - Starting new HTTPS connection (1): api.csgofloat.com:443
2024-06-05 17:44:50,910 - DEBUG - https://api.csgofloat.com:443 "GET /?url=https://steamcommunity.com/profiles/76561198034390551/inventory/ HTTP/1.1" 429 132
2024-06-05 17:44:50,911 - ERROR - Attempt 4: 429 Too Many Requests. Retrying in 80 seconds.

Ответы (0 шт):