Stock/print_json.py

55 lines
1.4 KiB
Python
Raw Permalink Normal View History

2022-05-25 03:16:39 +09:00
import sqlite3
from render import *
import db as database
import json
import argparse
import time
import datetime
parser = argparse.ArgumentParser(description="print corp data to json")
parser.add_argument("--krx", action="store_true" , default=False,
help="print krx corp data")
def printKRXCorpJson(corp):
print("{", end="")
i = 0
for attr in corp.__slots__:
if i > 0:
print(",",end="")
print("\"",attr,"\":\"",corp.__getattribute__(attr),"\"",sep="",end="")
i += 1
print("}",end="")
if __name__ == "__main__":
args = parser.parse_args()
db = sqlite3.connect("stock.db")
if args.krx:
print("[")
corp_list = database.GetAllKRXCorp(db)
j = 0
for corp in corp_list:
if j > 0:
print(",")
printKRXCorpJson(corp)
j += 1
#print(json.dumps(corp_list,ensure_ascii=False,indent=2))
print("]")
else:
stock_list = GetAllStockPrice(db,60)
i = 0
print("[")
for stock in stock_list:
if i > 0:
print(",")
s = [*map(str,stock)][2:]
code = f'"{stock[0]}"'
date = str(datetime.datetime.fromisoformat(stock[1]).timestamp())
print("[",code,",",date,",".join(s),"]",sep="",end="")
i += 1
print("]")
db.close()