From 932340431fc838408a206cf101401f80cd5345e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Wed, 5 Jul 2023 18:47:09 +0200 Subject: [PATCH] Handle invalid player numbers on export On hanab.live, some games have an invalid number of players stored in the database, this leads to some games not being able to be exported from the site. We catch these cases and write occurrences to a local data file --- hanabi/live/download_data.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/hanabi/live/download_data.py b/hanabi/live/download_data.py index 1e8313d..6877378 100644 --- a/hanabi/live/download_data.py +++ b/hanabi/live/download_data.py @@ -2,15 +2,17 @@ import alive_progress from typing import Dict, Optional import psycopg2.errors +import platformdirs from hanabi import hanab_game +from hanabi import constants +from hanabi import logger from hanabi.database import database from hanabi.live import site_api from hanabi.live import compress from hanabi.live import variants from hanabi.live import hanab_live -from hanabi import logger class GameExportError(ValueError): @@ -72,7 +74,7 @@ def detailed_export_game( game_json = site_api.get("export/{}".format(game_id)) if game_json is None: - raise GameExportNoResponseFromSiteError + raise GameExportNoResponseFromSiteError(game_id) if type(game_json) != dict: raise GameExportInvalidResponseTypeError(game_id, type(game_json)) @@ -177,7 +179,14 @@ def _process_game_row(game: Dict, var_id, export_all_games: bool = False): raise ValueError("Unknown response format on hanab.live") if len(users) != num_players: - raise GameExportInvalidNumberOfPlayersError(game_id, num_players, users) + logger.error("Invalid number of players reported when processing row {}".format(game)) + f = platformdirs.user_data_dir(constants.APP_NAME, ensure_exists=True) + '/invalid_game_ids.txt' + with open(f, "a+") as invalid_games_file: + invalid_games_file.writelines( + "{}, {}, {}\n".format(game_id, num_players, ", ".join(users)) + ) + return +# raise GameExportInvalidNumberOfPlayersError(game_id, num_players, users) if export_all_games: detailed_export_game(game_id, score=score, var_id=var_id)