3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

LiquidTap Python Clientを使う③

Last updated at Posted at 2020-03-04

目次

はじめに

「Liquid by Quoine」のAPIを叩こう~その3~

「Liquid by Quoine」のログを確認してみる

LiquidTap Python Clientを使う①で作成したprice.pyのログをファイルに吐こうとしたところ、偶々ログにLiquidTapライブラリのログが吐かれたので出力内容を見てみることにします。
(2020.03.04)

LiquidTapのログを見る前に。

ログを確認するために用意したコードと設定ファイルです。

price.py
import liquidtap
import time
from logging import config

def update_callback_buy(data):
  print("buy:" + data)

def update_callback_sell(data):
  print("sell:" + data)

def on_connect(data):
  tap.pusher.subscribe("price_ladders_cash_btcjpy_buy").bind('updated', update_callback_buy)
  tap.pusher.subscribe("price_ladders_cash_btcjpy_sell").bind('updated', update_callback_sell)

if __name__ == "__main__":
  config.fileConfig('logging.conf')

  tap = liquidtap.Client()
  tap.pusher.connection.bind('pusher:connection_established', on_connect)
  tap.pusher.connect()

  while True: # 無限ループ
    time.sleep(1)

ログの設定ファイルです。
(コンソールとログファイルで確認しようとしてたので、、、コンソールにも吐かれます。)

logging.conf
[loggers]
keys=root

[handlers]
keys=fileHandler,consoleHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=INFO
handlers=fileHandler,consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=simpleFormatter
args=(sys.stdout,)

[handler_fileHandler]
class=handlers.RotatingFileHandler
level=INFO
formatter=simpleFormatter
args=("logs/default.log", "a", 1024*1024, 5)

