FastAPI app.on_event -> lifespan 변경
This commit is contained in:
@@ -7,7 +7,7 @@ from typing import Dict, Optional
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import update
|
||||
|
||||
# 프로젝트 구조에 따라 .models 또는 models에서 Asset을 가져옵니다.
|
||||
# [중요] 순환 참조 방지를 위해 상단에서는 Asset만 가져옵니다.
|
||||
try:
|
||||
from .models import Asset
|
||||
except ImportError:
|
||||
@@ -15,7 +15,7 @@ except ImportError:
|
||||
|
||||
class DataFetcher:
|
||||
def __init__(self):
|
||||
# 인베스팅닷컴은 헤더가 없으면 403 에러를 뱉습니다. 브라우저와 동일하게 설정합니다.
|
||||
# 인베스팅닷컴 등 외부 사이트 차단 방지용 헤더
|
||||
self.headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
|
||||
@@ -26,18 +26,15 @@ class DataFetcher:
|
||||
self.daily_closing_prices = {}
|
||||
|
||||
async def fetch_google_finance(self, client: httpx.AsyncClient, asset_code: str) -> Optional[float]:
|
||||
"""인베스팅보다 빠르고 야후보다 안정적인 구글 파이낸스 우회"""
|
||||
"""구글 파이낸스 환율/지수 수집"""
|
||||
try:
|
||||
# 구글 파이낸스 환율/지수 URL
|
||||
symbol = "USD-KRW" if asset_code == "USD/KRW" else "INDEXDXY:CURRENCY" if asset_code == "USD/DXY" else None
|
||||
if not symbol: return None
|
||||
|
||||
url = f"https://www.google.com/finance/quote/{symbol}"
|
||||
# 구글은 헤더만 있으면 응답 속도가 정말 빠릅니다.
|
||||
res = await client.get(url, timeout=5)
|
||||
if res.status_code != 200: return None
|
||||
|
||||
# 구글 특유의 가격 클래스 추출 (정규식)
|
||||
m = re.search(r'data-last-price="([\d,.]+)"', res.text)
|
||||
if m:
|
||||
return float(m.group(1).replace(",", ""))
|
||||
@@ -46,38 +43,30 @@ class DataFetcher:
|
||||
return None
|
||||
|
||||
async def fetch_investing_com(self, client: httpx.AsyncClient, asset_code: str) -> Optional[float]:
|
||||
"""인베스팅닷컴 비동기 수집 (USD/KRW 포함 전용)"""
|
||||
"""인베스팅닷컴 비동기 수집 (기존 패턴들 모두 포함)"""
|
||||
try:
|
||||
# 자산별 URL 매핑
|
||||
if asset_code == "USD/DXY":
|
||||
url = "https://www.investing.com/indices/usdollar"
|
||||
# elif asset_code == "USD/KRW":
|
||||
# url = "https://kr.investing.com/currencies/usd-krw"
|
||||
else:
|
||||
url = f"https://www.investing.com/currencies/{asset_code.lower().replace('/', '-')}"
|
||||
|
||||
# 인베스팅은 쿠키와 리다이렉트가 중요하므로 follow_redirects 사용
|
||||
response = await client.get(url, timeout=15, follow_redirects=True)
|
||||
|
||||
if response.status_code != 200:
|
||||
print(f"⚠️ Investing 응답 에러 ({asset_code}): {response.status_code}")
|
||||
return None
|
||||
|
||||
html = response.text
|
||||
|
||||
# 인베스팅닷컴의 다양한 HTML 구조에 대응하는 정규식 (우선순위 순)
|
||||
patterns = [
|
||||
r'data-test="instrument-price-last">([\d,.]+)<', # 최신 메인 패턴
|
||||
r'last_last">([\d,.]+)<', # 구형/세부 페이지 패턴
|
||||
r'instrument-price-last">([\d,.]+)<', # 클래식 패턴
|
||||
r'class="[^"]*text-2xl[^"]*">([\d,.]+)<' # 비상용 패턴
|
||||
r'data-test="instrument-price-last">([\d,.]+)<',
|
||||
r'last_last">([\d,.]+)<',
|
||||
r'instrument-price-last">([\d,.]+)<',
|
||||
r'class="[^"]*text-2xl[^"]*">([\d,.]+)<'
|
||||
]
|
||||
|
||||
for pattern in patterns:
|
||||
p = re.search(pattern, html)
|
||||
if p:
|
||||
val_str = p.group(1).replace(',', '')
|
||||
return float(val_str)
|
||||
return float(p.group(1).replace(',', ''))
|
||||
|
||||
print(f"⚠️ Investing 패턴 매칭 실패 ({asset_code})")
|
||||
except Exception as e:
|
||||
@@ -85,7 +74,7 @@ class DataFetcher:
|
||||
return None
|
||||
|
||||
async def fetch_binance(self, client: httpx.AsyncClient) -> Optional[float]:
|
||||
"""바이낸스 BTC/USDT"""
|
||||
"""바이낸스 BTC/USD"""
|
||||
try:
|
||||
url = "https://api.binance.com/api/v3/ticker/price?symbol=BTCUSD"
|
||||
res = await client.get(url, timeout=5)
|
||||
@@ -111,7 +100,7 @@ class DataFetcher:
|
||||
except: return None
|
||||
|
||||
async def update_closing_prices(self, db: Session):
|
||||
"""매일 아침 기준가를 스냅샷 찍어 메모리에 저장"""
|
||||
"""매일 정해진 시간에 기준가 스냅샷 업데이트"""
|
||||
results = await self.update_realtime_prices(db)
|
||||
for symbol, price in results.items():
|
||||
if price:
|
||||
@@ -119,11 +108,11 @@ class DataFetcher:
|
||||
print(f"📌 [기준가 업데이트] 완료: {self.daily_closing_prices}")
|
||||
|
||||
async def update_realtime_prices(self, db: Session) -> Dict:
|
||||
"""[핵심] 비동기 수집 후 DB 즉시 업데이트"""
|
||||
"""[핵심] 비동기 수집 후 DB 즉시 업데이트 및 특수 로직 처리"""
|
||||
start_time = time.time()
|
||||
|
||||
async with httpx.AsyncClient(headers=self.headers, follow_redirects=True) as client:
|
||||
# 1. 병렬 수집 태스크 정의 (USD/KRW도 인베스팅닷컴 함수 사용)
|
||||
# 1. 병렬 수집
|
||||
tasks = {
|
||||
"XAU/USD": self.fetch_investing_com(client, "XAU/USD"),
|
||||
"XAU/CNY": self.fetch_investing_com(client, "XAU/CNY"),
|
||||
@@ -138,63 +127,55 @@ class DataFetcher:
|
||||
keys = list(tasks.keys())
|
||||
values = await asyncio.gather(*tasks.values(), return_exceptions=True)
|
||||
|
||||
# 결과 가공
|
||||
raw_results = {}
|
||||
for i, val in enumerate(values):
|
||||
symbol = keys[i]
|
||||
if isinstance(val, (int, float)):
|
||||
raw_results[symbol] = val
|
||||
else:
|
||||
raw_results[symbol] = None
|
||||
print(f"❌ {symbol} 수집 실패: {val}")
|
||||
raw_results = {keys[i]: (v if isinstance(v, (int, float)) else None) for i, v in enumerate(values)}
|
||||
|
||||
# [수정 구간] 2. XAU/KRW 계산 및 프리미엄용 전일종가 매핑
|
||||
# 2. XAU/KRW 실시간 계산 (국제 금 시세 * 환율)
|
||||
if raw_results.get("XAU/USD") and raw_results.get("USD/KRW"):
|
||||
# (현재가) 국제 금 시세 실시간 계산
|
||||
raw_results["XAU/KRW"] = round((raw_results["XAU/USD"] / 31.1034768) * raw_results["USD/KRW"], 0)
|
||||
|
||||
# [추가] XAU/KRW의 '전일종가' 필드에 'KRX/GLD 현재가'를 강제로 주입
|
||||
# 이렇게 하면 화면에서 (국제계산가 - 국내현물가)가 실시간 변동액으로 표시됨
|
||||
if raw_results.get("KRX/GLD"):
|
||||
from app.models import UserAsset, Asset
|
||||
asset_xau = db.query(Asset).filter(Asset.symbol == "XAU/KRW").first()
|
||||
if asset_xau:
|
||||
db.execute(
|
||||
update(UserAsset)
|
||||
.where(UserAsset.asset_id == asset_xau.id)
|
||||
.values(previous_close=raw_results["KRX/GLD"])
|
||||
)
|
||||
|
||||
# 2) [핵심 추가] 메모리에 저장된 기준가도 실시간 KRX 가격으로 갱신!
|
||||
# 그래야 밑에 있는 '3. DB 업데이트 수행' 루프에서 state(up/down)가 실시간으로 계산됩니다.
|
||||
self.daily_closing_prices["XAU/KRW"] = raw_results["KRX/GLD"]
|
||||
else:
|
||||
raw_results["XAU/KRW"] = None
|
||||
|
||||
# 3. DB 업데이트 수행
|
||||
for symbol, price in raw_results.items():
|
||||
if price is not None:
|
||||
state = "stable"
|
||||
if symbol in self.daily_closing_prices:
|
||||
closing = self.daily_closing_prices[symbol]
|
||||
if price > closing: state = "up"
|
||||
elif price < closing: state = "down"
|
||||
try:
|
||||
# [A] 기본 Asset 테이블 업데이트
|
||||
for symbol, price in raw_results.items():
|
||||
if price is not None:
|
||||
state = "stable"
|
||||
if symbol in self.daily_closing_prices:
|
||||
closing = self.daily_closing_prices[symbol]
|
||||
if price > closing: state = "up"
|
||||
elif price < closing: state = "down"
|
||||
|
||||
# SQL 실행
|
||||
db.execute(
|
||||
update(Asset)
|
||||
.where(Asset.symbol == symbol)
|
||||
.values(
|
||||
current_price=price,
|
||||
price_state=state,
|
||||
last_updated=datetime.now()
|
||||
db.execute(
|
||||
update(Asset)
|
||||
.where(Asset.symbol == symbol)
|
||||
.values(current_price=price, price_state=state, last_updated=datetime.now())
|
||||
)
|
||||
)
|
||||
|
||||
db.commit()
|
||||
|
||||
# [B] XAU/KRW 프리미엄용 특수 로직 (KRX 가격을 전일종가로 세팅)
|
||||
if raw_results.get("KRX/GLD"):
|
||||
try:
|
||||
from .models import UserAsset, Asset as ModelAsset # 로컬 임포트 유지
|
||||
except ImportError:
|
||||
from models import UserAsset, Asset as ModelAsset
|
||||
|
||||
target_asset = db.query(ModelAsset).filter(ModelAsset.symbol == "XAU/KRW").first()
|
||||
if target_asset:
|
||||
db.execute(
|
||||
update(UserAsset)
|
||||
.where(UserAsset.asset_id == target_asset.id)
|
||||
.values(previous_close=raw_results["KRX/GLD"])
|
||||
)
|
||||
# 메모리 기준가도 함께 갱신하여 UI 상태 화살표 유지
|
||||
self.daily_closing_prices["XAU/KRW"] = raw_results["KRX/GLD"]
|
||||
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(f"❌ DB 저장 중 오류 발생: {e}")
|
||||
|
||||
print(f"✅ [{datetime.now().strftime('%H:%M:%S')}] 수집 및 DB 저장 완료 ({time.time()-start_time:.2f}s)")
|
||||
return raw_results
|
||||
|
||||
# 싱글톤 인스턴스 생성
|
||||
# 싱글톤 인스턴스
|
||||
fetcher = DataFetcher()
|
||||
Reference in New Issue
Block a user