Update main.py
Browse files
main.py
CHANGED
|
@@ -96,127 +96,6 @@ def _classify_market_cap(market_cap_crore: float) -> str:
|
|
| 96 |
return "Small Cap"
|
| 97 |
return "Unknown"
|
| 98 |
|
| 99 |
-
# ─────────────────────────────────────────
|
| 100 |
-
# BULK INDEX FETCHER (NEXT LEVEL SPEED)
|
| 101 |
-
# ─────────────────────────────────────────
|
| 102 |
-
# def _refresh_index_cache(*args):
|
| 103 |
-
# """Fetches Nifty 500 data and ranks by Market Cap"""
|
| 104 |
-
# try:
|
| 105 |
-
# url = "https://www.nseindia.com/api/equity-stockIndices?index=NIFTY%20500"
|
| 106 |
-
# payload = nsefetch(url)
|
| 107 |
-
# data = payload.get("data", [])
|
| 108 |
-
# if not data:
|
| 109 |
-
# return
|
| 110 |
-
|
| 111 |
-
# # Sort by Free Float Market Cap to determine Cap Type
|
| 112 |
-
# sorted_data = sorted(data, key=lambda x: x.get("ffmc", 0), reverse=True)
|
| 113 |
-
|
| 114 |
-
# new_cache = {}
|
| 115 |
-
# for i, row in enumerate(sorted_data):
|
| 116 |
-
# sym = row.get("symbol", "")
|
| 117 |
-
# if not sym: continue
|
| 118 |
-
|
| 119 |
-
# # Classification based on rank
|
| 120 |
-
# rank = i + 1
|
| 121 |
-
# if rank <= 100:
|
| 122 |
-
# cap_type = "Large Cap"
|
| 123 |
-
# elif rank <= 250:
|
| 124 |
-
# cap_type = "Mid Cap"
|
| 125 |
-
# else:
|
| 126 |
-
# cap_type = "Small Cap"
|
| 127 |
-
|
| 128 |
-
# mapped = {
|
| 129 |
-
# "symbol": sym,
|
| 130 |
-
# "currentPrice": round_price(row.get("lastPrice", 0)),
|
| 131 |
-
# "dayHigh": round_price(row.get("dayHigh", 0)),
|
| 132 |
-
# "dayLow": round_price(row.get("dayLow", 0)),
|
| 133 |
-
# "high52w": round_price(row.get("yearHigh", 0)),
|
| 134 |
-
# "low52w": round_price(row.get("yearLow", 0)),
|
| 135 |
-
# "previousClose": round_price(row.get("previousClose", 0)),
|
| 136 |
-
# "change": round_percent(row.get("change", 0)),
|
| 137 |
-
# "changePercent": round_percent(row.get("pChange", 0)),
|
| 138 |
-
# "volume": row.get("totalTradedVolume", 0),
|
| 139 |
-
# "openPrice": round_price(row.get("open", 0)),
|
| 140 |
-
# "history": [],
|
| 141 |
-
# "source": "NSE Nifty 500 Index",
|
| 142 |
-
# "sector": "Other", # Will be patched if known
|
| 143 |
-
# "capType": cap_type,
|
| 144 |
-
# "error": False,
|
| 145 |
-
# "cachedAt": datetime.now().isoformat(),
|
| 146 |
-
# }
|
| 147 |
-
# new_cache[sym] = mapped
|
| 148 |
-
|
| 149 |
-
# index_cache["data"] = new_cache
|
| 150 |
-
# index_cache["updated_at"] = datetime.now()
|
| 151 |
-
# logger.info(f"Refreshed Nifty 500 Index Cache with {len(new_cache)} symbols")
|
| 152 |
-
# except Exception as e:
|
| 153 |
-
# logger.error(f"Failed to refresh index cache: {e}")
|
| 154 |
-
|
| 155 |
-
# ─────────────────────────────────────────
|
| 156 |
-
# FETCH LOGIC (PARALLEL)
|
| 157 |
-
# ─────────────────────────────────────────
|
| 158 |
-
# def _get_single_stock_data(symbol: str) -> Optional[Dict]:
|
| 159 |
-
# """Fallback fetch for a single stock using nsepython"""
|
| 160 |
-
# nse_sym = _nse_symbol(symbol)
|
| 161 |
-
# try:
|
| 162 |
-
# # 1. Quote
|
| 163 |
-
# url = f"https://www.nseindia.com/api/quote-equity?symbol={nse_sym}"
|
| 164 |
-
# data = nsefetch(url)
|
| 165 |
-
# price_info = data.get("priceInfo", {})
|
| 166 |
-
# if not price_info:
|
| 167 |
-
# return None
|
| 168 |
-
|
| 169 |
-
# # 2. Industry Metadata (Cached for 1 day)
|
| 170 |
-
# sector = sector_cache.get(nse_sym, "Other")
|
| 171 |
-
# if sector == "Other":
|
| 172 |
-
# try:
|
| 173 |
-
# meta_url = f"https://www.nseindia.com/api/equity-meta-info?symbol={nse_sym}"
|
| 174 |
-
# meta = nsefetch(meta_url)
|
| 175 |
-
# industry = meta.get("industry", "Other")
|
| 176 |
-
# is_etf = meta.get("isETFSec", False)
|
| 177 |
-
|
| 178 |
-
# if is_etf or industry == "Mutual Fund Scheme":
|
| 179 |
-
# sector = "ETFs & Mutual Funds"
|
| 180 |
-
# else:
|
| 181 |
-
# sector = industry
|
| 182 |
-
|
| 183 |
-
# sector_cache[nse_sym] = sector
|
| 184 |
-
# except: pass
|
| 185 |
-
|
| 186 |
-
# intra = price_info.get("intraDayHighLow", {})
|
| 187 |
-
# week = price_info.get("weekHighLow", {})
|
| 188 |
-
# current = float(price_info.get("lastPrice", 0))
|
| 189 |
-
# prev_close = float(price_info.get("previousClose", 0))
|
| 190 |
-
|
| 191 |
-
# change = current - prev_close
|
| 192 |
-
# change_pct = safe_divide(change, prev_close) * 100.0
|
| 193 |
-
|
| 194 |
-
# result = {
|
| 195 |
-
# "symbol": symbol,
|
| 196 |
-
# "currentPrice": round_price(current),
|
| 197 |
-
# "dayHigh": round_price(intra.get("max", 0)),
|
| 198 |
-
# "dayLow": round_price(intra.get("min", 0)),
|
| 199 |
-
# "high52w": round_price(week.get("max", 0)),
|
| 200 |
-
# "low52w": round_price(week.get("min", 0)),
|
| 201 |
-
# "previousClose": round_price(prev_close),
|
| 202 |
-
# "change": round_percent(change),
|
| 203 |
-
# "changePercent": round_percent(change_pct),
|
| 204 |
-
# "volume": 0,
|
| 205 |
-
# "openPrice": round_price(price_info.get("open", 0)),
|
| 206 |
-
# "history": [],
|
| 207 |
-
# "source": "NSE India (Direct Fallback)",
|
| 208 |
-
# "sector": sector,
|
| 209 |
-
# "capType": "Small Cap", # Fallback for non-index stocks
|
| 210 |
-
# "error": False,
|
| 211 |
-
# "cachedAt": datetime.now().isoformat(),
|
| 212 |
-
# }
|
| 213 |
-
|
| 214 |
-
# price_cache[symbol] = {"data": result, "cached_at": datetime.now()}
|
| 215 |
-
# return result
|
| 216 |
-
# except Exception as e:
|
| 217 |
-
# logger.error(f"Error fetching {symbol}: {e}")
|
| 218 |
-
# return None
|
| 219 |
-
|
| 220 |
|
| 221 |
def _get_single_stock_data(symbol: str) -> Dict:
|
| 222 |
try:
|
|
|
|
| 96 |
return "Small Cap"
|
| 97 |
return "Unknown"
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
def _get_single_stock_data(symbol: str) -> Dict:
|
| 101 |
try:
|