подгрузка данных для кубка россии. тест 2
This commit is contained in:
@@ -3,6 +3,7 @@ from numpy import place
|
||||
from db import get_connection
|
||||
from repositories.team_repository import get_team_id_by_external_id
|
||||
from repositories.stadium_repository import get_or_create_stadium
|
||||
from parsers.parser_sources import get_default_source_key, list_parser_sources
|
||||
|
||||
|
||||
def ensure_match_source_key_column() -> None:
|
||||
@@ -24,6 +25,121 @@ def ensure_match_source_key_column() -> None:
|
||||
conn.close()
|
||||
|
||||
|
||||
|
||||
|
||||
def _normalize_source_key(value: str | None) -> str:
|
||||
return str(value or "").upper().strip()
|
||||
|
||||
|
||||
def infer_match_source_key(
|
||||
explicit_source_key: str | None = None,
|
||||
season: str | None = None,
|
||||
tour: str | None = None,
|
||||
) -> str:
|
||||
"""Определяет источник матча, даже если старые строки matches.source_key ещё пустые.
|
||||
|
||||
Приоритет:
|
||||
1. matches.source_key;
|
||||
2. точное совпадение season с parser_sources.season;
|
||||
3. кубковые слова в tour;
|
||||
4. обычный дефолт из настроек проекта.
|
||||
"""
|
||||
explicit = _normalize_source_key(explicit_source_key)
|
||||
if explicit:
|
||||
return explicit
|
||||
|
||||
season_value = str(season or "").strip()
|
||||
tour_value = str(tour or "").lower().strip()
|
||||
|
||||
try:
|
||||
sources = list_parser_sources()
|
||||
except Exception:
|
||||
sources = []
|
||||
|
||||
if season_value and sources:
|
||||
matches = [
|
||||
_normalize_source_key(source.get("key"))
|
||||
for source in sources
|
||||
if str(source.get("season") or "").strip() == season_value
|
||||
]
|
||||
matches = [key for key in matches if key]
|
||||
if len(matches) == 1:
|
||||
return matches[0]
|
||||
|
||||
cup_markers = (
|
||||
"кубок",
|
||||
"финал",
|
||||
"полуфинал",
|
||||
"четвертьфинал",
|
||||
"1/2",
|
||||
"1/4",
|
||||
"1/8",
|
||||
"1/16",
|
||||
"групп",
|
||||
"этап",
|
||||
"стад",
|
||||
)
|
||||
if any(marker in tour_value for marker in cup_markers):
|
||||
known_keys = {_normalize_source_key(source.get("key")) for source in sources}
|
||||
if "RUSSIAN_CUP" in known_keys:
|
||||
return "RUSSIAN_CUP"
|
||||
|
||||
try:
|
||||
return _normalize_source_key(get_default_source_key()) or "SUPERLEAGUE"
|
||||
except Exception:
|
||||
return "SUPERLEAGUE"
|
||||
|
||||
|
||||
def resolve_match_source_key(match_id: int | str | None, fallback: str | None = None) -> str:
|
||||
"""Возвращает source_key для матча и, если возможно, записывает его в matches.
|
||||
|
||||
Это нужно для старых матчей Кубка России, которые могли быть загружены до появления
|
||||
колонки source_key. Без этого vMix скачивает пресет и собирает пути как для Суперлиги.
|
||||
"""
|
||||
fallback_key = _normalize_source_key(fallback)
|
||||
if not match_id:
|
||||
return fallback_key or infer_match_source_key(fallback_key)
|
||||
|
||||
conn = get_connection()
|
||||
try:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT source_key, season, tour
|
||||
FROM matches
|
||||
WHERE id = %s
|
||||
LIMIT 1;
|
||||
""",
|
||||
(match_id,),
|
||||
)
|
||||
row = cur.fetchone()
|
||||
|
||||
if not row:
|
||||
return fallback_key or infer_match_source_key(fallback_key)
|
||||
|
||||
explicit_key, season, tour = row
|
||||
source_key = infer_match_source_key(explicit_key or fallback_key, season, tour)
|
||||
|
||||
if source_key and not _normalize_source_key(explicit_key):
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE matches
|
||||
SET source_key = %s, updated_at = NOW()
|
||||
WHERE id = %s
|
||||
AND (source_key IS NULL OR TRIM(source_key) = '');
|
||||
""",
|
||||
(source_key, match_id),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
return source_key
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
return fallback_key or infer_match_source_key(fallback_key)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def upsert_match(
|
||||
external_id: str,
|
||||
home_team_id: int,
|
||||
|
||||
@@ -2,6 +2,7 @@ import secrets
|
||||
|
||||
from db import get_connection
|
||||
from parsers.parser_sources import build_logo_path
|
||||
from repositories.match_repository import resolve_match_source_key
|
||||
|
||||
|
||||
def create_match_session(
|
||||
@@ -88,7 +89,9 @@ def get_match_session_by_token(session_token: str):
|
||||
return None
|
||||
|
||||
row = list(row)
|
||||
source_key = row[21] if len(row) > 21 else None
|
||||
source_key = resolve_match_source_key(row[1], row[21] if len(row) > 21 else None)
|
||||
if len(row) > 21:
|
||||
row[21] = source_key
|
||||
row[16] = build_logo_path(source_key, row[16])
|
||||
row[20] = build_logo_path(source_key, row[20])
|
||||
return tuple(row)
|
||||
|
||||
Reference in New Issue
Block a user