chore(cod_api_tool.py): remove whitespace & add try except for cleaning match_info and stats files

This commit is contained in:
Rim 2025-03-10 10:45:04 -04:00
parent 61b910ab45
commit dfc647e500

View File

@ -258,64 +258,81 @@ class CodStatsManager:
self.beautify_match_data(timezone, player_name) self.beautify_match_data(timezone, player_name)
self.beautify_feed_data(timezone) self.beautify_feed_data(timezone)
self.clean_json_files('friendFeed.json', 'eventFeed.json') self.clean_json_files('friendFeed.json', 'eventFeed.json')
print("All data beautified successfully.") print("Data beautified successfully.")
def beautify_stats_data(self, timezone='GMT', player_name=None): def beautify_stats_data(self, timezone='GMT', player_name=None):
"""Beautify stats data.""" """Beautify stats data."""
if player_name: if player_name is None:
file_name = f'stats-{self._extract_player_name(player_name)}.json' print("Please specify a Player Name to sort Stats")
else: return 0
file_name = 'stats.json' try:
if player_name:
file_name = f'stats-{self._extract_player_name(player_name)}.json'
# else:
# file_name = 'stats.json'
file_path = os.path.join(STATS_DIR, file_name) file_path = os.path.join(STATS_DIR, file_name)
if not os.path.exists(file_path): if not os.path.exists(file_path):
print(f"File {file_path} not found. Skipping beautification.") print(f"File {file_path} not found. Skipping beautification.")
return return
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
data = json.load(file) data = json.load(file)
# Convert times and durations # Convert times and durations
self._replace_time_and_duration_recursive(data, timezone) self._replace_time_and_duration_recursive(data, timezone)
# Replace keys with more readable names # Replace keys with more readable names
data = self._recursive_key_replace(data) data = self._recursive_key_replace(data)
# Sort data by relevant metrics # Sort data by relevant metrics
data = self._sort_data(data) data = self._sort_data(data)
# Save modified data # Save modified data
with open(file_path, 'w') as file: with open(file_path, 'w') as file:
json.dump(data, file, indent=4) json.dump(data, file, indent=4)
print(f"Keys sorted and replaced in {file_path}.") print(f"Keys sorted and replaced in {file_path}.")
except Exception as e:
print("An error occurred while processing match data.")
return 0
def beautify_match_data(self, timezone='GMT', player_name=None): def beautify_match_data(self, timezone='GMT', player_name=None):
"""Beautify match data.""" """Beautify match data."""
if player_name: if player_name is None:
file_name = f'match_info-{self._extract_player_name(player_name)}.json' print("Please specify a Player Name to sort Match Info")
else: return 0
file_name = 'match_info.json'
file_path = os.path.join(STATS_DIR, file_name) try:
if not os.path.exists(file_path): if player_name:
print(f"File {file_path} not found. Skipping beautification.") file_name = f'match_info-{self._extract_player_name(player_name)}.json'
return # else:
# file_name = 'match_info.json'
with open(file_path, 'r') as file: file_path = os.path.join(STATS_DIR, file_name)
data = json.load(file) if not os.path.exists(file_path):
print(f"File {file_path} not found. Skipping beautification.")
return
# Convert times and durations with open(file_path, 'r') as file:
self._replace_time_and_duration_recursive(data, timezone) data = json.load(file)
# Replace keys with more readable names # Convert times and durations
data = self._recursive_key_replace(data) self._replace_time_and_duration_recursive(data, timezone)
# Save modified data # Replace keys with more readable names
with open(file_path, 'w') as file: data = self._recursive_key_replace(data)
json.dump(data, file, indent=4)
print(f"Keys replaced in {file_path}.") # Save modified data
with open(file_path, 'w') as file:
json.dump(data, file, indent=4)
print(f"Keys replaced in {file_path}.")
except Exception as e:
print("An error occurred while processing match data.")
return 0
def beautify_feed_data(self, timezone='GMT', player_name=None): def beautify_feed_data(self, timezone='GMT', player_name=None):
"""Beautify feed data files.""" """Beautify feed data files."""