Allow for setting timezone to convert to

This commit is contained in:
Rim 2023-12-25 22:18:47 -05:00
parent 49916431cf
commit dc68740f4b

View File

@ -152,13 +152,13 @@ def display_menu():
choice = input("Enter your choice: ") choice = input("Enter your choice: ")
return int(choice) return int(choice)
def beautify_feed_data(): def beautify_feed_data(timezone='GMT'):
for feed_file in ['friendFeed.json', 'eventFeed.json']: for feed_file in ['friendFeed.json', 'eventFeed.json']:
file_path = os.path.join(DIR_NAME, feed_file) file_path = os.path.join(DIR_NAME, feed_file)
if os.path.exists(file_path): if os.path.exists(file_path):
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
data = json.load(file) data = json.load(file)
replace_time_and_duration_recursive(data) replace_time_and_duration_recursive(data, timezone)
data = recursive_key_replace(data) data = recursive_key_replace(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)
@ -221,7 +221,7 @@ def sort_data(data):
data[key] = sort_data(value) data[key] = sort_data(value)
return data return data
def replace_time_and_duration_recursive(data): def replace_time_and_duration_recursive(data, timezone):
""" """
Recursively replace epoch times for specific keys in a nested dictionary or list. Recursively replace epoch times for specific keys in a nested dictionary or list.
""" """
@ -233,25 +233,25 @@ def replace_time_and_duration_recursive(data):
if isinstance(data, list): if isinstance(data, list):
for item in data: for item in data:
replace_time_and_duration_recursive(item) replace_time_and_duration_recursive(item, timezone)
elif isinstance(data, dict): elif isinstance(data, dict):
for key, value in data.items(): for key, value in data.items():
if key in date_keys: if key in date_keys:
data[key] = epoch_milli_to_human_readable(value) data[key] = epoch_milli_to_human_readable(value, timezone)
if key in time_keys: if key in time_keys:
data[key] = convert_duration_seconds(value) data[key] = convert_duration_seconds(value)
elif key == "utcStartSeconds": elif key == "utcStartSeconds":
data[key] = epoch_to_human_readable(value) data[key] = epoch_to_human_readable(value, timezone)
# For EST conversion: # For EST conversion:
# data[key] = epoch_to_human_readable(value, "EST") # data[key] = epoch_to_human_readable(value, "EST")
elif key == "utcEndSeconds": elif key == "utcEndSeconds":
data[key] = epoch_to_human_readable(value) data[key] = epoch_to_human_readable(value, timezone)
# For EST conversion: # For EST conversion:
# data[key] = epoch_to_human_readable(value, "EST") # data[key] = epoch_to_human_readable(value, "EST")
elif key == "duration": elif key == "duration":
data[key] = convert_duration_milliseconds(value) data[key] = convert_duration_milliseconds(value)
else: else:
replace_time_and_duration_recursive(value) replace_time_and_duration_recursive(value, timezone)
def epoch_milli_to_human_readable(epoch_millis, timezone='GMT'): def epoch_milli_to_human_readable(epoch_millis, timezone='GMT'):
""" """
@ -266,6 +266,12 @@ def epoch_milli_to_human_readable(epoch_millis, timezone='GMT'):
elif timezone == 'EST': elif timezone == 'EST':
dt_object -= datetime.timedelta(hours=4) # Adjust for EST dt_object -= datetime.timedelta(hours=4) # Adjust for EST
date_str = dt_object.strftime("EST: %A, %B %d, %Y %I:%M:%S %p") date_str = dt_object.strftime("EST: %A, %B %d, %Y %I:%M:%S %p")
elif timezone == 'CST':
dt_object -= datetime.timedelta(hours=5) # Adjust for EST
date_str = dt_object.strftime("CST: %A, %B %d, %Y %I:%M:%S %p")
elif timezone == 'PST':
dt_object -= datetime.timedelta(hours=6) # Adjust for EST
date_str = dt_object.strftime("PST: %A, %B %d, %Y %I:%M:%S %p")
else: else:
raise ValueError("Unsupported timezone.") raise ValueError("Unsupported timezone.")
return date_str return date_str
@ -279,6 +285,12 @@ def epoch_to_human_readable(epoch_timestamp, timezone='GMT'):
elif timezone == 'EST': elif timezone == 'EST':
dt_object -= datetime.timedelta(hours=4) # Using 4 hours for EST conversion instead of 5? dt_object -= datetime.timedelta(hours=4) # Using 4 hours for EST conversion instead of 5?
date_str = dt_object.strftime("EST: %A, %B %d, %Y %I:%M:%S %p") date_str = dt_object.strftime("EST: %A, %B %d, %Y %I:%M:%S %p")
elif timezone == 'CST':
dt_object -= datetime.timedelta(hours=5) # Using 4 hours for EST conversion instead of 5?
date_str = dt_object.strftime("CST: %A, %B %d, %Y %I:%M:%S %p")
elif timezone == 'PST':
dt_object -= datetime.timedelta(hours=4) # Using 4 hours for EST conversion instead of 5?
date_str = dt_object.strftime("PST: %A, %B %d, %Y %I:%M:%S %p")
else: else:
raise ValueError("Unsupported timezone.") raise ValueError("Unsupported timezone.")
return date_str return date_str
@ -310,22 +322,22 @@ def convert_duration_seconds(seconds):
return f"{days} Days {hours} Hours {minutes} Minutes {seconds} Seconds" return f"{days} Days {hours} Hours {minutes} Minutes {seconds} Seconds"
def beautify_data(): def beautify_data(timezone='GMT'):
file_path = (os.path.join(DIR_NAME, 'stats.json')) file_path = (os.path.join(DIR_NAME, 'stats.json'))
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
data = json.load(file) data = json.load(file)
replace_time_and_duration_recursive(data) replace_time_and_duration_recursive(data, timezone)
data = recursive_key_replace(data) data = recursive_key_replace(data)
data = sort_data(data) data = sort_data(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}.")
def beautify_match_data(): def beautify_match_data(timezone='GMT'):
file_path = (os.path.join(DIR_NAME, 'match_info.json')) file_path = (os.path.join(DIR_NAME, 'match_info.json'))
with open(file_path, 'r') as file: with open(file_path, 'r') as file:
data = json.load(file) data = json.load(file)
replace_time_and_duration_recursive(data) replace_time_and_duration_recursive(data, timezone)
data = recursive_key_replace(data) data = recursive_key_replace(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)
@ -353,7 +365,7 @@ def split_matches_into_files():
isinstance(sample_match.get("duration"), int)): isinstance(sample_match.get("duration"), int)):
print("Cleaning match data...") print("Cleaning match data...")
replace_time_and_duration_recursive(data) replace_time_and_duration_recursive(data, timezone)
# Save the cleaned data back to match_info.json # Save the cleaned data back to match_info.json
with open(os.path.join(DIR_NAME, 'match_info.json'), 'w') as file: with open(os.path.join(DIR_NAME, 'match_info.json'), 'w') as file:
@ -445,9 +457,13 @@ def main():
parser = argparse.ArgumentParser(description="Detailed Modern Warfare (2019) Statistics Tool", epilog=help_text, formatter_class=argparse.RawDescriptionHelpFormatter) parser = argparse.ArgumentParser(description="Detailed Modern Warfare (2019) Statistics Tool", epilog=help_text, formatter_class=argparse.RawDescriptionHelpFormatter)
# Group related arguments # Group related arguments
group_default = parser.add_argument_group("Default Options")
group_data = parser.add_argument_group("Data Fetching Options") group_data = parser.add_argument_group("Data Fetching Options")
group_cleaning = parser.add_argument_group("Data Cleaning Options") group_cleaning = parser.add_argument_group("Data Cleaning Options")
# Add an argument for timezone
group_default.add_argument("-tz", "--timezone", type=str, default="GMT", choices=["GMT", "EST", "CST", "PST"], help="Specify the timezone (GMT, EST, CST, PST)")
# Add arguments for Data Fetching Options # Add arguments for Data Fetching Options
group_data.add_argument("-p", "--player_name", type=str, help="Player's username (with #1234567)") group_data.add_argument("-p", "--player_name", type=str, help="Player's username (with #1234567)")
group_data.add_argument("-a", "--all_stats", action="store_true", help="Fetch all the different types of stats data") group_data.add_argument("-a", "--all_stats", action="store_true", help="Fetch all the different types of stats data")
@ -461,6 +477,7 @@ def main():
group_data.add_argument("-ca", "--connected_accounts", action="store_true", help="Fetch only the map list data") group_data.add_argument("-ca", "--connected_accounts", action="store_true", help="Fetch only the map list data")
group_data.add_argument("-s", "--settings", action="store_true", help="Fetch only your account settings") group_data.add_argument("-s", "--settings", action="store_true", help="Fetch only your account settings")
# Add arguments for Cleaning Options # Add arguments for Cleaning Options
group_cleaning.add_argument("-c", "--clean", action="store_true", help="Beautify all data") group_cleaning.add_argument("-c", "--clean", action="store_true", help="Beautify all data")
group_cleaning.add_argument("-sm", "--split_matches", action="store_true", help="Split the matches into separate JSON files within the 'matches' subfolder") group_cleaning.add_argument("-sm", "--split_matches", action="store_true", help="Split the matches into separate JSON files within the 'matches' subfolder")
@ -484,13 +501,13 @@ def main():
if args.split_matches: if args.split_matches:
split_matches_into_files() split_matches_into_files()
elif args.clean_stats_data: elif args.clean_stats_data:
beautify_data() beautify_data(timezone=args.timezone)
elif args.clean_match_data: elif args.clean_match_data:
beautify_match_data() beautify_match_data(timezone=args.timezone)
elif args.clean: elif args.clean:
beautify_data() beautify_data(timezone=args.timezone)
beautify_match_data() beautify_match_data(timezone=args.timezone)
beautify_feed_data() beautify_feed_data(timezone=args.timezone)
clean_json_files('friendFeed.json', 'eventFeed.json') clean_json_files('friendFeed.json', 'eventFeed.json')
elif args.clean_friend_feed: elif args.clean_friend_feed:
clean_json_files('friendFeed.json') clean_json_files('friendFeed.json')