Advertisement
Guest User

Untitled

a guest
Jan 12th, 2024
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.50 KB | None | 0 0
  1. import json
  2. import os.path
  3. import time
  4.  
  5. from urllib.request import urlopen, Request
  6.  
  7. VERSION = "0.1.0"
  8. is_steam_matches = False
  9. print_verbose = True
  10. print_summary = False
  11. # Make sure player id is in quotes, as a string.
  12. PLAYER_ID = "56939869"
  13.  
  14.  
  15. # Opens the matches.json file and loads the data from it into an object
  16. # match file should be a json array of objects with a key "match_id" with the match_id.
  17. def load_matches_from_file():
  18.     match_file = f'./matches.json'
  19.     with open(match_file) as match_file:
  20.         return json.load(match_file)
  21.  
  22.  
  23. # Fetches the match information from opendota's matches API
  24. # Saves it to match_id.json. Skips the fetch if the file is
  25. # already there (saves calls on subsequent runs)
  26. def get_matches_from_opendota(match_json):
  27.     for match in match_json:
  28.         match_id = match["match_id"]
  29.         if os.path.isfile(f'./{match_id}.json'):
  30.             continue
  31.         url = f'https://api.opendota.com/api/matches/{match_id}'
  32.         print(f"Fetching match {match_id}")
  33.         match_request = Request(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'})
  34.         match_response = urlopen(match_request)
  35.         time.sleep(1) # Super lazy rate limiting to guarantee not hitting opendotas rate limiter
  36.         print(f"Received match {match_id}")
  37.         if match_response.status == 200:
  38.             with open(f'./{match_id}.json', 'w', encoding='utf=8') as thefile:
  39.                 thefile.write(match_response.read().decode('utf-8'))
  40.  
  41.  
  42. def check_chat(match_id, player_id):
  43.     match_json = json.load(open(f'{match_id}.json', encoding='utf-8'))
  44.     player_slot = get_player_slot(match_json, player_id)
  45.     chat_detected = False
  46.     all_chat_detected = False
  47.     print(f'Checking match {match_id}. Player slot: {player_slot[0]}  ')
  48.     print(f'\tPings: {player_slot[1]}  ')
  49.     events_stats = {"71": {"count": 0, "string": "Hero is missing"}, "7": {"count": 0, "string": "Well played!"}, "106001": {"count": 0, "string": "Ember laugh"}, "15001": {"count": 0, "string": "Razor laugh"}, "chat": {"count": 0, "string": "All chat"}}
  50.     for chat_event in match_json["chat"]:
  51.         if chat_event["player_slot"] == player_slot[0]:
  52.             if print_verbose:
  53.                 print(f'\t{chat_event}  ')
  54.             chat_detected = True
  55.             key = chat_event["key"]
  56.             event_type = chat_event["type"]
  57.             if event_type == "chat":
  58.                 all_chat_detected = True
  59.                 events_stats["chat"]["count"] = events_stats["chat"]["count"] + 1
  60.             if event_type == "chatwheel":
  61.                 try:
  62.                     events_stats[key]["count"] = events_stats[key]["count"] + 1
  63.                 except:
  64.                     events_stats[key] = {"count": 1, "string": f'Other Voiceline ({key})'}
  65.  
  66.     if not print_summary:
  67.         reddit_print_stats(events_stats)
  68.     return chat_detected, all_chat_detected
  69.  
  70.  
  71. def reddit_print_stats(event_stats):
  72.     for event in event_stats.values():
  73.         if int(event["count"]) > 0:
  74.             print(f'\tEvent: {event["string"]}, Count: {event["count"]}  ')
  75.  
  76.  
  77. def get_player_slot(match_json, player_id):
  78.     for player in match_json["players"]:
  79.         account_id = player.get("account_id", -1)
  80.         if account_id == int(player_id):
  81.             return [player["player_slot"], player["pings"]]
  82.     print("ERROR COULDN'T FIND PLAYER. Wrong match set or player id?")
  83.  
  84.  
  85. def main():
  86.     match_json = load_matches_from_file()
  87.  
  88.     if is_steam_matches:
  89.         match_json = match_json["result"]["matches"]
  90.  
  91.     # Check you're not going to burn through all your API calls at once
  92.     if len(match_json) > 1000:
  93.         print("You've loaded more than 1000 matches, this will require more API calls than open dota allows without a key.")
  94.         print("Aborting. Please adjust script or contact script maintainer if you need this case handled.")
  95.         raise Exception("TooManyMatches")
  96.  
  97.     total_count = 0
  98.     all_chat_count = 0
  99.     get_matches_from_opendota(match_json)
  100.     for match in match_json:
  101.         results = check_chat(match["match_id"], PLAYER_ID)
  102.         if results[0]:
  103.             total_count = total_count + 1
  104.         if results[1]:
  105.             all_chat_count = all_chat_count + 1
  106.  
  107.     print(f'Chat detected in {total_count} games  ')
  108.     print(f'All Chat detected in {all_chat_count} games  ')
  109.  
  110.  
  111. if __name__ == '__main__':
  112.     print(f'Version: ChatCheck V{VERSION}')
  113.     main()
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement