bitFlyer LightningのHTTP Public APIから直近の取引情報を取得し、データベースに書き込むPython3のプログラム。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# See https://pandanote.info/?p=5966 for details. | |
# | |
import sys | |
import os | |
import re | |
import json | |
import httplib2 | |
from datetime import datetime | |
import dateutil.parser | |
import mysql.connector | |
db_config_file = 'db_config.json' | |
db_config = {} | |
with open(db_config_file) as dbfile: | |
t = dbfile.read() | |
dbconfig = json.loads(t) | |
h = httplib2.Http(".cache") | |
(resp_headers,content) = h.request("https://api.bitflyer.com/v1/getticker","GET") | |
conn = mysql.connector.connect(user=dbconfig["user"],password=dbconfig["password"],host=dbconfig["host"],database=dbconfig["dbname"]) | |
c = json.loads(content) | |
info_at = dateutil.parser.parse(c["timestamp"]).strftime("%Y-%m-%dT%H:%M:%S.%f") | |
#print(info_at) | |
insert_sql="insert into crypto_currency_info_cache(product_code,info_at,tick_id,best_bid,best_ask,best_bid_size,best_ask_size,total_bid_depth,total_ask_depth,ltp,volume,volume_by_product) values('{0:s}','{1:s}',{2:d},{3:f},{4:f},{5:f},{6:f},{7:f},{8:f},{9:f},{10:f},{11:f}) on duplicate key update info_at='{12:s}',tick_id={13:d},best_bid={14:f},best_ask={15:f},best_bid_size={16:f},best_ask_size={17:f},total_bid_depth={18:f},total_ask_depth={19:f},ltp={20:f},volume={21:f},volume_by_product={22:f}".format(c["product_code"],info_at,c["tick_id"],c["best_bid"],c["best_ask"],c["best_bid_size"],c["best_ask_size"],c["total_bid_depth"],c["total_ask_depth"],c["ltp"],c["volume"],c["volume_by_product"],info_at,c["tick_id"],c["best_bid"],c["best_ask"],c["best_bid_size"],c["best_ask_size"],c["total_bid_depth"],c["total_ask_depth"],c["ltp"],c["volume"],c["volume_by_product"]) | |
cur = conn.cursor() | |
cur.execute(insert_sql) | |
conn.commit() | |
cur.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment