-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.py
61 lines (50 loc) · 2.02 KB
/
subscriber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import asyncio
import aiohttp
import argparse
import json
from datetime import datetime
# local: "ws://localhost:8080/ws"
BACKEND = "ws://localhost:8080/ws"
async def subscribe_to_meals(jwt_token: str):
"""Subscribe to meal analysis updates via WebSocket"""
url = f"{BACKEND}?token={jwt_token}"
async with aiohttp.ClientSession() as session:
try:
async with session.ws_connect(url) as ws:
print("Connected to WebSocket server")
print("Waiting for messages...")
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
try:
data = json.loads(msg.data)
timestamp = datetime.fromisoformat(
data["data"]["timestamp"]
)
print("\nReceived message at", timestamp)
print(json.dumps(data, indent=2))
except Exception as e:
print(f"Error parsing message: {e}")
print("Raw message:", msg.data)
elif msg.type == aiohttp.WSMsgType.ERROR:
print(f"WebSocket error: {ws.exception()}")
break
elif msg.type == aiohttp.WSMsgType.CLOSED:
print("WebSocket connection closed")
break
except aiohttp.ClientError as e:
print(f"Connection error: {e}")
except Exception as e:
print(f"Unexpected error: {e}")
def main():
parser = argparse.ArgumentParser(
description="Subscribe to Calorily meal analysis updates"
)
parser.add_argument("jwt", help="JWT token for authentication")
args = parser.parse_args()
try:
asyncio.run(subscribe_to_meals(args.jwt))
except KeyboardInterrupt:
print("\nSubscriber stopped by user")
if __name__ == "__main__":
main()