обновление для Кубка России

переделаны все парсеры на ссылки из базы
This commit is contained in:
2026-07-02 12:46:29 +03:00
parent b0fe3ad5a4
commit 57907fd86b
29 changed files with 1838 additions and 240 deletions

View File

@@ -0,0 +1,148 @@
from __future__ import annotations
from pathlib import Path
from typing import Any
import os
import shutil
from datetime import datetime
from dotenv import dotenv_values, load_dotenv, set_key
from parsers import parser_sources
PROJECT_ROOT = Path(__file__).resolve().parent.parent
ENV_PATH = PROJECT_ROOT / ".env"
def _field(key: str, label: str, value: str = "", *, field_type: str = "text", options: list[dict] | None = None, help_text: str = "", wide: bool = False) -> dict[str, Any]:
return {
"key": key,
"label": label,
"value": value or "",
"type": field_type,
"options": options or [],
"help": help_text,
"wide": wide,
}
def _source_field_key(source_key: str, field: str) -> str:
return f"{source_key}_{field}"
def get_env_file_path() -> Path:
return ENV_PATH
def _dotenv_values() -> dict[str, str]:
if not ENV_PATH.exists():
return {}
raw = dotenv_values(ENV_PATH)
return {key: str(value or "") for key, value in raw.items() if key}
def build_env_settings_context() -> dict[str, Any]:
"""Данные для формы редактирования безопасных ключей .env."""
env_values = _dotenv_values()
sources = parser_sources.list_parser_sources()
source_options = [
{"value": source["key"], "label": source.get("title") or source["key"]}
for source in sources
]
groups: list[dict[str, Any]] = [
{
"title": "Общие настройки источников",
"subtitle": "Эти значения влияют на выбор активного турнира по умолчанию и базовый сайт RFS.",
"fields": [
_field(
"DATA_EVENT_CODE",
"Активный источник по умолчанию",
parser_sources.get_default_source_key(),
field_type="select",
options=source_options,
help_text="Используется, если парсер запускается без выбора источника.",
),
_field(
"RFS_BASE_URL",
"Базовый URL RFS/WFL",
parser_sources.RFS_BASE_URL,
help_text="Обычно https://wfl.rfs.ru",
wide=True,
),
],
}
]
for source in sources:
source_key = source["key"]
title = source.get("title") or source_key
groups.append(
{
"title": title,
"subtitle": f"Ключ источника: {source_key}",
"fields": [
_field(_source_field_key(source_key, "EVENT_NAME"), "Название в интерфейсе", source.get("title", "")),
_field(_source_field_key(source_key, "SEASON"), "Сезон", source.get("season", "")),
_field(_source_field_key(source_key, "TOURNAMENT_ID"), "Tournament ID", source.get("tournament_id", "")),
_field(_source_field_key(source_key, "ROUND_ID"), "Round ID", source.get("round_id", "")),
_field(
_source_field_key(source_key, "CALENDAR_TYPE"),
"Тип календаря",
source.get("calendar_type", "tours"),
field_type="select",
options=[
{"value": "tours", "label": "tours — туры"},
{"value": "stages", "label": "stages — стадии"},
],
),
_field(_source_field_key(source_key, "LOGO_BASE_PATH"), "Папка логотипов", source.get("logo_base_path", ""), wide=True),
_field(_source_field_key(source_key, "TEAMS_URL"), "Ссылка на команды", source.get("teams_url", ""), wide=True),
_field(_source_field_key(source_key, "SCHEDULE_URL"), "Ссылка на расписание", source.get("schedule_url", ""), wide=True),
_field(_source_field_key(source_key, "STANDINGS_URL"), "Ссылка на турнирку", source.get("standings_url", ""), wide=True),
_field(_source_field_key(source_key, "MATCH_BASE_URL"), "Базовая ссылка матча", source.get("match_base_url", ""), wide=True),
],
}
)
editable_keys = [field["key"] for group in groups for field in group["fields"]]
return {
"env_path": str(ENV_PATH),
"groups": groups,
"editable_keys": editable_keys,
"raw_values": env_values,
}
def get_editable_env_keys() -> set[str]:
context = build_env_settings_context()
return set(context["editable_keys"])
def save_env_settings(form_values: dict[str, str]) -> list[str]:
"""Сохраняет только разрешённые ключи в .env и обновляет os.environ."""
editable_keys = get_editable_env_keys()
updates: list[str] = []
ENV_PATH.parent.mkdir(parents=True, exist_ok=True)
if not ENV_PATH.exists():
ENV_PATH.write_text("", encoding="utf-8")
backup_path = ENV_PATH.with_suffix(".env.bak")
try:
shutil.copy2(ENV_PATH, backup_path)
except Exception:
# Бэкап полезен, но не должен ломать сохранение настроек.
pass
for key in sorted(editable_keys):
if key not in form_values:
continue
value = str(form_values.get(key) or "").strip()
set_key(str(ENV_PATH), key, value, quote_mode="always")
os.environ[key] = value
updates.append(key)
load_dotenv(ENV_PATH, override=True)
return updates

View File

@@ -0,0 +1,142 @@
from __future__ import annotations
from typing import Any
from parsers import parser_sources
from repositories.project_settings_repository import update_app_setting, update_parser_source
def _field(
key: str,
label: str,
value: str = "",
*,
field_type: str = "text",
options: list[dict] | None = None,
help_text: str = "",
wide: bool = False,
) -> dict[str, Any]:
return {
"key": key,
"label": label,
"value": value or "",
"type": field_type,
"options": options or [],
"help": help_text,
"wide": wide,
}
def _source_field_key(source_key: str, field: str) -> str:
return f"{source_key}_{field}"
def build_project_settings_context() -> dict[str, Any]:
"""Данные для формы редактирования настроек проекта из БД."""
sources = parser_sources.list_parser_sources()
source_options = [
{"value": source["key"], "label": source.get("title") or source["key"]}
for source in sources
]
groups: list[dict[str, Any]] = [
{
"title": "Общие настройки проекта",
"subtitle": "Эти значения хранятся в базе данных, а не в .env.",
"fields": [
_field(
"DATA_EVENT_CODE",
"Активный источник по умолчанию",
parser_sources.get_default_source_key(),
field_type="select",
options=source_options,
help_text="Используется, если парсер запускается без выбора источника.",
),
_field(
"RFS_BASE_URL",
"Базовый URL RFS/WFL",
parser_sources.get_rfs_base_url(),
help_text="Обычно https://wfl.rfs.ru",
wide=True,
),
],
}
]
for source in sources:
source_key = source["key"]
title = source.get("title") or source_key
groups.append(
{
"title": title,
"subtitle": f"Ключ источника: {source_key}",
"fields": [
_field(_source_field_key(source_key, "EVENT_NAME"), "Название в интерфейсе", source.get("title", "")),
_field(_source_field_key(source_key, "SEASON"), "Сезон", source.get("season", "")),
_field(_source_field_key(source_key, "TOURNAMENT_ID"), "Tournament ID", source.get("tournament_id", "")),
_field(_source_field_key(source_key, "ROUND_ID"), "Round ID", source.get("round_id", "")),
_field(
_source_field_key(source_key, "CALENDAR_TYPE"),
"Тип календаря",
source.get("calendar_type", "tours"),
field_type="select",
options=[
{"value": "tours", "label": "tours — туры"},
{"value": "stages", "label": "stages — стадии"},
],
),
_field(_source_field_key(source_key, "LOGO_BASE_PATH"), "Папка логотипов", source.get("logo_base_path", ""), wide=True),
_field(_source_field_key(source_key, "PHOTO_BASE_PATH"), "Папка фотографий", source.get("photo_base_path", ""), wide=True),
_field(_source_field_key(source_key, "TEAMS_URL"), "Ссылка на команды", source.get("teams_url", ""), wide=True),
_field(_source_field_key(source_key, "SCHEDULE_URL"), "Ссылка на расписание", source.get("schedule_url", ""), wide=True),
_field(_source_field_key(source_key, "STANDINGS_URL"), "Ссылка на турнирку", source.get("standings_url", ""), wide=True),
_field(_source_field_key(source_key, "MATCH_BASE_URL"), "Базовая ссылка матча", source.get("match_base_url", ""), wide=True),
],
}
)
editable_keys = [field["key"] for group in groups for field in group["fields"]]
return {
"storage_title": "База данных",
"groups": groups,
"editable_keys": editable_keys,
}
def save_project_settings(form_values: dict[str, str]) -> list[str]:
"""Сохраняет настройки проекта в БД."""
updated: list[str] = []
default_source_key = str(form_values.get("DATA_EVENT_CODE") or "SUPERLEAGUE").upper().strip()
rfs_base_url = str(form_values.get("RFS_BASE_URL") or "https://wfl.rfs.ru").strip().rstrip("/")
update_app_setting("default_parser_source_key", default_source_key)
update_app_setting("rfs_base_url", rfs_base_url)
updated.extend(["DATA_EVENT_CODE", "RFS_BASE_URL"])
sources = parser_sources.list_parser_sources()
field_map = {
"EVENT_NAME": "title",
"SEASON": "season",
"TOURNAMENT_ID": "tournament_id",
"ROUND_ID": "round_id",
"CALENDAR_TYPE": "calendar_type",
"LOGO_BASE_PATH": "logo_base_path",
"PHOTO_BASE_PATH": "photo_base_path",
"TEAMS_URL": "teams_url",
"SCHEDULE_URL": "schedule_url",
"STANDINGS_URL": "standings_url",
"MATCH_BASE_URL": "match_base_url",
}
for source in sources:
source_key = source["key"]
values: dict[str, str] = {"base_url": rfs_base_url}
for form_field, db_field in field_map.items():
full_key = _source_field_key(source_key, form_field)
if full_key in form_values:
values[db_field] = str(form_values.get(full_key) or "").strip()
updated.append(full_key)
update_parser_source(source_key, values)
return updated

