Really-amin commited on
Commit
d5abb64
·
verified ·
1 Parent(s): fa4147c

fix: gainers/losers binance fallback chain

Browse files
backend/routers/expanded_market_api.py CHANGED
@@ -118,6 +118,47 @@ async def _fetch_markets_from_coincap(limit: int) -> List[dict]:
118
  return rows
119
 
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  async def _fetch_markets_from_coinpaprika(limit: int) -> List[dict]:
122
  tickers = await fetch_from_coinpaprika("tickers")
123
  if not tickers:
@@ -174,6 +215,10 @@ async def fetch_market_movers(limit: int) -> tuple[List[dict], str]:
174
  logger.warning("CoinCap movers failed, trying CoinPaprika...")
175
  rows = await _fetch_markets_from_coinpaprika(limit)
176
  source = "coinpaprika"
 
 
 
 
177
 
178
  if not rows:
179
  raise HTTPException(status_code=503, detail="All market mover providers unavailable")
 
118
  return rows
119
 
120
 
121
+ async def _fetch_markets_from_binance(limit: int) -> List[dict]:
122
+ """Binance 24h ticker — reliable fallback when aggregators rate-limit."""
123
+ try:
124
+ async with httpx.AsyncClient(timeout=12.0) as client:
125
+ response = await client.get("https://api.binance.com/api/v3/ticker/24hr")
126
+ response.raise_for_status()
127
+ tickers = response.json()
128
+ except Exception as e:
129
+ logger.error(f"Binance movers error: {e}")
130
+ return []
131
+
132
+ rows: List[dict] = []
133
+ for item in tickers:
134
+ symbol = str(item.get("symbol", ""))
135
+ if not symbol.endswith("USDT"):
136
+ continue
137
+ try:
138
+ change = float(item.get("priceChangePercent", 0))
139
+ price = float(item.get("lastPrice", 0))
140
+ volume = float(item.get("quoteVolume", 0))
141
+ except (TypeError, ValueError):
142
+ continue
143
+ if price <= 0:
144
+ continue
145
+ base = symbol[:-4]
146
+ rows.append(
147
+ _normalize_market_row(
148
+ {
149
+ "id": base.lower(),
150
+ "symbol": base,
151
+ "name": base,
152
+ "priceUsd": price,
153
+ "changePercent24Hr": change,
154
+ "volumeUsd24Hr": volume,
155
+ },
156
+ "binance",
157
+ )
158
+ )
159
+ return rows[: max(limit * 3, 100)]
160
+
161
+
162
  async def _fetch_markets_from_coinpaprika(limit: int) -> List[dict]:
163
  tickers = await fetch_from_coinpaprika("tickers")
164
  if not tickers:
 
215
  logger.warning("CoinCap movers failed, trying CoinPaprika...")
216
  rows = await _fetch_markets_from_coinpaprika(limit)
217
  source = "coinpaprika"
218
+ if not rows:
219
+ logger.warning("CoinPaprika movers failed, trying Binance...")
220
+ rows = await _fetch_markets_from_binance(limit)
221
+ source = "binance"
222
 
223
  if not rows:
224
  raise HTTPException(status_code=503, detail="All market mover providers unavailable")