Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import common
|
| 3 |
import stock
|
| 4 |
import indices_html
|
|
@@ -33,7 +39,7 @@ STOCK_REQ = [
|
|
| 33 |
|
| 34 |
INDEX_REQ = [
|
| 35 |
"indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
|
| 36 |
-
"nse_events", "nse_future", "nse_bhav", "nse_highlow","stock_highlow",
|
| 37 |
"index_history", "nse_largedeals", "nse_most_active",
|
| 38 |
"largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
|
| 39 |
"index_pe_pb_div", "index_total_returns"
|
|
@@ -59,19 +65,47 @@ def handle_stock(req: FetchRequest):
|
|
| 59 |
raise HTTPException(status_code=400, detail=f"Invalid stock req_type: {req.req_type}")
|
| 60 |
|
| 61 |
t = req.req_type.lower()
|
| 62 |
-
|
| 63 |
-
if t == "
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if t == "
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if t == "
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
if t == "
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
return common.wrap(f"<h3>No handler for stock req_type: {t}</h3>")
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
return {"error": str(e)}
|
| 77 |
|
|
@@ -82,25 +116,71 @@ def handle_index(req: FetchRequest):
|
|
| 82 |
raise HTTPException(status_code=400, detail=f"Invalid index req_type: {req.req_type}")
|
| 83 |
|
| 84 |
t = req.req_type.lower()
|
| 85 |
-
|
| 86 |
-
if t == "
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
if t == "
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
if t == "
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
if t == "
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
if t == "
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
if t == "
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
return common.wrap(f"<h3>No handler for index req_type: {t}</h3>")
|
|
|
|
| 104 |
except Exception as e:
|
| 105 |
return {"error": str(e)}
|
| 106 |
|
|
@@ -108,9 +188,11 @@ def handle_index(req: FetchRequest):
|
|
| 108 |
@app.post("/api/fetch")
|
| 109 |
def fetch_data(req: FetchRequest):
|
| 110 |
mode = req.mode.lower()
|
|
|
|
| 111 |
if mode == "stock":
|
| 112 |
return handle_stock(req)
|
| 113 |
-
|
|
|
|
| 114 |
return handle_index(req)
|
| 115 |
-
|
| 116 |
-
|
|
|
|
| 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
|
|
|
|
| 39 |
|
| 40 |
INDEX_REQ = [
|
| 41 |
"indices", "nse_open", "nse_preopen", "nse_fno", "nse_fiidii",
|
| 42 |
+
"nse_events", "nse_future", "nse_bhav", "nse_highlow", "stock_highlow",
|
| 43 |
"index_history", "nse_largedeals", "nse_most_active",
|
| 44 |
"largedeals_historical", "nse_bulkdeals", "nse_blockdeals",
|
| 45 |
"index_pe_pb_div", "index_total_returns"
|
|
|
|
| 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 |
|
|
|
|
| 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 |
|
|
|
|
| 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}")
|