Created
December 30, 2017 02:10
-
-
Save xtacocorex/ff7302af0374dd490ccad0de4b0403cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# SIMPLE COIN MONITOR USING | |
# https://pypi.python.org/pypi/websocket-client | |
import websocket | |
import json | |
import time | |
# CONSTANTS | |
URL = "wss://api.hitbtc.com/api/2/ws" | |
# BASED ON THE LONG LIVED CONNECTION EXAMPLE IN THE API DOCS | |
def on_message(ws, message): | |
print(json.loads(message)) | |
def on_error(ws, error): | |
print(error) | |
def on_close(ws): | |
print("### CONNECTION CLOSED ###") | |
def on_open(ws): | |
# CREATE INITIAL REQUEST | |
request = {"method": 'subscribeTicker', | |
"id": 1, | |
"params" : { | |
"symbol" : "NEOBTC" | |
}} | |
# SEND INITIAL REQUEST | |
ws.send(json.dumps(request)) | |
def main(): | |
# SETUP THE WEBSOCKET CLIENT STUFF | |
websocket.enableTrace(True) | |
ws = websocket.WebSocketApp(URL, | |
on_message = on_message, | |
on_error = on_error, | |
on_close = on_close) | |
ws.on_open = on_open | |
# DO THIS SO WE CAN CTRL-C TO KILL SHIZ, NEED TO CTRL-C TWICE THOUGH... | |
try: | |
ws.run_forever() | |
while True: | |
time.sleep(1) | |
print("SHUTTING DOWN") | |
except KeyboardInterrupt: | |
# CLOSE THE WEB SOCKET | |
ws.close() | |
finally: | |
# WANT THIS PRINT TO PUSH THINGS DOWN TO A NEW LINE | |
print("") | |
print("EXITING") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment