-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathscript-coinbase-webhooks.py
87 lines (73 loc) · 2.34 KB
/
script-coinbase-webhooks.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import sys
import json
import time
import hmac
import hashlib
from threading import Thread
from websocket import create_connection, WebSocketConnectionClosedException
sys.path.insert(0, ".")
from controllers.PyCryptoBot import PyCryptoBot # noqa: E402
from models.exchange.coinbase import AuthAPI as CBAuthAPI # noqa: E402
def main():
ws = None
thread = None
thread_running = False
thread_keepalive = None
app = PyCryptoBot(exchange="coinbase")
def websocket_thread():
global ws
channel = "level2"
timestamp = str(int(time.time()))
product_ids = ["BTC-USD"]
product_ids_str = ",".join(product_ids)
message = f"{timestamp}{channel}{product_ids_str}"
signature = hmac.new(app.api_secret.encode("utf-8"), message.encode("utf-8"), digestmod=hashlib.sha256).hexdigest()
ws = create_connection("wss://advanced-trade-ws.coinbase.com")
ws.send(
json.dumps(
{
"type": "subscribe",
"product_ids": [
"BTC-USD",
],
"channel": channel,
"api_key": app.api_key,
"timestamp": timestamp,
"signature": signature,
}
)
)
thread_keepalive.start()
while not thread_running:
try:
data = ws.recv()
if data != "":
msg = json.loads(data)
else:
msg = {}
except ValueError as e:
print(e)
print("{} - data: {}".format(e, data))
except Exception as e:
print(e)
print("{} - data: {}".format(e, data))
else:
if "result" not in msg:
print(msg)
try:
if ws:
ws.close()
except WebSocketConnectionClosedException:
pass
finally:
thread_keepalive.join()
def websocket_keepalive(interval=30):
global ws
while ws.connected:
ws.ping("keepalive")
time.sleep(interval)
thread = Thread(target=websocket_thread)
thread_keepalive = Thread(target=websocket_keepalive)
thread.start()
if __name__ == "__main__":
main()