def _run_websocket(self, ws: WebSocketApp) -> None: try: ws.on_open = self._wrap_callback(self._on_ws_open_callback) ws.run_forever(origin=self.ws_origin) except Exception as e: raise Exception(f'Unexpected error while running websocket: {e}') finally: self._reconnect(ws)
def crawl_start(self, url, ws_headers): enableTrace(False) # 参数是True时,会显示发送的msg ws = WebSocketApp(url=url, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close, header=ws_headers) ws.on_open = self.on_open ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) # 忽略认证
def main(): streamlist = list() streamlist.append('btcusdt@depth') streamlist.append('btcusdt@trade') if len(streamlist) > 1: stream_string = '/stream?streams=' for stream in streamlist: stream_string = '%s%s/' % (stream_string, stream) else: stream_string = '/ws/%s' % streamlist[0] ws = WebSocketApp('%s%s' % (WSS_URL, stream_string), on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open while True: ws.run_forever(http_proxy_host=proxy_url, http_proxy_port=proxy_port)
def __init__(self, oauth_token, channel, nick, logger=None, post_login=None): self.commands = {} self.oauth_token = oauth_token self.channel = channel self.nick = nick self.logger = logger self.post_login = post_login if self.logger is None: self.logger = logging.getLogger(__name__) ws = WebSocketApp('wss://irc-ws.chat.twitch.tv:443') def on_open(sock): self.on_open(sock) ws.on_open = on_open def on_message(sock, msg): self.process_message(sock, msg) ws.on_message = on_message self.ws = ws
def QA_SU_subscribe_bitmex_websocket(symbol='', is_orderbookl2=False): symbol_list = [] if type(symbol) == str: if symbol.find(',') > 0: symbol = symbol.split(',') if type(symbol) == str and len(symbol) > 0: symbol_list = [symbol] if is_orderbookl2: symbol_list.append('OrderBookL2:' % symbol) elif type(symbol) == list: symbol_list = symbol else: symbol_infolist = QA_fetch_bitmex_symbols(active=True) for index, symbol_info in enumerate(symbol_infolist): symbol_list.append('%s' % symbol_info['symbol']) subscribe_trade_str = '%s%s' % ('trade:', ',trade:'.join( (str(symbol) for symbol in symbol_list))) if is_orderbookl2: subscribe_orderBookL2_str = '%s%s' % ('orderBookL2:', ',orderBookL2:'.join( (str(symbol) for symbol in symbol_list))) subscribe_str = '%s,%s' % (subscribe_trade_str, subscribe_orderBookL2_str) else: subscribe_str = subscribe_trade_str stream_string = 'subscribe=%s' % subscribe_str ws = WebSocketApp('%s%s' % (WSS_URL, stream_string), on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open while True: ws.run_forever(http_proxy_host=proxy_url, http_proxy_port=proxy_port)
def QA_SU_subscribe_binance_websocket(symbol='', is_depth=False): streamlist = [] if type(symbol) == str: if symbol.find(',') > 0: symbol = symbol.split(',') if type(symbol) == str and len(symbol) > 0: streamlist.append('%s@trade' % symbol) if is_depth: streamlist.append('%s@depth' % symbol) elif type(symbol) == list: for symbol_name in symbol: streamlist.append('%s@trade' % symbol_name) if is_depth: streamlist.append('%s@depth' % symbol_name) else: symbol_infolist = QA_fetch_binance_symbols() for index, symbol_info in enumerate(symbol_infolist): streamlist.append('%s@trade' % symbol_info['symbol']) if is_depth: streamlist.append('%s@depth' % symbol_info['symbol']) if len(streamlist) > 1: stream_string = '/stream?streams=' for stream in streamlist: stream_string = '%s%s/' % (stream_string, stream) else: stream_string = '/ws/%s' % streamlist[0] stream_string = stream_string.lower() QA_util_log_debug('Binance subscribe string: %s' % stream_string) ws = WebSocketApp('%s%s' % (WSS_URL, stream_string), on_message=on_message, on_error=on_error, on_close=on_close) # ws = WebSocketApp('%s' % WSS_URL, on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open while True: ws.run_forever(http_proxy_host=proxy_url, http_proxy_port=proxy_port)
d = Path().resolve().parent with open(str(d) + '/credentials.txt', 'r') as file: credentials = file.read().replace('\n', '') mongo_client = MongoClient(credentials) # e.g. 'mongodb://localhost:27017' db = mongo_client['cryptocurrency'] hitbtc_coll = db['hitbtc'] def on_message(mes): #print(mes) message = json.loads(mes) result = {'_id': message['params']['data'][0]['id'], 'p': message['params']['data'][0]['price'], 'q': message['params']['data'][0]['quantity'], 'side': message['params']['data'][0]['side'], 't': message['params']['data'][0]['timestamp'], 's': message['params']['symbol'], 'date': datetime.datetime.utcnow().strftime('%Y-%m-%d')} print(datetime.datetime.now()) print(result) res = hitbtc_coll.insert_one(result) ws = WebSocketApp('wss://api.hitbtc.com/api/2/ws') ws.on_open = lambda self: self.send(json.dumps({ "method": "subscribeTrades", #"product_ids": ["BTC-USD", "ETH-USD", "LTC-USD"], "params": { "symbol": "XLMUSD" } })) ws.on_message = lambda self, evt: on_message(evt) ws.run_forever(ping_interval=10, ping_timeout=5)
s3_sensor_json = s3_bucket.Object(sensor_json_name) s3_sensor_json.put(ACL='public-read', Body=get_sensor_json(), ContentEncoding='utf-8', ContentType='application/json') message_json = dumps({ 'sender': IOT_CLIENT_ID, 'target': IOT_HOST_ID, 'data': { 'image': image_name, 'sensor': sensor_json_name } }) ws.send(message_json) time.sleep(5) start_new_thread(main, ()) ws = WebSocketApp(WS_ENDPOINT, on_message=on_message, on_error=lambda _0, _1: None, on_close=lambda _: None) ws.on_open = on_open ws.run_forever()
def __track_positions(self): if not hasattr(self, "__positions_feed") or self.__positions_feed is None: positions = defaultdict(float) for pair in self.__pairs: currency = pair.base positions[currency] = 0.0 self.__positions_queue.put(deepcopy(positions)) def on_open(ws): nonce = int(time.time() * 1000000) auth_payload = "AUTH{}".format(nonce) signature = hmac.new(self.__api_secret.encode(), msg=auth_payload.encode(), digestmod=hashlib.sha384).hexdigest() payload = { "apiKey": self.__api_key, "event": "auth", "authPayload": auth_payload, "authNonce": nonce, "authSig": signature, "filter": ["trading"], } ws.send(json.dumps(payload)) def on_message(ws, msg): msg = json.loads(msg) # Ignore heartbeats or other irrelevant msgs if not isinstance(msg, list) or len(msg) <= 1 or msg[1] == "hb": return def update_positions(update, value=None): currency = Bitfinex.decode_trading_pair(update[0]).base # Only track exchange (trading) wallet. if len(update) >= 6: # If position is inactive, zero out balance if update[1] != "ACTIVE": value = 0.0 if value is None: positions[currency] = float(update[2]) else: positions[currency] = value self.__positions_queue.put(deepcopy(positions)) # Disambiguate positions snapshot/update/new/closed: if msg[1] == "ps": for update in msg[2]: update_positions(update) elif msg[1] == "pn" or msg[1] == "pu": update_positions(msg[2]) elif msg[1] == "pc": update_positions(msg[2], 0.0) # TODO: should probably abort def on_error(ws, error): Log.warn( "WS error within __track_positions for exchange {}: {}".format( self.id, error)) # TODO: refactor to not be recursive (bc of stack growth) def on_close(ws): Log.warn("WS closed unexpectedly for exchange {}".format(self.id)) Log.info("restarting WS for exchange {}".format(self.id)) time.sleep(3) self.__track_positions() ws = WebSocketApp( "wss://api.bitfinex.com/ws/", on_message=on_message, on_error=on_error, on_close=on_close, ) ws.on_open = on_open ws.run_forever()
def on_message(mes): mes = inflate(mes) #print(mes) message = json.loads(mes, encoding='utf-8') if message[0]['data'][0][4] in ['bid', 'ask']: result = { '_id': message[0]['data'][0][0], 'p': message[0]['data'][0][1], 'q': message[0]['data'][0][2], 't': datetime.datetime.utcnow(), 'side': message[0]['data'][0][4], 's': 'ETH-USD', 'date': datetime.datetime.utcnow().strftime('%Y-%m-%d') } print(datetime.datetime.now()) print(result) res = okex_coll.insert_one(result) ws = WebSocketApp('wss://real.OKEx.com:8443/ws/v3') ws.on_open = lambda self: self.send( json.dumps({ "event": "addChannel", "channel": "ok_sub_spot_eth_usdt_deals" })) ws.on_message = lambda self, evt: on_message(evt) ws.run_forever(ping_interval=10, ping_timeout=5)
if message['type'] in ('last_match', 'match'): print(datetime.datetime.now()) print(message) result = { "_id": message['trade_id'], "maker_order_id": message['maker_order_id'], "taker_order_id": message['taker_order_id'], "side": message['side'], "q": message['size'], "p": message['price'], "s": message['product_id'], "t": message['time'], 'date': datetime.datetime.utcnow().strftime('%Y-%m-%d') } res = cbpro_coll.insert_one(result) ws = WebSocketApp('wss://ws-feed.pro.coinbase.com') ws.on_open = lambda self: self.send( json.dumps({ "type": "subscribe", "channels": [{ "name": "matches", "product_ids": ["LTC-USD"], }] })) ws.on_message = lambda self, evt: on_message(evt) ws.run_forever(ping_interval=10, ping_timeout=5)
# print(message['table']) if message['table'] in ['spot/ticker']: #print(message['data'][0]) result = { 'p': message['data'][0]['last'], 'q': message['data'][0]['last_qty'], 't': message['data'][0]['timestamp'], 's': message['data'][0]['instrument_id'], 'date': datetime.datetime.utcnow().strftime('%Y-%m-%d') } print(datetime.datetime.now()) print(result) res = okex_coll.insert_one(result) ws = WebSocketApp('wss://real.OKEx.com:8443/ws/v3') ws.on_open = lambda self: self.send( json.dumps({ "op": "subscribe", "args": [ "spot/ticker:BTC-USDT", "spot/ticker:BCH-USDT", "spot/ticker:ETC-USDT", "spot/ticker:ETH-USDT", "spot/ticker:LTC-USDT" ] })) ws.on_message = lambda self, evt: on_message(evt) ws.run_forever(ping_interval=10, ping_timeout=5)
def on_message(mes): print(mes) message = json.loads(mes) result = { '_id': message['params']['data'][0]['id'], 'p': message['params']['data'][0]['price'], 'q': message['params']['data'][0]['quantity'], 'side': message['params']['data'][0]['side'], 't': message['params']['data'][0]['timestamp'], 's': message['params']['symbol'], 'date': datetime.datetime.utcnow().strftime('%Y-%m-%d') } print(datetime.datetime.now()) print(result) res = hitbtc_coll.insert_one(result) ws = WebSocketApp('wss://api.hitbtc.com/api/2/ws') ws.on_open = lambda self: self.send( json.dumps({ "method": "subscribeTrades", "params": { "symbol": "ETHUSD" } })) ws.on_message = lambda self, evt: on_message(evt) ws.run_forever(ping_interval=10, ping_timeout=5)
"maker_order_id": message['maker_order_id'], "taker_order_id": message['taker_order_id'], "side": message['side'], "q": message['size'], "p": message['price'], "s": message['product_id'], "t": message['time'], 'date': datetime.datetime.utcnow().strftime('%Y-%m-%d') } res = cbpro_coll.insert_one(result) ws = WebSocketApp('wss://ws-feed.pro.coinbase.com') ws.on_open = lambda self: self.send( json.dumps({ "type": "subscribe", "channels": [{ "name": "matches", "product_ids": [ "BTC-USD", "ETH-USD", "LTC-USD", "XRP-USD", "BCH-USD", "XLM-USD", "ZRX-USD" ], }] })) ws.on_message = lambda self, evt: on_message(evt) ws.run_forever(ping_interval=10, ping_timeout=5)
from __future__ import print_function from websocket import WebSocketApp from kafka import KafkaProducer import time import pytz from utility import _current_time_stamp, _utc_timestamp import json producer = KafkaProducer(bootstrap_servers='localhost:9092') def _on_message(self, evt): producer.send('test', key=b'tBTCUSD', value=evt.encode('utf-8')) print(evt) ws = WebSocketApp('wss://api-pub.bitfinex.com/ws/2') ws.on_open = lambda self: self.send( '{"event": "subscribe", "channel": "book", "symbol": "tBTCUSD", "prec": "P0", "freq": "F0", "len": 25}' ) ws.on_message = _on_message ws.run_forever()