Spaces:
Sleeping
Sleeping
File size: 6,158 Bytes
58b5a46 c116ca7 58b5a46 fd65e12 e9f68d4 fd65e12 e9f68d4 58b5a46 fd65e12 e9f68d4 fd65e12 3718280 e9f68d4 58b5a46 239ff46 58b5a46 3718280 58b5a46 3718280 58b5a46 fd65e12 74c83cf fd65e12 908e4fd fd65e12 86c7881 fd65e12 86c7881 fd65e12 3718280 e9f68d4 fd65e12 58b5a46 fd65e12 58b5a46 fd65e12 58b5a46 fd65e12 58b5a46 fd65e12 e9f68d4 58b5a46 |
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 194 195 196 197 198 |
from fastapi import APIRouter, HTTPException
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
# -------------------------------------------------------
# Local modules
# -------------------------------------------------------
from . import common
from . import stock
from . import indices_html as indices
from . import index_live_html as live
from . import preopen_html as pre
from . import eq_html as eq
from . import bhavcopy_html as bhav
from . import build_nse_fno as fno
from . import nsepythonmodified as ns
from . import yahooinfo
from . import screener # executor only
# -------------------------------------------------------
# Router
# -------------------------------------------------------
router = APIRouter()
# -------------------------------------------------------
# REQ TYPE MAP (stock & index)
# -------------------------------------------------------
REQ_TYPE_MAP = {
"stock": [
"info", "intraday", "daily", "nse_eq", "qresult",
"result", "balance", "cashflow", "dividend",
"split", "other", "stock_hist"
],
"index": [
"indices", "open", "preopen", "fno",
"fiidii", "events",
"index_highlow", "stock_highlow", "bhav",
"largedeals", "bulkdeals", "blockdeals",
"most_active", "index_history",
"Hlargedeals", "pe_pb",
"total_returns"
],
}
# -------------------------------------------------------
# HTML builder for req_type discovery
# -------------------------------------------------------
def build_req_type_list_html():
html = ["<div id='req_type_list'>"]
for mode, items in REQ_TYPE_MAP.items():
html.append(f"<h3>{mode.upper()}</h3><ul>")
for it in items:
html.append(
f"<li class='{mode}-req' data-mode='{mode}'>{it}</li>"
)
html.append("</ul>")
# SCREENER (from screener.py)
html.append("<h3>SCREENER</h3><ul>")
for key in screener.SCREENER_MAP.keys():
html.append(
f"<li class='screener-req' data-mode='screener'>{key}</li>"
)
html.append("</ul></div>")
return "".join(html)
# -------------------------------------------------------
# Request model
# -------------------------------------------------------
class FetchRequest(BaseModel):
mode: str
req_type: str
name: str = ""
date_start: str = ""
date_end: str = ""
# -------------------------------------------------------
# STOCK handler
# -------------------------------------------------------
def handle_stock(req: FetchRequest):
t = req.req_type.lower()
if t == "info":
return yahooinfo.fetch_info(req.name)
if t == "intraday":
return stock.fetch_intraday(req.name)
if t == "daily":
return stock.fetch_daily(req.name, req.date_end,req.date_start)
if t == "nse_eq":
return eq.build_eq_html(req.name)
if t == "qresult":
return stock.fetch_qresult(req.name)
if t == "result":
return stock.fetch_result(req.name)
if t == "balance":
return stock.fetch_balance(req.name)
if t == "cashflow":
return stock.fetch_cashflow(req.name)
if t == "dividend":
return stock.fetch_dividend(req.name)
if t == "split":
return stock.fetch_split(req.name)
if t == "other":
return stock.fetch_other(req.name)
if t == "stock_hist":
return ns.nse_stock_hist(
req.date_start, req.date_end, req.name
).to_html()
return common.wrap(f"<h3>Unhandled stock req_type: {t}</h3>")
# -------------------------------------------------------
# INDEX handler
# -------------------------------------------------------
def handle_index(req: FetchRequest):
t = req.req_type.lower()
if t == "indices":
return indices.build_indices_html()
if t == "open":
return live.build_index_live_html(req.name)
if t == "preopen":
return pre.build_preopen_html(req.name)
if t == "fno":
return fno.nse_fno_html(req.date_end, req.name)
if t == "fiidii":
return ns.nse_fiidii()
if t == "events":
return ns.nse_events()
if t == "index_highlow":
return ns.nse_highlow(req.date_end)
if t == "stock_highlow":
return ns.stock_highlow(req.date_end)
if t == "bhav":
return bhav.build_bhavcopy_html(req.date_end)
if t == "largedeals":
return ns.nse_largedeals()
if t == "bulkdeals":
return ns.nse_bulkdeals()
if t == "blockdeals":
return ns.nse_blockdeals()
if t == "most_active":
return ns.nse_most_active()
if t == "index_history":
return ns.index_history("NIFTY", req.date_start, req.date_end)
if t == "Hlargedeals":
return ns.nse_largedeals_historical(
req.date_start, req.date_end
)
if t == "pe_pb":
return ns.index_pe_pb_div(
"NIFTY", req.date_start, req.date_end
)
if t == "total_returns":
return ns.index_total_returns(
"NIFTY", req.date_start, req.date_end
)
return common.wrap(f"<h3>Unhandled index req_type: {t}</h3>")
# -------------------------------------------------------
# SCREENER handler
# -------------------------------------------------------
def handle_screener(req: FetchRequest):
return screener.fetch_screener(req.req_type.lower())
# -------------------------------------------------------
# Routes
# -------------------------------------------------------
@router.get("/")
def health():
return {"status": "ok", "service": "backend alive"}
@router.post("/api/fetch", response_class=HTMLResponse)
def fetch_data(req: FetchRequest):
mode = req.mode.lower()
# frontend discovery call
if mode == "list":
return HTMLResponse(content=build_req_type_list_html())
if mode == "stock":
html = handle_stock(req)
elif mode == "index":
html = handle_index(req)
elif mode == "screener":
html = handle_screener(req)
else:
raise HTTPException(status_code=400, detail="Invalid mode")
return HTMLResponse(content=str(html))
|