fix: improve XAU price fallback with multiple symbol attempts
This commit is contained in:
+36
-16
@@ -43,25 +43,30 @@ class XAUBriefAgent:
|
|||||||
return self._get_gold_fallback()
|
return self._get_gold_fallback()
|
||||||
|
|
||||||
def _get_gold_fallback(self):
|
def _get_gold_fallback(self):
|
||||||
"""Fallback: dados da Binance (XAU/USDT)."""
|
"""Fallback: dados da Binance via par alternativo (GOLD/USDT)."""
|
||||||
result = {}
|
result = {}
|
||||||
try:
|
# Tenta XAU/BRL ou XAU/USDT na Binance
|
||||||
r = requests.get(
|
symbols = ["XAUUSDT", "GLDUSDT", "PAXGUSDT"]
|
||||||
"https://api.binance.com/api/v3/ticker/24hr?symbol=XAUUSDT",
|
for sym in symbols:
|
||||||
timeout=8
|
try:
|
||||||
)
|
r = requests.get(
|
||||||
if r.status_code == 200:
|
f"https://api.binance.com/api/v3/ticker/24hr?symbol={sym}",
|
||||||
data = r.json()
|
timeout=8
|
||||||
result["xau_usd"] = float(data.get("lastPrice", 0))
|
)
|
||||||
result["chp"] = float(data.get("priceChangePercent", 0))
|
if r.status_code == 200:
|
||||||
result["high"] = float(data.get("highPrice", 0))
|
data = r.json()
|
||||||
result["low"] = float(data.get("lowPrice", 0))
|
result["xau_usd"] = float(data.get("lastPrice", 0))
|
||||||
result["volume"] = float(data.get("quoteVolume", 0))
|
result["chp"] = float(data.get("priceChangePercent", 0))
|
||||||
except:
|
result["high"] = float(data.get("highPrice", 0))
|
||||||
pass
|
result["low"] = float(data.get("lowPrice", 0))
|
||||||
|
result["volume"] = float(data.get("quoteVolume", 0))
|
||||||
|
if result["xau_usd"] > 100: # preço válido de ouro
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
# Segundo fallback: exchangerate
|
# Segundo fallback: exchangerate
|
||||||
if not result.get("xau_usd"):
|
if not result.get("xau_usd") or result["xau_usd"] < 100:
|
||||||
try:
|
try:
|
||||||
r = requests.get(
|
r = requests.get(
|
||||||
"https://api.exchangerate-api.com/v4/latest/XAU",
|
"https://api.exchangerate-api.com/v4/latest/XAU",
|
||||||
@@ -74,6 +79,21 @@ class XAUBriefAgent:
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# Terceiro fallback: Metal-price.org API pública
|
||||||
|
if not result.get("xau_usd") or result["xau_usd"] < 100:
|
||||||
|
try:
|
||||||
|
r = requests.get(
|
||||||
|
"https://api.metals.live/v1/spot/gold",
|
||||||
|
timeout=8
|
||||||
|
)
|
||||||
|
if r.status_code == 200:
|
||||||
|
data = r.json()
|
||||||
|
if isinstance(data, list) and len(data) > 0:
|
||||||
|
result["xau_usd"] = data[0].get("gold", 0)
|
||||||
|
result["chp"] = 0
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# ── Treasury Yields (US 10Y) ───────────────────────────────────────────────
|
# ── Treasury Yields (US 10Y) ───────────────────────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user