From dab247886efb77f2b3538246bbab869539e6b3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Tue, 7 Nov 2023 09:24:39 +0100 Subject: [PATCH] add team members not in list. sort columns by number of games. --- get_sheet.py | 66 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 20 deletions(-) diff --git a/get_sheet.py b/get_sheet.py index 5178af4..4f806cc 100644 --- a/get_sheet.py +++ b/get_sheet.py @@ -1,7 +1,10 @@ import requests +import requests_cache import json import csv +session = requests_cache.CachedSession('.hanab-live.cache') + player_mapping = { 'RamaNoVarjan': 'Ramanujan', 'purplejoe2': 'PurpleJoe', @@ -11,18 +14,24 @@ player_mapping = { 'MarkusKahlsen': 'Markus', 'NoVarkusKahlsen': 'Markus', 'spring': 'spring', - 'str8tsknacker': 'str8t', - 'novarknacker': 'str8t', + 'str8tsknacker': 'str8tsknacker', + 'novarknacker': 'str8tsknacker', 'StKildaFan': 'Kilda', - 'noarv': 'arv', - 'arv': 'arv', + 'noarv': 'Arv', + 'arv': 'Arv', 'ElenaDhynho': 'Elena', 'ElenaDhynh0': 'Elena', 'Elenanovar': 'Elena', - 'TimeHoodie': 'Hoodie' + 'TimeHoodie': 'Hoodie', + 'Hoodie': 'Hoodie', + 'newduke': 'Duke', } -players_lower = [s.lower() for s in player_mapping.keys()] +allowed_team_members = ['Carunty', 'HelanaAshryvr', 'rz'] + +allowed_players = [s.lower() for s in player_mapping.keys()] + [s.lower() for s in allowed_team_members] +print(allowed_players) + player_cols = set() for _, p in player_mapping.items(): player_cols.add(p) @@ -38,7 +47,7 @@ class Entry(): def get_player_games(player: str): - r = requests.get('https://hanab.live/api/v1/history-full/{}'.format(player)) + r = session.get('https://hanab.live/api/v1/history-full/{}?start={}'.format(player, 1044446)) if r.status_code == 200: return json.loads(r.text) @@ -61,7 +70,9 @@ def collect_player_games(): ok = True for player in players: - if player.lower() not in players_lower: + if player.lower() not in allowed_players: + print('rejecting game because of player {}'.format(player)) + print(allowed_players) ok = False if ok: global_games[game_id] = Entry(game_id, seed, score, players) @@ -69,16 +80,27 @@ def collect_player_games(): return global_games +def sort_players_by_num_games(games_dict): + nums = {} + for _, entry in games_dict.items(): + for player in entry.players: + col = player_mapping.get(player, None) + if col is not None: + num = nums.get(col, 0) + nums[col] = num + 1 + return sorted(player_cols, key = lambda col: -nums[col]) + if __name__ == "__main__": games = collect_player_games() streaks = {} fieldnames = ['Replay Number', 'Seed Name', 'Player #', 'W/L(1/0)'] - fieldnames += player_cols + fieldnames += sort_players_by_num_games(games) + fieldnames += ['Other'] - with open('games.csv', 'w') as f: - f.writelines([','.join(fieldnames)]) + with open('games.csv', 'w', newline='') as f: + f.writelines([','.join(fieldnames), "\n"]) - with open('games.csv', 'w+', newline='') as f: + with open('games.csv', 'a', newline='') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) for _, entry in sorted(games.items()): row = { @@ -88,15 +110,19 @@ if __name__ == "__main__": 'W/L(1/0)': entry.won } for player in entry.players: - col = player_mapping[player] - if entry.won: - streak = streaks.get(col, 0) - streak += 1 - streaks[col] = streak - row[col] = streak + col = player_mapping.get(player, None) + if col is not None: + if entry.won: + streak = streaks.get(col, 0) + streak += 1 + streaks[col] = streak + row[col] = streak + else: + streaks[col] = 0 + row[col] = 0 else: - streaks[col] = 0 - row[col] = 0 + num_others = row.get('Other', 0) + row['Other'] = num_others + 1 writer.writerow(row)