From 26eb5a9398fe68720361f1a8a4dd74e1316ca368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=AE=D1=80=D0=B8=D0=B9=20=D0=A7=D0=B5=D1=80=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=BA=D0=BE?= Date: Thu, 30 Oct 2025 10:58:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20=D0=B7=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=B8=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- get_data.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/get_data.py b/get_data.py index be6176f..76eaabf 100644 --- a/get_data.py +++ b/get_data.py @@ -155,7 +155,7 @@ def _select_lock(path: str): return _write_lock_api return _write_lock_out -def atomic_write_json(path: str, data: Any) -> None: +def write_json_simple(path: str, data: Any) -> None: """ Безопасно записывает JSON: 1. Сериализуем в память (без локов). @@ -189,6 +189,14 @@ def atomic_write_json(path: str, data: Any) -> None: # 2b. Атомарно подменяем os.replace(tmp_path, target) + + +def write_json_simple(path: str, data: Any) -> None: + full_path = f"static/{path}.json" + with open(full_path, "w", encoding="utf-8") as f: + json.dump(data, f, ensure_ascii=False, separators=(",", ":")) + + def read_local_json(name: str, in_dir: str = "static") -> Optional[dict]: """ @@ -272,7 +280,7 @@ def get_json(session: requests.Session, url: str, name: str) -> Any: resp = session.get(url, timeout=10) resp.raise_for_status() data = resp.json() - atomic_write_json(f"api_{name}", data) + write_json_simple(f"api_{name}", data) return data @@ -732,12 +740,12 @@ def render_loop(stop_event: threading.Event, out_name: str = "game") -> None: rs = state.get("result", {}) if isinstance(rs, dict) and "live_status" in rs: live_status_to_write = [rs["live_status"]] - atomic_write_json("live_status", live_status_to_write) + write_json_simple("live_status", live_status_to_write) except Exception as e: logger.debug(f"[RENDER_THREAD] skip live_status write: {e}") try: - atomic_write_json(out_name, state.get("result", {})) + write_json_simple(out_name, state.get("result", {})) except Exception as e: logger.debug(f"[RENDER_THREAD] skip {out_name}.json write: {e}") @@ -845,7 +853,7 @@ def Referee(merged: dict, *, out_dir: str = "static") -> None: ), ) out_path = "referee" - atomic_write_json(out_path, referees) + write_json_simple(out_path, referees) logging.info("Сохранил payload: {out_path}") except Exception as e: @@ -1051,7 +1059,7 @@ def render_once_after_game( Referee(state) Play_By_Play(state) - atomic_write_json(out_name, state["result"]) + write_json_simple(out_name, state["result"]) logger.info("[RENDER_ONCE] финальные json сохранены успешно") @@ -1290,7 +1298,7 @@ def Json_Team_Generation( ) # пишем полный ростер команды - atomic_write_json(who, sorted_team) + write_json_simple(who, sorted_team) logger.info(f"Сохранил payload: {who}.json") # топ-игроки по очкам/подборам/ассистам и т.д. @@ -1314,7 +1322,7 @@ def Json_Team_Generation( player["foul"] = "" top_name = f"top{who.replace('t', 'T')}" - atomic_write_json(top_name, top_sorted_team) + write_json_simple(top_name, top_sorted_team) logger.info(f"Сохранил payload: {top_name}.json") # кто прямо сейчас на площадке @@ -1327,7 +1335,7 @@ def Json_Team_Generation( key=lambda x: int(x.get("num") or 0), ) started_name = f"started_{who}" - atomic_write_json(started_name, started_team) + write_json_simple(started_name, started_team) logger.info(f"Сохранил payload: {started_name}.json") @@ -1597,7 +1605,7 @@ def Team_Both_Stat(merged: dict) -> None: } ) - atomic_write_json("team_stats", result_json) + write_json_simple("team_stats", result_json) logger.info("Сохранил payload: team_stats.json") except Exception as e: @@ -1668,7 +1676,7 @@ def Pregame_data_json(data: dict) -> None: "fouls": round((data_team["totalStats"]["foul"] / data_team["games"]), 1), } teams.append(temp_team) - atomic_write_json("team_comparison", teams) + write_json_simple("team_comparison", teams) logger.info("Сохранил payload: team_comparison.json") @@ -1693,7 +1701,7 @@ def Pregame_data(pregame_raw: dict, game_stub: dict) -> None: } Pregame_data_json(out["pregame"]) # сохраняем файл - # atomic_write_json(out["pregame"], "pregame") + # write_json_simple(out["pregame"], "pregame") # logger.info("Сохранил payload: pregame.json") except Exception as e: logger.error(f"Ошибка в Pregame_data: {e}", exc_info=True) @@ -1728,7 +1736,7 @@ def Scores_Quarter(merged: dict) -> None: else: logger.debug("Нет данных по счёту, сохраняем пустые значения.") - atomic_write_json("scores", score_by_quarter) + write_json_simple("scores", score_by_quarter) logger.info("Сохранил payload: scores.json") except Exception as e: @@ -1862,7 +1870,7 @@ def Standing_func( standings_payload = df.to_dict(orient="records") filename = f"standings_{league}_{comp_name}" - atomic_write_json(filename, standings_payload) + write_json_simple(filename, standings_payload) logger.info(f"[STANDINGS_THREAD] сохранил {filename}.json") elif item.get("playoffPairs"): @@ -1872,7 +1880,7 @@ def Standing_func( standings_payload = df.to_dict(orient="records") filename = f"standings_{league}_{comp_name}" - atomic_write_json(filename, standings_payload) + write_json_simple(filename, standings_payload) logger.info( f"[STANDINGS_THREAD] saved {filename}.json (playoffPairs, {len(standings_payload)} rows)" )