View File

@@ -16,4 +16,5 @@ def sync_matches(matches_data: list[dict]) -> None:
place=match.get("place"),
date_raw=match.get("date_raw"),
score_add=match.get("score_add"),
)
source_key=match.get("source_key"),
)

View File

@@ -1,27 +1,44 @@
# services/vmix_json_service.py
from db import get_connection
from repositories.match_lineup_repository import get_match_lineup_for_vmix
from parsers.parser_sources import build_empty_photo_path, build_logo_path, build_logo_variant_path, build_photo_path
PHOTO_BASE_PATH = r"D:\Графика\ФУТБОЛ\Женская Суперлига 2026\Photo"
EMPTY_PHOTO_PATH = PHOTO_BASE_PATH + r"\EMPTY.png"
DEFAULT_PHOTO_BASE_PATH = r"D:\Графика\ФУТБОЛ\Женская Суперлига 2026\Photo"
EMPTY_PHOTO_PATH = DEFAULT_PHOTO_BASE_PATH + r"\EMPTY.png"
def build_generated_player_photo_path(team_name: str, last_name: str, first_name: str) -> str:
return (
PHOTO_BASE_PATH
+ "\\"
+ str(team_name or "")
def _session_source_key(session_row) -> str | None:
try:
value = session_row[21]
except Exception:
value = None
return str(value).strip() if value else None
def build_generated_player_photo_path(
team_name: str,
last_name: str,
first_name: str,
source_key: str | None = None,
) -> str:
relative_file = (
str(team_name or "").strip()
+ "\\"
+ (str(last_name or "") + " " + str(first_name or "")).strip()
+ ".png"
)
return build_photo_path(source_key, relative_file)
def resolve_player_photo(photo_enabled: bool, generated_photo: str) -> str:
def resolve_player_photo(
photo_enabled: bool,
generated_photo: str,
source_key: str | None = None,
) -> str:
if photo_enabled:
return generated_photo
return EMPTY_PHOTO_PATH
return build_empty_photo_path(source_key) or EMPTY_PHOTO_PATH
def _normalize_text(value) -> str:
@@ -208,6 +225,7 @@ def resolve_player_photo_for_json(
player: dict,
generated_photo: str,
fallback_team_id=None,
source_key: str | None = None,
) -> str:
state = _find_player_photo_state(
player_id=player.get("player_id") or player.get("id"),
@@ -220,16 +238,24 @@ def resolve_player_photo_for_json(
)
if state is not None:
_photo, photo_enabled = state
return resolve_player_photo(photo_enabled=photo_enabled, generated_photo=generated_photo)
photo_value, photo_enabled = state
resolved_photo = build_photo_path(source_key, photo_value) if photo_value else generated_photo
return resolve_player_photo(
photo_enabled=photo_enabled,
generated_photo=resolved_photo,
source_key=source_key,
)
photo_value = player.get("photo") or ""
resolved_photo = build_photo_path(source_key, photo_value) if photo_value else generated_photo
return resolve_player_photo(
photo_enabled=player.get("photo_enabled"),
generated_photo=generated_photo,
generated_photo=resolved_photo,
source_key=source_key,
)
def build_lineup_json(match_id, home_team_id, away_team_id, name, team_a_name, team_b_name):
def build_lineup_json(match_id, home_team_id, away_team_id, name, team_a_name, team_b_name, source_key=None):
lineups = get_match_lineup_for_vmix(
match_id=match_id,
home_team_id=home_team_id,
@@ -274,8 +300,10 @@ def build_lineup_json(match_id, home_team_id, away_team_id, name, team_a_name, t
team_name=team_a_name if "home" in name else team_b_name,
last_name=p.get("last_name", ""),
first_name=p.get("first_name", ""),
source_key=source_key,
),
fallback_team_id=fallback_team_id,
source_key=source_key,
),
}
)
@@ -305,43 +333,16 @@ def get_vmix_match_info_by_token(session_token: str):
m.tour,
ht.logo_path AS home_logo,
REPLACE(at.logo_path, 'HOME', 'AWAY') AS away_logo,
at.logo_path AS away_logo,
ref1.referee_name AS referee1,
ref2.referee_name AS referee2,
ref3.referee_name AS referee3,
ref4.referee_name AS referee4,
CASE
WHEN ht.name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(ht.logo_path, 'HOME\\', ''), 'Динамо', 'Динамоелый')
WHEN ht.name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(ht.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Белый')
ELSE REPLACE(ht.logo_path, 'HOME\\', '')
END AS home_logo1,
CASE
WHEN at.name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(at.logo_path, 'HOME\\', ''), 'Динамо', 'Динамоелый')
WHEN at.name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(at.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Белый')
ELSE REPLACE(at.logo_path, 'HOME\\', '')
END AS away_logo1,
CASE
WHEN ht.name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(ht.logo_path, 'HOME\\', ''), 'Динамо', 'Динамо_Синий')
WHEN ht.name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(ht.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Синий')
ELSE REPLACE(ht.logo_path, 'HOME\\', '')
END AS home_logo2,
CASE
WHEN at.name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(at.logo_path, 'HOME\\', ''), 'Динамо', 'Динамо_Синий')
WHEN at.name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(at.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Синий')
ELSE REPLACE(at.logo_path, 'HOME\\', '')
END AS away_logo2,
ht.logo_path AS home_logo1,
at.logo_path AS away_logo1,
ht.logo_path AS home_logo2,
at.logo_path AS away_logo2,
ht.city AS home_city,
at.city AS away_city,
@@ -350,7 +351,8 @@ def get_vmix_match_info_by_token(session_token: str):
c1.amplua AS coach_amplua1,
TRIM(COALESCE(c2.name, '') || ' ' || COALESCE(c2.lastname, '')) AS coach_name2,
c2.amplua AS coach_amplua2
c2.amplua AS coach_amplua2,
m.source_key AS source_key
FROM match_sessions ms
JOIN matches m ON m.id = ms.match_id
@@ -378,23 +380,33 @@ def get_vmix_match_info_by_token(session_token: str):
try:
with conn.cursor() as cur:
cur.execute(query, (session_token,))
return cur.fetchone()
row = cur.fetchone()
if not row:
return None
row = list(row)
source_key = row[30] if len(row) > 30 else None
row[14] = build_logo_path(source_key, row[14])
row[15] = build_logo_path(source_key, row[15])
row[20] = build_logo_variant_path(source_key, row[20], "white")
row[21] = build_logo_variant_path(source_key, row[21], "white")
row[22] = build_logo_variant_path(source_key, row[22], "blue")
row[23] = build_logo_variant_path(source_key, row[23], "blue")
return tuple(row)
finally:
conn.close()
def get_vmix_standings(session_token: str):
def get_vmix_standings(session_row):
source_key = _session_source_key(session_row)
season = session_row[11] if len(session_row) > 11 else None
query = """
SELECT
s.position,
t.full_name,
CASE
WHEN t.full_name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(t.logo_path, 'HOME\\', ''), 'Динамо', 'Динамоелый')
WHEN t.full_name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(t.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Белый')
ELSE REPLACE(t.logo_path, 'HOME\\', '')
END AS logo,
COALESCE(t.full_name, t.name) AS team_name,
t.logo_path AS logo,
s.played,
s.wins,
s.losses,
@@ -404,35 +416,36 @@ def get_vmix_standings(session_token: str):
s.team_id
FROM standings s
LEFT JOIN teams t ON s.team_id = t.id
WHERE (%s IS NULL OR s.season = %s)
ORDER BY s.position
"""
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute(query, (session_token,))
return cur.fetchall()
cur.execute(query, (season, season))
rows = cur.fetchall()
result = []
for row in rows:
row = list(row)
row[2] = build_logo_variant_path(source_key, row[2], "white")
result.append(tuple(row))
return result
finally:
conn.close()
def get_vmix_schedule(session_token: str):
def get_vmix_schedule(session_row):
source_key = _session_source_key(session_row)
tour = session_row[10] if len(session_row) > 10 else None
season = session_row[11] if len(session_row) > 11 else None
source_filter = source_key or "SUPERLEAGUE"
query = """
SELECT
CASE
WHEN t1.full_name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(t1.logo_path, 'HOME\\', ''), 'Динамо', 'Динамоелый')
WHEN t1.full_name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(t1.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Белый')
ELSE REPLACE(t1.logo_path, 'HOME\\', '')
END AS logo1,
CASE
WHEN t2.full_name ILIKE '%%динамо%%'
THEN REPLACE(REPLACE(t2.logo_path, 'HOME\\', ''), 'Динамо', 'Динамоелый')
WHEN t2.full_name ILIKE '%%зенит%%'
THEN REPLACE(REPLACE(t2.logo_path, 'HOME\\', ''), 'Зенит', 'Зенит_Белый')
ELSE REPLACE(t2.logo_path, 'HOME\\', '')
END AS logo2,
t1.logo_path AS logo1,
t2.logo_path AS logo2,
m.home_score,
m.away_score,
m.match_date,
@@ -443,14 +456,24 @@ def get_vmix_schedule(session_token: str):
LEFT JOIN teams t1 ON m.home_team_id = t1.id
LEFT JOIN teams t2 ON m.away_team_id = t2.id
WHERE m.tour = %s
AND (%s IS NULL OR m.season = %s)
AND COALESCE(m.source_key, %s) = %s
ORDER BY m.match_date, m.id
"""
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute(query, (session_token[10],))
return cur.fetchall()
cur.execute(query, (tour, season, season, source_filter, source_filter))
rows = cur.fetchall()
result = []
for row in rows:
row = list(row)
row[0] = build_logo_variant_path(source_key, row[0], "white")
row[1] = build_logo_variant_path(source_key, row[1], "white")
result.append(tuple(row))
return result
finally:
conn.close()