From 2612a949484009c85e390b6d14a12356da9bf2c6 Mon Sep 17 00:00:00 2001 From: Rim Date: Mon, 10 Mar 2025 09:26:03 -0400 Subject: [PATCH] chore: add dev scripts --- dev/dev_script_full.py | 116 +++++++++++++++++++++++++++++++++++++++ dev/dev_script_simple.py | 19 +++++++ ref/convert.py | 19 +++++++ ref/convert_recursive.py | 29 ++++++++++ ref/serve_http.py | 40 ++++++++++++++ 5 files changed, 223 insertions(+) create mode 100644 dev/dev_script_full.py create mode 100644 dev/dev_script_simple.py create mode 100644 ref/convert.py create mode 100644 ref/convert_recursive.py create mode 100644 ref/serve_http.py diff --git a/dev/dev_script_full.py b/dev/dev_script_full.py new file mode 100644 index 0000000..7b2d2cb --- /dev/null +++ b/dev/dev_script_full.py @@ -0,0 +1,116 @@ +from cod_api import API, platforms +import os +import asyncio +import json + +# Configure asyncio for Windows +if os.name == 'nt': + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + +COOKIE_FILE = 'cookie.txt' +STATS_DIR = 'dev/' + +api = API() + +def save_to_file(data, filename): + """Save data to a JSON file.""" + file_path = os.path.join(STATS_DIR, filename) + with open(file_path, 'w') as json_file: + json.dump(data, json_file, indent=4) + print(f"Data saved to {file_path}") + +def ensure_directories_exist(): + if not os.path.exists(STATS_DIR): + os.makedirs(STATS_DIR) + +def get_api_key(): + if os.path.exists(COOKIE_FILE): + with open(COOKIE_FILE, 'r') as f: + return f.read().strip() + else: + api_key = input("Please enter your ACT_SSO_COOKIE: ") + with open(COOKIE_FILE, 'w') as f: + f.write(api_key) + return api_key + +# def doc(): + # print(api.Me.__doc__) + # print(api.Shop.__doc__) + # print(api.Misc.__doc__) + # print(api.ModernWarfare.__doc__) + # print(api.Warzone.__doc__) + # print(api.ColdWar.__doc__) + # print(api.Vanguard.__doc__) + # print(api.ModernWarfare2.__doc__) + +def get_mw_data(player_name): + api_key = get_api_key() + + api.login(api_key) + + player_stats = api.ModernWarfare.fullData(platforms.Activision, player_name) + match_info = api.ModernWarfare.combatHistory(platforms.Activision, player_name) + season_loot_data = api.ModernWarfare.seasonLoot(platforms.Activision, player_name) + identities_data = api.Me.loggedInIdentities() + map_list = api.ModernWarfare.mapList(platforms.Activision) + details = api.ModernWarfare.matchInfo(platforms.Battlenet, 4543631618795926894) + + save_to_file(details, 'details.json') + save_to_file(player_stats, 'stats.json') + save_to_file(match_info, 'match_info.json') + save_to_file(season_loot_data, 'season_loot.json') + save_to_file(map_list, 'map_list.json') + save_to_file(identities_data, 'identities.json') + + user_info_file = os.path.join('userInfo.json') + if os.path.exists(user_info_file): + info = api.Me.info() + friend_feed = api.Me.friendFeed() + event_feed = api.Me.eventFeed() + cod_points = api.Me.codPoints() + connected_accounts = api.Me.connectedAccounts() + settings = api.Me.settings() + + save_to_file(info, 'info.json') + save_to_file(friend_feed, 'friendFeed.json') + save_to_file(event_feed, 'eventFeed.json') + save_to_file(cod_points, 'cp.json') + save_to_file(connected_accounts, 'connectedAccounts.json') + +def get_game_data(player_name, game): + api_key = get_api_key() + + api.login(api_key) + + game_api = getattr(api, game, None) + + if game_api is None: + print(f"Error: The game '{game}' is not supported.") + return + + # Fetch game data + player_stats = game_api.fullData(platforms.Activision, player_name) + match_info = game_api.combatHistory(platforms.Activision, player_name) + # season_loot_data = game_api.seasonLoot(platforms.Activision, player_name) + # identities_data = api.Me.loggedInIdentities() + # map_list = game_api.mapList(platforms.Activision) + details = api.ModernWarfare.matchInfo(platforms.Battlenet, 4543631618795926894) + save_to_file(player_stats, 'stats.json') + save_to_file(match_info, 'match_info.json') + save_to_file(details, 'details.json') + # save_to_file(season_loot_data, 'season_loot.json') + # save_to_file(map_list, 'map_list.json') + # save_to_file(identities_data, 'identities.json') + +def main(): + player_name = 'Ahrimdon' + game = 'ModernWarfare' + + ensure_directories_exist() + get_api_key() + # doc() + # get_mw_data(player_name) + get_game_data(player_name, game) + +if __name__ == "__main__": + main() diff --git a/dev/dev_script_simple.py b/dev/dev_script_simple.py new file mode 100644 index 0000000..1e1b4bf --- /dev/null +++ b/dev/dev_script_simple.py @@ -0,0 +1,19 @@ +from cod_api import API, platforms +import asyncio +import os + +if os.name == 'nt': + asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) + +## sync +# initiating the API class +api = API() + +# login in with sso token +api.login('') + +# retrieving combat history +profile = api.Warzone.breakdownWithDate(platforms.Activision, "Ahrimdon", 1657919309, 1740896092) # returns data of type dict + +# printing results to console +print(profile) \ No newline at end of file diff --git a/ref/convert.py b/ref/convert.py new file mode 100644 index 0000000..e9922b4 --- /dev/null +++ b/ref/convert.py @@ -0,0 +1,19 @@ +import json +import yaml + +def convert(): + # Open and load JSON data + with open("stats.json", 'r') as json_file: + data = json.load(json_file) # Load JSON properly + + # Convert and write YAML data to a file + with open("stats.yaml", 'w') as yaml_file: + yaml.dump(data, yaml_file, default_flow_style=False) # Ensure readable YAML format + + print("Data saved to stats.yaml") # Print success message + +def main(): + convert() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/ref/convert_recursive.py b/ref/convert_recursive.py new file mode 100644 index 0000000..b115523 --- /dev/null +++ b/ref/convert_recursive.py @@ -0,0 +1,29 @@ +import json +import yaml +import os + +STATS_DIR = "stats" # Define the stats directory + +def convert_json_to_yaml(): + """Recursively process all JSON files in STATS_DIR and convert them to YAML.""" + for root, _, files in os.walk(STATS_DIR): + for file in files: + if file.endswith(".json"): + json_path = os.path.join(root, file) + yaml_path = os.path.join(root, file.replace(".json", ".yaml")) + + # Read JSON file + with open(json_path, 'r') as json_file: + data = json.load(json_file) + + # Write YAML file + with open(yaml_path, 'w', encoding='utf-8') as yaml_file: + yaml.dump(data, yaml_file, default_flow_style=False, sort_keys=False, allow_unicode=False, width=120, explicit_start=True, explicit_end=True, default_style="", line_break="unix", indent=2) + + print(f"Converted: {json_path} --> {yaml_path}") + +def main(): + convert_json_to_yaml() + +if __name__ == "__main__": + main() diff --git a/ref/serve_http.py b/ref/serve_http.py new file mode 100644 index 0000000..0a09ae9 --- /dev/null +++ b/ref/serve_http.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +import http.server +import socketserver +import sys +import os +import signal + +ADDRESS = '0.0.0.0' +PORT = 8080 + +# Check if directory was specified as command-line argument +if len(sys.argv) > 1: + os.chdir(sys.argv[1]) + print(f"Serving directory: {sys.argv[1]}") + +Handler = http.server.SimpleHTTPRequestHandler + +# Set MIME types - using text/yaml for YAML files +Handler.extensions_map = { + '.yaml': 'application/json', + '.yml': 'application/json', + '.json': 'application/json', + '': 'application/json', # octet-stream +} + +# Enable socket reuse to avoid "address already in use" errors +socketserver.TCPServer.allow_reuse_address = True + +# Create server +httpd = socketserver.TCPServer((ADDRESS, PORT), Handler) + +print(f"Serving at http://localhost:{PORT}") +print("Press Ctrl+C to stop the server") + +try: + httpd.serve_forever() +except KeyboardInterrupt: + print("\nShutting down server...") + httpd.server_close() + print("Server stopped")