前提
bitflyer Realtime APIに、websocketで接続して得たデータを変数に格納するために
試行錯誤しましたが、やり方がわかりませんでした
実現したいこと
bitflyer Realtime APIに、websocketで接続して得たデータを変数に格納する
発生している問題・エラーメッセージ
--- Logging error ---
Traceback (most recent call last):
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 383, in run_forever
dispatcher.read(self.sock.sock, read, check)
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 64, in read
r = self.select()
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 78, in select
r = sel.select(self.ping_timeout)
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/selectors.py", line 562, in select
kev_list = self._selector.control(None, max_ev, timeout)
KeyboardInterrupt
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 424, in _callback
callback(self, *args)
TypeError: on_close() takes 2 positional arguments but 4 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/logging/init.py", line 1086, in emit
stream.write(msg + self.terminator)
AttributeError: 'int' object has no attribute 'write'
Call stack:
File "/Users/a1/Desktop/ml/main.py", line 14, in <module>
json_rpc = RealtimeAPI(url=url, channel=channel)
File "/Users/a1/Desktop/ml/bitflyer.py", line 15, in init
self.connect()
File "/Users/a1/Desktop/ml/bitflyer.py", line 22, in connect
self.ws.run_forever()
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 390, in run_forever
teardown()
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 318, in teardown
self._callback(self.on_close, close_status_code, close_reason)
File "/Users/a1/opt/anaconda3/envs/m/lib/python3.9/site-packages/websocket/_app.py", line 429, in _callback
self.on_error(self, e)
File "/Users/a1/Desktop/ml/bitflyer.py", line 35, in on_error
logger.error(error)
Message: TypeError('on_close() takes 2 positional arguments but 4 were given')
Arguments: ()
該当のソースコード
使用言語 python3.9
コード ファイル名 bitflyer.py
import logging import websocket import json import pybitflyer logger = logging.getLogger(__name__)#logging ticker_data = 0#追記 class RealtimeAPI(object): def __init__(self, url, channel): self.url = url self.channel = channel self.connect() def connect(self): self.ws = websocket.WebSocketApp\ (self.url,header=None,on_open=self.on_open,\ on_message=self.on_message, on_error=self.on_error,\ on_close=self.on_close) self.ws.run_forever() logger.info('Web Socket process ended.') """ Below are callback functions of websocket. """ # when we get message def on_message(self, ws, message): resp = json.loads(message)['params']['message'] self.set_realtime_ticker(resp) # when error occurs def on_error(self, ws, error): logger.error(error) # when websocket closed. def on_close(self, ws): logger.info('disconnected streaming server') # when websocket opened. def on_open(self, ws): logger.info('connected streaming server') input_data = json.dumps( {'method' : 'subscribe', 'params' : {'channel' : self.channel} } ) ws.send(input_data) def set_realtime_ticker(self, resp): timestamp = resp['timestamp'] product_code = resp['product_code'] volume = float(resp['volume']) ticker = [product_code, timestamp, volume] print(ticker) logger.info(f'ticker={ticker}') global ticker_data#追記 ticker_data = ticker#追記 print(ticker_data)#ここにAPIから入ってくるtickerのデータを入れたいです ---------------------------------ここからもう一つのファイル--------------------------------------- #ファイル名 main.py import logging import sys from bitflyer import RealtimeAPI logging.basicConfig(level=logging.INFO, stream=sys.stdout) # logging.basicConfig(level=logging.INFO, stream=a) #ログレベル INFO 出力先 stream=sys.stdout if __name__ == "__main__": url = "wss://ws.lightstream.bitflyer.com/json-rpc" channel = "lightning_ticker_FX_BTC_JPY" json_rpc = RealtimeAPI(url=url, channel=channel) ------------------------------コードはここまで--------------------------------------- ### 試したこと ①ファイル名bitflyer.pyのdef set_realtime_tickerにあるtickerをグローバル変数にした ②ファイル名 main.pyのlogging.basicConfigにあるstream=変数にした ### 補足情報(FW/ツールのバージョンなど) Visual Studio Codeを使用しています ここにより詳細な情報を記載してください。 よろしくお願いいたします
回答1件
あなたの回答
tips
プレビュー