Add option menu alongside arguments for ease of use

This commit is contained in:
Rim 2023-12-25 19:44:48 -05:00
parent a41ccd0e15
commit 7208e07bad
2 changed files with 125 additions and 59 deletions

6
.gitignore vendored
View File

@ -1,6 +1,8 @@
__pycache__ __pycache__
uninstall.* uninstall.*
venv.* venv.*
/cod_pics
/venv /venv
/HTML /stats
/stats /userInfo.json
/cookie*

View File

@ -125,6 +125,24 @@ def get_and_save_data(player_name=None, all_stats=False, season_loot=False, iden
settings = api.Me.settings() settings = api.Me.settings()
save_to_file(settings, 'settings.json') save_to_file(settings, 'settings.json')
def display_menu():
print("Please choose an option:")
print("1) Get all stats")
print("2) Get season loot")
print("3) Get identities")
print("4) Get map list")
print("5) Get general information")
print("6) Get friend feed")
print("7) Get event feed")
print("8) Get COD Point balance")
print("9) Get connected accounts")
print("10) Get account settings")
print("11) Beautify all data")
print("12) Split matches into separate files")
print("0) Exit")
choice = input("Enter your choice: ")
return int(choice)
# Save results to a JSON file inside the stats directory # Save results to a JSON file inside the stats directory
def recursive_key_replace(obj): def recursive_key_replace(obj):
if isinstance(obj, dict): if isinstance(obj, dict):
@ -184,7 +202,6 @@ def replace_time_and_duration_recursive(data):
""" """
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.
""" """
time_keys = ["timePlayedTotal", "timePlayed", "objTime", "time", "timeProne", time_keys = ["timePlayedTotal", "timePlayed", "objTime", "time", "timeProne",
"timeSpentAsPassenger", "timeSpentAsDriver", "timeOnPoint", "timeSpentAsPassenger", "timeSpentAsDriver", "timeOnPoint",
"timeWatchingKillcams", "timeCrouched", "timesSelectedAsSquadLeader", "timeWatchingKillcams", "timeCrouched", "timesSelectedAsSquadLeader",
@ -193,25 +210,20 @@ 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)
elif isinstance(data, dict): elif isinstance(data, dict):
for key, value in data.items(): for key, value in data.items():
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)
# 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)
# 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)
@ -332,61 +344,113 @@ def main():
- Enter the value when prompted - Enter the value when prompted
""" """
parser = argparse.ArgumentParser(description="Detailed Modern Warfare (2019) Statistics Tool", epilog=help_text, formatter_class=argparse.RawDescriptionHelpFormatter) # Check if the script is run without any additional command-line arguments
if len(sys.argv) == 1:
if os.path.exists(COOKIE_FILE):
with open(COOKIE_FILE, 'r') as f:
api_key = f.read().strip()
else:
api_key = input("Please enter your ACT_SSO_COOKIE: ")
with open(COOKIE_FILE, 'w') as f:
f.write(api_key)
# Group related arguments player_name = input("Please enter the player's username (with #1234567): ")
group_data = parser.add_argument_group("Data Fetching Options") api.login(api_key)
group_cleaning = parser.add_argument_group("Data Cleaning Options")
# Add arguments for Data Fetching Options while True:
group_data.add_argument("-p", "--player_name", type=str, help="Player's username (with #1234567)") choice = display_menu()
group_data.add_argument("-a", "--all_stats", action="store_true", help="Fetch all the different types of stats data")
group_data.add_argument("-sl", "--season_loot", action="store_true", help="Fetch only the season loot data")
group_data.add_argument("-id", "--identities", action="store_true", help="Fetch only the logged-in identities data")
group_data.add_argument("-m", "--maps", action="store_true", help="Fetch only the map list data")
group_data.add_argument("-i", "--info", action="store_true", help="Fetch only general information")
group_data.add_argument("-f", "--friendFeed", action="store_true", help="Fetch only your friend feed")
group_data.add_argument("-e", "--eventFeed", action="store_true", help="Fetch only your event feed")
group_data.add_argument("-cp", "--cod_points", action="store_true", help="Fetch only your COD Point balance")
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")
# Add arguments for Cleaning Options if choice == 1:
group_cleaning.add_argument("-c", "--clean", action="store_true", help="Beautify all data") get_and_save_data(player_name, all_stats=True)
group_cleaning.add_argument("-sm", "--split_matches", action="store_true", help="Split the matches into separate JSON files within the 'matches' subfolder") elif choice == 2:
group_cleaning.add_argument("-csd", "--clean_stats_data", action="store_true", help="Beautify the data and convert to human-readable strings in stats.json") get_and_save_data(player_name, season_loot=True)
group_cleaning.add_argument("-cmd", "--clean_match_data", action="store_true", help="Beautify the match data and convert to human-readable strings in match_info.json") elif choice == 3:
group_cleaning.add_argument("-cff", "--clean_friend_feed", action="store_true", help="Clean the friend feed data") get_and_save_data(player_name, identities=True)
group_cleaning.add_argument("-cef", "--clean_event_feed", action="store_true", help="Clean the event feed data") elif choice == 4:
get_and_save_data(player_name, maps=True)
args = parser.parse_args() elif choice == 5:
get_and_save_data(player_name, info=True)
# Custom error handling elif choice == 6:
# try: get_and_save_data(player_name, friendFeed=True)
# args = parser.parse_args() elif choice == 7:
# except SystemExit: get_and_save_data(player_name, eventFeed=True)
# # Check if 'player_name' is in sys.argv, if not, raise exception elif choice == 8:
# if '--player_name' not in sys.argv and '-p' not in sys.argv: get_and_save_data(player_name, cod_points=True)
# print('You must specify a player name!') elif choice == 9:
# # Otherwise, re-raise the error or print the default error message. get_and_save_data(player_name, connected_accounts=True)
# sys.exit(1) elif choice == 10:
get_and_save_data(player_name, settings=True)
if args.split_matches: elif choice == 11:
split_matches_into_files() beautify_data()
elif args.clean_stats_data: beautify_match_data()
beautify_data() beautify_feed_data()
elif args.clean_match_data: clean_json_files('friendFeed.json', 'eventFeed.json')
beautify_match_data() elif choice == 12:
elif args.clean: split_matches_into_files()
beautify_data() elif choice == 0:
beautify_match_data() print("Exiting...")
clean_json_files('friendFeed.json', 'eventFeed.json') break
elif args.clean_friend_feed: else:
clean_json_files('friendFeed.json') print("Invalid choice. Please try again.")
elif args.clean_event_feed: continue
clean_json_files('eventFeed.json') break
else: else:
get_and_save_data(args.player_name, args.all_stats, args.season_loot, args.identities, args.maps, args.info, args.friendFeed, args.eventFeed, args.cod_points, args.connected_accounts, args.settings) parser = argparse.ArgumentParser(description="Detailed Modern Warfare (2019) Statistics Tool", epilog=help_text, formatter_class=argparse.RawDescriptionHelpFormatter)
# Group related arguments
group_data = parser.add_argument_group("Data Fetching Options")
group_cleaning = parser.add_argument_group("Data Cleaning 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("-a", "--all_stats", action="store_true", help="Fetch all the different types of stats data")
group_data.add_argument("-sl", "--season_loot", action="store_true", help="Fetch only the season loot data")
group_data.add_argument("-id", "--identities", action="store_true", help="Fetch only the logged-in identities data")
group_data.add_argument("-m", "--maps", action="store_true", help="Fetch only the map list data")
group_data.add_argument("-i", "--info", action="store_true", help="Fetch only general information")
group_data.add_argument("-f", "--friendFeed", action="store_true", help="Fetch only your friend feed")
group_data.add_argument("-e", "--eventFeed", action="store_true", help="Fetch only your event feed")
group_data.add_argument("-cp", "--cod_points", action="store_true", help="Fetch only your COD Point balance")
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")
# Add arguments for Cleaning Options
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("-csd", "--clean_stats_data", action="store_true", help="Beautify the data and convert to human-readable strings in stats.json")
group_cleaning.add_argument("-cmd", "--clean_match_data", action="store_true", help="Beautify the match data and convert to human-readable strings in match_info.json")
group_cleaning.add_argument("-cff", "--clean_friend_feed", action="store_true", help="Clean the friend feed data")
group_cleaning.add_argument("-cef", "--clean_event_feed", action="store_true", help="Clean the event feed data")
args = parser.parse_args()
# Custom error handling
# try:
# args = parser.parse_args()
# except SystemExit:
# # Check if 'player_name' is in sys.argv, if not, raise exception
# if '--player_name' not in sys.argv and '-p' not in sys.argv:
# print('You must specify a player name!')
# # Otherwise, re-raise the error or print the default error message.
# sys.exit(1)
if args.split_matches:
split_matches_into_files()
elif args.clean_stats_data:
beautify_data()
elif args.clean_match_data:
beautify_match_data()
elif args.clean:
beautify_data()
beautify_match_data()
beautify_feed_data()
clean_json_files('friendFeed.json', 'eventFeed.json')
elif args.clean_friend_feed:
clean_json_files('friendFeed.json')
elif args.clean_event_feed:
clean_json_files('eventFeed.json')
else:
get_and_save_data(args.player_name, args.all_stats, args.season_loot, args.identities, args.maps, args.info, args.friendFeed, args.eventFeed, args.cod_points, args.connected_accounts, args.settings)
if __name__ == "__main__": if __name__ == "__main__":
main() main()