Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import os.path
- import time
- from urllib.request import urlopen, Request
- VERSION = "0.1.0"
- is_steam_matches = False
- print_verbose = True
- print_summary = False
- # Make sure player id is in quotes, as a string.
- PLAYER_ID = "56939869"
- # Opens the matches.json file and loads the data from it into an object
- # match file should be a json array of objects with a key "match_id" with the match_id.
- def load_matches_from_file():
- match_file = f'./matches.json'
- with open(match_file) as match_file:
- return json.load(match_file)
- # Fetches the match information from opendota's matches API
- # Saves it to match_id.json. Skips the fetch if the file is
- # already there (saves calls on subsequent runs)
- def get_matches_from_opendota(match_json):
- for match in match_json:
- match_id = match["match_id"]
- if os.path.isfile(f'./{match_id}.json'):
- continue
- url = f'https://api.opendota.com/api/matches/{match_id}'
- print(f"Fetching match {match_id}")
- 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'})
- match_response = urlopen(match_request)
- time.sleep(1) # Super lazy rate limiting to guarantee not hitting opendotas rate limiter
- print(f"Received match {match_id}")
- if match_response.status == 200:
- with open(f'./{match_id}.json', 'w', encoding='utf=8') as thefile:
- thefile.write(match_response.read().decode('utf-8'))
- def check_chat(match_id, player_id):
- match_json = json.load(open(f'{match_id}.json', encoding='utf-8'))
- player_slot = get_player_slot(match_json, player_id)
- chat_detected = False
- all_chat_detected = False
- print(f'Checking match {match_id}. Player slot: {player_slot[0]} ')
- print(f'\tPings: {player_slot[1]} ')
- 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"}}
- for chat_event in match_json["chat"]:
- if chat_event["player_slot"] == player_slot[0]:
- if print_verbose:
- print(f'\t{chat_event} ')
- chat_detected = True
- key = chat_event["key"]
- event_type = chat_event["type"]
- if event_type == "chat":
- all_chat_detected = True
- events_stats["chat"]["count"] = events_stats["chat"]["count"] + 1
- if event_type == "chatwheel":
- try:
- events_stats[key]["count"] = events_stats[key]["count"] + 1
- except:
- events_stats[key] = {"count": 1, "string": f'Other Voiceline ({key})'}
- if not print_summary:
- reddit_print_stats(events_stats)
- return chat_detected, all_chat_detected
- def reddit_print_stats(event_stats):
- for event in event_stats.values():
- if int(event["count"]) > 0:
- print(f'\tEvent: {event["string"]}, Count: {event["count"]} ')
- def get_player_slot(match_json, player_id):
- for player in match_json["players"]:
- account_id = player.get("account_id", -1)
- if account_id == int(player_id):
- return [player["player_slot"], player["pings"]]
- print("ERROR COULDN'T FIND PLAYER. Wrong match set or player id?")
- def main():
- match_json = load_matches_from_file()
- if is_steam_matches:
- match_json = match_json["result"]["matches"]
- # Check you're not going to burn through all your API calls at once
- if len(match_json) > 1000:
- print("You've loaded more than 1000 matches, this will require more API calls than open dota allows without a key.")
- print("Aborting. Please adjust script or contact script maintainer if you need this case handled.")
- raise Exception("TooManyMatches")
- total_count = 0
- all_chat_count = 0
- get_matches_from_opendota(match_json)
- for match in match_json:
- results = check_chat(match["match_id"], PLAYER_ID)
- if results[0]:
- total_count = total_count + 1
- if results[1]:
- all_chat_count = all_chat_count + 1
- print(f'Chat detected in {total_count} games ')
- print(f'All Chat detected in {all_chat_count} games ')
- if __name__ == '__main__':
- print(f'Version: ChatCheck V{VERSION}')
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement