|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# File: example_websocket_api.py |
| 5 | +# |
| 6 | +# Part of ‘UNICORN Binance WebSocket API’ |
| 7 | +# Project website: https://www.lucit.tech/unicorn-binance-websocket-api.html |
| 8 | +# Github: https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api |
| 9 | +# Documentation: https://unicorn-binance-websocket-api.docs.lucit.tech |
| 10 | +# PyPI: https://pypi.org/project/unicorn-binance-websocket-api |
| 11 | +# LUCIT Online Shop: https://shop.lucit.services/software |
| 12 | +# |
| 13 | +# Author: LUCIT Systems and Development |
| 14 | +# |
| 15 | +# Copyright (c) 2019-2024, LUCIT Systems and Development (https://www.lucit.tech) |
| 16 | +# All rights reserved. |
| 17 | +# |
| 18 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 19 | +# copy of this software and associated documentation files (the |
| 20 | +# "Software"), to deal in the Software without restriction, including |
| 21 | +# without limitation the rights to use, copy, modify, merge, publish, dis- |
| 22 | +# tribute, sublicense, and/or sell copies of the Software, and to permit |
| 23 | +# persons to whom the Software is furnished to do so, subject to the fol- |
| 24 | +# lowing conditions: |
| 25 | +# |
| 26 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 27 | +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 28 | +# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 29 | +# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 30 | +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 31 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 32 | +# IN THE SOFTWARE. |
| 33 | + |
| 34 | +from unicorn_binance_websocket_api.manager import BinanceWebSocketApiManager |
| 35 | +from unicorn_binance_rest_api import BinanceRestApiManager |
| 36 | +import asyncio |
| 37 | +import logging |
| 38 | +import os |
| 39 | +import time |
| 40 | + |
| 41 | +api_key = "" |
| 42 | +api_secret = "" |
| 43 | + |
| 44 | + |
| 45 | +async def binance_stream(ubwa): |
| 46 | + async def handle_socket_message(stream_id=None): |
| 47 | + while ubwa.is_stop_request(stream_id=stream_id) is False: |
| 48 | + data = await ubwa.get_stream_data_from_asyncio_queue(stream_id=stream_id) |
| 49 | + try: |
| 50 | + if data['result']['serverTime'] is not None: |
| 51 | + print(f"UBWA: Time between request and answer: {(time.time() - start_time_get_server_time_ubwa)} " |
| 52 | + f"seconds\r\nreceived data:\r\n{data}\r\n") |
| 53 | + except KeyError: |
| 54 | + print(f"received data:\r\n{data}\r\n") |
| 55 | + except TypeError: |
| 56 | + print(f"received data:\r\n{data}\r\n") |
| 57 | + |
| 58 | + api_stream = ubwa.create_stream(api=True, api_key=api_key, api_secret=api_secret, |
| 59 | + stream_label="Bobs Websocket API", |
| 60 | + process_asyncio_queue=handle_socket_message) |
| 61 | + time.sleep(5) |
| 62 | + for i in range(10): |
| 63 | + print(f"##################################################################") |
| 64 | + start_time_get_server_time_ubwa = time.time() |
| 65 | + ubwa.api.get_server_time(stream_id=api_stream) |
| 66 | + ubwa.api.get_server_time(stream_id=api_stream) |
| 67 | + ubwa_server_time = ubwa.api.get_server_time(stream_id=api_stream, return_response=True) |
| 68 | + print(f"UBRA: Time between request and answer: {(time.time() - start_time_get_server_time_ubwa)} " |
| 69 | + f"seconds\r\nreceived data:\r\n{ubwa_server_time}\r\n") |
| 70 | + print(f"Finished! Waiting for responses ...") |
| 71 | + time.sleep(2) |
| 72 | + print(f"------------------------------------------------------------------") |
| 73 | + start_time_get_server_time_ubra = time.time() |
| 74 | + ubra_server_time = ubra.get_server_time() |
| 75 | + print(f"UBRA: Time between request and answer: {(time.time() - start_time_get_server_time_ubra)} " |
| 76 | + f"seconds\r\nreceived data:\r\n{ubra_server_time}\r\n") |
| 77 | + ubra_server_time = ubra.get_server_time() |
| 78 | + print(f"UBRA: Time between request and answer: {(time.time() - start_time_get_server_time_ubra)} " |
| 79 | + f"seconds\r\nreceived data:\r\n{ubra_server_time}\r\n") |
| 80 | + ubra_server_time = ubra.get_server_time() |
| 81 | + print(f"UBRA: Time between request and answer: {(time.time() - start_time_get_server_time_ubra)} " |
| 82 | + f"seconds\r\nreceived data:\r\n{ubra_server_time}\r\n") |
| 83 | + print(f"Stopping!") |
| 84 | + ubwa.stop_manager() |
| 85 | + ubra.stop_manager() |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + logging.getLogger("unicorn_binance_websocket_api") |
| 89 | + logging.basicConfig(level=logging.DEBUG, |
| 90 | + filename=os.path.basename(__file__) + '.log', |
| 91 | + format="{asctime} [{levelname:8}] {process} {thread} {module}: {message}", |
| 92 | + style="{") |
| 93 | + |
| 94 | + ubwa = BinanceWebSocketApiManager(exchange='binance.com', output_default="dict") |
| 95 | + ubra = BinanceRestApiManager(exchange='binance.com') |
| 96 | + try: |
| 97 | + asyncio.run(binance_stream(ubwa)) |
| 98 | + except KeyboardInterrupt: |
| 99 | + print("\r\nGracefully stopping ...") |
| 100 | + ubwa.stop_manager() |
| 101 | + ubra.stop_manager() |
0 commit comments