Last active
September 25, 2021 09:23
-
-
Save whittlem/728b7081abddc51ede48fc8495455d24 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, time | |
from threading import Thread | |
from websocket import create_connection, WebSocketConnectionClosedException | |
def main(): | |
ws = None | |
thread = None | |
thread_running = False | |
thread_keepalive = None | |
def websocket_thread(): | |
global ws | |
ws = create_connection("wss://ws-feed.pro.coinbase.com") | |
ws.send( | |
json.dumps( | |
{ | |
"type": "subscribe", | |
"product_ids": ['BTC-USD'], | |
"channels": ["matches"], | |
} | |
) | |
) | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment