eshan6704 commited on
Commit
b60ea99
·
verified ·
1 Parent(s): cb729cb

Create app/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.middleware.cors import CORSMiddleware
3
+ from pydantic import BaseModel
4
+ import os
5
+ import importlib.util
6
+
7
+ # -------------------------------------------------------
8
+ # Explicit module loader (HF-safe)
9
+ # -------------------------------------------------------
10
+ BASE_DIR = os.path.dirname(os.path.abspath(__file__))
11
+
12
+ def load_module(name):
13
+ path = os.path.join(BASE_DIR, f"{name}.py")
14
+ if not os.path.exists(path):
15
+ raise ImportError(f"Module file not found: {path}")
16
+
17
+ spec = importlib.util.spec_from_file_location(name, path)
18
+ module = importlib.util.module_from_spec(spec)
19
+ spec.loader.exec_module(module)
20
+ return module
21
+
22
+ # -------------------------------------------------------
23
+ # Load ALL local modules explicitly
24
+ # -------------------------------------------------------
25
+ common = load_module("common")
26
+ stock = load_module("stock")
27
+ indices_html = load_module("indices_html")
28
+ index_live_html = load_module("index_live_html")
29
+ preopen_html = load_module("preopen_html")
30
+ eq_html = load_module("eq_html")
31
+ bhavcopy_html = load_module("bhavcopy_html")
32
+ build_nse_fno = load_module("build_nse_fno")
33
+
34
+ # External libs (installed via requirements.txt)
35
+ import nsepython
36
+ import yahooinfo
37
+
38
+ # -------------------------------------------------------
39
+ # FastAPI app
40
+ # -------------------------------------------------------
41
+ app = FastAPI(title="Stock / Index Backend")
42
+
43
+ app.add_middleware(
44
+ CORSMiddleware,
45
+ allow_origins=["*"],
46
+ allow_methods=["*"],
47
+ allow_headers=["*"],
48
+ )
49
+
50
+ # -------------------------------------------------------
51
+ # Valid request types
52
+ # -------------------------------------------------------
53
+ STOCK_REQ = [
54
+ "info", "intraday", "daily", "nse_eq", "qresult", "result",
55
+ "balance", "cashflow", "dividend", "split", "other", "stock_hist"
56
+ ]
57
+
58
+ INDEX_REQ = [
59
+ "indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
60
+ "nse_events", "nse_future", "nse_bhav", "nse_highlow", "stock_highlow",
61
+ "index_history", "nse_largedeals", "nse_most_active",
62
+ "largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
63
+ "index_pe_pb_div", "index_total_returns"
64
+ ]
65
+
66
+ # -------------------------------------------------------
67
+ # Request model
68
+ # -------------------------------------------------------
69
+ class FetchRequest(BaseModel):
70
+ mode: str
71
+ req_type: str
72
+ name: str
73
+ date_start: str
74
+ date_end: str
75
+
76
+ # -------------------------------------------------------
77
+ # Health
78
+ # -------------------------------------------------------
79
+ @app.get("/")
80
+ def health():
81
+ return {"status": "ok", "service": "backend alive"}
82
+
83
+ # -------------------------------------------------------
84
+ # STOCK handler
85
+ # -------------------------------------------------------
86
+ def handle_stock(req: FetchRequest):
87
+ t = req.req_type.lower()
88
+
89
+ if t == "info":
90
+ return common.wrap(yahooinfo.fetch_info(req.name))
91
+ if t == "intraday":
92
+ return common.wrap(stock.fetch_intraday(req.name, req.date_start, req.date_end))
93
+ if t == "daily":
94
+ return common.wrap(stock.fetch_daily(req.name, req.date_start, req.date_end))
95
+ if t == "nse_eq":
96
+ return eq_html.build_eq_html(req.name)
97
+ if t == "qresult":
98
+ return common.wrap(stock.fetch_qresult(req.name))
99
+ if t == "result":
100
+ return common.wrap(stock.fetch_result(req.name))
101
+ if t == "balance":
102
+ return common.wrap(stock.fetch_balance(req.name))
103
+ if t == "cashflow":
104
+ return common.wrap(stock.fetch_cashflow(req.name))
105
+ if t == "dividend":
106
+ return common.wrap(stock.fetch_dividend(req.name))
107
+ if t == "split":
108
+ return common.wrap(stock.fetch_split(req.name))
109
+ if t == "other":
110
+ return common.wrap(stock.fetch_other(req.name))
111
+ if t == "stock_hist":
112
+ return nsepython.nse_stock_hist(
113
+ req.date_start, req.date_end, req.name
114
+ ).to_html()
115
+
116
+ return common.wrap(f"<h3>Unhandled stock req_type: {t}</h3>")
117
+
118
+ # -------------------------------------------------------
119
+ # INDEX handler
120
+ # -------------------------------------------------------
121
+ def handle_index(req: FetchRequest):
122
+ t = req.req_type.lower()
123
+
124
+ if t == "indices":
125
+ return indices_html.build_indices_html()
126
+ if t == "nse_open":
127
+ return index_live_html.build_index_live_html()
128
+ if t == "nse_preopen":
129
+ return preopen_html.build_preopen_html()
130
+ if t == "nse_fno":
131
+ return build_nse_fno.nse_fno_html(req.date_end, req.name)
132
+ if t == "nse_fiidii":
133
+ return nsepython.nse_fiidii().to_html()
134
+ if t == "nse_events":
135
+ return nsepython.nse_events().to_html()
136
+ if t == "nse_future":
137
+ return common.wrap(nsepython.nse_future(req.name))
138
+ if t == "nse_highlow":
139
+ return nsepython.nse_highlow(req.date_end).to_html()
140
+ if t == "stock_highlow":
141
+ return nsepython.stock_highlow(req.date_end).to_html()
142
+ if t == "nse_bhav":
143
+ return bhavcopy_html.build_bhavcopy_html(req.date_end)
144
+ if t == "nse_largedeals":
145
+ return nsepython.nse_largedeals().to_html()
146
+ if t == "nse_bulkdeals":
147
+ return nsepython.nse_bulkdeals().to_html()
148
+ if t == "nse_blockdeals":
149
+ return nsepython.nse_blockdeals().to_html()
150
+ if t == "nse_most_active":
151
+ return nsepython.nse_most_active().to_html()
152
+ if t == "index_history":
153
+ return nsepython.index_history("NIFTY", req.date_start, req.date_end).to_html()
154
+ if t == "largedeals_historical":
155
+ return nsepython.nse_largedeals_historical(
156
+ req.date_start, req.date_end
157
+ ).to_html()
158
+ if t == "index_pe_pb_div":
159
+ return nsepython.index_pe_pb_div(
160
+ "NIFTY", req.date_start, req.date_end
161
+ ).to_html()
162
+ if t == "index_total_returns":
163
+ return nsepython.index_total_returns(
164
+ "NIFTY", req.date_start, req.date_end
165
+ ).to_html()
166
+
167
+ return common.wrap(f"<h3>Unhandled index req_type: {t}</h3>")
168
+
169
+ # -------------------------------------------------------
170
+ # Main API
171
+ # -------------------------------------------------------
172
+ @app.post("/api/fetch")
173
+ def fetch_data(req: FetchRequest):
174
+ if req.mode.lower() == "stock":
175
+ return handle_stock(req)
176
+ if req.mode.lower() == "index":
177
+ return handle_index(req)
178
+
179
+ raise HTTPException(status_code=400, detail="Invalid mode")