Spaces:
Running
Running
Update app/app.py
Browse files- app/app.py +48 -174
app/app.py
CHANGED
|
@@ -4,9 +4,6 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 4 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
| 7 |
-
# -------------------------------------------------------
|
| 8 |
-
# Local modules
|
| 9 |
-
# -------------------------------------------------------
|
| 10 |
from . import common
|
| 11 |
from . import stock
|
| 12 |
from . import indices_html as indices
|
|
@@ -17,16 +14,11 @@ from . import bhavcopy_html as bhav
|
|
| 17 |
from . import build_nse_fno as fno
|
| 18 |
from . import nsepythonmodified as ns
|
| 19 |
from . import yahooinfo
|
| 20 |
-
from . import screener
|
|
|
|
| 21 |
|
| 22 |
-
# -------------------------------------------------------
|
| 23 |
-
# FastAPI app
|
| 24 |
-
# -------------------------------------------------------
|
| 25 |
app = FastAPI(title="Stock / Index Backend")
|
| 26 |
|
| 27 |
-
# -------------------------------------------------------
|
| 28 |
-
# Middleware
|
| 29 |
-
# -------------------------------------------------------
|
| 30 |
app.add_middleware(
|
| 31 |
CORSMiddleware,
|
| 32 |
allow_origins=["*"],
|
|
@@ -35,69 +27,45 @@ app.add_middleware(
|
|
| 35 |
)
|
| 36 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 37 |
|
| 38 |
-
# -------------------------------------------------------
|
| 39 |
-
# Request model
|
| 40 |
-
# -------------------------------------------------------
|
| 41 |
class FetchRequest(BaseModel):
|
| 42 |
mode: str
|
| 43 |
-
req_type: str
|
| 44 |
name: str = ""
|
| 45 |
date_start: str = ""
|
| 46 |
date_end: str = ""
|
| 47 |
|
| 48 |
-
# -------------------------------------------------------
|
| 49 |
-
# Health
|
| 50 |
-
# -------------------------------------------------------
|
| 51 |
@app.get("/")
|
| 52 |
def health():
|
| 53 |
-
return {"status": "ok"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
# -------------------------------------------------------
|
| 56 |
-
# REQ TYPE MAP (stock & index only)
|
| 57 |
-
# -------------------------------------------------------
|
| 58 |
REQ_TYPE_MAP = {
|
| 59 |
-
"stock": [
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
"nse_fiidii", "nse_events", "nse_future",
|
| 67 |
-
"nse_highlow", "stock_highlow", "nse_largedeals",
|
| 68 |
-
"nse_bulkdeals", "nse_blockdeals", "nse_most_active",
|
| 69 |
-
"index_history", "largedeals_historical",
|
| 70 |
-
"index_pe_pb_div", "index_total_returns"
|
| 71 |
-
],
|
| 72 |
}
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
FNO_STOCK_LIST = ["ITC","ZEEL","PNB","NIFTY"] # stock & nse_bhav
|
| 78 |
-
OPEN_INDICES_LIST = ["NIFTY 50","NIFTY BANK"] # nse_open
|
| 79 |
-
PREOPEN_INDICES_LIST = ["NIFTY 50","NIFTY BANK"] # nse_preopen
|
| 80 |
|
| 81 |
-
# -------------------------------------------------------
|
| 82 |
-
# HTML builder for req_type discovery
|
| 83 |
-
# -------------------------------------------------------
|
| 84 |
def build_req_type_list_html():
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
| 88 |
-
html.append("<h3>STOCK</h3><ul>")
|
| 89 |
for t in REQ_TYPE_MAP["stock"]:
|
| 90 |
-
if t in ["stock_hist",
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
names = ""
|
| 94 |
-
html.append(
|
| 95 |
-
f"<li class='stock-req' data-mode='stock' data-type='{t}' data-names='{names}'>{t}</li>"
|
| 96 |
-
)
|
| 97 |
-
html.append("</ul>")
|
| 98 |
|
| 99 |
-
|
| 100 |
-
html.append("<h3>INDEX</h3><ul>")
|
| 101 |
for t in REQ_TYPE_MAP["index"]:
|
| 102 |
if t == "nse_open":
|
| 103 |
names = ",".join(OPEN_INDICES_LIST)
|
|
@@ -105,122 +73,28 @@ def build_req_type_list_html():
|
|
| 105 |
names = ",".join(PREOPEN_INDICES_LIST)
|
| 106 |
else:
|
| 107 |
names = ""
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
)
|
| 119 |
-
html.append("</ul></div>")
|
| 120 |
-
|
| 121 |
-
return "".join(html)
|
| 122 |
-
|
| 123 |
-
# -------------------------------------------------------
|
| 124 |
-
# STOCK handler
|
| 125 |
-
# -------------------------------------------------------
|
| 126 |
-
def handle_stock(req: FetchRequest):
|
| 127 |
-
t = req.req_type.lower()
|
| 128 |
-
|
| 129 |
-
if t == "info":
|
| 130 |
-
return yahooinfo.fetch_info(req.name)
|
| 131 |
-
if t == "intraday":
|
| 132 |
-
return stock.fetch_intraday(req.name)
|
| 133 |
-
if t == "daily":
|
| 134 |
-
return stock.fetch_daily(req.name, req.date_end)
|
| 135 |
-
if t == "nse_eq":
|
| 136 |
-
return eq.build_eq_html(req.name)
|
| 137 |
-
if t == "qresult":
|
| 138 |
-
return stock.fetch_qresult(req.name)
|
| 139 |
-
if t == "result":
|
| 140 |
-
return stock.fetch_result(req.name)
|
| 141 |
-
if t == "balance":
|
| 142 |
-
return stock.fetch_balance(req.name)
|
| 143 |
-
if t == "cashflow":
|
| 144 |
-
return stock.fetch_cashflow(req.name)
|
| 145 |
-
if t == "dividend":
|
| 146 |
-
return stock.fetch_dividend(req.name)
|
| 147 |
-
if t == "split":
|
| 148 |
-
return stock.fetch_split(req.name)
|
| 149 |
-
if t == "other":
|
| 150 |
-
return stock.fetch_other(req.name)
|
| 151 |
-
if t in ["stock_hist", "nse_bhav"]:
|
| 152 |
-
return ns.nse_stock_hist(req.date_start, req.date_end, req.name).to_html()
|
| 153 |
-
|
| 154 |
-
return common.wrap(f"<h3>Unhandled stock req_type: {t}</h3>")
|
| 155 |
-
|
| 156 |
-
# -------------------------------------------------------
|
| 157 |
-
# INDEX handler
|
| 158 |
-
# -------------------------------------------------------
|
| 159 |
-
def handle_index(req: FetchRequest):
|
| 160 |
-
t = req.req_type.lower()
|
| 161 |
-
|
| 162 |
-
if t == "indices":
|
| 163 |
-
return indices.build_indices_html()
|
| 164 |
-
if t == "nse_open":
|
| 165 |
-
return live.build_index_live_html()
|
| 166 |
-
if t == "nse_preopen":
|
| 167 |
-
return pre.build_preopen_html()
|
| 168 |
-
if t == "nse_fno":
|
| 169 |
-
return fno.nse_fno_html(req.date_end, req.name)
|
| 170 |
-
if t == "nse_fiidii":
|
| 171 |
-
return ns.nse_fiidii()
|
| 172 |
-
if t == "nse_events":
|
| 173 |
-
return ns.nse_events()
|
| 174 |
-
if t == "nse_future":
|
| 175 |
-
return ns.nse_future(req.name)
|
| 176 |
-
if t == "nse_highlow":
|
| 177 |
-
return ns.nse_highlow(req.date_end)
|
| 178 |
-
if t == "stock_highlow":
|
| 179 |
-
return ns.stock_highlow(req.date_end)
|
| 180 |
-
if t == "nse_largedeals":
|
| 181 |
-
return ns.nse_largedeals()
|
| 182 |
-
if t == "nse_bulkdeals":
|
| 183 |
-
return ns.nse_bulkdeals()
|
| 184 |
-
if t == "nse_blockdeals":
|
| 185 |
-
return ns.nse_blockdeals()
|
| 186 |
-
if t == "nse_most_active":
|
| 187 |
-
return ns.nse_most_active()
|
| 188 |
-
if t == "index_history":
|
| 189 |
-
return ns.index_history("NIFTY", req.date_start, req.date_end)
|
| 190 |
-
if t == "largedeals_historical":
|
| 191 |
-
return ns.nse_largedeals_historical(req.date_start, req.date_end)
|
| 192 |
-
if t == "index_pe_pb_div":
|
| 193 |
-
return ns.index_pe_pb_div("NIFTY", req.date_start, req.date_end)
|
| 194 |
-
if t == "index_total_returns":
|
| 195 |
-
return ns.index_total_returns("NIFTY", req.date_start, req.date_end)
|
| 196 |
-
|
| 197 |
-
return common.wrap(f"<h3>Unhandled index req_type: {t}</h3>")
|
| 198 |
-
|
| 199 |
-
# -------------------------------------------------------
|
| 200 |
-
# SCREENER handler
|
| 201 |
-
# -------------------------------------------------------
|
| 202 |
-
def handle_screener(req: FetchRequest):
|
| 203 |
-
return screener.fetch_screener(req.req_type.lower())
|
| 204 |
-
|
| 205 |
-
# -------------------------------------------------------
|
| 206 |
-
# Main API
|
| 207 |
-
# -------------------------------------------------------
|
| 208 |
@app.post("/api/fetch", response_class=HTMLResponse)
|
| 209 |
-
def
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
if mode == "
|
| 214 |
-
return HTMLResponse(
|
| 215 |
-
|
| 216 |
-
if mode == "
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
raise HTTPException(status_code=400, detail="Invalid mode")
|
| 224 |
-
|
| 225 |
-
# 🔒 Always return HTML
|
| 226 |
-
return HTMLResponse(content=str(html))
|
|
|
|
| 4 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
from . import common
|
| 8 |
from . import stock
|
| 9 |
from . import indices_html as indices
|
|
|
|
| 14 |
from . import build_nse_fno as fno
|
| 15 |
from . import nsepythonmodified as ns
|
| 16 |
from . import yahooinfo
|
| 17 |
+
from . import screener
|
| 18 |
+
from . import ui_html # ✅ NEW
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
app = FastAPI(title="Stock / Index Backend")
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
app.add_middleware(
|
| 23 |
CORSMiddleware,
|
| 24 |
allow_origins=["*"],
|
|
|
|
| 27 |
)
|
| 28 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
class FetchRequest(BaseModel):
|
| 31 |
mode: str
|
| 32 |
+
req_type: str = ""
|
| 33 |
name: str = ""
|
| 34 |
date_start: str = ""
|
| 35 |
date_end: str = ""
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
@app.get("/")
|
| 38 |
def health():
|
| 39 |
+
return {"status": "ok"}
|
| 40 |
+
|
| 41 |
+
@app.get("/ui", response_class=HTMLResponse)
|
| 42 |
+
def frontend():
|
| 43 |
+
return ui_html.build_frontend_html()
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
REQ_TYPE_MAP = {
|
| 46 |
+
"stock": ["info","intraday","daily","nse_eq","qresult","result","balance",
|
| 47 |
+
"cashflow","dividend","split","other","stock_hist","nse_bhav"],
|
| 48 |
+
"index": ["indices","nse_open","nse_preopen","nse_fno","nse_fiidii",
|
| 49 |
+
"nse_events","nse_future","nse_highlow","stock_highlow",
|
| 50 |
+
"nse_largedeals","nse_bulkdeals","nse_blockdeals",
|
| 51 |
+
"nse_most_active","index_history","largedeals_historical",
|
| 52 |
+
"index_pe_pb_div","index_total_returns"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
+
FNO_STOCK_LIST = ["ITC","ZEEL","PNB","NIFTY"]
|
| 56 |
+
OPEN_INDICES_LIST = ["NIFTY 50","NIFTY BANK"]
|
| 57 |
+
PREOPEN_INDICES_LIST = ["NIFTY 50","NIFTY BANK"]
|
|
|
|
|
|
|
|
|
|
| 58 |
|
|
|
|
|
|
|
|
|
|
| 59 |
def build_req_type_list_html():
|
| 60 |
+
h = ["<div>"]
|
| 61 |
|
| 62 |
+
h.append("<h3>STOCK</h3><ul>")
|
|
|
|
| 63 |
for t in REQ_TYPE_MAP["stock"]:
|
| 64 |
+
names = ",".join(FNO_STOCK_LIST) if t in ["stock_hist","nse_bhav"] else ""
|
| 65 |
+
h.append(f"<li data-mode='stock' data-type='{t}' data-names='{names}'>{t}</li>")
|
| 66 |
+
h.append("</ul>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
h.append("<h3>INDEX</h3><ul>")
|
|
|
|
| 69 |
for t in REQ_TYPE_MAP["index"]:
|
| 70 |
if t == "nse_open":
|
| 71 |
names = ",".join(OPEN_INDICES_LIST)
|
|
|
|
| 73 |
names = ",".join(PREOPEN_INDICES_LIST)
|
| 74 |
else:
|
| 75 |
names = ""
|
| 76 |
+
h.append(f"<li data-mode='index' data-type='{t}' data-names='{names}'>{t}</li>")
|
| 77 |
+
h.append("</ul>")
|
| 78 |
+
|
| 79 |
+
h.append("<h3>SCREENER</h3><ul>")
|
| 80 |
+
for k in screener.SCREENER_MAP:
|
| 81 |
+
h.append(f"<li data-mode='screener' data-type='{k}'>{k}</li>")
|
| 82 |
+
h.append("</ul></div>")
|
| 83 |
+
|
| 84 |
+
return "".join(h)
|
| 85 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
@app.post("/api/fetch", response_class=HTMLResponse)
|
| 87 |
+
def fetch(req: FetchRequest):
|
| 88 |
+
if req.mode == "list":
|
| 89 |
+
return build_req_type_list_html()
|
| 90 |
+
|
| 91 |
+
if req.mode == "stock":
|
| 92 |
+
return HTMLResponse(str(stock.fetch_intraday(req.name))) if req.req_type=="intraday" else yahooinfo.fetch_info(req.name)
|
| 93 |
+
|
| 94 |
+
if req.mode == "index":
|
| 95 |
+
return indices.build_indices_html()
|
| 96 |
+
|
| 97 |
+
if req.mode == "screener":
|
| 98 |
+
return screener.fetch_screener(req.req_type)
|
| 99 |
+
|
| 100 |
+
raise HTTPException(400, "Invalid mode")
|
|
|
|
|
|
|
|
|
|
|
|