Spaces:
Sleeping
Sleeping
File size: 1,560 Bytes
7dfd114 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | """yfinance μ°¨λ¨(μ: λ°μ΄ν°μΌν° IP μ Yahoo Finance 401) μ μ¬μ©νλ fallback μΊμ λ‘λ.
backend/cache/ λλ ν°λ¦¬μ μ¬μ λΉλλ JSON μ μ½μ΄ λ°ννλ€.
μ μ(yfinance λμ) κ²½λ‘μμλ νΈμΆλμ§ μμΌλ©°, λΌμ°ν°μ except/μ€ν¨ λΆκΈ°μμλ§ μ°μΈλ€.
μΊμλ scripts/build_cache.py λ‘ λ‘컬(yfinance λμ νκ²½)μμ μμ±νλ€.
"""
import json
import os
# μ΄ νμΌ(backend/cache_fallback.py) κΈ°μ€μΌλ‘ cache/ λ₯Ό μ°Ύλλ€ (CWD μ μμ‘΄νμ§ μμ).
_CACHE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "cache")
def load_cache(key, ticker=None):
"""μ¬μ λΉλ μΊμλ₯Ό λ°ννλ€.
- ticker κ° μμΌλ©΄ cache/<key>.json
- ticker κ° μμΌλ©΄ cache/<key>/<ticker>.json
νμΌμ΄ μκ±°λ μμλλ©΄ None μ λ°ννλ€ (graceful).
"""
if ticker is not None:
path = os.path.join(_CACHE_DIR, key, f"{ticker}.json")
else:
path = os.path.join(_CACHE_DIR, f"{key}.json")
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError, OSError):
return None
def load_meta():
"""μΊμ μμ± λ©ν(cache/.meta.json)λ₯Ό λ°ννλ€. μμΌλ©΄ {'generated_at': None}."""
path = os.path.join(_CACHE_DIR, ".meta.json")
try:
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError, OSError):
return {"generated_at": None}
|