from . import nsepythonmodified as ns import pandas as pd from datetime import datetime import os # ============================== # Simple daily cache helpers # ============================== CACHE_DIR = "./cache_eq" os.makedirs(CACHE_DIR, exist_ok=True) def _cache_path(key): return os.path.join(CACHE_DIR, f"{key}.html") def _is_today(path): if not os.path.exists(path): return False mtime = datetime.fromtimestamp(os.path.getmtime(path)).date() return mtime == datetime.now().date() def cache_load(key): path = _cache_path(key) if _is_today(path): try: with open(path, "r", encoding="utf-8") as f: return f.read() except: return False return False def cache_save(key, html): with open(_cache_path(key), "w", encoding="utf-8") as f: f.write(html) # ============================== # MAIN FUNCTION (CACHED) # ============================== def build_eq_html(symbol): """ Build full HTML page for eq(symbol) output DAILY CACHED """ cache_key = f"eq_{symbol.upper()}" # ---------- CACHE HIT ---------- cached = cache_load(cache_key) if cached: return cached # ------------------------------------------------------- # CALL eq() function # ------------------------------------------------------- out = ns.eq(symbol) if not isinstance(out, dict): return "

Error: EQ data not available

" # ------------------------------------------------------- # Helper: DataFrame → HTML table # ------------------------------------------------------- def df_to_table(df): if df is None or df.empty: return '
No data
' return df.to_html(index=False, escape=False, border=0, classes="tbl") # ------------------------------------------------------- # ORDER # ------------------------------------------------------- section_order = [ "metadata", "securityInfo", "priceInfo", "industryInfo", "pdSectorIndAll", "info", "preOpen", "preOpenMarket" ] # Normalize to DataFrames normalized = {} for sec in section_order: val = out.get(sec) if isinstance(val, pd.DataFrame): normalized[sec] = val elif isinstance(val, list): normalized[sec] = pd.DataFrame(val) elif isinstance(val, dict): normalized[sec] = pd.DataFrame([val]) else: normalized[sec] = pd.DataFrame() # ------------------------------------------------------- # Sections HTML # ------------------------------------------------------- section_html = "" for sec in section_order: section_html += f"""
{sec}
{df_to_table(normalized[sec])}
""" # ------------------------------------------------------- # FINAL HTML # ------------------------------------------------------- html = f""" Equity Report - {symbol}

Equity Report — {symbol}

{section_html} """ # ---------- SAVE CACHE ---------- cache_save(cache_key, html) return html