def on_messages(msg_strs): if not msg_strs: logging.error('the message is not valid') msgs = json.loads(msg_strs) for msg in msgs: on_message(msg)
def on_message(aggregations_run, msg, shard_id, shard_size): if not msg: logging.error('the message is not valid') if 'e' not in msg: logging.error('"e" field not present in the message: {msg}'.format(msg=msg)) e = msg['e'] if e == 'kline': _on_kline_message(aggregations_run, msg, shard_id, shard_size) else: _on_undefined_message(aggregations_run, msg)
def _on_kline_message(aggregations_run, msg, shard_id, shard_size): global _cnt_msg _cnt_msg += 1 if _cnt_msg % 100 == 0: print("< {msg}".format(msg=msg)) if 'k' not in msg: logging.error('"k" field not present in the kline message: {msg}'.format(msg=msg)) k = msg['k'] bar_with_time = _binance_kline_msg_to_on_bar_with_time(k, shard_id, shard_size) if bar_with_time: aggregations_run.on_bar_with_time(bar_with_time) else: pass # print('does not correspond to this shard')
def on_message(msg): if not msg: logging.error('the message is not valid') if 'ev' not in msg: logging.error( '"ev" field not present in the message: {msg}'.format(msg=msg)) ev = msg['ev'] if ev == 'status': _on_status_message(msg) elif ev == 'T': _on_T_message(msg) elif ev == 'Q': _on_Q_message(msg) elif ev == 'A': _on_A_message(msg) elif ev == 'AM': _on_AM_message(msg) else: _on_undefined_message(msg)
def run_loop(polygon_aggregations_run, subscription_id): project_id = os.getenv('GOOGLE_CLOUD_PROJECT') subscriber = pubsub_v1.SubscriberClient() subscription_path = subscriber.subscription_path(project_id, subscription_id) def callback(message): msg_str = json.loads(message.data.decode('utf-8')) message.ack() on_message(polygon_aggregations_run, json.loads(msg_str)[0]) streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback) print("Listening for messages on {}\n".format(subscription_path)) try: streaming_pull_future.result() except Exception as ex: # noqa logging.error(ex) streaming_pull_future.cancel()
def _on_undefined_message(polygon_aggregations_run, msg): print("< (undefined) {msg}".format(msg=msg)) logging.error("< (undefined) {msg}".format(msg=msg))