File size: 4,690 Bytes
93a56ae
2a1ebf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a56ae
2a1ebf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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 "<h3>Error: EQ data not available</h3>"

    # -------------------------------------------------------
    # Helper: DataFrame → HTML table
    # -------------------------------------------------------
    def df_to_table(df):
        if df is None or df.empty:
            return '<div class="empty">No data</div>'
        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"""
        <div class="section">
            <div class="section-header">
                <div class="section-title">{sec}</div>
                <button class="toggle-btn" onclick="toggleSection('{sec}')">View / Hide</button>
            </div>
            <div id="{sec}" class="section-body">
                {df_to_table(normalized[sec])}
            </div>
        </div>
        """

    # -------------------------------------------------------
    # FINAL HTML
    # -------------------------------------------------------
    html = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Equity Report - {symbol}</title>
<style>
body {{
    font-family: Arial, sans-serif;
    background: #fafafa;
    margin: 0;
    padding: 12px;
}}
h2 {{
    color: #0366d6;
}}
.section {{
    background: #fff;
    border: 1px solid #ddd;
    margin-bottom: 14px;
    padding: 12px;
    border-radius: 6px;
}}
.section-header {{
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
}}
.section-title {{
    font-weight: bold;
    color: #0b69a3;
}}
.toggle-btn {{
    background: #0b69a3;
    color: #fff;
    border: none;
    padding: 6px 10px;
    font-size: 12px;
    border-radius: 5px;
    cursor: pointer;
}}
.tbl {{
    border-collapse: collapse;
    width: 100%;
    font-size: 13px;
}}
.tbl th {{
    background: #0b69a3;
    color: #fff;
    padding: 6px;
    border: 1px solid #ccc;
}}
.tbl td {{
    padding: 6px;
    border: 1px solid #ccc;
}}
.empty {{
    padding: 10px;
    color: #777;
}}
</style>

<script>
function toggleSection(id) {{
    let e = document.getElementById(id);
    e.style.display = (e.style.display === "none") ? "block" : "none";
}}
</script>
</head>

<body>
<h2>Equity Report — {symbol}</h2>
{section_html}
</body>
</html>
"""

    # ---------- SAVE CACHE ----------
    cache_save(cache_key, html)
    return html