Spaces:
Running
Running
Delete app/app.py
Browse files- app/app.py +0 -187
app/app.py
DELETED
|
@@ -1,187 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
from fastapi.middleware.gzip import GZipMiddleware
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
-
|
| 7 |
-
# -------------------------------------------------------
|
| 8 |
-
# Local modules (UNCHANGED)
|
| 9 |
-
# -------------------------------------------------------
|
| 10 |
-
from . import common
|
| 11 |
-
from . import stock
|
| 12 |
-
from . import indices_html as indices
|
| 13 |
-
from . import index_live_html as live
|
| 14 |
-
from . import preopen_html as pre
|
| 15 |
-
from . import eq_html as eq
|
| 16 |
-
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 |
-
from . import ui_html # ✅ ONLY HTML, no logic
|
| 22 |
-
|
| 23 |
-
# -------------------------------------------------------
|
| 24 |
-
# FastAPI app
|
| 25 |
-
# -------------------------------------------------------
|
| 26 |
-
app = FastAPI(title="Stock / Index Backend")
|
| 27 |
-
|
| 28 |
-
app.add_middleware(
|
| 29 |
-
CORSMiddleware,
|
| 30 |
-
allow_origins=["*"],
|
| 31 |
-
allow_methods=["*"],
|
| 32 |
-
allow_headers=["*"],
|
| 33 |
-
)
|
| 34 |
-
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 35 |
-
|
| 36 |
-
# -------------------------------------------------------
|
| 37 |
-
# Request model
|
| 38 |
-
# -------------------------------------------------------
|
| 39 |
-
class FetchRequest(BaseModel):
|
| 40 |
-
mode: str
|
| 41 |
-
req_type: str = ""
|
| 42 |
-
name: str = ""
|
| 43 |
-
date_start: str = ""
|
| 44 |
-
date_end: str = ""
|
| 45 |
-
|
| 46 |
-
# -------------------------------------------------------
|
| 47 |
-
# Health
|
| 48 |
-
# -------------------------------------------------------
|
| 49 |
-
@app.get("/")
|
| 50 |
-
def health():
|
| 51 |
-
return {"status": "ok", "service": "backend alive"}
|
| 52 |
-
|
| 53 |
-
# -------------------------------------------------------
|
| 54 |
-
# CONFIG (single source of truth)
|
| 55 |
-
# -------------------------------------------------------
|
| 56 |
-
REQ_TYPE_MAP = {
|
| 57 |
-
"stock": [
|
| 58 |
-
"info","intraday","daily","nse_eq","qresult",
|
| 59 |
-
"result","balance","cashflow","dividend",
|
| 60 |
-
"split","other","stock_hist","nse_bhav"
|
| 61 |
-
],
|
| 62 |
-
"index": [
|
| 63 |
-
"indices","nse_open","nse_preopen","nse_fno",
|
| 64 |
-
"nse_fiidii","nse_events","nse_future",
|
| 65 |
-
"nse_highlow","stock_highlow","nse_largedeals",
|
| 66 |
-
"nse_bulkdeals","nse_blockdeals","nse_most_active",
|
| 67 |
-
"index_history","largedeals_historical",
|
| 68 |
-
"index_pe_pb_div","index_total_returns"
|
| 69 |
-
],
|
| 70 |
-
}
|
| 71 |
-
|
| 72 |
-
# -------------------------------------------------------
|
| 73 |
-
# Name lists (backend authority)
|
| 74 |
-
# -------------------------------------------------------
|
| 75 |
-
FNO_STOCK_LIST = ["ITC","ZEEL","PNB","NIFTY"]
|
| 76 |
-
OPEN_INDICES_LIST = ["NIFTY 50","NIFTY BANK"]
|
| 77 |
-
PREOPEN_INDICES_LIST = ["NIFTY 50","NIFTY BANK"]
|
| 78 |
-
|
| 79 |
-
# -------------------------------------------------------
|
| 80 |
-
# 🔹 LIST BUILDER (frontend bootstrap)
|
| 81 |
-
# -------------------------------------------------------
|
| 82 |
-
def build_req_type_list_html():
|
| 83 |
-
html = ["<div id='backend_meta'>"]
|
| 84 |
-
|
| 85 |
-
# STOCK
|
| 86 |
-
for t in REQ_TYPE_MAP["stock"]:
|
| 87 |
-
names = ",".join(FNO_STOCK_LIST) if t in ["stock_hist","nse_bhav"] else ""
|
| 88 |
-
html.append(
|
| 89 |
-
f"<li data-mode='stock' data-type='{t}' data-names='{names}'></li>"
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
# INDEX
|
| 93 |
-
for t in REQ_TYPE_MAP["index"]:
|
| 94 |
-
if t == "nse_open":
|
| 95 |
-
names = ",".join(OPEN_INDICES_LIST)
|
| 96 |
-
elif t == "nse_preopen":
|
| 97 |
-
names = ",".join(PREOPEN_INDICES_LIST)
|
| 98 |
-
else:
|
| 99 |
-
names = ""
|
| 100 |
-
html.append(
|
| 101 |
-
f"<li data-mode='index' data-type='{t}' data-names='{names}'></li>"
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
# SCREENER
|
| 105 |
-
for key in screener.SCREENER_MAP:
|
| 106 |
-
html.append(
|
| 107 |
-
f"<li data-mode='screener' data-type='{key}'></li>"
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
html.append("</div>")
|
| 111 |
-
return "".join(html)
|
| 112 |
-
|
| 113 |
-
# -------------------------------------------------------
|
| 114 |
-
# UI (HTML only)
|
| 115 |
-
# -------------------------------------------------------
|
| 116 |
-
@app.get("/ui", response_class=HTMLResponse)
|
| 117 |
-
def ui():
|
| 118 |
-
return ui_html.HTML
|
| 119 |
-
|
| 120 |
-
# -------------------------------------------------------
|
| 121 |
-
# HANDLERS (ALL preserved)
|
| 122 |
-
# -------------------------------------------------------
|
| 123 |
-
def handle_stock(req: FetchRequest):
|
| 124 |
-
t = req.req_type.lower()
|
| 125 |
-
if t == "info": return yahooinfo.fetch_info(req.name)
|
| 126 |
-
if t == "intraday": return stock.fetch_intraday(req.name)
|
| 127 |
-
if t == "daily": return stock.fetch_daily(req.name, req.date_end)
|
| 128 |
-
if t == "nse_eq": return eq.build_eq_html(req.name)
|
| 129 |
-
if t == "qresult": return stock.fetch_qresult(req.name)
|
| 130 |
-
if t == "result": return stock.fetch_result(req.name)
|
| 131 |
-
if t == "balance": return stock.fetch_balance(req.name)
|
| 132 |
-
if t == "cashflow": return stock.fetch_cashflow(req.name)
|
| 133 |
-
if t == "dividend": return stock.fetch_dividend(req.name)
|
| 134 |
-
if t == "split": return stock.fetch_split(req.name)
|
| 135 |
-
if t == "other": return stock.fetch_other(req.name)
|
| 136 |
-
if t in ["stock_hist","nse_bhav"]:
|
| 137 |
-
return ns.nse_stock_hist(req.date_start, req.date_end, req.name).to_html()
|
| 138 |
-
return common.wrap(f"<h3>Unhandled stock req_type: {t}</h3>")
|
| 139 |
-
|
| 140 |
-
def handle_index(req: FetchRequest):
|
| 141 |
-
t = req.req_type.lower()
|
| 142 |
-
if t == "indices": return indices.build_indices_html()
|
| 143 |
-
if t == "nse_open": return live.build_index_live_html()
|
| 144 |
-
if t == "nse_preopen": return pre.build_preopen_html()
|
| 145 |
-
if t == "nse_fno": return fno.nse_fno_html(req.date_end, req.name)
|
| 146 |
-
if t == "nse_fiidii": return ns.nse_fiidii()
|
| 147 |
-
if t == "nse_events": return ns.nse_events()
|
| 148 |
-
if t == "nse_future": return ns.nse_future(req.name)
|
| 149 |
-
if t == "nse_highlow": return ns.nse_highlow(req.date_end)
|
| 150 |
-
if t == "stock_highlow": return ns.stock_highlow(req.date_end)
|
| 151 |
-
if t == "nse_largedeals": return ns.nse_largedeals()
|
| 152 |
-
if t == "nse_bulkdeals": return ns.nse_bulkdeals()
|
| 153 |
-
if t == "nse_blockdeals": return ns.nse_blockdeals()
|
| 154 |
-
if t == "nse_most_active": return ns.nse_most_active()
|
| 155 |
-
if t == "index_history":
|
| 156 |
-
return ns.index_history("NIFTY", req.date_start, req.date_end)
|
| 157 |
-
if t == "largedeals_historical":
|
| 158 |
-
return ns.nse_largedeals_historical(req.date_start, req.date_end)
|
| 159 |
-
if t == "index_pe_pb_div":
|
| 160 |
-
return ns.index_pe_pb_div("NIFTY", req.date_start, req.date_end)
|
| 161 |
-
if t == "index_total_returns":
|
| 162 |
-
return ns.index_total_returns("NIFTY", req.date_start, req.date_end)
|
| 163 |
-
return common.wrap(f"<h3>Unhandled index req_type: {t}</h3>")
|
| 164 |
-
|
| 165 |
-
def handle_screener(req: FetchRequest):
|
| 166 |
-
return screener.fetch_screener(req.req_type.lower())
|
| 167 |
-
|
| 168 |
-
# -------------------------------------------------------
|
| 169 |
-
# MAIN API
|
| 170 |
-
# -------------------------------------------------------
|
| 171 |
-
@app.post("/api/fetch", response_class=HTMLResponse)
|
| 172 |
-
def fetch_data(req: FetchRequest):
|
| 173 |
-
|
| 174 |
-
# 🔑 Frontend bootstrap
|
| 175 |
-
if req.mode == "list":
|
| 176 |
-
return build_req_type_list_html()
|
| 177 |
-
|
| 178 |
-
if req.mode == "stock":
|
| 179 |
-
html = handle_stock(req)
|
| 180 |
-
elif req.mode == "index":
|
| 181 |
-
html = handle_index(req)
|
| 182 |
-
elif req.mode == "screener":
|
| 183 |
-
html = handle_screener(req)
|
| 184 |
-
else:
|
| 185 |
-
raise HTTPException(status_code=400, detail="Invalid mode")
|
| 186 |
-
|
| 187 |
-
return HTMLResponse(content=str(html))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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