mirror of
https://git.hmsn.ink/coin/api.git
synced 2026-03-20 00:02:16 +09:00
first
This commit is contained in:
BIN
__pycache__/models.cpython-313.pyc
Normal file
BIN
__pycache__/models.cpython-313.pyc
Normal file
Binary file not shown.
11
api.iml
Normal file
11
api.iml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.13 (api)" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
59
models.py
Normal file
59
models.py
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
from sqlalchemy import create_engine, Table, Column, Integer, String, Float, Double, DateTime
|
||||||
|
from sqlalchemy.orm import registry, sessionmaker
|
||||||
|
|
||||||
|
# 메타데이터 객체 관리
|
||||||
|
mapper_registry = registry()
|
||||||
|
|
||||||
|
# DB 유저 테이블
|
||||||
|
ticker_table = Table(
|
||||||
|
"ticker",
|
||||||
|
mapper_registry.metadata,
|
||||||
|
Column("contract", String(20), primary_key=True),
|
||||||
|
Column("clast", String(50)),
|
||||||
|
Column("change_percentage", Double),
|
||||||
|
Column("total_size", Integer),
|
||||||
|
Column("volume_24h", Integer),
|
||||||
|
Column("volume_24h_base", Integer),
|
||||||
|
Column("volume_24h_quote", Integer),
|
||||||
|
Column("volume_24h_settle", Integer),
|
||||||
|
Column("funding_rate", Double),
|
||||||
|
Column("funding_rate_indicative", Double),
|
||||||
|
Column("quanto_base_rate", String(100)),
|
||||||
|
Column("low_24h", Double),
|
||||||
|
Column("high_24h", Double),
|
||||||
|
Column("price_type", String(10)),
|
||||||
|
Column("change_from", String(20)),
|
||||||
|
Column("change_price", Double),
|
||||||
|
Column("ts", DateTime, primary_key=True),
|
||||||
|
)
|
||||||
|
|
||||||
|
# Python 유저 객체
|
||||||
|
class Ticker:
|
||||||
|
contract: str
|
||||||
|
clast:float
|
||||||
|
change_percentage:float
|
||||||
|
total_size:int
|
||||||
|
volume_24h:int
|
||||||
|
volume_24h_base:int
|
||||||
|
volume_24h_quote:int
|
||||||
|
volume_24h_settle:int
|
||||||
|
funding_rate:float
|
||||||
|
funding_rate_indicative:float
|
||||||
|
quanto_base_rate:str
|
||||||
|
low_24h:float
|
||||||
|
high_24h:float
|
||||||
|
price_type:str
|
||||||
|
change_from:str
|
||||||
|
change_price:float
|
||||||
|
ts:str
|
||||||
|
|
||||||
|
# 테이블과 클래스의 매핑 설정
|
||||||
|
mapper_registry.map_imperatively(Ticker, ticker_table)
|
||||||
|
|
||||||
|
def get_session():
|
||||||
|
# 데이터베이스 설정
|
||||||
|
engine = create_engine('postgresql+psycopg2://bangae1:fpdlwms1@hmsn.ink:35432/coin')
|
||||||
|
mapper_registry.metadata.create_all(engine) # 테이블 생성
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
session = Session()
|
||||||
|
return session
|
||||||
23
test.py
Normal file
23
test.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import and_, or_, desc, asc
|
||||||
|
|
||||||
|
from models import get_session, Ticker
|
||||||
|
|
||||||
|
|
||||||
|
def get_ticker(session: Session, contract: str):
|
||||||
|
return session.query(Ticker).filter(Ticker.contract == contract).limit(500).all()
|
||||||
|
|
||||||
|
def get_ticker_count(session: Session, contract: str):
|
||||||
|
return session.query(Ticker).filter(Ticker.contract == contract).count()
|
||||||
|
|
||||||
|
# 사용 예시
|
||||||
|
|
||||||
|
session = get_session()
|
||||||
|
try:
|
||||||
|
cnt = get_ticker_count(session, "BTC_USDT")
|
||||||
|
print(cnt)
|
||||||
|
tickers = get_ticker(session, "BTC_USDT")
|
||||||
|
for ticker in tickers:
|
||||||
|
print(ticker.clast)
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
Reference in New Issue
Block a user