Last active
September 3, 2018 19:43
-
-
Save sacOO7/3a0040b1e7e53a5c68d37a3347fafde2 to your computer and use it in GitHub Desktop.
Coinigy API client implementation in python using Socketcluster python client (https://github.com/sacOO7/socketcluster-client-python)
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
# This is a console python application to connect conigy api, please replace api keys with your own coinigy credentials | |
from socketclusterclient import Socketcluster | |
import json | |
import logging | |
api_credentials = json.loads('{}') | |
api_credentials["apiKey"]="xxx" | |
api_credentials["apiSecret"]="xxx" | |
logger = logging.getLogger(__name__) | |
logger.addHandler(logging.StreamHandler()) | |
logger.setLevel(logging.DEBUG) | |
def your_code_starts_here(socket): | |
###Code for subscription | |
socket.subscribe('TRADE-BITF--BTC--USD') # Channel to be subscribed | |
def channelmessage(key, data): # Messages will be received here | |
logger.info("Data received :: " + json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))+" from channel "+key) | |
socket.onchannel('TRADE-BITF--BTC--USD', channelmessage) # This is used for watching messages over channel | |
###Code for emit | |
def exchangeack(eventname, error, data): | |
logger.info("Exchange List :: " + json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))) | |
socket.emitack("exchanges",None, exchangeack) | |
def channelack(eventname, error, data): | |
logger.info("Channel list :: " + json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))) | |
# Exchange code should be sent as parameter to set of channels | |
socket.emitack("channels", "GDAX", channelack) | |
def onconnect(socket): | |
logger.info("Connected to server") | |
def ondisconnect(socket): | |
logger.info("Disconnected from server") | |
def onConnectError(socket, error): | |
logger.info("Error occured while connecting to server " + error) | |
def onSetAuthentication(socket, token): | |
logger.info("Received auth Token " + token) | |
socket.setAuthtoken(token) | |
def onAuthentication(socket, isauthenticated): | |
logger.info("Received authentication status " + str(isauthenticated)) | |
def ack(eventname, error, data): | |
logger.info("token is "+ json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))) | |
your_code_starts_here(socket); | |
socket.emitack("auth", api_credentials, ack) | |
if __name__ == "__main__": | |
socket = Socketcluster.socket("wss://sc-02.coinigy.com/socketcluster/") | |
socket.setBasicListener(onconnect, ondisconnect, onConnectError) | |
socket.setAuthenticationListener(onSetAuthentication, onAuthentication) | |
socket.setreconnection(False) | |
socket.enablelogger(True) | |
socket.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I need some help, this code don't work for me :(