Spaces:
Running
Running
Update app/app.py
Browse files- app/app.py +7 -215
app/app.py
CHANGED
|
@@ -1,24 +1,12 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 5 |
-
|
| 6 |
# -------------------------------------------------------
|
| 7 |
-
#
|
| 8 |
# -------------------------------------------------------
|
| 9 |
-
from . import
|
| 10 |
-
from . import stock
|
| 11 |
-
from . import indices_html as indices
|
| 12 |
-
from . import index_live_html as live
|
| 13 |
-
from . import preopen_html as pre
|
| 14 |
-
from . import eq_html as eq
|
| 15 |
-
from . import bhavcopy_html as bhav
|
| 16 |
-
from . import build_nse_fno as fno
|
| 17 |
-
from . import nsepythonmodified as ns
|
| 18 |
-
from . import yahooinfo
|
| 19 |
-
from . import screener # screener owns its map
|
| 20 |
|
| 21 |
-
#from .router import *
|
| 22 |
# -------------------------------------------------------
|
| 23 |
# FastAPI app
|
| 24 |
# -------------------------------------------------------
|
|
@@ -34,206 +22,10 @@ app.add_middleware(
|
|
| 34 |
allow_headers=["*"],
|
| 35 |
)
|
| 36 |
|
|
|
|
| 37 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
# -------------------------------------------------------
|
| 43 |
-
#
|
| 44 |
-
# -------------------------------------------------------
|
| 45 |
-
REQ_TYPE_MAP = {
|
| 46 |
-
"stock": [
|
| 47 |
-
"info", "intraday", "daily", "nse_eq", "qresult",
|
| 48 |
-
"result", "balance", "cashflow", "dividend",
|
| 49 |
-
"split", "other", "stock_hist"
|
| 50 |
-
],
|
| 51 |
-
"index": [
|
| 52 |
-
"indices", "open", "preopen", "fno",
|
| 53 |
-
"fiidii", "events",
|
| 54 |
-
"index_highlow", "stock_highlow", "bhav",
|
| 55 |
-
"largedeals", "bulkdeals", "blockdeals",
|
| 56 |
-
"most_active", "index_history",
|
| 57 |
-
"Hlargedeals", "pe_pb",
|
| 58 |
-
"total_returns"
|
| 59 |
-
],
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
# ===============================
|
| 63 |
-
# Screener name → URL mapping
|
| 64 |
-
# ===============================
|
| 65 |
-
SCREENER_MAP = {
|
| 66 |
-
"from_high": "https://www.screener.in/screens/3355081/from-high/",
|
| 67 |
-
"sales_wise": "https://www.screener.in/screens/880780/sales_wise/",
|
| 68 |
-
"fii_buying": "https://www.screener.in/screens/343087/fii-buying/",
|
| 69 |
-
"debt_reduction": "https://www.screener.in/screens/126864/debt-reduction/",
|
| 70 |
-
"magic_formula": "https://www.screener.in/screens/59/magic-formula/",
|
| 71 |
-
|
| 72 |
-
}
|
| 73 |
-
# -------------------------------------------------------
|
| 74 |
-
# HTML builder for req_type discovery
|
| 75 |
-
# -------------------------------------------------------
|
| 76 |
-
def build_req_type_list_html():
|
| 77 |
-
html = ["<div id='req_type_list'>"]
|
| 78 |
-
|
| 79 |
-
# STOCK & INDEX
|
| 80 |
-
for mode, items in REQ_TYPE_MAP.items():
|
| 81 |
-
html.append(f"<h3>{mode.upper()}</h3><ul>")
|
| 82 |
-
for it in items:
|
| 83 |
-
html.append(
|
| 84 |
-
f"<li class='{mode}-req' data-mode='{mode}'>{it}</li>"
|
| 85 |
-
)
|
| 86 |
-
html.append("</ul>")
|
| 87 |
-
|
| 88 |
-
# SCREENER (keys extracted from screener.py)
|
| 89 |
-
html.append("<h3>SCREENER</h3><ul>")
|
| 90 |
-
for key in SCREENER_MAP.keys():
|
| 91 |
-
html.append(
|
| 92 |
-
f"<li class='screener-req' data-mode='screener'>{key}</li>"
|
| 93 |
-
)
|
| 94 |
-
html.append("</ul></div>")
|
| 95 |
-
|
| 96 |
-
return "".join(html)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
# -------------------------------------------------------
|
| 100 |
-
# Request model
|
| 101 |
-
# -------------------------------------------------------
|
| 102 |
-
class FetchRequest(BaseModel):
|
| 103 |
-
mode: str
|
| 104 |
-
req_type: str
|
| 105 |
-
name: str = ""
|
| 106 |
-
date_start: str = ""
|
| 107 |
-
date_end: str = ""
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# -------------------------------------------------------
|
| 112 |
-
# STOCK handler
|
| 113 |
-
# -------------------------------------------------------
|
| 114 |
-
def handle_stock(req: FetchRequest):
|
| 115 |
-
t = req.req_type.lower()
|
| 116 |
-
|
| 117 |
-
if t == "info":
|
| 118 |
-
return yahooinfo.fetch_info(req.name)
|
| 119 |
-
if t == "intraday":
|
| 120 |
-
return stock.fetch_intraday(req.name)
|
| 121 |
-
if t == "daily":
|
| 122 |
-
return stock.fetch_daily(req.name, req.date_end)
|
| 123 |
-
if t == "nse_eq":
|
| 124 |
-
return eq.build_eq_html(req.name)
|
| 125 |
-
if t == "qresult":
|
| 126 |
-
return stock.fetch_qresult(req.name)
|
| 127 |
-
if t == "result":
|
| 128 |
-
return stock.fetch_result(req.name)
|
| 129 |
-
if t == "balance":
|
| 130 |
-
return stock.fetch_balance(req.name)
|
| 131 |
-
if t == "cashflow":
|
| 132 |
-
return stock.fetch_cashflow(req.name)
|
| 133 |
-
if t == "dividend":
|
| 134 |
-
return stock.fetch_dividend(req.name)
|
| 135 |
-
if t == "split":
|
| 136 |
-
return stock.fetch_split(req.name)
|
| 137 |
-
if t == "other":
|
| 138 |
-
return stock.fetch_other(req.name)
|
| 139 |
-
if t == "stock_hist":
|
| 140 |
-
return ns.nse_stock_hist(
|
| 141 |
-
req.date_start, req.date_end, req.name
|
| 142 |
-
).to_html()
|
| 143 |
-
|
| 144 |
-
return common.wrap(f"<h3>Unhandled stock req_type: {t}</h3>")
|
| 145 |
-
|
| 146 |
-
|
| 147 |
# -------------------------------------------------------
|
| 148 |
-
|
| 149 |
-
# -------------------------------------------------------
|
| 150 |
-
def handle_index(req: FetchRequest):
|
| 151 |
-
t = req.req_type.lower()
|
| 152 |
-
|
| 153 |
-
if t == "indices":
|
| 154 |
-
return indices.build_indices_html()
|
| 155 |
-
if t == "open":
|
| 156 |
-
return live.build_index_live_html()
|
| 157 |
-
if t == "preopen":
|
| 158 |
-
return pre.build_preopen_html()
|
| 159 |
-
if t == "fno":
|
| 160 |
-
return fno.nse_fno_html(req.date_end, req.name)
|
| 161 |
-
if t == "fiidii":
|
| 162 |
-
return ns.nse_fiidii()
|
| 163 |
-
if t == "events":
|
| 164 |
-
return ns.nse_events()
|
| 165 |
-
|
| 166 |
-
if t == "index_highlow":
|
| 167 |
-
return ns.nse_highlow(req.date_end)
|
| 168 |
-
if t == "stock_highlow":
|
| 169 |
-
return ns.stock_highlow(req.date_end)
|
| 170 |
-
if t == "bhav":
|
| 171 |
-
return bhav.build_bhavcopy_html(req.date_end)
|
| 172 |
-
if t == "largedeals":
|
| 173 |
-
return ns.nse_largedeals()
|
| 174 |
-
if t == "bulkdeals":
|
| 175 |
-
return ns.nse_bulkdeals()
|
| 176 |
-
if t == "blockdeals":
|
| 177 |
-
return ns.nse_blockdeals()
|
| 178 |
-
if t == "most_active":
|
| 179 |
-
return ns.nse_most_active()
|
| 180 |
-
if t == "index_history":
|
| 181 |
-
return ns.index_history("NIFTY", req.date_start, req.date_end)
|
| 182 |
-
if t == "Hlargedeals":
|
| 183 |
-
return ns.nse_largedeals_historical(
|
| 184 |
-
req.date_start, req.date_end
|
| 185 |
-
)
|
| 186 |
-
if t == "pe_pb":
|
| 187 |
-
return ns.index_pe_pb_div(
|
| 188 |
-
"NIFTY", req.date_start, req.date_end
|
| 189 |
-
)
|
| 190 |
-
if t == "total_returns":
|
| 191 |
-
return ns.index_total_returns(
|
| 192 |
-
"NIFTY", req.date_start, req.date_end
|
| 193 |
-
)
|
| 194 |
-
|
| 195 |
-
return common.wrap(f"<h3>Unhandled index req_type: {t}</h3>")
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
# -------------------------------------------------------
|
| 199 |
-
# SCREENER handler
|
| 200 |
-
# -------------------------------------------------------
|
| 201 |
-
def handle_screener(req: FetchRequest):
|
| 202 |
-
return screener.fetch_screener(req.req_type.lower())
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
# -------------------------------------------------------
|
| 207 |
-
# Health
|
| 208 |
-
# -------------------------------------------------------
|
| 209 |
-
@app.get("/")
|
| 210 |
-
def health():
|
| 211 |
-
return {"status": "ok", "service": "backend alive"}
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
# -------------------------------------------------------
|
| 215 |
-
# Main API
|
| 216 |
-
# -------------------------------------------------------
|
| 217 |
-
@app.post("/api/fetch", response_class=HTMLResponse)
|
| 218 |
-
def fetch_data(req: FetchRequest):
|
| 219 |
-
|
| 220 |
-
mode = req.mode.lower()
|
| 221 |
-
|
| 222 |
-
# ✅ Used by frontend on page load
|
| 223 |
-
if mode == "list":
|
| 224 |
-
return HTMLResponse(content=build_req_type_list_html())
|
| 225 |
-
|
| 226 |
-
if mode == "stock":
|
| 227 |
-
html = handle_stock(req)
|
| 228 |
-
|
| 229 |
-
elif mode == "index":
|
| 230 |
-
html = handle_index(req)
|
| 231 |
-
|
| 232 |
-
elif mode == "screener":
|
| 233 |
-
html = handle_screener(req)
|
| 234 |
-
|
| 235 |
-
else:
|
| 236 |
-
raise HTTPException(status_code=400, detail="Invalid mode")
|
| 237 |
-
|
| 238 |
-
# 🔒 Always return HTML
|
| 239 |
-
return HTMLResponse(content=str(html))
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
|
|
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from fastapi.middleware.gzip import GZipMiddleware
|
| 4 |
+
|
| 5 |
# -------------------------------------------------------
|
| 6 |
+
# Router
|
| 7 |
# -------------------------------------------------------
|
| 8 |
+
from .router import router
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
|
|
|
| 10 |
# -------------------------------------------------------
|
| 11 |
# FastAPI app
|
| 12 |
# -------------------------------------------------------
|
|
|
|
| 22 |
allow_headers=["*"],
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# Enable gzip compression for large HTML responses
|
| 26 |
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
# -------------------------------------------------------
|
| 29 |
+
# Routes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# -------------------------------------------------------
|
| 31 |
+
app.include_router(router)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|