LoginSignup
6
8

More than 3 years have passed since last update.

kabuステーション®API - PUSH API用にPythonラッパーをアップデートした

Last updated at Posted at 2020-10-14

概要

python-kabusapi という kabuステーションAPI用Pythonラッパーを作成したが、REST APIのみの対応であり、PUSH API によるwebsocket配信には未対応であった。

そのため、「kabuステーション®APIのWebsocketをPythonで受ける」のようなコードを書く必要があったが、初心者にとって asyncio 等の概念が煩雑であるため、そのあたりを排除できるよう、ライブラリをアップデートした。

環境

  • Windows 10
  • Python 3.8.5 ( Microsoft Store からインストール )

追加パッケージ

  • websockets
  • requests

コード

import kabusapi

url = "localhost"
port = "18081"  # 検証用, 本番用は18080

# 初期設定 PUSH配信にトークン・パスワードは不要
api = kabusapi.Context(url, port,)

# 受信用関数 情報が受信される度にここが呼ばれる
@api.websocket
def recieve(msg):
    # ここで処理を行う msgはdict形式である。
    print("{} {} {}".format(
        msg['Symbol'],
        msg['SymbolName'],
        msg['CurrentPrice'],
    ))

# 受信開始
api.websocket.run()

関連記事

6
8
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
6
8