Spaces:
Running
Running
fix: HF data instead of OpenDART realtime
Browse files- src/dartlab/server/api/dart.py +129 -54
src/dartlab/server/api/dart.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
-
"""DART API
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
"""
|
| 6 |
|
| 7 |
from __future__ import annotations
|
|
@@ -13,72 +13,93 @@ from fastapi import APIRouter, HTTPException, Query
|
|
| 13 |
router = APIRouter(prefix="/api/dart", tags=["dart"])
|
| 14 |
_log = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
-
_MAX_ROWS =
|
| 17 |
|
| 18 |
|
| 19 |
-
def
|
| 20 |
-
"""
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def _sanitize(data: dict | list) -> dict | list:
|
| 27 |
-
"""์๋ต์์ crtfc_key ํ๋๋ฅผ ์ ๊ฑฐ."""
|
| 28 |
-
if isinstance(data, dict):
|
| 29 |
-
return {k: _sanitize(v) for k, v in data.items() if k != "crtfc_key"}
|
| 30 |
-
if isinstance(data, list):
|
| 31 |
-
return [_sanitize(item) if isinstance(item, (dict, list)) else item for item in data]
|
| 32 |
-
return data
|
| 33 |
|
| 34 |
|
| 35 |
@router.get("/filings")
|
| 36 |
def dart_filings(
|
| 37 |
-
corp: str | None = Query(None, description="์ข
๋ชฉ์ฝ๋
|
| 38 |
-
|
| 39 |
-
end: str | None = Query(None, description="์ข
๋ฃ์ผ (YYYYMMDD)"),
|
| 40 |
-
type: str | None = Query(None, description="๊ณต์์ ํ ํํฐ"),
|
| 41 |
):
|
| 42 |
-
"""๊ณต์ ๋ชฉ๋ก
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 52 |
|
| 53 |
|
| 54 |
@router.get("/company/{corp}")
|
| 55 |
def dart_company_info(corp: str):
|
| 56 |
-
"""๊ธฐ์
๊ธฐ๋ณธ ์ ๋ณด."""
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
@router.get("/finance/{corp}")
|
| 68 |
def dart_finance(
|
| 69 |
corp: str,
|
| 70 |
-
|
| 71 |
-
|
| 72 |
):
|
| 73 |
-
"""์ฌ๋ฌด์ ํ
|
| 74 |
try:
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 83 |
|
| 84 |
|
|
@@ -86,15 +107,69 @@ def dart_finance(
|
|
| 86 |
def dart_report(
|
| 87 |
corp: str,
|
| 88 |
category: str,
|
| 89 |
-
year: int | None = Query(None, description="์ฌ์
์ฐ๋"),
|
| 90 |
):
|
| 91 |
-
"""๋ณด๊ณ ์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
|
|
|
| 1 |
+
"""DART ๋ฐ์ดํฐ API โ HuggingFace ์ฌ์ ๋น๋ ๋ฐ์ดํฐ ๊ธฐ๋ฐ ์ฆ์ ์๋ต.
|
| 2 |
|
| 3 |
+
์ฌ๋ฌด์ ํ, ๋ณด๊ณ ์, ๊ณต์ ๋ชฉ๋ก ๋ฑ ๋๋ถ๋ถ์ ๋ฐ์ดํฐ๋ ์ด๋ฏธ HuggingFace์ ์๋ค.
|
| 4 |
+
OpenDART API๋ฅผ ์ค์๊ฐ ํธ์ถํ์ง ์๊ณ , dartlab Company/listing์ผ๋ก HF ๋ฐ์ดํฐ๋ฅผ ๋ฐํ.
|
| 5 |
"""
|
| 6 |
|
| 7 |
from __future__ import annotations
|
|
|
|
| 13 |
router = APIRouter(prefix="/api/dart", tags=["dart"])
|
| 14 |
_log = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
+
_MAX_ROWS = 200
|
| 17 |
|
| 18 |
|
| 19 |
+
def _df_to_response(df, max_rows: int = _MAX_ROWS) -> dict:
|
| 20 |
+
"""DataFrame โ JSON ์๋ต."""
|
| 21 |
+
if df is None or (hasattr(df, "is_empty") and df.is_empty()):
|
| 22 |
+
return {"count": 0, "total": 0, "rows": []}
|
| 23 |
+
total = df.height if hasattr(df, "height") else len(df)
|
| 24 |
+
rows = df.head(max_rows).to_dicts() if hasattr(df, "to_dicts") else []
|
| 25 |
+
return {"count": len(rows), "total": total, "rows": rows}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
@router.get("/filings")
|
| 29 |
def dart_filings(
|
| 30 |
+
corp: str | None = Query(None, description="์ข
๋ชฉ์ฝ๋"),
|
| 31 |
+
topK: int = Query(20, description="์ต๋ ๊ฑด์"),
|
|
|
|
|
|
|
| 32 |
):
|
| 33 |
+
"""๊ณต์ ๋ชฉ๋ก โ HF ๋ฐ์ดํฐ ๊ธฐ๋ฐ."""
|
| 34 |
try:
|
| 35 |
+
import dartlab
|
| 36 |
+
|
| 37 |
+
if corp:
|
| 38 |
+
c = dartlab.Company(corp)
|
| 39 |
+
df = c.filings(topK=topK)
|
| 40 |
+
else:
|
| 41 |
+
df = dartlab.listing("filings")
|
| 42 |
+
return _df_to_response(df, max_rows=topK)
|
| 43 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 44 |
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 45 |
|
| 46 |
|
| 47 |
@router.get("/company/{corp}")
|
| 48 |
def dart_company_info(corp: str):
|
| 49 |
+
"""๊ธฐ์
๊ธฐ๋ณธ ์ ๋ณด โ HF ๋ฐ์ดํฐ ๊ธฐ๋ฐ."""
|
| 50 |
try:
|
| 51 |
+
import dartlab
|
| 52 |
|
| 53 |
+
c = dartlab.Company(corp)
|
| 54 |
+
return {
|
| 55 |
+
"corpName": c.corpName,
|
| 56 |
+
"stockCode": c.stockCode,
|
| 57 |
+
"market": getattr(c, "market", None),
|
| 58 |
+
"currency": getattr(c, "currency", None),
|
| 59 |
+
}
|
| 60 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 61 |
+
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
| 62 |
|
| 63 |
|
| 64 |
@router.get("/finance/{corp}")
|
| 65 |
def dart_finance(
|
| 66 |
corp: str,
|
| 67 |
+
statement: str = Query("IS", description="IS/BS/CF/CIS/SCE"),
|
| 68 |
+
freq: str = Query("Q", description="Q(๋ถ๊ธฐ)/Y(์ฐ๊ฐ)"),
|
| 69 |
):
|
| 70 |
+
"""์ฌ๋ฌด์ ํ โ HF parquet ์ฆ์ ๋ฐํ."""
|
| 71 |
try:
|
| 72 |
+
import dartlab
|
| 73 |
+
|
| 74 |
+
c = dartlab.Company(corp)
|
| 75 |
+
if freq == "Y":
|
| 76 |
+
df = c.show(statement, freq="Y")
|
| 77 |
+
else:
|
| 78 |
+
df = c.show(statement)
|
| 79 |
+
return _df_to_response(df)
|
| 80 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 81 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 82 |
|
| 83 |
+
|
| 84 |
+
@router.get("/show/{corp}/{topic}")
|
| 85 |
+
def dart_show(
|
| 86 |
+
corp: str,
|
| 87 |
+
topic: str,
|
| 88 |
+
period: str | None = Query(None, description="๊ธฐ๊ฐ ํํฐ"),
|
| 89 |
+
):
|
| 90 |
+
"""๊ณต์ ํ ํฝ ๋ฐ์ดํฐ โ HF parquet ์ฆ์ ๋ฐํ."""
|
| 91 |
+
try:
|
| 92 |
+
import dartlab
|
| 93 |
+
|
| 94 |
+
c = dartlab.Company(corp)
|
| 95 |
+
if period:
|
| 96 |
+
result = c.show(topic, period=period)
|
| 97 |
+
else:
|
| 98 |
+
result = c.show(topic)
|
| 99 |
+
if hasattr(result, "to_dicts"):
|
| 100 |
+
return _df_to_response(result)
|
| 101 |
+
return {"data": str(result)[:5000]}
|
| 102 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 103 |
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 104 |
|
| 105 |
|
|
|
|
| 107 |
def dart_report(
|
| 108 |
corp: str,
|
| 109 |
category: str,
|
|
|
|
| 110 |
):
|
| 111 |
+
"""๋ณด๊ณ ์ (๋ฐฐ๋น, ์ง์, ์์ ๋ฑ) โ HF parquet ์ฆ์ ๋ฐํ."""
|
| 112 |
+
try:
|
| 113 |
+
import dartlab
|
| 114 |
+
|
| 115 |
+
c = dartlab.Company(corp)
|
| 116 |
+
df = c.show(category)
|
| 117 |
+
if hasattr(df, "to_dicts"):
|
| 118 |
+
return _df_to_response(df)
|
| 119 |
+
return {"data": str(df)[:5000]}
|
| 120 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 121 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@router.get("/scan/{axis}")
|
| 125 |
+
def dart_scan(
|
| 126 |
+
axis: str,
|
| 127 |
+
target: str | None = Query(None, description="์ถ๋ณ ๋์ (account: ๊ณ์ ๋ช
, ratio: ๋น์จ๋ช
)"),
|
| 128 |
+
):
|
| 129 |
+
"""์ ์ข
๋ชฉ ํก๋จ๋ถ์ โ ํ๋ฆฌ๋น๋ parquet ์ฆ์ ๋ฐํ."""
|
| 130 |
+
try:
|
| 131 |
+
import dartlab
|
| 132 |
+
|
| 133 |
+
if target:
|
| 134 |
+
df = dartlab.scan(axis, target)
|
| 135 |
+
else:
|
| 136 |
+
df = dartlab.scan(axis)
|
| 137 |
+
return _df_to_response(df)
|
| 138 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 139 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
@router.get("/search")
|
| 143 |
+
def dart_search(
|
| 144 |
+
q: str = Query(..., description="๊ฒ์์ด"),
|
| 145 |
+
corp: str | None = Query(None, description="์ข
๋ชฉ์ฝ๋ ํํฐ"),
|
| 146 |
+
):
|
| 147 |
+
"""๊ณต์ ์๋ฌธ ๊ฒ์ โ stemIndex ์ฆ์ ๋ฐํ."""
|
| 148 |
try:
|
| 149 |
+
import dartlab
|
| 150 |
+
|
| 151 |
+
if corp:
|
| 152 |
+
df = dartlab.search(q, corp=corp)
|
| 153 |
+
else:
|
| 154 |
+
df = dartlab.search(q)
|
| 155 |
+
return _df_to_response(df)
|
| 156 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 157 |
+
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
| 158 |
|
| 159 |
+
|
| 160 |
+
@router.get("/listing")
|
| 161 |
+
def dart_listing(
|
| 162 |
+
kind: str = Query("companies", description="companies/filings/topics"),
|
| 163 |
+
corp: str | None = Query(None, description="filings ์ ์ข
๋ชฉ์ฝ๋"),
|
| 164 |
+
):
|
| 165 |
+
"""์์ฅ ์ข
๋ชฉ/๊ณต์ ๋ชฉ๋ก."""
|
| 166 |
+
try:
|
| 167 |
+
import dartlab
|
| 168 |
+
|
| 169 |
+
if kind == "filings" and corp:
|
| 170 |
+
df = dartlab.listing("filings", corp=corp)
|
| 171 |
+
else:
|
| 172 |
+
df = dartlab.listing(kind)
|
| 173 |
+
return _df_to_response(df)
|
| 174 |
+
except (ValueError, KeyError, RuntimeError, FileNotFoundError) as exc:
|
| 175 |
raise HTTPException(status_code=400, detail=str(exc)) from exc
|