#!/usr/bin/env python3

import asyncio
import json
import websockets

URI = "wss://testapi.tprofit.io/ws/"


async def listen():
    async with websockets.connect(
        URI,
        ping_interval=20,
        ping_timeout=20,
        max_queue=2
    ) as ws:

        print("Connected to server. Waiting for 5-seconds snapshots...")

        try:
            while True:
                msg = await ws.recv()  # will arrive every 5 seconds from server
                data = json.loads(msg)

                print("Snapshot received:")
                print(data)

        except websockets.exceptions.ConnectionClosed:
            print("Connection closed")
        except Exception as e:
            print("Error:", e)


if __name__ == "__main__":
    asyncio.run(listen())