feat(mls): MLS 리그 추가 — ESPN 비공식 API 연동

- 일정·결과·컨퍼런스 순위·프리뷰(폼/시즌전적/맞대결/머니라인) 수집
- 라이브 카드: 스코어·경기시간·득점/카드·선발 라인업(피치 뷰)·교체 현황
- 문자중계: ESPN commentary 규칙 기반 한글 변환 (미지원 템플릿은 팀명만 한글화)
- MLS 레코드는 야구와 키 구조가 같아 sync_baseball_schedule/settle 재사용
- AI 예측 데이터 블록에 MLS 전용 컨텍스트 추가

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
JinwooKimandClaude Opus 5 committed 2026-08-14 13:47:13 +09:00
1 parent ad5bbec82e
commit 74de57fc1c
22 files changed
+1723 -83

No files matched your search

+27 -10
View File
@@ -30,7 +30,7 @@ from .database import SessionLocal, init_db
from .domain import compute_phase, ensure_aware, now_utc
from .models import AIPrediction, Match, UserPrediction
from .scoring import load_scoring_data
from .services import baseball_data, baseball_details, football_data
from .services import baseball_data, baseball_details, football_data, mls_espn
from .services.ai import MatchContext, PROVIDERS, ProviderUnavailable
from .services.baseball_fetch import fetch_baseball_results, fetch_baseball_schedule
from .services.baseball_sync import match_seq, sync_baseball_schedule
@@ -62,12 +62,19 @@ async def sync_schedule_job() -> None:
async def sync_baseball_job() -> None:
"""야구(kbo/mlb) 일정 동기화 + 프리뷰·순위 캐시 갱신."""
"""야구(kbo/mlb)+MLS 일정 동기화 + 프리뷰·순위 캐시 갱신.
MLS 도 (리그, KST 날짜, 팀쌍, 차수) 키 구조가 동일해 야구 sync 를 그대로 탄다.
"""
inserted_any = False
for league in settings.league_list:
if league not in ("kbo", "mlb"):
if league not in ("kbo", "mlb", "mls"):
continue
records = await fetch_baseball_schedule(league)
records = (
await mls_espn.fetch_mls_schedule()
if league == "mls"
else await fetch_baseball_schedule(league)
)
if not records:
continue
async with SessionLocal() as db:
@@ -80,9 +87,12 @@ async def sync_baseball_job() -> None:
)
).scalars().all()
try:
await baseball_details.refresh_baseball_details(db, league, rows)
if league == "mls":
await mls_espn.refresh_mls_details(db, rows)
else:
await baseball_details.refresh_baseball_details(db, league, rows)
except Exception as e: # noqa: BLE001
log.warning("baseball details(%s) 갱신 실패: %s", league, e)
log.warning("details(%s) 갱신 실패: %s", league, e)
inserted_any = inserted_any or bool(result.get("inserted"))
await tick_status()
if inserted_any:
@@ -139,9 +149,12 @@ async def generate_ai_predictions(only_missing: bool = True) -> None:
]
for m in matches:
# 실데이터 블록 — 리그별 소스(축구=API-Football 캐시, 야구=자체DB+프리뷰).
# 실데이터 블록 — 리그별 소스(축구=API-Football 캐시, 야구=자체DB+프리뷰,
# MLS=자체DB+ESPN 프리뷰).
if m.league in ("kbo", "mlb"):
data_block = await baseball_data.build_baseball_data_block(db, m)
elif m.league == "mls":
data_block = await mls_espn.build_mls_data_block(db, m)
else:
data_block = await football_data.build_data_block(db, m)
ctx = MatchContext(
@@ -313,12 +326,12 @@ async def settle_matches() -> None:
await maybe_send_result_emails()
# ── 2.6) 야구 결과 자동 정산 — (리그, KST 날짜, 팀쌍) 키 매칭 ──
# ── 2.6) 야구·MLS 결과 자동 정산 — (리그, KST 날짜, 팀쌍) 키 매칭 ──
async def settle_baseball() -> None:
_KST = timedelta(hours=9)
now = now_utc()
for league in settings.league_list:
if league not in ("kbo", "mlb"):
if league not in ("kbo", "mlb", "mls"):
continue
async with SessionLocal() as db:
pending = (
@@ -333,7 +346,11 @@ async def settle_baseball() -> None:
due = [m.match_id for m in pending if ensure_aware(m.kickoff_at) <= now]
if not due:
continue
results = await fetch_baseball_results(league)
results = (
await mls_espn.fetch_mls_results()
if league == "mls"
else await fetch_baseball_results(league)
)
by_key: dict[tuple, tuple[int, int]] = {}
for r in results:
d, a2, b2, s = r["dateKst"], r["teamA"], r["teamB"], r.get("seq", 1)