Advertisement
Guest User

Untitled

a guest
Jan 11th, 2024
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.53 KB | None | 0 0
  1. import json
  2. import os.path
  3.  
  4. from urllib.request import urlopen, Request
  5.  
  6. is_steam_matches = True
  7. GORGC_PLAYER_ID = "56939869"
  8.  
  9.  
  10. def load_matches_from_file():
  11.     match_file = f'./matches.json'
  12.     if is_steam_matches:
  13.         match_file = f'./steam_matches.json'
  14.     with open(match_file) as match_file:
  15.         return json.load(match_file)
  16.  
  17.  
  18. def get_matches_from_opendota(match_json):
  19.     for match in match_json:
  20.         match_id = match["match_id"]
  21.         if os.path.isfile(f'./{match_id}.json'):
  22.             continue
  23.         url = f'https://api.opendota.com/api/matches/{match_id}'
  24.         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'})
  25.         match_response = urlopen(match_request)
  26.         if match_response.status == 200:
  27.             with open(f'./{match_id}.json', 'w', encoding='utf=8') as thefile:
  28.                 thefile.write(match_response.read().decode('utf-8'))
  29.  
  30.  
  31. def check_chat(match_id, player_id):
  32.     match_json = json.load(open(f'{match_id}.json', encoding='utf-8'))
  33.     player_slot = get_player_slot(match_json, player_id)
  34.     chat_detected = False
  35.     all_chat_detected = False
  36.     print(f'Checking match {match_id}. Player slot: {player_slot[0]}  ')
  37.     print(f'\tPings: {player_slot[1]}  ')
  38.     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"}}
  39.     for chat_event in match_json["chat"]:
  40.         if chat_event["player_slot"] == player_slot[0]:
  41.             if is_steam_matches:
  42.                 print(f'\t{chat_event}  ')
  43.             chat_detected = True
  44.             key = chat_event["key"]
  45.             event_type = chat_event["type"]
  46.             if event_type == "chat":
  47.                 all_chat_detected = True
  48.                 events_stats["chat"]["count"] = events_stats["chat"]["count"] + 1
  49.             if event_type == "chatwheel":
  50.                 try:
  51.                     events_stats[key]["count"] = events_stats[key]["count"] + 1
  52.                 except:
  53.                     events_stats[key] = {"count": 1, "string": f'Other Voiceline ({key})'}
  54.  
  55.     if not is_steam_matches:
  56.         reddit_print_stats(events_stats)
  57.     return chat_detected, all_chat_detected
  58.  
  59.  
  60. def reddit_print_stats(event_stats):
  61.     for event in event_stats.values():
  62.         if int(event["count"]) > 0:
  63.             print(f'\tEvent: {event["string"]}, Count: {event["count"]}  ')
  64.  
  65.  
  66. def get_player_slot(match_json, player_id):
  67.     for player in match_json["players"]:
  68.         account_id = player.get("account_id", -1)
  69.         if account_id == int(player_id):
  70.             return [player["player_slot"], player["pings"]]
  71.     print("ERROR COULDN'T FIND PLAYER. Wrong match set or player id?")
  72.  
  73.  
  74. def main():
  75.     match_json = load_matches_from_file()
  76.     if is_steam_matches:
  77.         match_json = match_json["result"]["matches"]
  78.     total_count = 0
  79.     all_chat_count = 0
  80.     get_matches_from_opendota(match_json)
  81.     for match in match_json:
  82.         results = check_chat(match["match_id"], GORGC_PLAYER_ID)
  83.         if results[0]:
  84.             total_count = total_count + 1
  85.         if results[1]:
  86.             all_chat_count = all_chat_count + 1
  87.  
  88.     print(f'Chat detected in {total_count} games  ')
  89.     print(f'All Chat detected in {all_chat_count} games  ')
  90.  
  91.  
  92. if __name__ == '__main__':
  93.     main()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement