"""기업 펀더멘털 (시총/PER/PBR/EPS/BPS/배당) API. pykrx 한 번 호출 = 종목당 ~1~2초. 인메모리 daily 캐시(get_fundamentals) 가 처리. SkippedMissingKey 같은 거 없음 — pykrx 는 무인증 KRX 스크래핑. """ from __future__ import annotations from datetime import date from fastapi import APIRouter, HTTPException from sqlalchemy import text from app.db.connection import get_engine from app.fetch.fundamentals import get_fundamentals router = APIRouter(prefix="/api/fundamentals", tags=["fundamentals"]) @router.get("/{code}") def code_fundamentals(code: str) -> dict: """code 의 최신 영업일 fundamentals 스냅샷.""" eng = get_engine() with eng.connect() as conn: sym = conn.execute( text("SELECT code, name, market FROM symbols WHERE code = :c"), {"c": code}, ).first() if not sym: raise HTTPException(status_code=404, detail=f"unknown code: {code}") f = get_fundamentals(code) if f is None: return { "code": sym[0], "name": sym[1], "market": sym[2], "status": "no_data", "snapshot_date": None, "market_cap": None, "shares_outstanding": None, "per": None, "pbr": None, "eps": None, "bps": None, "div": None, "dvd_yield": None, } return { "code": sym[0], "name": sym[1], "market": sym[2], "status": "ok", "snapshot_date": f.snapshot_date.isoformat(), "market_cap": f.market_cap, "shares_outstanding": f.shares_outstanding, "per": f.per, "pbr": f.pbr, "eps": f.eps, "bps": f.bps, "div": f.div, "dvd_yield": f.dvd_yield, }