eshan6704 commited on
Commit
d609811
·
verified ·
1 Parent(s): 0ab582c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -40
app.py CHANGED
@@ -1,7 +1,6 @@
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
-
5
  import os
6
  import importlib
7
 
@@ -17,7 +16,7 @@ for file in os.listdir(current_dir):
17
  except Exception as e:
18
  print(f"Failed to import {module_name}: {e}")
19
 
20
-
21
  app = FastAPI(title="Stock / Index Backend")
22
 
23
  # ---------- CORS ----------
@@ -57,49 +56,55 @@ def health():
57
 
58
  # ---------- STOCK Handlers ----------
59
  def handle_stock(req: FetchRequest):
60
- if req.req_type not in STOCK_REQ:
61
- raise HTTPException(status_code=400, detail=f"Invalid stock req_type: {req.req_type}")
 
62
 
63
- t = req.req_type.lower()
64
- if t == "info": return wrap(yahooinfo.fetch_info(req.name))
65
- if t == "intraday": return wrap(stock.fetch_intraday(req.name, req.date_start, req.date_end))
66
- if t == "daily": return wrap(stock.fetch_daily(req.name, req.date_start, req.date_end))
67
- if t == "nse_eq": return eq_html.build_eq_html(req.name)
68
- if t == "qresult": return wrap(stock.fetch_qresult(req.name))
69
- if t == "result": return wrap(stock.fetch_result(req.name))
70
- if t == "balance": return wrap(stock.fetch_balance(req.name))
71
- if t == "cashflow": return wrap(stock.fetch_cashflow(req.name))
72
- if t == "dividend": return wrap(stock.fetch_dividend(req.name))
73
- if t == "split": return wrap(stock.fetch_split(req.name))
74
- if t == "other": return wrap(stock.fetch_other(req.name))
75
- if t == "stock_hist": return nsepython.nse_stock_hist(req.date_start, req.date_end, req.name).to_html()
76
- return wrap(f"<h3>No handler for stock req_type: {t}</h3>")
 
 
77
 
78
  # ---------- INDEX Handlers ----------
79
  def handle_index(req: FetchRequest):
80
- if req.req_type not in INDEX_REQ:
81
- raise HTTPException(status_code=400, detail=f"Invalid index req_type: {req.req_type}")
 
82
 
83
- t = req.req_type.lower()
84
- if t == "indices": return indices_html.build_indices_html()
85
- if t == "nse_open": return index_live_html.build_index_live_html()
86
- if t == "nse_preopen": return preopen_html.build_preopen_html()
87
- if t == "nse_fno": return build_nse_fno.nse_fno_html(req.date_end, req.name)
88
- if t == "nse_fiidii": return nsepython.nse_fiidii().to_html()
89
- if t == "nse_events": return nsepython.nse_events().to_html()
90
- if t == "nse_future": return wrap(nsepython.nse_future(req.name))
91
- if t == "nse_highlow": return nsepython.nse_highlow(req.date_end).to_html()
92
- if t == "stock_highlow": return nsepython.stock_highlow(req.date_end).to_html()
93
- if t == "nse_bhav": return bhavcopy_html.build_bhavcopy_html(req.date_end)
94
- if t == "nse_largedeals": return nsepython.nse_largedeals().to_html()
95
- if t == "nse_bulkdeals": return nsepython.nse_bulkdeals().to_html()
96
- if t == "nse_blockdeals": return nsepython.nse_blockdeals().to_html()
97
- if t == "nse_most_active": return nsepython.nse_most_active().to_html()
98
- if t == "index_history": return nsepython.index_history("NIFTY", req.date_start, req.date_end).to_html()
99
- if t == "largedeals_historical": return nsepython.nse_largedeals_historical(req.date_start, req.date_end).to_html()
100
- if t == "index_pe_pb_div": return nsepython.index_pe_pb_div("NIFTY", req.date_start, req.date_end).to_html()
101
- if t == "index_total_returns": return nsepython.index_total_returns("NIFTY", req.date_start, req.date_end).to_html()
102
- return wrap(f"<h3>No handler for index req_type: {t}</h3>")
 
 
103
 
104
  # ---------- Main POST Endpoint ----------
105
  @app.post("/api/fetch")
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
 
4
  import os
