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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -157
app.py CHANGED
@@ -1,29 +1,45 @@
1
- import sys
2
- import os
3
-
4
- # 🔑 CRITICAL for Hugging Face Spaces
5
- sys.path.append(os.path.dirname(__file__))
6
-
7
- # ---------- Always explicitly import local modules ----------
8
- import common
9
- import stock
10
- import indices_html
11
- import index_live_html
12
- import preopen_html
13
- import eq_html
14
- import bhavcopy_html
15
- import nsepython
16
- import yahooinfo
17
- import build_nse_fno
18
-
19
  from fastapi import FastAPI, HTTPException
20
  from fastapi.middleware.cors import CORSMiddleware
21
  from pydantic import BaseModel
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # ---------- FastAPI app ----------
 
 
24
  app = FastAPI(title="Stock / Index Backend")
25
 
26
- # ---------- CORS ----------
27
  app.add_middleware(
28
  CORSMiddleware,
29
  allow_origins=["*"],
@@ -31,7 +47,9 @@ app.add_middleware(
31
  allow_headers=["*"],
32
  )
33
 
34
- # ---------- Valid req_type options ----------
 
 
35
  STOCK_REQ = [
36
  "info", "intraday", "daily", "nse_eq", "qresult", "result",
37
  "balance", "cashflow", "dividend", "split", "other", "stock_hist"
@@ -45,154 +63,117 @@ INDEX_REQ = [
45
  "index_pe_pb_div", "index_total_returns"
46
  ]
47
 
48
- # ---------- Request Model ----------
 
 
49
  class FetchRequest(BaseModel):
50
- mode: str # stock / index
51
  req_type: str
52
  name: str
53
- date_start: str # dd-mm-yyyy
54
- date_end: str # dd-mm-yyyy
55
 
56
- # ---------- Health ----------
 
 
57
  @app.get("/")
58
  def health():
59
  return {"status": "ok", "service": "backend alive"}
60
 
61
- # ---------- STOCK Handlers ----------
 
 
62
  def handle_stock(req: FetchRequest):
63
- try:
64
- if req.req_type not in STOCK_REQ:
65
- raise HTTPException(status_code=400, detail=f"Invalid stock req_type: {req.req_type}")
66
-
67
- t = req.req_type.lower()
68
-
69
- if t == "info":
70
- return common.wrap(yahooinfo.fetch_info(req.name))
71
-
72
- if t == "intraday":
73
- return common.wrap(stock.fetch_intraday(req.name, req.date_start, req.date_end))
74
-
75
- if t == "daily":
76
- return common.wrap(stock.fetch_daily(req.name, req.date_start, req.date_end))
77
-
78
- if t == "nse_eq":
79
- return eq_html.build_eq_html(req.name)
80
-
81
- if t == "qresult":
82
- return common.wrap(stock.fetch_qresult(req.name))
83
-
84
- if t == "result":
85
- return common.wrap(stock.fetch_result(req.name))
86
-
87
- if t == "balance":
88
- return common.wrap(stock.fetch_balance(req.name))
89
-
90
- if t == "cashflow":
91
- return common.wrap(stock.fetch_cashflow(req.name))
92
-
93
- if t == "dividend":
94
- return common.wrap(stock.fetch_dividend(req.name))
95
-
96
- if t == "split":
97
- return common.wrap(stock.fetch_split(req.name))
98
-
99
- if t == "other":
100
- return common.wrap(stock.fetch_other(req.name))
101
-
102
- if t == "stock_hist":
103
- return nsepython.nse_stock_hist(
104
- req.date_start, req.date_end, req.name
105
- ).to_html()
106
-
107
- return common.wrap(f"<h3>No handler for stock req_type: {t}</h3>")
108
-
109
- except Exception as e:
110
- return {"error": str(e)}
111
-
112
- # ---------- INDEX Handlers ----------
113
  def handle_index(req: FetchRequest):
114
- try:
115
- if req.req_type not in INDEX_REQ:
116
- raise HTTPException(status_code=400, detail=f"Invalid index req_type: {req.req_type}")
117
-
118
- t = req.req_type.lower()
119
-
120
- if t == "indices":
121
- return indices_html.build_indices_html()
122
-
123
- if t == "nse_open":
124
- return index_live_html.build_index_live_html()
125
-
126
- if t == "nse_preopen":
127
- return preopen_html.build_preopen_html()
128
-
129
- if t == "nse_fno":
130
- return build_nse_fno.nse_fno_html(req.date_end, req.name)
131
-
132
- if t == "nse_fiidii":
133
- return nsepython.nse_fiidii().to_html()
134
-
135
- if t == "nse_events":
136
- return nsepython.nse_events().to_html()
137
-
138
- if t == "nse_future":
139
- return common.wrap(nsepython.nse_future(req.name))
140
-
141
- if t == "nse_highlow":
142
- return nsepython.nse_highlow(req.date_end).to_html()
143
-
144
- if t == "stock_highlow":
145
- return nsepython.stock_highlow(req.date_end).to_html()
146
-
147
- if t == "nse_bhav":
148
- return bhavcopy_html.build_bhavcopy_html(req.date_end)
149
-
150
- if t == "nse_largedeals":
151
- return nsepython.nse_largedeals().to_html()
152
-
153
- if t == "nse_bulkdeals":
154
- return nsepython.nse_bulkdeals().to_html()
155
-
156
- if t == "nse_blockdeals":
157
- return nsepython.nse_blockdeals().to_html()
158
-
159
- if t == "nse_most_active":
160
- return nsepython.nse_most_active().to_html()
161
-
162
- if t == "index_history":
163
- return nsepython.index_history(
164
- "NIFTY", req.date_start, req.date_end
165
- ).to_html()
166
-
167
- if t == "largedeals_historical":
168
- return nsepython.nse_largedeals_historical(
169
- req.date_start, req.date_end
170
- ).to_html()
171
-
172
- if t == "index_pe_pb_div":
173
- return nsepython.index_pe_pb_div(
174
- "NIFTY", req.date_start, req.date_end
175
- ).to_html()
176
-
177
- if t == "index_total_returns":
178
- return nsepython.index_total_returns(
179
- "NIFTY", req.date_start, req.date_end
180
- ).to_html()
181
-
182
- return common.wrap(f"<h3>No handler for index req_type: {t}</h3>")
183
-
184
- except Exception as e:
185
- return {"error": str(e)}
186
-
187
- # ---------- Main POST Endpoint ----------
188
  @app.post("/api/fetch")
189
  def fetch_data(req: FetchRequest):
190
- mode = req.mode.lower()
191
-
192
- if mode == "stock":
193
  return handle_stock(req)
194
-
195
- if mode == "index":
196
  return handle_index(req)
197
 
198
- raise HTTPException(status_code=400, detail=f"Invalid mode: {req.mode}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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=["*"],
 
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"
 
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")