#!/usr/bin/env python3 """ Test script to read data from Redis Streams """ import redis import orjson import sys def test_read_streams(): """Read and display data from all Redis Streams""" # Connect to Redis try: r = redis.Redis(host='localhost', port=6379, db=0, decode_responses=False) r.ping() print("āœ“ Connected to Redis\n") except Exception as e: print(f"āœ— Failed to connect to Redis: {e}") sys.exit(1) # Stream keys to read streams = [ 'binance:raw:kline:5m', 'binance:raw:depth:20', 'binance:raw:trade', ] print("=" * 80) print("Reading data from Redis Streams") print("=" * 80) for stream_key in streams: print(f"\nšŸ“Š Stream: {stream_key}") print("-" * 80) try: # Get stream length length = r.xlen(stream_key) print(f"Stream length: {length}") if length == 0: print("No data available yet\n") continue # Read last 3 messages messages = r.xrevrange(stream_key, count=3) for i, (msg_id, fields) in enumerate(messages, 1): print(f"\n[Message {i}]") print(f"ID: {msg_id.decode()}") # Parse JSON data data = orjson.loads(fields[b'data']) # Display based on stream type if 'kline' in stream_key: kline = data.get('k', {}) print(f"Symbol: {data.get('s')}") print(f"Open: {kline.get('o')}") print(f"High: {kline.get('h')}") print(f"Low: {kline.get('l')}") print(f"Close: {kline.get('c')}") print(f"Volume: {kline.get('v')}") print(f"Closed: {kline.get('x')}") elif 'depth' in stream_key: print(f"Symbol: {data.get('s')}") print(f"Event time: {data.get('E')}") print(f"First update ID: {data.get('U')}") print(f"Last update ID: {data.get('u')}") bids = data.get('b', [])[:3] asks = data.get('a', [])[:3] print(f"Top 3 bids: {bids}") print(f"Top 3 asks: {asks}") elif 'trade' in stream_key: print(f"Symbol: {data.get('s')}") print(f"Price: {data.get('p')}") print(f"Quantity: {data.get('q')}") print(f"Time: {data.get('T')}") print(f"Buyer is maker: {data.get('m')}") print(f"Received at: {data.get('_received_at')}") except Exception as e: print(f"Error reading stream {stream_key}: {e}") print("\n" + "=" * 80) print("āœ“ Test completed") print("=" * 80) if __name__ == "__main__": test_read_streams()