feat(worker): 경기 결과 자동 정산 + '맞춘 사람만' 결과 메일
- 킥오프+result_settle_hours 경과 경기를 외부 스코어로 자동 채점(settle_matches) - schedule_fetch.fetch_results: openfootball/football-data 확정 스코어 수집 - 결과 메일을 승패 적중자에게만 발송하는 옵션(result_email_correct_only) - 스케줄러에 settle 잡 추가 + 기동 시 1회 밀린 경기 정산 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
464145a234
commit
4244518ffd
3 files changed
+156
-14
No files matched your search
@@ -110,3 +110,85 @@ async def fetch_schedule() -> list[dict]:
|
||||
except Exception as e: # noqa: BLE001
|
||||
log.error("schedule fetch 실패(%s): %s — 기존 일정 유지", src, e)
|
||||
return []
|
||||
|
||||
|
||||
# ── 경기 결과(스코어) 수집 — 자동 정산용 ─────────────────────
|
||||
def _extract_ft(m: dict) -> tuple[int, int] | None:
|
||||
"""openfootball 매치에서 정규시간 스코어 추출(다양한 표기 포괄)."""
|
||||
sc = m.get("score")
|
||||
if isinstance(sc, dict):
|
||||
ft = sc.get("ft")
|
||||
if isinstance(ft, (list, tuple)) and len(ft) == 2 and ft[0] is not None:
|
||||
return int(ft[0]), int(ft[1])
|
||||
if m.get("score1") is not None and m.get("score2") is not None:
|
||||
return int(m["score1"]), int(m["score2"])
|
||||
return None
|
||||
|
||||
|
||||
async def _results_openfootball() -> list[dict]:
|
||||
import httpx
|
||||
|
||||
async with httpx.AsyncClient(timeout=20) as c:
|
||||
r = await c.get(settings.schedule_url)
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
|
||||
out: list[dict] = []
|
||||
for m in data.get("matches", []):
|
||||
if (m.get("group") or "").strip() != settings.schedule_group:
|
||||
continue
|
||||
a, b = code_for(m.get("team1", "")), code_for(m.get("team2", ""))
|
||||
if not a or not b:
|
||||
continue
|
||||
ft = _extract_ft(m)
|
||||
if ft is None: # 아직 미진행/미집계
|
||||
continue
|
||||
out.append({"teamA": a, "teamB": b, "scoreA": ft[0], "scoreB": ft[1]})
|
||||
return out
|
||||
|
||||
|
||||
async def _results_football_data() -> list[dict]:
|
||||
if not settings.football_data_token:
|
||||
return []
|
||||
import httpx
|
||||
|
||||
url = (
|
||||
f"https://api.football-data.org/v4/competitions/"
|
||||
f"{settings.football_data_competition}/matches"
|
||||
)
|
||||
async with httpx.AsyncClient(timeout=20) as c:
|
||||
r = await c.get(url, headers={"X-Auth-Token": settings.football_data_token})
|
||||
r.raise_for_status()
|
||||
data = r.json()
|
||||
|
||||
out: list[dict] = []
|
||||
for m in data.get("matches", []):
|
||||
if m.get("status") != "FINISHED":
|
||||
continue
|
||||
a = code_for((m.get("homeTeam") or {}).get("name", "") or "")
|
||||
b = code_for((m.get("awayTeam") or {}).get("name", "") or "")
|
||||
if not a or not b:
|
||||
continue
|
||||
ft = ((m.get("score") or {}).get("fullTime") or {})
|
||||
if ft.get("home") is None or ft.get("away") is None:
|
||||
continue
|
||||
out.append({"teamA": a, "teamB": b, "scoreA": int(ft["home"]), "scoreB": int(ft["away"])})
|
||||
return out
|
||||
|
||||
|
||||
async def fetch_results() -> list[dict]:
|
||||
"""확정된 경기 스코어 수집 → [{teamA, teamB, scoreA, scoreB}]. 실패 시 빈 리스트."""
|
||||
src = settings.schedule_source.lower()
|
||||
try:
|
||||
if src == "openfootball":
|
||||
out = await _results_openfootball()
|
||||
elif src == "football-data":
|
||||
out = await _results_football_data()
|
||||
else:
|
||||
return []
|
||||
if out:
|
||||
log.info("results: %s 에서 %d경기 스코어 수집", src, len(out))
|
||||
return out
|
||||
except Exception as e: # noqa: BLE001
|
||||
log.error("results fetch 실패(%s): %s", src, e)
|
||||
return []
|
||||
Reference in new issue
Block a user