-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathsubscribe_all_card_updates.py
54 lines (44 loc) · 1.19 KB
/
subscribe_all_card_updates.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
import websocket
import json
import time
w_socket = 'wss://ws.sorare.com/cable'
identifier = json.dumps({"channel": "GraphqlChannel"})
subscription_query = {
"query": "subscription { anyCardWasUpdated { slug } }",
"variables": {},
"action": "execute"
}
def on_open(ws):
subscribe_command = {"command": "subscribe", "identifier": identifier}
ws.send(json.dumps(subscribe_command).encode())
time.sleep(1)
message_command = {
"command": "message",
"identifier": identifier,
"data": json.dumps(subscription_query)
}
ws.send(json.dumps(message_command).encode())
def on_message(ws, data):
message = json.loads(data)
type = message.get('type')
if type == 'welcome':
pass
elif type == 'ping':
pass
elif message.get('message') is not None:
print(message['message'])
def on_error(ws, error):
print('Error:', error)
def on_close(ws, close_status_code, close_message):
print('WebSocket Closed:', close_message, close_status_code)
def long_connection():
ws = websocket.WebSocketApp(
w_socket,
on_message=on_message,
on_close=on_close,
on_error=on_error,
on_open=on_open
)
ws.run_forever()
if __name__ == '__main__':
long_connection()