eshan6704 commited on
Commit
4fcb9e6
·
verified ·
1 Parent(s): 0ae2bb7

Create app.py

Browse files
Files changed (1) hide show
  1. app/app.py +179 -0
app/app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 # ✅ screener owns its map
21
+
22
+
23
+ # -------------------------------------------------------
24
+ # FastAPI app
25
+ # -------------------------------------------------------
26
+ app = FastAPI(title="Stock / Index Backend")
27
+
28
+ # -------------------------------
29
+ # CORS
30
+ # -------------------------------
31
+ app.add_middleware(
32
+ CORSMiddleware,
33
+ allow_origins=["*"],
34
+ allow_methods=["*"],
35
+ allow_headers=["*"],
36
+ )
37
+
38
+ # -------------------------------
39
+ # GZip
40
+ # -------------------------------
41
+ app.add_middleware(GZipMiddleware, minimum_size=1000)
42
+
43
+
44
+ # -------------------------------------------------------
45
+ # Request model
46
+ # -------------------------------------------------------
47
+ class FetchRequest(BaseModel):
48
+ mode: str
49
+ req_type: str
50
+ name: str
51
+ date_start: str = ""
52
+ date_end: str = ""
53
+
54
+
55
+ # -------------------------------------------------------
56
+ # Health
57
+ # -------------------------------------------------------
58
+ @app.get("/")
59
+ def health():
60
+ return {"status": "ok", "service": "backend alive"}
61
+
62
+
63
+ # -------------------------------------------------------
64
+ # STOCK handler
65
+ # -------------------------------------------------------
66
+ def handle_stock(req: FetchRequest):
67
+ t = req.req_type.lower()
68
+
69
+ if t == "info":
70
+ return yahooinfo.fetch_info(req.name)
71
+ if t == "intraday":
72
+ return stock.fetch_intraday(req.name)
73
+ if t == "daily":
74
+ return stock.fetch_daily(req.name, req.date_end)
75
+ if t == "nse_eq":
76
+ return eq.build_eq_html(req.name)
77
+ if t == "qresult":
78
+ return stock.fetch_qresult(req.name)
79
+ if t == "result":
80
+ return stock.fetch_result(req.name)
81
+ if t == "balance":
82
+ return stock.fetch_balance(req.name)
83
+ if t == "cashflow":
84
+ return stock.fetch_cashflow(req.name)
85
+ if t == "dividend":
86
+ return stock.fetch_dividend(req.name)
87
+ if t == "split":
88
+ return stock.fetch_split(req.name)
89
+ if t == "other":
90
+ return stock.fetch_other(req.name)
91
+ if t == "stock_hist":
92
+ return ns.nse_stock_hist(
93
+ req.date_start, req.date_end, req.name
94
+ ).to_html()
95
+
96
+ return common.wrap(f"<h3>Unhandled stock req_type: {t}</h3>")
97
+
98
+
99
+ # -------------------------------------------------------
100
+ # INDEX handler
101
+ # -------------------------------------------------------
102
+ def handle_index(req: FetchRequest):
103
+ t = req.req_type.lower()
104
+
105
+ if t == "indices":
106
+ return indices.build_indices_html()
107
+ if t == "nse_open":
108
+ return live.build_index_live_html()
109
+ if t == "nse_preopen":
110
+ return pre.build_preopen_html()
111
+ if t == "nse_fno":
112
+ return fno.nse_fno_html(req.date_end, req.name)
113
+ if t == "nse_fiidii":
114
+ return ns.nse_fiidii()
115
+ if t == "nse_events":
116
+ return ns.nse_events()
117
+ if t == "nse_future":
118
+ return ns.nse_future(req.name)
119
+ if t == "nse_highlow":
120
+ return ns.nse_highlow(req.date_end)
121
+ if t == "stock_highlow":
122
+ return ns.stock_highlow(req.date_end)
123
+ if t == "nse_bhav":
124
+ return bhav.build_bhavcopy_html(req.date_end)
125
+ if t == "nse_largedeals":
126
+ return ns.nse_largedeals()
127
+ if t == "nse_bulkdeals":
128
+ return ns.nse_bulkdeals()
129
+ if t == "nse_blockdeals":
130
+ return ns.nse_blockdeals()
131
+ if t == "nse_most_active":
132
+ return ns.nse_most_active()
133
+ if t == "index_history":
134
+ return ns.index_history("NIFTY", req.date_start, req.date_end)
135
+ if t == "largedeals_historical":
136
+ return ns.nse_largedeals_historical(
137
+ req.date_start, req.date_end
138
+ )
139
+ if t == "index_pe_pb_div":
140
+ return ns.index_pe_pb_div(
141
+ "NIFTY", req.date_start, req.date_end
142
+ )
143
+ if t == "index_total_returns":
144
+ return ns.index_total_returns(
145
+ "NIFTY", req.date_start, req.date_end
146
+ )
147
+
148
+ return common.wrap(f"<h3>Unhandled index req_type: {t}</h3>")
149
+
150
+
151
+ # -------------------------------------------------------
152
+ # SCREENER handler
153
+ # -------------------------------------------------------
154
+ def handle_screener(req: FetchRequest):
155
+ return screener.fetch_screener(req.name)
156
+
157
+
158
+ # -------------------------------------------------------
159
+ # Main API
160
+ # -------------------------------------------------------
161
+ @app.post("/api/fetch", response_class=HTMLResponse)
162
+ def fetch_data(req: FetchRequest):
163
+
164
+ mode = req.mode.lower()
165
+
166
+ if mode == "stock":
167
+ html = handle_stock(req)
168
+
169
+ elif mode == "index":
170
+ html = handle_index(req)
171
+
172
+ elif mode == "screener":
173
+ html = handle_screener(req)
174
+
175
+ else:
176
+ raise HTTPException(status_code=400, detail="Invalid mode")
177
+
178
+ # 🔒 Always return HTML
179
+ return HTMLResponse(content=str(html))