05/31 Fix yfinance issue

This commit is contained in:
2026-05-31 21:57:44 -04:00
parent 04dc35388a
commit 9bc8bb3238
3 changed files with 115 additions and 57 deletions
+20 -11
View File
@@ -77,17 +77,26 @@ def force_refresh():
# ── Fetchers ──────────────────────────────────────────────────────────────────
def _fetch_yfinance():
"""Fetch USDVND=X via yfinance. Most reliable — uses Yahoo Finance forex."""
try:
import yfinance as yf
ticker = yf.Ticker('USDVND=X')
hist = ticker.history(period='5d')
if not hist.empty:
rate = float(hist['Close'].iloc[-1])
log.info(f'[fx] yfinance: 1 USD = {rate:,.0f} VND')
return rate
except Exception as e:
log.warning(f'[fx] yfinance failed: {e}')
"""Fetch USDVND=X via Yahoo Finance v8 chart API (direct HTTP, no yfinance needed)."""
for subdomain in ('query1', 'query2'):
url = (
f'https://{subdomain}.finance.yahoo.com/v8/finance/chart/USDVND=X'
'?range=5d&interval=1d&includePrePost=false'
)
try:
resp = requests.get(url, timeout=REQUEST_TIMEOUT,
headers={'User-Agent': 'Mozilla/5.0', 'Accept': 'application/json'})
if resp.status_code == 200:
result = resp.json().get('chart', {}).get('result')
if result:
closes = result[0]['indicators']['quote'][0]['close']
valid = [c for c in closes if c is not None]
if valid:
rate = float(valid[-1])
log.info(f'[fx] Yahoo v8: 1 USD = {rate:,.0f} VND')
return rate
except Exception as e:
log.warning(f'[fx] Yahoo v8 {subdomain}: {e}')
return None