добавил новый json INFO
вывожу данные по текущей игре
This commit is contained in:
109
get_data.py
109
get_data.py
@@ -236,7 +236,7 @@ def start_live_threads(season, game_id):
|
||||
args=(
|
||||
"live-status",
|
||||
URLS["live-status"].format(host=HOST, game_id=game_id),
|
||||
.5,
|
||||
0.5,
|
||||
stop_event_live,
|
||||
),
|
||||
daemon=True,
|
||||
@@ -246,7 +246,7 @@ def start_live_threads(season, game_id):
|
||||
args=(
|
||||
"box-score",
|
||||
URLS["box-score"].format(host=HOST, game_id=game_id),
|
||||
.5,
|
||||
0.5,
|
||||
stop_event_live,
|
||||
),
|
||||
daemon=True,
|
||||
@@ -284,12 +284,16 @@ def stop_live_threads():
|
||||
for t in threads_live:
|
||||
t.join(timeout=2)
|
||||
if t.is_alive():
|
||||
logger.warning(f"[{current_time}] [threads] LIVE thread is still alive: {t.name}")
|
||||
logger.warning(
|
||||
f"[{current_time}] [threads] LIVE thread is still alive: {t.name}"
|
||||
)
|
||||
still_alive.append(t.name)
|
||||
|
||||
threads_live = []
|
||||
if still_alive:
|
||||
logger.warning(f"[{current_time}] [threads] Some LIVE threads did not stop: {still_alive}")
|
||||
logger.warning(
|
||||
f"[{current_time}] [threads] Some LIVE threads did not stop: {still_alive}"
|
||||
)
|
||||
else:
|
||||
logger.info("[threads] LIVE threads stopped")
|
||||
|
||||
@@ -308,20 +312,28 @@ def stop_offline_threads():
|
||||
|
||||
# Функция запускаемая в потоках
|
||||
def get_data_from_API(
|
||||
name: str, url: str, sleep_time: float, stop_event: threading.Event, stop_when_live=False
|
||||
name: str,
|
||||
url: str,
|
||||
sleep_time: float,
|
||||
stop_event: threading.Event,
|
||||
stop_when_live=False,
|
||||
):
|
||||
did_first_fetch = False
|
||||
while not stop_event.is_set():
|
||||
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")
|
||||
if stop_when_live and globals().get("STATUS") == "live" and did_first_fetch:
|
||||
logger.info(f"{[{current_time}]} [{name}] stopping because STATUS='live' and first fetch done")
|
||||
logger.info(
|
||||
f"{[{current_time}]} [{name}] stopping because STATUS='live' and first fetch done"
|
||||
)
|
||||
break
|
||||
start = time.time()
|
||||
try:
|
||||
value = requests.get(url, timeout=5).json()
|
||||
did_first_fetch = True # помечаем, что один заход сделали
|
||||
except json.JSONDecodeError as json_err:
|
||||
logger.warning(f"[{current_time}] [{name}] Ошибка парсинга JSON: {json_err}")
|
||||
logger.warning(
|
||||
f"[{current_time}] [{name}] Ошибка парсинга JSON: {json_err}"
|
||||
)
|
||||
value = {"error": f"JSON decode error: {json_err}"}
|
||||
except requests.exceptions.Timeout:
|
||||
logger.warning(f"[{current_time}] [{name}] Таймаут при запросе {url}")
|
||||
@@ -339,7 +351,9 @@ def get_data_from_API(
|
||||
"fail",
|
||||
"no-status",
|
||||
):
|
||||
logger.warning(f"[{current_time}] [{name}] API вернул статус '{value.get('status')}'")
|
||||
logger.warning(
|
||||
f"[{current_time}] [{name}] API вернул статус '{value.get('status')}'"
|
||||
)
|
||||
|
||||
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
|
||||
results_q.put({"source": name, "ts": ts, "data": value})
|
||||
@@ -357,7 +371,9 @@ def get_data_from_API(
|
||||
if stop_event.is_set():
|
||||
break
|
||||
if stop_when_live and globals().get("STATUS") == "live" and did_first_fetch:
|
||||
logger.info(f"[{name}] stopping during sleep because STATUS='live' and first fetch done")
|
||||
logger.info(
|
||||
f"[{name}] stopping during sleep because STATUS='live' and first fetch done"
|
||||
)
|
||||
return
|
||||
time.sleep(1)
|
||||
slept += 1
|
||||
@@ -530,7 +546,9 @@ def results_consumer():
|
||||
globals()["OFFLINE_SWITCH_AT"] = None
|
||||
|
||||
if globals().get("STATUS") != "live":
|
||||
logger.info("[status] match became LIVE → switch to LIVE threads")
|
||||
logger.info(
|
||||
"[status] match became LIVE → switch to LIVE threads"
|
||||
)
|
||||
globals()["STATUS"] = "live"
|
||||
start_live_threads(SEASON, GAME_ID)
|
||||
|
||||
@@ -542,7 +560,9 @@ def results_consumer():
|
||||
else:
|
||||
if source == "game":
|
||||
# has_game_already = "teams" in latest_data
|
||||
has_game_already = "game" in latest_data and isinstance(latest_data.get("game"), dict)
|
||||
has_game_already = "game" in latest_data and isinstance(
|
||||
latest_data.get("game"), dict
|
||||
)
|
||||
|
||||
# есть ли в ответе ПОЛНАЯ структура
|
||||
is_full = (
|
||||
@@ -558,14 +578,21 @@ def results_consumer():
|
||||
"data": payload,
|
||||
}
|
||||
else:
|
||||
if globals().get("STATUS") in ("live", "live_soon") and has_game_already:
|
||||
logger.debug("results_consumer: LIVE & partial game → keep previous one")
|
||||
if (
|
||||
globals().get("STATUS") in ("live", "live_soon")
|
||||
and has_game_already
|
||||
):
|
||||
logger.debug(
|
||||
"results_consumer: LIVE & partial game → keep previous one"
|
||||
)
|
||||
else:
|
||||
if not has_game_already:
|
||||
# раньше не было вообще — положим хоть что-то
|
||||
latest_data["game"] = {"ts": msg["ts"], "data": payload}
|
||||
else:
|
||||
logger.debug("results_consumer: got partial game, keeping previous one")
|
||||
logger.debug(
|
||||
"results_consumer: got partial game, keeping previous one"
|
||||
)
|
||||
# # game неполный
|
||||
# if not has_game_already:
|
||||
# # 👉 раньше game вообще не было — лучше положить хоть что-то
|
||||
@@ -705,7 +732,11 @@ def build_pretty_status_message():
|
||||
if isinstance(raw, dict):
|
||||
# ваш нормальный полный ответ по game имеет структуру: {"data": {"result": {...}}}
|
||||
# но на всякий случай поддержим и вариант, где сразу {"result": {...}} или уже {"game": ...}
|
||||
result = raw.get("data", {}).get("result", {}) if "data" in raw else (raw.get("result") or raw)
|
||||
result = (
|
||||
raw.get("data", {}).get("result", {})
|
||||
if "data" in raw
|
||||
else (raw.get("result") or raw)
|
||||
)
|
||||
else:
|
||||
result = {}
|
||||
|
||||
@@ -899,7 +930,7 @@ async def lifespan(app: FastAPI):
|
||||
GAME_TODAY = is_today
|
||||
|
||||
logger.info(
|
||||
f"\nЛига: {LEAGUE}\nСезон: {season}\nКоманда: {TEAM}\nGame ID: {game_id}"
|
||||
f"Лига: {LEAGUE}\nСезон: {season}\nКоманда: {TEAM}\nGame ID: {game_id}"
|
||||
)
|
||||
|
||||
# 4. запускаем "длинные" потоки (они у тебя и так всегда)
|
||||
@@ -1945,9 +1976,7 @@ async def regular_standings():
|
||||
and "totalGoalMinus" in df.columns
|
||||
):
|
||||
tw = (
|
||||
pd.to_numeric(df["totalWin"], errors="coerce")
|
||||
.fillna(0)
|
||||
.astype(int)
|
||||
pd.to_numeric(df["totalWin"], errors="coerce").fillna(0).astype(int)
|
||||
)
|
||||
td = (
|
||||
pd.to_numeric(df["totalDefeat"], errors="coerce")
|
||||
@@ -2027,6 +2056,48 @@ async def live_status():
|
||||
return [{"foulsA": 0, "foulsB": 0}]
|
||||
|
||||
|
||||
@app.get("/info")
|
||||
async def info():
|
||||
data = latest_data["game"]["data"]["result"]
|
||||
team1_name = data["team1"]["name"]
|
||||
team2_name = data["team2"]["name"]
|
||||
team1_logo = data["team1"]["logo"]
|
||||
team2_logo = data["team2"]["logo"]
|
||||
arena = data["arena"]["name"]
|
||||
arena_short = data["arena"]["shortName"]
|
||||
region = data["region"]["name"]
|
||||
date_obj = datetime.strptime(data["game"]["localDate"], "%d.%m.%Y")
|
||||
league = data["league"]["abcName"]
|
||||
league_full = data["league"]["name"]
|
||||
season = f'{str(data["league"]["season"]-1)}/{str(data["league"]["season"])[2:]}'
|
||||
stadia = data["comp"]["name"]
|
||||
try:
|
||||
full_format = date_obj.strftime("%A, %-d %B %Y")
|
||||
short_format = date_obj.strftime("%A, %-d %b")
|
||||
except ValueError:
|
||||
full_format = date_obj.strftime("%A, %#d %B %Y")
|
||||
short_format = date_obj.strftime("%A, %#d %b")
|
||||
|
||||
|
||||
return [
|
||||
{
|
||||
"team1": team1_name,
|
||||
"team2": team2_name,
|
||||
"logo1": team1_logo,
|
||||
"logo2": team2_logo,
|
||||
"arena": arena,
|
||||
"short_arena": arena_short,
|
||||
"region": region,
|
||||
"league": league,
|
||||
"league_full": league_full,
|
||||
"season": season,
|
||||
"stadia": stadia,
|
||||
"date1": str(full_format),
|
||||
"date2": str(short_format),
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"get_data:app", host="0.0.0.0", port=8000, reload=True, log_level="debug"
|
||||
|
||||
Reference in New Issue
Block a user