Python實現http介面請求資料後,往RabbitMQ裡面插入資料

何双新發表於2024-03-14

python實現http介面請求資料服務後,往RABBITmq裡面插入資料

import time
import requests
import pika
import datetime
base_url = 'https://www.okx.com'
api_url = '/api/v5/market/history-mark-price-candles'
# 時間顆粒度 [1m/3m/5m/15m/30m/1H/2H/4H]
time_unit = ['1m', '3m', '5m', '15m', '30m', '1h', '2h', '4h']
# 幣種list
instId_list = ['ADA-USDT-SWAP', 'ANT-USDT-SWAP', 'ATOM-USDT-SWAP', 'BAT-USDT-SWAP', 'BCH-USDT-SWAP', 'BTC-USDT-SWAP', 'CFX-USDT-SWAP', 'DOGE-USDT-SWAP', 'ENS-USDT-SWAP', 'EOS-USDT-SWAP', 'ETC-USDT-SWAP', 'ETH-USDT-SWAP', 'GFT-USDT-SWAP', 'LTC-USDT-SWAP', 'ORDI-USDT-SWAP', 'STORJ-USDT-SWAP', 'TRX-USDT-SWAP', 'WLD-USDT-SWAP', 'XLM-USDT-SWAP', 'XRP-USDT-SWAP', 'YGG-USDT-SWAP']
# 迴圈時間顆粒度查詢
for bar in time_unit:
    # 迴圈幣種
    for line in instId_list:
        # 開始時間
        start_date = '2024-01-01 00:00:00'
        end_date = '2023-01-01 00:00:00'
        start_timestamp = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S').timestamp()
        end_timestamp = datetime.datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S').timestamp()
        start_timestamp = int(start_timestamp * 1000)
        end_timestamp = int(end_timestamp * 1000)
        # 引數url
        instId = line
        after = start_timestamp
        bar = bar
        args_url = '?instId={}&after={}&bar={}'.format(instId, after, bar)
        url = base_url + api_url + args_url

        while after > end_timestamp:
            try:
                response = requests.get(url)
                result = response.json()
                if result['code'] == '0' and result['data']:
                    result.update({'instId': instId, 'bar': bar})
                    # 將result 寫入到rabbitmq 實現邏輯
                    # RabbitMQ連線資訊
                    serverURL = "amqp://mel:mel@1.94.96.154:5672/okx_vhost"
                    exchangeName = "okx_all_exchange"
                    queueName = "okx_all_queue"
                    # 建立RabbitMQ連線
                    connection = pika.BlockingConnection(pika.URLParameters(serverURL))
                    channel = connection.channel()
                    # 宣告exchange和queue
                    channel.exchange_declare(exchange=exchangeName, exchange_type='fanout', durable=True)
                    channel.queue_declare(queue=queueName, durable=True)
                    # 將資料寫入RabbitMQ
                    channel.basic_publish(exchange=exchangeName, routing_key=queueName, body=str(result))
                    # 關閉連線
                    connection.close()
                    # 具體實現邏輯
                    after = result['data'][-1][0]
                    after = int(after)
                    str_date = datetime.datetime.fromtimestamp(after / 1000).strftime('%Y-%m-%d %H:%M:%S')
                    print('===bar===', bar)
                    print('===instId===', instId)
                    print('===str_date===', str_date)
                    # time.sleep(1)
                    # 更新引數url
                    args_url = '?instId={}&after={}&bar={}'.format(instId, after, bar)
                    url = base_url + api_url + args_url
                else:
                    break
            except requests.exceptions.ProxyError:
                print("ProxyError occurred. Retrying...")
                time.sleep(1)  # 延遲1秒後重試
                continue

相關文章