5
  import importlib
6
 
 
16
  except Exception as e:
17
  print(f"Failed to import {module_name}: {e}")
18
 
19
+ # ---------- FastAPI app ----------
20
  app = FastAPI(title="Stock / Index Backend")
21
 
22
  # ---------- CORS ----------
 
56
 
57
  # ---------- STOCK Handlers ----------
58
  def handle_stock(req: FetchRequest):
59
+ try:
60
+ if req.req_type not in STOCK_REQ:
61
+ raise HTTPException(status_code=400, detail=f"Invalid stock req_type: {req.req_type}")
62
 
63
+ t = req.req_type.lower()
64
+ if t == "info": return common.wrap(yahooinfo.fetch_info(req.name))
65
+ if t == "intraday": return common.wrap(stock.fetch_intraday(req.name, req.date_start, req.date_end))
66
+ if t == "daily": return common.wrap(stock.fetch_daily(req.name, req.date_start, req.date_end))
67
+ if t == "nse_eq": return eq_html.build_eq_html(req.name)
68
+ if t == "qresult": return common.wrap(stock.fetch_qresult(req.name))
69
+ if t == "result": return common.wrap(stock.fetch_result(req.name))
70
+ if t == "balance": return common.wrap(stock.fetch_balance(req.name))
71
+ if t == "cashflow": return common.wrap(stock.fetch_cashflow(req.name))
72
+ if t == "dividend": return common.wrap(stock.fetch_dividend(req.name))
73
+ if t == "split": return common.wrap(stock.fetch_split(req.name))
74
+ if t == "other": return common.wrap(stock.fetch_other(req.name))
75
+ if t == "stock_hist": return nsepython.nse_stock_hist(req.date_start, req.date_end, req.name).to_html()
76
+ return common.wrap(f"<h3>No handler for stock req_type: {t}</h3>")
77
+ except Exception as e:
78
+ return {"error": str(e)}
79
 
80
  # ---------- INDEX Handlers ----------
81
  def handle_index(req: FetchRequest):
82
+ try:
83
+ if req.req_type not in INDEX_REQ:
84
+ raise HTTPException(status_code=400, detail=f"Invalid index req_type: {req.req_type}")
85
 
86
+ t = req.req_type.lower()
87
+ if t == "indices": return indices_html.build_indices_html()
88
+ if t == "nse_open": return index_live_html.build_index_live_html()
89
+ if t == "nse_preopen": return preopen_html.build_preopen_html()
90
+ if t == "nse_fno": return build_nse_fno.nse_fno_html(req.date_end, req.name)
91
+ if t == "nse_fiidii": return nsepython.nse_fiidii().to_html()
92
+ if t == "nse_events": return nsepython.nse_events().to_html()
93
+ if t == "nse_future": return common.wrap(nsepython.nse_future(req.name))
94
+ if t == "nse_highlow": return nsepython.nse_highlow(req.date_end).to_html()
95
+ if t == "stock_highlow": return nsepython.stock_highlow(req.date_end).to_html()
96
+ if t == "nse_bhav": return bhavcopy_html.build_bhavcopy_html(req.date_end)
97
+ if t == "nse_largedeals": return nsepython.nse_largedeals().to_html()
98
+ if t == "nse_bulkdeals": return nsepython.nse_bulkdeals().to_html()
99
+ if t == "nse_blockdeals": return nsepython.nse_blockdeals().to_html()
100
+ if t == "nse_most_active": return nsepython.nse_most_active().to_html()
101
+ if t == "index_history": return nsepython.index_history("NIFTY", req.date_start, req.date_end).to_html()
102
+ if t == "largedeals_historical": return nsepython.nse_largedeals_historical(req.date_start, req.date_end).to_html()
103
+ if t == "index_pe_pb_div": return nsepython.index_pe_pb_div("NIFTY", req.date_start, req.date_end).to_html()
104
+ if t == "index_total_returns": return nsepython.index_total_returns("NIFTY", req.date_start, req.date_end).to_html()
105
+ return common.wrap(f"<h3>No handler for index req_type: {t}</h3>")
106
+ except Exception as e:
107
+ return {"error": str(e)}
108
 
109
  # ---------- Main POST Endpoint ----------
110
  @app.post("/api/fetch")