[formatter_simpleFormatter]
# フォーマットを指定 (https://docs.python.jp/3/library/logging.html#logrecord-attributes)
format=%(asctime)s %(levelname)-8s [%(module)s#%(funcName)s %(lineno)d] %(message)s

出力されたLiquidTapのログを見る

(websocketの)接続

この辺り

tap = liquidtap.Client()
tap.pusher.connection.bind('pusher:connection_established', on_connect)
tap.pusher.connect()

2020-03-04 19:54:17,928 INFO     [connection#_on_open 129] Connection: Connection opened
2020-03-04 19:54:17,945 INFO     [connection#_on_message 143] Connection: Message - {"data":"{\"activity_timeout\":120,\"socket_id\":\"0600663276.5838793824\"}","event":"pusher:connection_established"}

_on_openで接続して、_on_messageでメッセージ受信する仕組み(?)のようです。
ソケットIDやタイムアウトの時間が読み取れます。

subscribeの送信と結果受信

この辺り

tap.pusher.subscribe("price_ladders_cash_btcjpy_buy").bind('updated', update_callback_buy)
tap.pusher.subscribe("price_ladders_cash_btcjpy_sell").bind('updated', update_callback_sell)

2020-03-04 19:54:17,947 INFO     [connection#send_event 217] Connection: Sending event - {"event": "pusher:subscribe", "data": {"channel": "price_ladders_cash_btcjpy_buy"}}
2020-03-04 19:54:17,950 INFO     [connection#send_event 217] Connection: Sending event - {"event": "pusher:subscribe", "data": {"channel": "price_ladders_cash_btcjpy_sell"}}
2020-03-04 19:54:17,963 INFO     [connection#_on_message 143] Connection: Message - {"channel":"price_ladders_cash_btcjpy_buy","data":{},"event":"pusher_internal:subscription_succeeded"}
2020-03-04 19:54:17,972 INFO     [connection#_on_message 143] Connection: Message - {"channel":"price_ladders_cash_btcjpy_sell","data":{},"event":"pusher_internal:subscription_succeeded"}

PING/PONG

LiquidTapでは何も通信がないと切断される仕様(activity_timeoutの値?)になっています。
そのために定期的にPINGを送信して、PONGを受信しています。
https://developers.liquid.com/#ping-/-pong

2020-03-04 19:54:17,942 INFO     [connection#send_ping 224] Connection: ping to pusher
2020-03-04 19:54:17,961 INFO     [connection#_pong_handler 276] Connection: pong from pusher

ホントにPING/PONGを送受信している。。。

PRICE受信

2020-03-04 19:54:17,966 INFO     [connection#_on_message 143] Connection: Message - {"channel":"price_ladders_cash_btcjpy_buy","data":"[[\"942742.00000\",\"0.01812556\"],[\"942740.00000\",\"0.00100000\"],[\"942731.00000\",\"0.01935063\"],[\"942700.00000\",\"0.01000000\"],[\"942678.00000\",\"0.00200000\"],[\"942667.00000\",\"0.00100000\"],[\"942662.00000\",\"0.01935057\"],[\"942653.00000\",\"0.03708848\"],[\"942640.00000\",\"0.01200000\"],[\"942639.00000\",\"0.10468137\"],[\"942615.00000\",\"0.18070000\"],[\"942613.00000\",\"0.02000000\"],[\"942577.00000\",\"0.04000000\"],[\"942567.00000\",\"0.04643000\"],[\"942551.00000\",\"0.50000013\"],[\"942524.00000\",\"0.00500000\"],[\"942502.00000\",\"0.01000000\"],[\"942490.00000\",\"0.00100000\"],[\"942467.00000\",\"0.00200000\"],[\"942459.00000\",\"0.10000000\"],[\"942451.00000\",\"0.22500000\"],[\"942424.00000\",\"0.03000000\"],[\"942419.00000\",\"1.50000000\"],[\"942409.00000\",\"0.01000000\"],[\"942393.00000\",\"0.15000000\"],[\"942351.00000\",\"0.02000000\"],[\"942328.00000\",\"0.02000000\"],[\"942290.00000\",\"1.00000013\"],[\"942275.00000\",\"0.10000000\"],[\"942240.00000\",\"0.00100000\"],[\"942144.00000\",\"0.05000000\"],[\"942129.00000\",\"0.01000000\"],[\"942071.00000\",\"0.07000000\"],[\"942059.00000\",\"0.09044679\"],[\"941982.00000\",\"0.02000000\"],[\"941966.00000\",\"0.00533240\"],[\"941862.00000\",\"0.24000000\"],[\"941780.00000\",\"0.01510000\"],[\"941779.00000\",\"4.00000000\"],[\"941771.00000\",\"0.05000000\"]]","event":"updated"}
2020-03-04 19:54:17,975 INFO     [connection#_on_message 143] Connection: Message - {"channel":"price_ladders_cash_btcjpy_sell","data":"[[\"943079.00000\",\"0.00200000\"],[\"943080.00000\",\"0.03708848\"],[\"943084.00000\",\"0.01935057\"],[\"943092.00000\",\"0.84000000\"],[\"943099.00000\",\"0.00500000\"],[\"943101.00000\",\"0.00200000\"],[\"943104.00000\",\"0.00100000\"],[\"943105.00000\",\"0.00696180\"],[\"943109.00000\",\"0.05000000\"],[\"943140.00000\",\"0.09739999\"],[\"943200.00000\",\"0.04000000\"],[\"943219.00000\",\"0.05000000\"],[\"943225.00000\",\"0.01000000\"],[\"943240.00000\",\"0.00100000\"],[\"943251.00000\",\"0.01000000\"],[\"943253.00000\",\"0.02000000\"],[\"943270.00000\",\"0.01000000\"],[\"943311.00000\",\"0.01680000\"],[\"943312.00000\",\"0.00200000\"],[\"943387.00000\",\"0.00100000\"],[\"943388.00000\",\"0.03869479\"],[\"943400.00000\",\"0.07743077\"],[\"943404.00000\",\"0.01935063\"],[\"943411.00000\",\"0.01740000\"],[\"943425.00000\",\"0.22500000\"],[\"943426.00000\",\"0.02000000\"],[\"943427.00000\",\"4.00000000\"],[\"943437.00000\",\"0.00200000\"],[\"943459.00000\",\"0.02500000\"],[\"943461.00000\",\"0.20000000\"],[\"943489.00000\",\"0.15000000\"],[\"943490.00000\",\"0.00100000\"],[\"943501.00000\",\"0.01000000\"],[\"943509.00000\",\"0.01000000\"],[\"943565.00000\",\"0.05000000\"],[\"943626.00000\",\"0.01000000\"],[\"943636.00000\",\"0.01900000\"],[\"943659.00000\",\"0.08774868\"],[\"943660.00000\",\"0.03999999\"],[\"943708.00000\",\"0.01000000\"]]","event":"updated"}

コールバックで受信する内容がそのまま出力されているのがわかります。

ログ出力に注意しないと、LiquidTapのログでログファイルが一杯になってしまいます。。。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?